forked from james4k/rcon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrcon.go
316 lines (261 loc) · 8.56 KB
/
rcon.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
/*
A Go written library for the RCON Protocol from Valve.
Information to the protocol can be found under:
https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
This is a fork from https://github.com/james4k/rcon with the support for go
modules and with a rework of the original implementation for better readability.
*/
package rcon
import (
"bytes"
"encoding/binary"
"errors"
"net"
"sync"
"time"
)
const (
typeAuth = 3
typeExecCommand = 2
typeResponseValue = 0
typeAuthResponse = 2
fieldPackageSize = 4
fieldIDSize = 4
fieldTypeSize = 4
fieldMinBodySize = 1
fieldEndSize = 1
)
// The minimum package size contains:
// 4 bytes for the ID field
// 4 bytes for the Type field
// 1 byte minimum for an empty body string
// 1 byte for the empty string at the end
//
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Packet_Size
// The 4 bytes representing the size of the package are not included.
const minPackageSize = fieldIDSize + fieldTypeSize + fieldMinBodySize + fieldEndSize
// maxPackageSize of a request/response package.
// This size does not include the size field.
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Packet_Size
const maxPackageSize = 4096
// RemoteConsole holds the information to communicate withe remote console.
type RemoteConsole struct {
conn net.Conn
readBuff []byte
readMutex sync.Mutex
queuedBuff []byte
}
var (
// ErrAuthFailed the authentication against the server failed.
// This happens if the request id doesn't match the response id.
ErrAuthFailed = errors.New("rcon: authentication failed")
// ErrInvalidAuthResponse the response of an authentication request doesn't match the correct type.
ErrInvalidAuthResponse = errors.New("rcon: invalid response type during auth")
// ErrUnexpectedFormat the response package is not correctly formatted.
ErrUnexpectedFormat = errors.New("rcon: unexpected response format")
// ErrCommandTooLong the command is bigger than the bodyBufferSize.
ErrCommandTooLong = errors.New("rcon: command too long")
// ErrResponseTooLong the response package is bigger than the maxPackageSize.
ErrResponseTooLong = errors.New("rcon: response too long")
)
// Dial establishes a connection with the remote server.
// It can return multiple errors:
// - ErrInvalidAuthResponse
// - ErrAuthFailed
// - and other types of connection errors that are not specified in this package.
func Dial(host, password string) (*RemoteConsole, error) {
const timeout = 10 * time.Second
conn, err := net.DialTimeout("tcp", host, timeout)
if err != nil {
return nil, err
}
remoteConsole := &RemoteConsole{
conn: conn,
readBuff: make([]byte, maxPackageSize+fieldPackageSize),
}
remoteConsole.auth(password, timeout)
if err != nil {
return nil, err
}
return remoteConsole, nil
}
// LocalAddr returns the local network address.
func (r *RemoteConsole) LocalAddr() net.Addr {
return r.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (r *RemoteConsole) RemoteAddr() net.Addr {
return r.conn.RemoteAddr()
}
// Write a command to the server.
//
// It can return ErrCommandTooLong if the given cmd str is too long.
// Additionally it can return any other connection related errors.
func (r *RemoteConsole) Write(cmd string) (requestID int, err error) {
requestID = int(newRequestID())
err = r.writeCmd(int32(requestID), typeExecCommand, cmd)
return
}
// Read a incoming response from the server.
// If the response doesn't contain the correct ResponseValue it will return a response with a empty string an the request id = 0.
// This is also the case if an error happens even though the error will be returned.
//
// It can return following errors:
// - ErrResponseTooLong
// - ErrUnexpectedFormat
// - or a connection error that isn't typed in this package
func (r *RemoteConsole) Read() (response string, requestID int, err error) {
var respType int
var respBytes []byte
respType, requestID, respBytes, err = r.readResponse(2 * time.Minute)
if err != nil || respType != typeResponseValue {
response = ""
requestID = 0
} else {
response = string(respBytes)
}
return
}
// Close the connection to the server.
func (r *RemoteConsole) Close() error {
return r.conn.Close()
}
func newRequestID() int32 {
return int32((time.Now().UnixNano() / 100000) % 100000)
}
func (r *RemoteConsole) auth(password string, timeout time.Duration) error {
reqID := newRequestID()
err := r.writeCmd(reqID, typeAuth, password)
if err != nil {
return err
}
respType, responseID, _, err := r.readResponse(timeout)
if err != nil {
return err
}
// if we didn't get an auth response back, try again. it is often a bug
// with RCON servers that you get an empty response before receiving the
// auth response.
if respType != typeAuthResponse {
respType, responseID, _, err = r.readResponse(timeout)
}
if err != nil {
return err
}
if respType != typeAuthResponse {
return ErrInvalidAuthResponse
}
if responseID != int(reqID) {
return ErrAuthFailed
}
return nil
}
func (r *RemoteConsole) writeCmd(reqID, pkgType int32, cmd string) error {
if len(cmd) > maxPackageSize-minPackageSize {
return ErrCommandTooLong
}
buffer := bytes.NewBuffer(make([]byte, 0, minPackageSize+fieldPackageSize+len(cmd)))
// packet size
binary.Write(buffer, binary.LittleEndian, int32(minPackageSize+len(cmd)))
// request id
binary.Write(buffer, binary.LittleEndian, int32(reqID))
// type of the package
binary.Write(buffer, binary.LittleEndian, int32(pkgType))
// body
buffer.WriteString(cmd)
// double null termination
binary.Write(buffer, binary.LittleEndian, byte(0))
binary.Write(buffer, binary.LittleEndian, byte(0))
r.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
_, err := r.conn.Write(buffer.Bytes())
return err
}
func (r *RemoteConsole) readResponse(timeout time.Duration) (int, int, []byte, error) {
r.readMutex.Lock()
defer r.readMutex.Unlock()
r.conn.SetReadDeadline(time.Now().Add(timeout))
var readBytes int
var err error
if r.queuedBuff != nil {
copy(r.readBuff, r.queuedBuff)
readBytes = len(r.queuedBuff)
r.queuedBuff = nil
} else {
readBytes, err = r.conn.Read(r.readBuff)
if err != nil {
return 0, 0, nil, err
}
}
dataSize, readBytes, err := r.readResponsePackageSize(readBytes)
if err != nil {
return 0, 0, nil, err
}
if dataSize > maxPackageSize {
return 0, 0, nil, ErrResponseTooLong
}
totalPackageSize := dataSize + fieldPackageSize
readBytes, err = r.readResponsePackage(totalPackageSize, readBytes)
if err != nil {
return 0, 0, nil, err
}
// The data has to be explicitly selected to prevent copying empty bytes.
data := r.readBuff[fieldPackageSize:totalPackageSize]
// Save not package related bytes for the next read.
if readBytes > totalPackageSize {
// start of the next buffer was at the end of this packet.
// save it for the next read.
// The data has to be explicitly selected to prevent copying empty bytes.
r.queuedBuff = r.readBuff[totalPackageSize:readBytes]
}
return r.readResponseData(data)
}
// readResponsePackageSize wait until first 4 bytes are read to get the package size.
// Takes as param how many bytes are already read. The returned size does not include the size field.
func (r *RemoteConsole) readResponsePackageSize(readBytes int) (int, int, error) {
for readBytes < fieldPackageSize {
// need the 4 byte packet size...
b, err := r.conn.Read(r.readBuff[readBytes:])
if err != nil {
return 0, 0, err
}
readBytes += b
}
var size int32
b := bytes.NewBuffer(r.readBuff[:fieldPackageSize])
err := binary.Read(b, binary.LittleEndian, &size)
if err != nil {
return 0, 0, err
}
if size < minPackageSize {
return 0, 0, ErrUnexpectedFormat
}
return int(size), readBytes, nil
}
// readResponsePackage waits until the whole package is read including the size field.
func (r *RemoteConsole) readResponsePackage(totalPackageSize, readBytes int) (int, error) {
for totalPackageSize > readBytes {
b, err := r.conn.Read(r.readBuff[readBytes:])
if err != nil {
return readBytes, err
}
readBytes += b
}
return readBytes, nil
}
func (r *RemoteConsole) readResponseData(data []byte) (int, int, []byte, error) {
var requestID, responseType int32
buffer := bytes.NewBuffer(data)
err := binary.Read(buffer, binary.LittleEndian, &requestID)
if err != nil {
return 0, 0, []byte{}, err
}
binary.Read(buffer, binary.LittleEndian, &responseType)
if err != nil {
return 0, 0, []byte{}, err
}
// the rest of the buffer is the body.
body := buffer.Bytes()
// remove the to null terminations
body = body[:len(body)-2]
return int(responseType), int(requestID), body, nil
}