forked from gamedig/node-gamedig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
336 lines (297 loc) · 9.46 KB
/
core.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
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
import { EventEmitter } from 'node:events'
import * as net from 'node:net'
import got from 'got'
import Reader from '../lib/reader.js'
import { debugDump } from '../lib/HexUtil.js'
import Logger from '../lib/Logger.js'
import DnsResolver from '../lib/DnsResolver.js'
import { Results } from '../lib/Results.js'
import Promises from '../lib/Promises.js'
let uid = 0
export default class Core extends EventEmitter {
constructor () {
super()
this.encoding = 'utf8'
this.byteorder = 'le'
this.delimiter = '\0'
this.srvRecord = null
this.abortedPromise = null
this.logger = new Logger()
this.dnsResolver = new DnsResolver(this.logger)
// Sent to us by QueryRunner
this.options = null
/** @type GlobalUdpSocket */
this.udpSocket = null
this.shortestRTT = 0
this.usedTcp = false
}
// Runs a single attempt with a timeout and cleans up afterward
async runOnceSafe () {
const { debug, attemptTimeout } = this.options
if (debug) {
this.logger.debugEnabled = true
}
this.logger.prefix = 'Q#' + (uid++)
this.logger.debug('Starting')
this.logger.debug('Protocol: ' + this.constructor.name)
this.logger.debug('Options:', this.options)
let abortCall = null
this.abortedPromise = new Promise((_resolve, reject) => {
abortCall = () => reject(new Error('Query is finished -- cancelling outstanding promises'))
}).catch(() => {
// Make sure that if this promise isn't attached to, it doesn't throw an unhandled promise rejection
})
let timeout
try {
const promise = this.runOnce()
timeout = Promises.createTimeout(attemptTimeout, 'Attempt')
const result = await Promise.race([promise, timeout])
this.logger.debug('Query was successful')
return result
} catch (e) {
this.logger.debug('Query failed with error', e)
throw e
} finally {
timeout?.cancel()
try {
abortCall?.()
} catch (e) {
this.logger.debug('Error during abort cleanup: ' + e.stack)
}
}
}
async runOnce () {
const { options, dnsResolver } = this
if (('host' in options) && !('address' in options)) {
const resolved = await dnsResolver.resolve(options.host, options.ipFamily, this.srvRecord)
options.address = resolved.address
options.port ||= resolved.port
}
const state = new Results()
await this.run(state)
state.queryPort = options.port
// because lots of servers prefix with spaces to try to appear first
state.name = (state.name || '').trim()
state.connect = state.connect || `${state.gameHost || options.host || options.address}:${state.gamePort || options.port}`
state.ping = this.shortestRTT
delete state.gameHost
delete state.gamePort
this.logger.debug(log => {
log('Size of players array:', state.players.length)
log('Size of bots array:', state.bots.length)
})
return state
}
async run (/** Results */ state) {}
/** Param can be a time in ms, or a promise (which will be timed) */
registerRtt (param) {
if (param instanceof Promise) {
const start = Date.now()
param.then(() => {
this.registerRtt(Date.now() - start)
}).catch((_) => {})
} else {
this.logger.debug('Registered RTT: ' + param + 'ms')
if (this.shortestRTT === 0 || param < this.shortestRTT) {
this.shortestRTT = param
}
}
}
// utils
/** @returns {Reader} */
reader (buffer) {
return new Reader(this, buffer)
}
translate (obj, trans) {
for (const from of Object.keys(trans)) {
const to = trans[from]
if (from in obj) {
if (to) obj[to] = obj[from]
delete obj[from]
}
}
}
trueTest (str) {
if (typeof str === 'boolean') return str
if (typeof str === 'number') return str !== 0
if (typeof str === 'string') {
if (str.toLowerCase() === 'true') return true
if (str.toLowerCase() === 'yes') return true
if (str === '1') return true
}
return false
}
assertValidPort (port) {
if (!port) {
throw new Error('Could not determine port to query. Did you provide a port?')
}
if (port < 1 || port > 65535) {
throw new Error('Invalid tcp/ip port: ' + port)
}
}
/**
* @template T
* @param {function(NodeJS.Socket):Promise<T>} fn
* @param {number=} port
* @returns {Promise<T>}
*/
async withTcp (fn, port) {
this.usedTcp = true
const { options, logger } = this
const address = this.options.address
port ??= options.port
this.assertValidPort(port)
let socket, connectionTimeout
try {
socket = net.connect(port, address)
socket.setNoDelay(true)
// Prevent unhandled 'error' events from dumping straight to console
socket.on('error', () => {})
logger.debug(log => {
logger.debug(address + ':' + port + ' TCP Connecting')
const writeHook = socket.write
socket.write = (...args) => {
log(address + ':' + port + ' TCP-->')
log(debugDump(args[0]))
writeHook.apply(socket, args)
}
socket.on('error', e => log('TCP Error:', e))
socket.on('close', () => log('TCP Closed'))
socket.on('data', (data) => {
log(`${address}:${port} <--TCP`)
log(data)
})
socket.on('ready', () => log(`${address}:${port} TCP Connected`))
})
const connectionPromise = new Promise((resolve, reject) => {
socket.on('ready', resolve)
socket.on('close', () => reject(new Error('TCP Connection Refused')))
})
this.registerRtt(connectionPromise)
connectionTimeout = Promises.createTimeout(this.options.socketTimeout, 'TCP Opening')
await Promise.race([
connectionPromise,
connectionTimeout,
this.abortedPromise
])
return await fn(socket)
} finally {
socket?.destroy()
connectionTimeout?.cancel()
}
}
/**
* @template T
* @param {NodeJS.Socket} socket
* @param {Buffer|string} buffer
* @param {function(Buffer):T} ondata
* @returns Promise<T>
*/
async tcpSend (socket, buffer, ondata) {
let timeout
try {
const promise = new Promise((resolve, _reject) => {
let received = Buffer.from([])
const onData = (data) => {
received = Buffer.concat([received, data])
const result = ondata(received)
if (result !== undefined) {
socket.removeListener('data', onData)
resolve(result)
}
}
socket.on('data', onData)
socket.write(buffer)
})
timeout = Promises.createTimeout(this.options.socketTimeout, 'TCP')
return await Promise.race([promise, timeout, this.abortedPromise])
} finally {
timeout?.cancel()
}
}
/**
* @param {Buffer|string} buffer
* @param {function(Buffer):T=} onPacket
* @param {(function():T)=} onTimeout
* @returns Promise<T>
* @template T
*/
async udpSend (buffer, onPacket, onTimeout) {
const { address, port, debug, socketTimeout } = this.options
this.assertValidPort(port)
if (typeof buffer === 'string') buffer = Buffer.from(buffer, 'binary')
const socket = this.udpSocket
await socket.send(buffer, address, port, this.options.debug)
if (!onPacket && !onTimeout) {
return null
}
let socketCallback
let timeout
try {
const promise = new Promise((resolve, reject) => {
const start = Date.now()
socketCallback = (fromAddress, fromPort, buffer) => {
try {
if (fromAddress !== address || fromPort !== port) return
this.registerRtt(Date.now() - start)
const result = onPacket(buffer)
if (result !== undefined) {
this.logger.debug('UDP send finished by callback')
resolve(result)
}
} catch (e) {
reject(e)
}
}
socket.addCallback(socketCallback, debug)
})
timeout = Promises.createTimeout(socketTimeout, 'UDP')
const wrappedTimeout = Promise.resolve(timeout).catch((e) => {
this.logger.debug('UDP timeout detected')
if (onTimeout) {
const result = onTimeout()
if (result !== undefined) {
this.logger.debug('UDP timeout resolved by callback')
return result
}
}
throw e
})
return await Promise.race([promise, wrappedTimeout, this.abortedPromise])
} finally {
timeout?.cancel()
socketCallback && socket.removeCallback(socketCallback)
}
}
async tcpPing () {
// This will give a much more accurate RTT than using the rtt of an http request.
if (!this.usedTcp) {
await this.withTcp(() => {})
}
}
async request (params) {
await this.tcpPing()
let requestPromise
try {
requestPromise = got({
...params,
timeout: {
request: this.options.socketTimeout
}
})
this.logger.debug(log => {
log(() => params.url + ' HTTP-->')
requestPromise
.then((response) => log(params.url + ' <--HTTP ' + response.statusCode))
.catch(() => {})
})
const wrappedPromise = requestPromise.then(response => {
if (response.statusCode !== 200) throw new Error('Bad status code: ' + response.statusCode)
return response.body
})
return await Promise.race([wrappedPromise, this.abortedPromise])
} finally {
requestPromise?.cancel()
}
}
}