-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrcon.go
163 lines (142 loc) · 4.01 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
package main
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"sync"
"log"
"fmt"
"sync/atomic"
"time"
)
const (
SERVERDATA_AUTH = 3
SERVERDATA_EXECCOMMAND = 2
SERVERDATA_AUTH_RESPONSE = 0
SERVERDATA_RESPONSE_VALUE = 2
)
const readBufferSize = 4110
// RCON Protocol specs can be found here: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
// Thanks to https://github.com/james4k/ (james4k) for some of the RCON functions
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")
ErrResponseTooLong = errors.New("rcon: response too long")
)
func (r *RemoteConsole) WriteData(data string, v ...interface{}) (requestId int, err error){
buffer := fmt.Sprintf(data, v...)
log.Printf("Sent(RCON): %s\n", buffer)
return r.writeCmd(SERVERDATA_EXECCOMMAND, buffer)
}
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 != SERVERDATA_RESPONSE_VALUE {
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) {
buffer := bytes.NewBuffer(make([]byte, 0, 14+len(str)))
reqid := atomic.LoadInt32(&r.reqid)
reqid = newRequestId(reqid)
atomic.StoreInt32(&r.reqid, reqid)
binary.Write(buffer, binary.LittleEndian, int32(10+len(str)))
binary.Write(buffer, binary.LittleEndian, int32(reqid))
binary.Write(buffer, binary.LittleEndian, int32(cmdType))
buffer.WriteString(str)
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 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
}