/
steamserverinfo.go
executable file
·392 lines (316 loc) · 9.71 KB
/
steamserverinfo.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
// steamserverinfo.go
// https://github.com/wriley/steamserverinfo
//
// A server query program written in golang that uses the Steam Server Query API
// https://developer.valvesoftware.com/wiki/Server_queries
//
// Ported from original C version found here https://github.com/wriley/arma2serverinfo
package main
import (
"fmt"
"net"
"os"
"time"
"encoding/binary"
"bytes"
"path/filepath"
"encoding/hex"
)
var debug bool = false
var colorize bool = false
func CheckNoError(err error) bool {
if err != nil {
fmt.Println("Error: ", err)
return false
} else {
return true
}
}
func Colorize(s string) string {
return "\033[1;31m" + s + "\033[0m"
}
func MyHexDump(arr []byte, s int) string {
var b = make([]byte, s)
for i := 0; i < s; i++ {
b[i] = arr[i]
}
if colorize {
return Colorize(hex.Dump(b))
} else {
return hex.Dump(b)
}
}
func SendPacket(conn net.Conn, arr []byte, timeout time.Duration) (int, []byte) {
if(debug) { fmt.Fprintln(os.Stderr, "Writing...") }
ret, err := conn.Write(arr)
if(debug) { fmt.Fprintf(os.Stderr, MyHexDump(arr, ret)) }
if CheckNoError(err) {
if(debug) { fmt.Fprintf(os.Stderr, "Wrote %d bytes\n", ret) }
buffer := make([]byte, 1500)
if(debug) { fmt.Fprintln(os.Stderr, "Reading...") }
conn.SetReadDeadline(time.Now().Add(timeout))
n, err := conn.Read(buffer)
if CheckNoError(err) {
if(debug) { fmt.Fprintf(os.Stderr, "Read %d bytes\n", n) }
if(debug) { fmt.Fprintf(os.Stderr, MyHexDump(buffer, n)) }
return n, buffer
} else {
return 0, nil
}
} else {
return 0, nil
}
}
func stripCtlAndExtFromBytes(str string) string {
b := make([]byte, len(str))
var bl int
for i := 0; i < len(str); i++ {
c := str[i]
if c >= 32 && c < 127 {
b[bl] = c
bl++
}
}
return string(b[:bl])
}
func GetString(arr []byte, index int) (string, int) {
data := ""
for i := index; i < len(arr); i++ {
index = i
if arr[i] == 0x00 {
break
} else {
data = data + string(arr[i])
}
}
index++
data = stripCtlAndExtFromBytes(data)
return data, index
}
func GetUInt16(arr []byte, index int) (uint16, int) {
num1 := arr[index]
index++
num2 := arr[index]
index++
num := uint16(num1) | uint16(num2)<<8
return num, index
}
func GetUInt32(arr []byte, index int) (uint32, int) {
num1 := arr[index]
index++
num2 := arr[index]
index++
num3 := arr[index]
index++
num4 := arr[index]
index++
num := uint32(num4)<<24 | uint32(num3)<<16 | uint32(num2)<<8 | uint32(num1)
return num, index
}
func CheckHeader(hdr byte, chk byte) bool {
if hdr != chk {
fmt.Fprintf(os.Stderr, "Header was 0x%x instead of 0x%x\n", hdr, chk)
return false
} else {
return true
}
}
func main() {
A2S_INFO := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00}
CHALLENGE_RESPONSE := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00}
A2S_RULES := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x56, 0xFF, 0xFF, 0xFF, 0xFF}
A2S_PLAYER := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0xFF, 0xFF, 0xFF, 0xFF}
argsWithProg := os.Args
if len(argsWithProg) < 3 {
fmt.Printf("Usage: %s <server> <port>\n", filepath.Base(argsWithProg[0]))
os.Exit(1)
}
server := argsWithProg[1]
port := argsWithProg[2]
if len(argsWithProg) > 3 {
debug = true
}
if len(argsWithProg) > 4 {
colorize = true
}
seconds := 15
timeout := time.Duration(seconds) * time.Second
if(debug) { fmt.Fprintln(os.Stderr, "Opening UDP connection...") }
Conn, err := net.DialTimeout("udp", server+":"+port, timeout)
if !CheckNoError(err) {
os.Exit(2)
}
defer Conn.Close()
// Get Info
if(debug) { fmt.Fprintln(os.Stderr, "Sending A2S_INFO...") }
start := time.Now()
n, BytesReceived := SendPacket(Conn, A2S_INFO, timeout)
t := time.Now()
elapsed1 := t.Sub(start)
if BytesReceived == nil || n == 0 {
fmt.Fprintln(os.Stderr, "Received no data!")
os.Exit(2)
}
if CheckHeader(BytesReceived[4], 0x41) {
CHALLENGE_RESPONSE[25] = BytesReceived[5]
CHALLENGE_RESPONSE[26] = BytesReceived[6]
CHALLENGE_RESPONSE[27] = BytesReceived[7]
CHALLENGE_RESPONSE[28] = BytesReceived[8]
n, BytesReceived = SendPacket(Conn, CHALLENGE_RESPONSE, timeout)
}
if !CheckHeader(BytesReceived[4], 0x49) { os.Exit(2) }
if(debug) { fmt.Fprintf(os.Stderr, "HEADER: 0x%x\n", BytesReceived[4]) }
if(debug) { fmt.Fprintf(os.Stderr, "PROTOCOL: 0x%x\n", BytesReceived[5]) }
var sPtr int
var info string
sPtr = 5
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("NAME: %s\n", info)
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("MAP: %s\n", info)
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("FOLDER: %s\n", info)
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("GAME: %s\n", info)
var id uint16
id, sPtr = GetUInt16(BytesReceived, sPtr)
fmt.Printf("ID: %d\n", id)
fmt.Printf("PLAYERS: %d\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("MAXPLAYERS: %d\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("BOTS: %d\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("SERVERTYPE: %c\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("ENVIRONMENT: %c\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("VISIBILITY: %d\n", BytesReceived[sPtr])
sPtr++
fmt.Printf("VAC: %d\n", BytesReceived[sPtr])
sPtr++
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("VERSION: %s\n", info)
if n > sPtr {
// EDF
edf := BytesReceived[sPtr]
sPtr++
// PORT
if edf & 0x80 != 0 {
var port uint16
port, _ = GetUInt16(BytesReceived, sPtr)
fmt.Printf("PORT: %d\n", port)
}
// STEAMID
if edf & 0x10 != 0 {
sPtr += 8
}
// Keywords
if edf & 0x20 != 0 {
info, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("KEYWORDS: %s\n", info)
}
}
// Get Rules
sPtr = 5
if(debug) { fmt.Fprintln(os.Stderr, "Sending A2S_RULES...") }
start = time.Now()
n, BytesReceived = SendPacket(Conn, A2S_RULES, timeout)
t = time.Now()
elapsed2 := t.Sub(start)
if BytesReceived == nil || n == 0 {
fmt.Fprintln(os.Stderr, "Received no data!")
os.Exit(2)
}
if !CheckHeader(BytesReceived[4], 0x41) { os.Exit(2) }
// Challenge number
var chnum uint32
chnum, sPtr = GetUInt32(BytesReceived, sPtr)
if(debug) { fmt.Fprintf(os.Stderr,"Challenge number: %d\n", chnum) }
A2S_RULES[5] = byte(chnum)
A2S_RULES[6] = byte(chnum >> 8)
A2S_RULES[7] = byte(chnum >> 16)
A2S_RULES[8] = byte(chnum >> 24)
if(debug) { fmt.Fprintln(os.Stderr, "Sending A2S_RULES...") }
start = time.Now()
n, BytesReceived = SendPacket(Conn, A2S_RULES, timeout)
t = time.Now()
elapsed3 := t.Sub(start)
if BytesReceived == nil || n == 0 {
fmt.Fprintln(os.Stderr, "Received no data!")
os.Exit(2)
}
elapsed := (elapsed1 + elapsed2 + elapsed3) / 3
fmt.Printf("PING: %d\n", int(elapsed) / 1000000)
if !CheckHeader(BytesReceived[4], 0x45) { os.Exit(2) }
// reset sPtr
sPtr = 5
var rules uint16
rules, sPtr = GetUInt16(BytesReceived, sPtr)
if(rules > 0) {
fmt.Println("RULE LIST:")
}
for i := uint16(0); i < rules; i++ {
// Name
info, sPtr = GetString(BytesReceived, sPtr)
// Value
val := ""
val, sPtr = GetString(BytesReceived, sPtr)
fmt.Printf("%s %s\n", info, val)
}
// Get Players
sPtr = 5
if(debug) { fmt.Fprintln(os.Stderr, "Sending A2S_PLAYER...") }
n, BytesReceived = SendPacket(Conn, A2S_PLAYER, timeout)
if BytesReceived == nil || n == 0 {
fmt.Fprintln(os.Stderr, "Received no data!")
os.Exit(2)
}
if !CheckHeader(BytesReceived[4], 0x41) { os.Exit(2) }
// Challenge number
chnum, sPtr = GetUInt32(BytesReceived, sPtr)
if(debug) { fmt.Fprintf(os.Stderr,"Challenge number: %d\n", chnum) }
A2S_PLAYER[5] = byte(chnum)
A2S_PLAYER[6] = byte(chnum >> 8)
A2S_PLAYER[7] = byte(chnum >> 16)
A2S_PLAYER[8] = byte(chnum >> 24)
if(debug) { fmt.Fprintln(os.Stderr, "Sending A2S_PLAYER...") }
n, BytesReceived = SendPacket(Conn, A2S_PLAYER, timeout)
if BytesReceived == nil || n == 0 {
fmt.Fprintln(os.Stderr, "Received no data!")
os.Exit(2)
}
if !CheckHeader(BytesReceived[4], 0x44) { os.Exit(2) }
sPtr = 5
players := BytesReceived[sPtr]
sPtr++
if players > 0 {
fmt.Println("PLAYER LIST:");
}
var score uint32
for i := 0; i < int(players); i++ {
// Index (this seems to always be 0, so skipping it)
sPtr++
// Name
info, sPtr = GetString(BytesReceived, sPtr)
// Score
score, sPtr = GetUInt32(BytesReceived, sPtr)
// Duration
b := []byte{0x00, 0x00, 0x00, 0x00}
b[0] = BytesReceived[sPtr]
sPtr++
b[1] = BytesReceived[sPtr]
sPtr++
b[2] = BytesReceived[sPtr]
sPtr++
b[3] = BytesReceived[sPtr]
sPtr++
var duration float32
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.LittleEndian, &duration)
if err != nil {
fmt.Fprintln(os.Stderr, "Float conversion failed:", err)
}
fmt.Printf("%s %d %.0f\n", info, score, duration)
}
}