forked from sostronk/go-steam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.go
458 lines (398 loc) · 9.65 KB
/
comm.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package steam
import (
"bytes"
"fmt"
"math/rand"
"strconv"
"strings"
)
const (
hInfoRequest = 'T'
hInfoResponse = 'I'
hPlayersInfoRequest = 'U'
hPlayersInfoChallengeResponse = 'A'
hPlayersInfoResponse = 'D'
)
// ServerType indicates the type of the server.
type ServerType int
func (st *ServerType) unmarshalBinary(data []byte) error {
switch data[0] {
case 'd':
*st = STDedicated
case 'l':
*st = STNonDedicated
case 'p':
*st = STProxy
default:
return errBadData
}
return nil
}
func (st ServerType) String() string {
return serverTypeStrings[st]
}
const (
// STInvalid describes a invalid server type.
STInvalid ServerType = iota
// STDedicated indicates a dedicated server type.
STDedicated
// STNonDedicated indicates a non dedicated server type.
STNonDedicated
// STProxy indicates a proxy server type.
STProxy
)
var serverTypeStrings = map[ServerType]string{
STInvalid: "Invalid",
STDedicated: "Dedicated",
STNonDedicated: "Non Dedicated",
STProxy: "Proxy",
}
// Environment indicates the server's host environment.
type Environment int
func (e *Environment) unmarshalBinary(data []byte) error {
switch data[0] {
case 'l':
*e = ELinux
return nil
case 'w':
*e = EWindows
return nil
case 'm', 'o':
*e = EMac
return nil
default:
return errBadData
}
}
func (e Environment) String() string {
return environmentStrings[e]
}
const (
// EInvalid indicates a invalid host environment.
EInvalid Environment = iota
// ELinux indicates that the server is hosted on Linux.
ELinux
// EWindows indicates that the server is hosted on Windows.
EWindows
// EMac indicates that the server is hosted on Mac OS X.
EMac
)
var environmentStrings = map[Environment]string{
EInvalid: "Invalid",
ELinux: "Linux",
EWindows: "Windows",
EMac: "Mac",
}
// Visibility indicates the visibility of the server.
type Visibility int
func (v *Visibility) unmarshalBinary(data []byte) error {
switch data[0] {
case 0:
*v = VPublic
return nil
case 1:
*v = VPrivate
return nil
default:
return errBadData
}
}
func (v Visibility) String() string {
return visibilityStrings[v]
}
const (
// VInvalid indicates a invalid visibility.
VInvalid Visibility = iota
// VPublic indicates a publicly visibly server.
VPublic
// VPrivate indicates a private server.
VPrivate
)
var visibilityStrings = map[Visibility]string{
VInvalid: "Invalid",
VPublic: "Public",
VPrivate: "Private",
}
// VAC indicates the status of the Valve Anti Cheat system.
type VAC int
func (v *VAC) unmarshalBinary(data []byte) error {
switch data[0] {
case 0:
*v = VACUnsecured
return nil
case 1:
*v = VACSecure
return nil
default:
return errBadData
}
}
func (v VAC) String() string {
return vacStrings[v]
}
const (
// VACInvalid indicates a invalid VAC configuration.
VACInvalid VAC = iota
// VACUnsecured indicates a insecure server (VAC disabled).
VACUnsecured
// VACSecure indicates a secure server (VAC enabled).
VACSecure
)
var vacStrings = map[VAC]string{
VACInvalid: "Invalid",
VACUnsecured: "Unsecured",
VACSecure: "Secured",
}
type infoRequest struct {
}
func (infoRequest) marshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
writeRequestPrefix(buf)
writeByte(buf, hInfoRequest)
writeString(buf, "Source Engine Query")
return buf.Bytes(), nil
}
// InfoResponse represents a response to a info query.
type InfoResponse struct {
Protocol int
Name string
Map string
Folder string
Game string
ID int
Players int
MaxPlayers int
Bots int
ServerType ServerType
Environment Environment
Visibility Visibility
VAC VAC
Version string
Port int
SteamID int64
SourceTVPort int
SourceTVName string
Keywords string
GameID int64
}
const (
edfPort = 0x80
edfSteamID = 0x10
edfSourceTV = 0x40
edfKeywords = 0x20
edfGameID = 0x01
)
func (r *InfoResponse) unmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
buf := bytes.NewBuffer(data)
header := readByte(buf)
if header != hInfoResponse {
panic(errBadData)
}
r.Protocol = toInt(readByte(buf))
r.Name = readString(buf)
r.Map = readString(buf)
r.Folder = readString(buf)
r.Game = readString(buf)
r.ID = toInt(readShort(buf))
r.Players = toInt(readByte(buf))
r.MaxPlayers = toInt(readByte(buf))
r.Bots = toInt(readByte(buf))
must(r.ServerType.unmarshalBinary(readBytes(buf, 1)))
must(r.Environment.unmarshalBinary(readBytes(buf, 1)))
must(r.Visibility.unmarshalBinary(readBytes(buf, 1)))
must(r.VAC.unmarshalBinary(readBytes(buf, 1)))
r.Version = readString(buf)
// Check if EDF byte is present
if buf.Len() < 1 {
return nil
}
// EDF byte present
edf := readByte(buf)
if edf&edfPort != 0 {
r.Port = toInt(readShort(buf))
}
if edf&edfSteamID != 0 {
r.SteamID = readLongLong(buf)
}
if edf&edfSourceTV != 0 {
r.SourceTVPort = toInt(readShort(buf))
r.SourceTVName = readString(buf)
}
if edf&edfKeywords != 0 {
r.Keywords = readString(buf)
}
if edf&edfGameID != 0 {
r.GameID = readLongLong(buf)
r.ID = int(r.GameID & 0xFFFFFF)
}
return nil
}
func (r *InfoResponse) String() string {
return fmt.Sprintf("%v %v %v/%v (%v bots) %v", r.Name, r.Map, r.Players, r.MaxPlayers, r.Bots, r.VAC)
}
type playersInfoRequest struct {
Challenge int
}
func (r playersInfoRequest) marshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
writeRequestPrefix(buf)
writeByte(buf, hPlayersInfoRequest)
writeLong(buf, int32(r.Challenge))
return buf.Bytes(), nil
}
func isPlayersInfoChallengeResponse(b []byte) bool {
return b[0] == hPlayersInfoChallengeResponse
}
type playersInfoChallengeResponse struct {
Challenge int
}
func (r *playersInfoChallengeResponse) unmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
buf := bytes.NewBuffer(data)
header := readByte(buf)
if header != hPlayersInfoChallengeResponse {
panic(errBadData)
}
r.Challenge = toInt(readLong(buf))
return nil
}
// PlayersInfoResponse represents a response to a player info query.
type PlayersInfoResponse struct {
Players []*Player
}
func (r *PlayersInfoResponse) unmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
buf := bytes.NewBuffer(data)
header := readByte(buf)
if header != hPlayersInfoResponse {
panic(errBadData)
}
count := toInt(readByte(buf))
for i := 0; i < count; i++ {
// Read the chunk index
readByte(buf)
var p Player
p.Name = readString(buf)
p.Score = toInt(readLong(buf))
p.Duration = float64(readFloat(buf))
r.Players = append(r.Players, &p)
}
return nil
}
// Player represents a player entity in the server.
type Player struct {
Name string
Score int
Duration float64
}
type rconRequestType int32
const (
rrtAuth rconRequestType = 3
rrtExecCmd rconRequestType = 2
rrtAuthResp rconRequestType = 2
rrtRespValue rconRequestType = 0
)
type rconRequest struct {
size int32
id int32
typ rconRequestType
body string
}
func (r *rconRequest) String() string {
return fmt.Sprintf("%v %v %v %v", r.size, r.id, r.typ, r.body)
}
func newRCONRequest(typ rconRequestType, body string) *rconRequest {
return &rconRequest{
size: int32(len(body) + 10),
id: rand.Int31(),
typ: typ,
body: body,
}
}
func (r *rconRequest) marshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
writeLong(buf, r.size)
writeLong(buf, r.id)
writeLong(buf, int32(r.typ))
buf.WriteString(r.body)
writeNull(buf)
writeNull(buf)
return buf.Bytes(), nil
}
type rconResponse struct {
size int32
id int32
typ rconRequestType
body []byte
}
func (r *rconResponse) String() string {
return fmt.Sprintf("%v %v %v %v", r.size, r.id, r.typ, string(r.body))
}
func (r *rconResponse) unmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
buf := bytes.NewBuffer(data)
r.size = readLong(buf)
r.id = readLong(buf)
r.typ = rconRequestType(readLong(buf))
r.body = readBytes(buf, int(r.size-10))
return nil
}
// StatsResponse represents a stats response from server.
//
// srcds typically sends the following output:
//
// CPU NetIn NetOut Uptime Maps FPS Players Svms +-ms ~tick
// 10.0 241763.2 1518923.5 10419 58 127.98 16 3.72 1.56 0.36
type StatsResponse struct {
CPU int
NetIn float64
NetOut float64
Uptime int
Maps int
FPS float64
Players int
Sv float64
SvDeviation float64
Tick float64
}
func (r *StatsResponse) String() string {
return fmt.Sprintf("cpu: %d netin: %0.2f netout: %0.2f uptime: %d maps: %d fps: %0.2f players: %d sv: %0.2f sv+-: %0.2f tick: %0.2f",
r.CPU, r.NetIn, r.NetOut, r.Uptime, r.Maps, r.FPS, r.Players, r.Sv, r.SvDeviation, r.Tick,
)
}
func (r *StatsResponse) unmarshalString(data string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
fields := strings.Fields(data)
r.CPU = int(mustInterface(strconv.ParseFloat(fields[10], 64)).(float64))
r.NetIn = mustInterface(strconv.ParseFloat(fields[11], 64)).(float64)
r.NetOut = mustInterface(strconv.ParseFloat(fields[12], 64)).(float64)
r.Uptime = mustInterface(strconv.Atoi(fields[13])).(int)
r.Maps = mustInterface(strconv.Atoi(fields[14])).(int)
r.FPS = mustInterface(strconv.ParseFloat(fields[15], 64)).(float64)
r.Players = mustInterface(strconv.Atoi(fields[16])).(int)
r.Sv = mustInterface(strconv.ParseFloat(fields[17], 64)).(float64)
r.SvDeviation = mustInterface(strconv.ParseFloat(fields[18], 64)).(float64)
r.Tick = mustInterface(strconv.ParseFloat(fields[19], 64)).(float64)
return nil
}