forked from gamedig/node-gamedig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tribes1master.js
80 lines (70 loc) · 2.15 KB
/
tribes1master.js
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
import Core from './core.js'
/** Unsupported -- use at your own risk!! */
export default class tribes1master extends Core {
constructor () {
super()
this.encoding = 'latin1'
}
async run (state) {
const queryBuffer = Buffer.from([
0x10, // standard header
0x03, // dump servers
0xff, // ask for all packets
0x00, // junk
0x01, 0x02 // challenge
])
const parts = new Map()
let total = 0
const full = await this.udpSend(queryBuffer, (buffer) => {
const reader = this.reader(buffer)
const header = reader.uint(2)
if (header !== 0x0610) {
this.logger.debug('Header response does not match: ' + header.toString(16))
return
}
const num = reader.uint(1)
const t = reader.uint(1)
if (t <= 0 || (total > 0 && t !== total)) {
throw new Error('Conflicting packet total: ' + t)
}
total = t
if (num < 1 || num > total) {
this.logger.debug('Invalid packet number: ' + num + ' ' + total)
return
}
if (parts.has(num)) {
this.logger.debug('Duplicate part: ' + num)
return
}
reader.skip(2) // challenge (0x0201)
reader.skip(2) // always 0x6600
parts.set(num, reader.rest())
if (parts.size === total) {
const ordered = []
for (let i = 1; i <= total; i++) ordered.push(parts.get(i))
return Buffer.concat(ordered)
}
})
const fullReader = this.reader(full)
state.raw.name = this.readString(fullReader)
state.raw.motd = this.readString(fullReader)
state.raw.servers = []
while (!fullReader.done()) {
fullReader.skip(1) // junk ?
const count = fullReader.uint(1)
for (let i = 0; i < count; i++) {
const six = fullReader.uint(1)
if (six !== 6) {
throw new Error('Expecting 6')
}
const ip = fullReader.uint(4)
const port = fullReader.uint(2)
const ipStr = (ip & 255) + '.' + (ip >> 8 & 255) + '.' + (ip >> 16 & 255) + '.' + (ip >>> 24)
state.raw.servers.push(ipStr + ':' + port)
}
}
}
readString (reader) {
return reader.pascalString(1)
}
}