-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrcon.go
223 lines (189 loc) · 5.14 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
package rcon
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"sync"
"sync/atomic"
"time"
)
const (
cmdAuth = 3
cmdExecCommand = 2
respResponse = 0
respAuthResponse = 2
)
// 12 byte header, up to 4096 bytes of data, 2 bytes for null terminators.
// this should be the absolute max size of a single response.
const readBufferSize = 4110
type RemoteConsole struct {
conn net.Conn
readbuf []byte
readmu sync.Mutex
reqid int32
queuedbuf []byte
}
var (
ErrAuthFailed = errors.New("rcon: authentication failed")
ErrInvalidAuthResponse = errors.New("rcon: invalid response type during auth")
ErrUnexpectedFormat = errors.New("rcon: unexpected response format")
ErrCommandTooLong = errors.New("rcon: command too long")
ErrResponseTooLong = errors.New("rcon: response too long")
)
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
}
var reqid int
r := &RemoteConsole{conn: conn, reqid: 0x7fffffff}
reqid, err = r.writeCmd(cmdAuth, password)
if err != nil {
return nil, err
}
r.readbuf = make([]byte, readBufferSize)
var respType, requestId int
respType, requestId, _, err = r.readResponse(timeout)
if err != nil {
return nil, 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 != respAuthResponse {
respType, requestId, _, err = r.readResponse(timeout)
}
if err != nil {
return nil, err
}
if respType != respAuthResponse {
return nil, ErrInvalidAuthResponse
}
if requestId != reqid {
return nil, ErrAuthFailed
}
return r, nil
}
func (r *RemoteConsole) LocalAddr() net.Addr {
return r.conn.LocalAddr()
}
func (r *RemoteConsole) RemoteAddr() net.Addr {
return r.conn.RemoteAddr()
}
func (r *RemoteConsole) Write(cmd string) (requestId int, err error) {
return r.writeCmd(cmdExecCommand, cmd)
}
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 != respResponse {
response = ""
requestId = 0
} else {
response = string(respBytes)
}
return
}
func (r *RemoteConsole) Close() error {
return r.conn.Close()
}
func newRequestId(id int32) int32 {
if id&0x0fffffff != id {
return int32((time.Now().UnixNano() / 100000) % 100000)
}
return id + 1
}
func (r *RemoteConsole) writeCmd(cmdType int32, str string) (int, error) {
if len(str) > 1024-10 {
return -1, ErrCommandTooLong
}
buffer := bytes.NewBuffer(make([]byte, 0, 14+len(str)))
reqid := atomic.LoadInt32(&r.reqid)
reqid = newRequestId(reqid)
atomic.StoreInt32(&r.reqid, reqid)
// packet size
binary.Write(buffer, binary.LittleEndian, int32(10+len(str)))
// request id
binary.Write(buffer, binary.LittleEndian, int32(reqid))
// auth cmd
binary.Write(buffer, binary.LittleEndian, int32(cmdType))
// string (null terminated)
buffer.WriteString(str)
binary.Write(buffer, binary.LittleEndian, byte(0))
// string 2 (null terminated)
// we don't have a use for string 2
binary.Write(buffer, binary.LittleEndian, byte(0))
r.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
_, err := r.conn.Write(buffer.Bytes())
return int(reqid), err
}
func (r *RemoteConsole) readResponse(timeout time.Duration) (int, int, []byte, error) {
r.readmu.Lock()
defer r.readmu.Unlock()
r.conn.SetReadDeadline(time.Now().Add(timeout))
var size int
var err error
if r.queuedbuf != nil {
copy(r.readbuf, r.queuedbuf)
size = len(r.queuedbuf)
r.queuedbuf = nil
} else {
size, err = r.conn.Read(r.readbuf)
if err != nil {
return 0, 0, nil, err
}
}
if size < 4 {
// need the 4 byte packet size...
s, err := r.conn.Read(r.readbuf[size:])
if err != nil {
return 0, 0, nil, err
}
size += s
}
var dataSize32 int32
b := bytes.NewBuffer(r.readbuf[:size])
binary.Read(b, binary.LittleEndian, &dataSize32)
if dataSize32 < 10 {
return 0, 0, nil, ErrUnexpectedFormat
}
totalSize := size
dataSize := int(dataSize32)
if dataSize > 4106 {
return 0, 0, nil, ErrResponseTooLong
}
for dataSize+4 > totalSize {
size, err := r.conn.Read(r.readbuf[totalSize:])
if err != nil {
return 0, 0, nil, err
}
totalSize += size
}
data := r.readbuf[4 : 4+dataSize]
if totalSize > dataSize+4 {
// start of the next buffer was at the end of this packet.
// save it for the next read.
r.queuedbuf = r.readbuf[4+dataSize : totalSize]
}
return r.readResponseData(data)
}
func (r *RemoteConsole) readResponseData(data []byte) (int, int, []byte, error) {
var requestId, responseType int32
var response []byte
b := bytes.NewBuffer(data)
binary.Read(b, binary.LittleEndian, &requestId)
binary.Read(b, binary.LittleEndian, &responseType)
response, err := b.ReadBytes(0x00)
if err != nil && err != io.EOF {
return 0, 0, nil, err
}
if err == nil {
// if we didn't hit EOF, we have a null byte to remove
response = response[:len(response)-1]
}
return int(responseType), int(requestId), response, nil
}