-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
349 lines (274 loc) · 6.63 KB
/
client.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package rcon
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"time"
"github.com/mcstatus-io/mcutil/v3/options"
)
var (
// ErrNotConnected means the client attempted to send data but there was no connection to the server
ErrNotConnected = errors.New("rcon: not connected to the server")
// ErrAlreadyLoggedIn means the RCON client was already logged in but a second login attempt was made
ErrAlreadyLoggedIn = errors.New("rcon: already successfully logged in")
// ErrInvalidPassword means the password used in the RCON login was incorrect
ErrInvalidPassword = errors.New("rcon: incorrect password")
// ErrNotAuthenticated means the client attempted to execute a command before a login was successful
ErrNotAuthenticated = errors.New("rcon: not authenticated with the server")
)
var (
defaultOptions = options.RCON{
Timeout: time.Second * 5,
}
)
// Client is a client for interacting with RCON and contains multiple methods
type Client struct {
conn net.Conn
Messages chan string
runTrigger chan bool
authSuccess bool
requestID int32
}
// Connect connects to the server using the address provided and returns a new client
func Connect(host string, port uint16, options ...options.RCON) (*Client, error) {
opts := parseOptions(options...)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), opts.Timeout)
if err != nil {
return nil, err
}
return &Client{
conn: conn,
Messages: make(chan string),
runTrigger: make(chan bool),
authSuccess: false,
requestID: 0,
}, nil
}
// Login communicates authentication with the server using the plaintext password
func (r *Client) Login(password string) error {
if r.conn == nil {
return ErrNotConnected
}
if r.authSuccess {
return ErrAlreadyLoggedIn
}
requestID := r.nextRequestID()
// Login request packet
// https://wiki.vg/RCON#3:_Login
{
buf := &bytes.Buffer{}
// Length - int32
if err := binary.Write(buf, binary.LittleEndian, int32(10+len(password))); err != nil {
return err
}
// Request ID - int32
if err := binary.Write(buf, binary.LittleEndian, requestID); err != nil {
return err
}
// Type - int32
if err := binary.Write(buf, binary.LittleEndian, int32(3)); err != nil {
return err
}
// Payload - null-terminated string
if _, err := buf.Write(append([]byte(password), 0x00)); err != nil {
return err
}
// Padding - byte
if err := buf.WriteByte(0x00); err != nil {
return err
}
if _, err := io.Copy(r.conn, buf); err != nil {
return err
}
}
// Login response packet
// https://wiki.vg/RCON#3:_Login
{
var packetLength int32
// Length - int32
{
if err := binary.Read(r.conn, binary.LittleEndian, &packetLength); err != nil {
return err
}
}
// Request ID - int32
{
var reqID int32
if err := binary.Read(r.conn, binary.LittleEndian, &reqID); err != nil {
return err
}
if reqID == -1 {
return ErrInvalidPassword
} else if reqID != requestID {
return fmt.Errorf("rcon: received unexpected request ID (expected=%d, received=%d)", requestID, reqID)
}
}
// Type - int32
{
var packetType int32
if err := binary.Read(r.conn, binary.LittleEndian, &packetType); err != nil {
return err
}
if packetType != 0x02 {
return fmt.Errorf("rcon: received unexpected packet type (expected=0x02, received=0x%02X)", packetType)
}
}
// Remaining bytes
{
data := make([]byte, packetLength-8)
if _, err := r.conn.Read(data); err != nil {
return err
}
}
}
r.authSuccess = true
go (func() {
for {
err := r.readMessage()
if err != nil {
fmt.Println(err)
r.conn.Close()
r.conn = nil
break
}
}
})()
return nil
}
// Run executes the command on the server but does not wait for a response
func (r *Client) Run(command string) error {
if r.conn == nil {
return ErrNotConnected
}
if !r.authSuccess {
return ErrNotAuthenticated
}
requestID := r.nextRequestID()
// Command packet
// https://wiki.vg/RCON#2:_Command
{
buf := &bytes.Buffer{}
// Length - int32
if err := binary.Write(buf, binary.LittleEndian, int32(10+len(command))); err != nil {
return err
}
// Request ID - int32
if err := binary.Write(buf, binary.LittleEndian, requestID); err != nil {
return err
}
// Type - int32
if err := binary.Write(buf, binary.LittleEndian, int32(2)); err != nil {
return err
}
// Payload - null-terminated string
if _, err := buf.Write(append([]byte(command), 0x00)); err != nil {
return err
}
if err := buf.WriteByte(0x00); err != nil {
return err
}
if _, err := io.Copy(r.conn, buf); err != nil {
return err
}
}
return nil
}
// Execute runs the command on the server and attempts to wait for a response
func (r *Client) Execute(ctx context.Context, command string) (string, error) {
if r.conn == nil {
return "", ErrNotConnected
}
if !r.authSuccess {
return "", ErrNotAuthenticated
}
if err := r.Run(command); err != nil {
return "", err
}
for {
select {
case v := <-r.Messages:
return v, nil
case <-ctx.Done():
return "", context.DeadlineExceeded
}
}
}
// Close closes the connection to the server
func (r *Client) Close() error {
r.authSuccess = false
r.requestID = 0
if r.conn != nil {
if err := r.conn.Close(); err != nil {
return err
}
}
r.conn = nil
return nil
}
func (r *Client) readMessage() error {
// Command response packet
// https://wiki.vg/RCON#0:_Command_response
{
var (
packetLength int32
requestID int32
)
// Length - int32
{
// TODO convert to binary.Read() for the rest of the package
data := make([]byte, 4)
n, err := r.conn.Read(data)
if err != nil {
return err
}
if n < 4 {
return nil
}
packetLength = int32(binary.LittleEndian.Uint16(data))
}
// Request ID - int32
{
if err := binary.Read(r.conn, binary.LittleEndian, &requestID); err != nil {
return err
}
}
// Type - int32
{
var packetType int32
if err := binary.Read(r.conn, binary.LittleEndian, &packetType); err != nil {
return err
}
if packetType != 0 {
return fmt.Errorf("rcon: received unexpected packet type (expected=0x00, received=0x%02X)", packetType)
}
}
// Payload - null-terminated string
{
data := make([]byte, packetLength-8)
n, err := r.conn.Read(data)
if err != nil {
return err
}
if n < 1 {
return nil
}
r.Messages <- string(data)
}
}
return nil
}
func (r *Client) nextRequestID() int32 {
value := r.requestID
r.requestID++
return value
}
func parseOptions(opts ...options.RCON) options.RCON {
if len(opts) < 1 {
return defaultOptions
}
return opts[0]
}