-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflex.go
332 lines (307 loc) · 6.4 KB
/
reflex.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
package main
import (
"errors"
"fmt"
"math"
"net"
"strconv"
"strings"
"time"
)
type ReflexServer struct {
ralias string
rhost string
rport string
steamport string
rhostname string
rpass string
rmapname string
folder string
game string
id int16
rmaxclients byte
nclients byte
rclients []Client
rping time.Duration
ronline bool
// Extra fields.
tv string // SourceTV addr+port
}
var (
ErrShort = errors.New("unexpected end of data")
ErrShortInfo = errors.New("short info response")
ErrShortExtra = errors.New("short extra info data")
ErrShortPlayer = errors.New("short player response")
ErrBad = errors.New("bad response")
)
// s string
// b byte
// S int16
// l int32
// L uint64
// f float32
func unpack(format string, b []byte, a ...interface{}) ([]byte, error) {
if len(format) != len(a) {
return b, errors.New("unpack: bad format string or args")
}
for i := 0; i < len(format); i++ {
switch format[i] {
case 's':
s, ok := a[i].(*string)
if !ok {
return b, errors.New("unpack: expected *string")
}
*s = ""
if len(b) < 1 {
return b, ErrShort
}
var k int
for k = 0; k < len(b) && b[k] != 0; k++ {
*s += string(b[k])
}
if b[k] != 0 {
return b, ErrShort
}
b = b[k+1:]
case 'b':
bb, ok := a[i].(*byte)
if !ok {
return b, errors.New("unpack: expected *byte")
}
if len(b) < 1 {
return b, ErrShort
}
*bb = b[0]
b = b[1:]
case 'S':
n, ok := a[i].(*int16)
if !ok {
return b, errors.New("unpack: expected *int16")
}
if len(b) < 2 {
return b, ErrShort
}
*n = int16(b[0]) | (int16(b[1]) << 8)
b = b[2:]
case 'l':
n, ok := a[i].(*int32)
if !ok {
return b, errors.New("unpack: expected *int32")
}
if len(b) < 4 {
return b, ErrShort
}
*n = int32(b[0]) | (int32(b[1]) << 8) | (int32(b[2]) << 16) | (int32(b[3]) << 24)
b = b[4:]
case 'L':
n, ok := a[i].(*uint64)
if !ok {
return b, errors.New("unpack: expected *uint64")
}
if len(b) < 8 {
return b, ErrShort
}
*n = uint64(b[0]) | (uint64(b[1]) << 8) | (uint64(b[2]) << 16) |
(uint64(b[3]) << 24) | (uint64(b[4]) << 32) | (uint64(b[5]) << 40) |
(uint64(b[6]) << 48) | (uint64(b[7]) << 56)
b = b[8:]
case 'f':
n, ok := a[i].(*float32)
if !ok {
return b, errors.New("unpack: expected *float32")
}
if len(b) < 4 {
return b, ErrShort
}
i := uint32(b[0]) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
*n = math.Float32frombits(i)
b = b[4:]
}
}
return b, nil
}
func unpackEDF(b []byte, port *int16, tv *string) ([]byte, error) {
if len(b) < 1 {
return b, errors.New("unpack: short extra data")
}
edf := b[0]
b = b[1:]
var err error
if edf&0x80 != 0 { // port
if b, err = unpack("S", b, port); err != nil {
return b, err
}
}
if edf&0x10 != 0 { // SteamID
if len(b) < 8 {
return b, ErrShortExtra
}
b = b[8:]
}
if edf&0x40 != 0 { // SourceTV
var tvport int16
var tvsrv string
if b, err = unpack("Ss", b, &tvport, &tvsrv); err != nil {
return b, err
}
*tv = tvsrv + ":" + strconv.Itoa(int(tvport))
}
return b, nil
}
func unpackclients(b []byte) ([]Client, error) {
if len(b) < 6 {
return nil, ErrShortPlayer
}
if b[4] != 0x44 {
return nil, ErrBad
}
nclients := int(b[5])
b = b[6:]
cs := make([]Client, nclients)
var err error
for i := 0; i < nclients; i++ {
var idx byte
var score int32
var dur float32
b, err = unpack("bslf", b, &idx, &cs[i].name, &score, &dur)
if err != nil {
return cs, err
}
cs[i].score = int(score)
}
return cs, nil
}
var ports = []string{
"25797",
"25798",
"25799",
"25800",
"25801",
"25803",
"25805",
"25807",
}
func newreflexserver(alias, host, pass string) Server {
srv := &ReflexServer{ralias: alias, rpass: pass}
srv.steamport = "25797"
split := strings.Split(host, ":")
srv.rhost = split[0]
if len(split) > 1 {
srv.steamport = split[1]
}
return srv
}
func (srv *ReflexServer) query() error {
srv.ronline = false
var c net.Conn
var err error
ports[0] = srv.steamport
for i := range ports {
host := fmt.Sprintf("%s:%s", srv.rhost, ports[i])
c, err = net.DialTimeout("udp", host, 1*time.Second)
if err == nil {
break
}
}
if err != nil {
return err
}
c.SetDeadline(time.Now().Add(1 * time.Second))
defer c.Close()
// Info query.
b := []byte("\xFF\xFF\xFF\xFFTSource Engine Query\x00")
r := make([]byte, 8192)
rr := r
t := time.Now()
if _, err := c.Write(b); err != nil {
return err
}
n, err := c.Read(r)
if err != nil {
return err
}
srv.rping = time.Now().Sub(t)
if len(r) <= 6 {
return errors.New("short info response")
}
var dum byte
var version string
r, err = unpack("ssssSbbbbbbbs", r[6:n], &srv.rhostname,
&srv.rmapname, &srv.folder, &srv.game, &srv.id,
&srv.nclients, &srv.rmaxclients, &dum, &dum, &dum,
&dum, &dum, &version)
if err != nil {
return err
}
var port int16
r, err = unpackEDF(r, &port, &srv.tv)
srv.rport = strconv.Itoa(int(port))
srv.ronline = true
r = rr
// Player query.
// Challenge request.
b = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0xFF, 0xFF, 0xFF, 0xFF}
if _, err := c.Write(b); err != nil {
return err
}
if n, err = c.Read(r); err != nil {
return err
}
if n < 9 {
return ErrShort
}
// Send the challenge back.
copy(b[5:9], r[5:9])
if _, err := c.Write(b); err != nil {
return err
}
if n, err = c.Read(r); err != nil {
return err
}
if srv.rclients, err = unpackclients(r[:n]); err != nil {
return err
}
return nil
}
func (srv *ReflexServer) alias() string {
return srv.ralias
}
func (srv *ReflexServer) host() string {
return srv.rhost
}
func (srv *ReflexServer) hostname() string {
return srv.rhostname
}
func (srv *ReflexServer) password() string {
return srv.rpass
}
func (srv *ReflexServer) clients() Clients {
return srv.rclients
}
func (srv *ReflexServer) maxclients() int {
return int(srv.rmaxclients)
}
func (srv *ReflexServer) mapname() string {
return srv.rmapname
}
func (srv *ReflexServer) gametype() string {
return "(Reflex)"
}
func (srv *ReflexServer) timelimit() int {
return 0
}
func (srv *ReflexServer) fraglimit() int {
return 0
}
func (srv *ReflexServer) capturelimit() int {
return 0
}
func (srv *ReflexServer) ping() time.Duration {
return srv.rping
}
func (srv *ReflexServer) port() string {
return srv.rport
}
func (srv *ReflexServer) online() bool {
return srv.ronline
}