-
Notifications
You must be signed in to change notification settings - Fork 2
/
websocket.go
149 lines (116 loc) · 3.26 KB
/
websocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package websocket
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"time"
"github.com/gorilla/websocket"
)
const (
// DefaultDialTimeout provides default auth timeout to remote server.
DefaultDialTimeout = 5 * time.Second
// DefaultDeadline provides default deadline to tcp read/write operations.
DefaultDeadline = 5 * time.Second
// MaxCommandLen is an artificial restriction, but it will help in case of random
// large queries.
MaxCommandLen = 1000
)
var (
// ErrAuthFailed is returned when the package id from authentication
// response is -1.
ErrAuthFailed = errors.New("authentication failed")
// ErrCommandTooLong is returned when executed command length is bigger
// than MaxCommandLen characters.
ErrCommandTooLong = errors.New("command too long")
// ErrCommandEmpty is returned when executed command length equal 0.
ErrCommandEmpty = errors.New("command too small")
)
// Conn represents a WebSocket connection.
type Conn struct {
conn *websocket.Conn
settings Settings
}
// Dial creates a new authorized WebSocket dialer connection.
func Dial(address string, password string, options ...Option) (*Conn, error) {
settings := DefaultSettings
for _, option := range options {
option(&settings)
}
u := url.URL{Scheme: "ws", Host: address, Path: password}
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
if err.Error() == `malformed HTTP response "\x88\x02\x03\xe8"` {
return nil, ErrAuthFailed
}
return nil, fmt.Errorf("webrcon: %w", err)
}
client := Conn{conn: conn, settings: settings}
return &client, nil
}
// Execute sends command string to execute to the remote server.
func (c *Conn) Execute(command string) (string, error) {
if command == "" {
return "", ErrCommandEmpty
}
if len(command) > MaxCommandLen {
return "", ErrCommandTooLong
}
request := newMessage(command)
data, err := json.Marshal(request)
if err != nil {
return "", fmt.Errorf("webrcon: %w", err)
}
if err := c.write(data); err != nil {
return "", err
}
for {
p, err := c.read()
if err != nil {
return "", err
}
var response Message
if err := json.Unmarshal(p, &response); err != nil {
return "", fmt.Errorf("webrcon: %w", err)
}
if response.Identifier == request.Identifier {
return response.Message, nil
}
}
}
// LocalAddr returns the local network address.
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
// Close closes the connection.
func (c *Conn) Close() error {
return c.conn.Close()
}
func (c *Conn) write(data []byte) error {
if c.settings.deadline != 0 {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.settings.deadline)); err != nil {
return fmt.Errorf("webrcon: %w", err)
}
}
if err := c.conn.WriteMessage(websocket.TextMessage, data); err != nil {
return fmt.Errorf("webrcon: %w", err)
}
return nil
}
func (c *Conn) read() ([]byte, error) {
if c.settings.deadline != 0 {
if err := c.conn.SetReadDeadline(time.Now().Add(c.settings.deadline)); err != nil {
return nil, fmt.Errorf("webrcon: %w", err)
}
}
_, p, err := c.conn.ReadMessage()
if err != nil {
return p, fmt.Errorf("webrcon: %w", err)
}
return p, nil
}