-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.go
656 lines (557 loc) · 18.7 KB
/
query.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package minequery
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
)
var (
queryRequestHeader = []byte{0xfe, 0xfd}
queryResponseStringTerminator = []byte{0x0}
queryFullStatPadding = []byte{0xff, 0xff, 0xff, 0x01}
queryKVSectionPadding = []byte{0x73, 0x70, 0x6c, 0x69, 0x74, 0x6e, 0x75, 0x6d, 0x00, 0x80, 0x00}
queryPlayerSectionPadding = []byte{0x01, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x5f, 0x00, 0x00}
)
const (
queryPacketTypeHandshake byte = 9
queryPacketTypeStat byte = 0
)
const (
querySessionIDMask int32 = 0x0f0f0f0f
)
const (
queryGameType = "SMP"
queryGameID = "MINECRAFT"
)
// BasicQueryStatus holds basic, simplified query status response returned Minecraft servers via Query protocol.
type BasicQueryStatus struct {
MOTD string
GameType string
Map string
OnlinePlayers int
MaxPlayers int
Port int
Host string
}
// FullQueryPluginEntry holds plugin entry info (name and version) of plugin sent via Query protocol.
type FullQueryPluginEntry struct {
Name string
Version string
}
// FullQueryStatus holds full query status response returned Minecraft servers via Query protocol.
type FullQueryStatus struct {
MOTD string
GameType string
GameID string
Version string
ServerVersion string
Plugins []FullQueryPluginEntry
Map string
OnlinePlayers int
MaxPlayers int
SamplePlayers []string
Port int
Host string
Data map[string]string
}
// QueryBasic queries Minecraft servers and returns simplified query response.
//
//goland:noinspection GoUnusedExportedFunction
func QueryBasic(host string, port int) (*BasicQueryStatus, error) {
return defaultPinger.QueryBasic(host, port)
}
// QueryBasic queries Minecraft servers and returns simplified query response.
//
//goland:noinspection GoUnusedExportedFunction
func (p *Pinger) QueryBasic(host string, port int) (*BasicQueryStatus, error) {
// Try to use cache first.
sessionData, hit := p.getCachedSession(host, port)
if hit {
// Open UDP connection with predefined local address from cache.
conn, err := p.openUDPConnWithLocalAddr(host, port, sessionData.Address)
if err != nil {
return nil, err
}
// Request basic query info with cached session.
res, err := p.requestBasicStat(conn, sessionData)
if err == nil {
_ = conn.Close()
return res, nil
}
_ = conn.Close()
// On error, fall back to creating a new session.
}
// Open UDP connection.
conn, err := p.openUDPConn(host, port)
if err != nil {
return nil, err
}
defer func() { _ = conn.Close() }()
// Create a new session and obtain challenge token.
sessionData, err = p.createAndCacheSession(port, host, conn)
if err != nil {
return nil, err
}
// Request basic query info with newly created session.
res, err := p.requestBasicStat(conn, sessionData)
if err != nil {
return nil, err
}
return res, nil
}
// QueryFull queries Minecraft servers and returns full query response.
//
//goland:noinspection GoUnusedExportedFunction
func QueryFull(host string, port int) (*FullQueryStatus, error) {
return defaultPinger.QueryFull(host, port)
}
// QueryFull queries Minecraft servers and returns full query response.
//
//goland:noinspection GoUnusedExportedFunction
func (p *Pinger) QueryFull(host string, port int) (*FullQueryStatus, error) {
// Try to use cache first.
sessionData, hit := p.getCachedSession(host, port)
if hit {
// Open UDP connection with predefined local address from cache.
conn, err := p.openUDPConnWithLocalAddr(host, port, sessionData.Address)
if err != nil {
return nil, err
}
// Request full query info with cached session.
res, err := p.requestFullStat(conn, sessionData)
if err == nil {
_ = conn.Close()
return res, nil
}
_ = conn.Close()
// On error, fall back to creating a new session.
}
// Open UDP connection.
conn, err := p.openUDPConn(host, port)
if err != nil {
return nil, err
}
defer func() { _ = conn.Close() }()
// Create a new session and obtain challenge token.
sessionData, err = p.createAndCacheSession(port, host, conn)
if err != nil {
return nil, err
}
// Request full query info with newly created session.
res, err := p.requestFullStat(conn, sessionData)
if err != nil {
return nil, err
}
return res, nil
}
func (p *Pinger) requestBasicStat(conn *net.UDPConn, session session) (*BasicQueryStatus, error) {
if err := p.writeQueryBasicStatPacket(conn, session.SessionID, session.Token); err != nil {
return nil, err
}
content, err := p.readQueryStatResponsePacket(conn, session.SessionID)
if err != nil {
return nil, err
}
return p.parseQueryBasicStatResponse(content)
}
func (p *Pinger) requestFullStat(conn *net.UDPConn, session session) (*FullQueryStatus, error) {
if err := p.writeQueryFullStatPacket(conn, session.SessionID, session.Token); err != nil {
return nil, err
}
content, err := p.readQueryStatResponsePacket(conn, session.SessionID)
if err != nil {
return nil, err
}
return p.parseQueryFullStatResponse(content)
}
// Session management
type session struct {
SessionID, Token int32
Address string
}
func getSessionCacheKey(host string, port int) string { return fmt.Sprintf("%s:%d", host, port) }
func generateSessionID() int32 { return int32(time.Now().Unix()) & querySessionIDMask }
func (p *Pinger) createAndCacheSession(port int, host string, conn *net.UDPConn) (session, error) {
// Generate new time-based session ID and write a handshake packet
sessionID := generateSessionID()
if err := p.writeQueryHandshakePacket(conn, sessionID); err != nil {
return session{}, err
}
// Read response packet and get data stream
content, err := p.readQueryHandshakeResponsePacket(conn, sessionID)
if err != nil {
return session{}, err
}
// Parse response and obtain challenge token
token, err := p.parseQueryHandshakeResponse(content)
if err != nil {
return session{}, err
}
sessionData := session{sessionID, token, conn.LocalAddr().String()}
if p.SessionCache != nil {
p.SessionCache.SetDefault(getSessionCacheKey(host, port), sessionData)
}
return sessionData, nil
}
func (p *Pinger) getCachedSession(host string, port int) (session, bool) {
if p.SessionCache == nil {
return session{}, false
}
key := getSessionCacheKey(host, port)
data, hit := p.SessionCache.Get(key)
if !hit {
return session{}, false
}
sessionData := data.(session)
return sessionData, true
}
// Communication
func (p *Pinger) writeQueryHandshakePacket(conn *net.UDPConn, sessionID int32) error {
var packet bytes.Buffer
// Write request packet header
_, _ = packet.Write(queryRequestHeader)
// Write packet type
_ = packet.WriteByte(queryPacketTypeHandshake)
// Write session ID
_ = binary.Write(&packet, binary.BigEndian, sessionID)
_, err := packet.WriteTo(conn)
return err
}
func (p *Pinger) readQueryHandshakeResponsePacket(conn *net.UDPConn, sessionID int32) (io.Reader, error) {
// Read UDP packet into 1024 byte buffer and create a reader
// of it to read data sequentially.
b := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(b)
if err != nil {
return nil, err
}
reader := bytes.NewReader(b[:n])
// Read packet type, which must be handshake
id, err := reader.ReadByte()
if err != nil {
return nil, err
} else if id != queryPacketTypeHandshake {
return nil, fmt.Errorf("expected packet ID %#x, but instead got %#x", queryPacketTypeHandshake, id)
}
// Read session ID from response, return an error if it's
// not the one in request.
var resSessionID int32
if err = binary.Read(reader, binary.BigEndian, &resSessionID); err != nil {
return nil, err
} else if resSessionID != sessionID {
return nil, fmt.Errorf("expected session ID %#x, but instead got %#x", sessionID, resSessionID)
}
return reader, nil
}
func (p *Pinger) writeQueryBasicStatPacket(conn *net.UDPConn, sessionID int32, token int32) error {
var packet bytes.Buffer
// Write request packet header
_, _ = packet.Write(queryRequestHeader)
// Write packet type
_ = packet.WriteByte(queryPacketTypeStat)
// Write session ID
_ = binary.Write(&packet, binary.BigEndian, sessionID)
// Write token
_ = binary.Write(&packet, binary.BigEndian, token)
_, err := packet.WriteTo(conn)
return err
}
func (p *Pinger) readQueryStatResponsePacket(conn *net.UDPConn, sessionID int32) (io.Reader, error) {
// Read UDP packet into 1024 byte buffer and create a reader
// of it to read data sequentially.
b := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(b)
if err != nil {
return nil, err
}
reader := bytes.NewReader(b[:n])
// Read packet type, which must be stat
id, err := reader.ReadByte()
if err != nil {
return nil, err
} else if id != queryPacketTypeStat {
return nil, fmt.Errorf("expected packet ID %#x, but instead got %#x", queryPacketTypeStat, id)
}
// Read session ID from response, return an error if it's
// not the one in request.
var resSessionID int32
if err = binary.Read(reader, binary.BigEndian, &resSessionID); err != nil {
return nil, err
} else if resSessionID != sessionID {
return nil, fmt.Errorf("expected session ID %#x, but instead got %#x", sessionID, resSessionID)
}
return reader, nil
}
func (p *Pinger) writeQueryFullStatPacket(conn *net.UDPConn, sessionID int32, token int32) error {
var packet bytes.Buffer
// Write request packet header
_, _ = packet.Write(queryRequestHeader)
// Write packet type
_ = packet.WriteByte(queryPacketTypeStat)
// Write session ID
_ = binary.Write(&packet, binary.BigEndian, sessionID)
// Write token
_ = binary.Write(&packet, binary.BigEndian, token)
// Write padding
_, _ = packet.Write(queryFullStatPadding)
_, err := packet.WriteTo(conn)
return err
}
// Response processing
func (p *Pinger) parseQueryHandshakeResponse(reader io.Reader) (int32, error) {
// Read all the remaining data in packet and ensure it has NUL terminator (if UseStrict)
token, _ := readAll(reader)
if len(token) == 0 {
return 0, fmt.Errorf("challenge token is empty")
}
if bytes.HasSuffix(token, queryResponseStringTerminator) {
token = token[:len(token)-1]
} else if p.UseStrict {
return 0, fmt.Errorf("challenge token did not end with NUL byte")
}
// Parse token string to int
tokenInt, err := strconv.ParseInt(string(token), 10, 32)
if err != nil {
return 0, err
}
return int32(tokenInt), nil
}
func (p *Pinger) parseQueryBasicStatResponse(reader io.Reader) (*BasicQueryStatus, error) {
// Read all the remaining data and ensure it has NUL terminator (if UseStrict)
data, _ := readAll(reader)
if len(data) == 0 {
return nil, fmt.Errorf("%w: empty response body", ErrInvalidStatus)
}
if bytes.HasSuffix(data, queryResponseStringTerminator) {
data = data[:len(data)-len(queryResponseStringTerminator)]
} else if p.UseStrict {
return nil, fmt.Errorf("%w: response body is not NUL-termianted", ErrInvalidStatus)
}
// Split response string by NUL bytes (into 6 substrings, because 6th also contains port and hostname
// that need to be parsed specially).
fields := strings.SplitN(string(data), string(queryResponseStringTerminator), 6)
if len(fields) != 6 {
return nil, fmt.Errorf("%w: expected 5 first string fields in response body, got %#v", ErrInvalidStatus, len(fields)-1)
}
motd, gameType, mapName, onlinePlayersStr, maxPlayerStr := fields[0], fields[1], fields[2], fields[3], fields[4]
// Ensure gametype is indeed a hardcoded SMP string
if gameType != queryGameType && p.UseStrict {
return nil, fmt.Errorf("%w: expected gametype field to be %#v, got %#v", ErrInvalidStatus, queryGameType, gameType)
}
// Parse online players integer
onlinePlayers, err := strconv.ParseInt(onlinePlayersStr, 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: could not parse online players count: %s", ErrInvalidStatus, err)
}
// Parse max players integer
maxPlayers, err := strconv.ParseInt(maxPlayerStr, 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: could not parse max players count: %s", ErrInvalidStatus, err)
}
// Create a reader for remaining data to read it sequentially (port + hostname)
remReader := bytes.NewReader([]byte(fields[5]))
// Unpack port as short integer (little-endian)
var port int16
if err = binary.Read(remReader, binary.LittleEndian, &port); err != nil {
return nil, err
}
// Read host as byte sequence
hostBytes, err := readAll(remReader)
if err != nil {
return nil, err
}
return &BasicQueryStatus{
MOTD: motd,
GameType: gameType,
Map: mapName,
OnlinePlayers: int(onlinePlayers),
MaxPlayers: int(maxPlayers),
Port: int(port),
Host: string(hostBytes),
}, nil
}
func (p *Pinger) parseQueryFullStatResponse(reader io.Reader) (*FullQueryStatus, error) {
// Read all the remaining data and ensure it has NUL terminator (if UseStrict)
data, _ := readAll(reader)
if len(data) == 0 {
return nil, fmt.Errorf("%w: empty response body", ErrInvalidStatus)
}
if bytes.HasSuffix(data, queryResponseStringTerminator) {
data = data[:len(data)-len(queryResponseStringTerminator)]
} else if p.UseStrict {
return nil, fmt.Errorf("%w: response body is not NUL-termianted", ErrInvalidStatus)
}
dataReader := bytes.NewReader(data)
// Read padding for Key-Value section and ensure it is equal to hardcoded value (if UseStrict)
pb := make([]byte, len(queryKVSectionPadding))
if _, err := dataReader.Read(pb); err != nil {
return nil, err
} else if !bytes.Equal(pb, queryKVSectionPadding) && p.UseStrict {
return nil, fmt.Errorf("%w: key-value section padding is invalid", ErrInvalidStatus)
}
// Read and parse KV map
fields, err := queryReadFullStatFieldMap(dataReader)
if err != nil {
return nil, err
}
// Read padding for players section and ensure it is also hardcoded (if UseStrict)
pb = make([]byte, len(queryPlayerSectionPadding))
if _, err = dataReader.Read(pb); err != nil {
return nil, err
} else if !bytes.Equal(pb, queryPlayerSectionPadding) && p.UseStrict {
return nil, fmt.Errorf("%w: player section padding is invalid", ErrInvalidStatus)
}
// Read player list
players, err := queryReadFullStatPlayerList(dataReader)
if err != nil {
return nil, err
}
// Read hostname field (MOTD)
motd, err := queryGetFullStatField(fields, "hostname")
if err != nil {
return nil, err
}
// Read gametype field and ensure it is a hardcoded SMP value (if UseStrict)
gameType, err := queryGetFullStatField(fields, "gametype")
if err != nil {
return nil, err
} else if gameType != queryGameType && p.UseStrict {
return nil, fmt.Errorf("%w: expected gametype field to be %#v, got %#v", ErrInvalidStatus, queryGameType, gameType)
}
// Read game_id field and ensure it is a hardcoded MINECRAFT value (if UseStrict)
gameID, err := queryGetFullStatField(fields, "game_id")
if err != nil {
return nil, err
} else if gameID != queryGameID && p.UseStrict {
return nil, fmt.Errorf("%w: expected game_id field to be %#v, got %#v", ErrInvalidStatus, queryGameID, gameID)
}
// Read version field
version, err := queryGetFullStatField(fields, "version")
if err != nil {
return nil, err
}
// Read server version and plugins field (still present on vanilla too) and parse it
serverVersionStr, err := queryGetFullStatField(fields, "plugins")
if err != nil {
return nil, err
}
serverVersion, plugins, err := queryParseFullStatPluginsList(serverVersionStr)
if err != nil {
return nil, fmt.Errorf("%w: could not parse plugins field: %s", ErrInvalidStatus, err)
}
// Read map field
mapName, err := queryGetFullStatField(fields, "map")
if err != nil {
return nil, err
}
// Read numplayers field (online players) and parse int from it
onlinePlayersStr, err := queryGetFullStatField(fields, "numplayers")
if err != nil {
return nil, err
}
onlinePlayers, err := strconv.ParseInt(onlinePlayersStr, 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: could not parse numplayers field: %s", ErrInvalidStatus, err)
}
// Read maxplayers field (max players) and parse int from it
maxPlayersStr, err := queryGetFullStatField(fields, "maxplayers")
if err != nil {
return nil, err
}
maxPlayers, err := strconv.ParseInt(maxPlayersStr, 10, 32)
if err != nil {
return nil, fmt.Errorf("%w: could not parse maxplayers field: %s", ErrInvalidStatus, err)
}
// Read hostport field (port) and parse int from it
portStr, err := queryGetFullStatField(fields, "hostport")
if err != nil {
return nil, err
}
port, err := strconv.ParseInt(portStr, 10, 16)
if err != nil {
return nil, fmt.Errorf("%w: could not parse hostport field: %s", ErrInvalidStatus, err)
}
// Read hostname field
hostname, err := queryGetFullStatField(fields, "hostip")
if err != nil {
return nil, err
}
return &FullQueryStatus{
MOTD: motd,
GameType: gameType,
GameID: gameID,
Version: version,
ServerVersion: serverVersion,
Plugins: plugins,
Map: mapName,
OnlinePlayers: int(onlinePlayers),
MaxPlayers: int(maxPlayers),
SamplePlayers: players,
Port: int(port),
Host: hostname,
Data: fields,
}, nil
}
func queryReadFullStatFieldMap(reader io.ByteReader) (map[string]string, error) {
fields := make(map[string]string)
for {
key, err := readAllUntilZero(reader)
if err != nil {
return nil, err
} else if len(key) == 0 {
break
}
value, err := readAllUntilZero(reader)
if err != nil {
return nil, err
}
fields[string(key)] = string(value)
}
return fields, nil
}
func queryReadFullStatPlayerList(reader io.ByteReader) ([]string, error) {
players := make([]string, 0, 10)
for {
nickname, err := readAllUntilZero(reader)
if err != nil {
return nil, err
} else if len(nickname) == 0 {
break
}
players = append(players, string(nickname))
}
return players, nil
}
func queryParseFullStatPluginsList(str string) (string, []FullQueryPluginEntry, error) {
// Split version string by color; left part is server version and brand, right part is plugins list
parts := strings.SplitN(str, ":", 2)
if len(parts) < 2 {
return parts[0], make([]FullQueryPluginEntry, 0), nil
}
ver, rem := parts[0], parts[1]
// Split plugins part by semicolon and process
pluginNames := strings.Split(rem, ";")
plugins := make([]FullQueryPluginEntry, len(pluginNames))
for i, name := range pluginNames {
// Split plugin name by space; left part is name, right is version (be sure to trim spaces)
// Also ensure plugin entry has two parts.
nameParts := strings.SplitN(strings.TrimSpace(name), " ", 2)
if len(nameParts) < 2 {
return "", nil, fmt.Errorf("invalid plugin field syntax")
}
plugins[i] = FullQueryPluginEntry{nameParts[0], nameParts[1]}
}
return ver, plugins, nil
}
func queryGetFullStatField(fields map[string]string, key string) (string, error) {
value, ok := fields[key]
if !ok {
return "", fmt.Errorf("%w: response body does not contain %s field", ErrInvalidStatus, key)
}
delete(fields, key)
return value, nil
}