-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
321 lines (241 loc) · 8.67 KB
/
index.ts
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
import dgram, { BindOptions, Socket } from 'dgram';
import { AddressInfo } from 'net';
class SourceQuerySocket {
public port?: number;
public address?: string;
public exclusive?: boolean;
public fd?: number;
private socket?: Socket;
public constructor(options: BindOptions = {}) {
if (options.port !== undefined) this.port = options.port;
if (options.address !== undefined) this.address = options.address;
if (options.exclusive !== undefined) this.exclusive = options.exclusive;
if (options.fd !== undefined) this.fd = options.fd;
}
private bind(): Promise<void> {
return new Promise((resolve, reject): void => {
const error = (err: unknown): void => {
this.socket?.close();
return reject(err);
};
const listening = (): void => {
this.socket?.removeListener('error', error);
return resolve();
};
this.socket = dgram.createSocket('udp4');
this.socket.once('error', error);
this.socket.once('listening', listening);
this.socket.bind({ port: this.port, address: this.address, exclusive: this.exclusive, fd: this.fd });
});
}
private assert(): Promise<void> {
return new Promise(async (resolve, reject): Promise<void> => {
if (this.socket === undefined) {
try {
await this.bind();
} catch (err) {
return reject(err);
}
return resolve();
}
try {
this.socket?.address();
} catch (err: unknown) {
return this.socket?.once('listening', () => resolve()) as unknown as void;
}
return resolve();
});
}
private close = (): void => {
this.socket?.close();
this.socket = undefined;
};
private validate(einfo: AddressInfo, rinfo: AddressInfo, request: Buffer, response: Buffer): boolean {
if (rinfo.port !== einfo.port) return false;
if (rinfo.address !== einfo.address) return false;
return true;
}
private send(einfo: AddressInfo, request: Buffer, duration: number): Promise<Buffer> {
return new Promise(async (resolve, reject): Promise<void> => {
try {
await this.assert();
} catch (err) {
return reject(err);
}
const timeout: NodeJS.Timeout = setTimeout((): void => {
this.socket?.removeListener('message', message);
return reject(new Error(`Request timed out. [${duration}ms]`));
}, duration);
const message = (response: Buffer, rinfo: AddressInfo): void => {
if (this.validate(einfo, rinfo, request, response) === false) return;
clearTimeout(timeout);
this.socket?.removeListener('message', message);
if (this.socket?.listenerCount('message') === 0) this.close();
return resolve(response);
};
this.socket?.on('message', message);
this.socket?.send(request, einfo.port, einfo.address, (err): void => {
if (err !== undefined && err !== null) return reject(err);
});
});
}
private pack(header: string, payload?: string, challenge?: number): Buffer {
const preamble: Buffer = Buffer.alloc(4);
preamble.writeInt32LE(-1, 0);
const request: Buffer = Buffer.from(header);
const data: Buffer = payload ? Buffer.concat([Buffer.from(payload), Buffer.alloc(1)]) : Buffer.alloc(0);
let prologue: Buffer = Buffer.alloc(0);
if (challenge !== undefined) {
prologue = Buffer.alloc(4);
prologue.writeInt32LE(challenge);
}
return Buffer.concat([preamble, request, data, prologue, preamble]);
}
private async solicit(
einfo: AddressInfo,
header: string,
payload: string | undefined,
duration: number
): Promise<Buffer> {
const request: Buffer = this.pack(header, payload);
const challenge: Buffer = await this.send(einfo, request, duration);
const type: string = challenge.slice(4, 5).toString();
if (type === 'A') {
const challenger: number = challenge.readInt32LE(5);
const result: Buffer = this.pack(header, payload, challenger);
return await this.send(einfo, result, duration);
}
return challenge;
}
public info = async (address: string, port: number | string, timeout: number = 1000) => {
const query: Buffer = await this.solicit(
{ address, port: parseInt(port as string, 10), family: '' },
'T',
'Source Engine Query',
timeout
);
const result: Record<string, any> = {};
let offset = 4;
result.header = query.slice(offset, offset + 1);
offset += 1;
result.header = result.header.toString();
result.protocol = query.readInt8(offset);
offset += 1;
result.name = query.slice(offset, query.indexOf(0, offset));
offset += result.name.length + 1;
result.name = result.name.toString();
result.map = query.slice(offset, query.indexOf(0, offset));
offset += result.map.length + 1;
result.map = result.map.toString();
result.folder = query.slice(offset, query.indexOf(0, offset));
offset += result.folder.length + 1;
result.folder = result.folder.toString();
result.game = query.slice(offset, query.indexOf(0, offset));
offset += result.game.length + 1;
result.game = result.game.toString();
result.id = query.readInt16LE(offset);
offset += 2;
result.players = query.readInt8(offset);
offset += 1;
result.max_players = query.readInt8(offset);
offset += 1;
result.bots = query.readInt8(offset);
offset += 1;
result.server_type = query.slice(offset, offset + 1).toString();
offset += 1;
result.environment = query.slice(offset, offset + 1).toString();
offset += 1;
result.visibility = query.readInt8(offset);
offset += 1;
result.vac = query.readInt8(offset);
offset += 1;
result.version = query.slice(offset, query.indexOf(0, offset));
offset += result.version.length + 1;
result.version = result.version.toString();
const extra: Buffer = query.slice(offset);
offset = 0;
if (extra.length < 1) return result;
const edf: number = extra.readInt8(offset);
offset += 1;
if (edf & 0x80) {
result.port = extra.readInt16LE(offset);
offset += 2;
}
if (edf & 0x10) {
result.steamid = extra.readBigUInt64LE(offset);
offset += 8;
}
if (edf & 0x40) {
result.tvport = extra.readInt16LE(offset);
offset += 2;
result.tvname = extra.slice(offset, extra.indexOf(0, offset));
offset += result.tvname.length + 1;
result.tvname = result.tvname.toString();
}
if (edf & 0x20) {
const keywords: Buffer = extra.slice(offset, extra.indexOf(0, offset));
offset += keywords.length + 1;
result.keywords = keywords.toString();
}
if (edf & 0x01) {
result.gameid = extra.readBigUInt64LE(offset);
offset += 8;
}
return result;
};
public players = async (address: string, port: number | string, timeout: number = 1000) => {
const query: Buffer = await this.solicit(
{ address, port: parseInt(port as string, 10), family: '' },
'U',
undefined,
timeout
);
let offset = 5;
const count: number = query.readInt8(offset);
offset += 1;
const result: Record<string, string | number>[] = [];
for (let i = 0; i < count; i += 1) {
const player: Record<string, any> = {};
player.index = query.readInt8(offset);
offset += 1;
player.name = query.slice(offset, query.indexOf(0, offset));
offset += player.name.length + 1;
player.name = player.name.toString();
player.score = query.readInt32LE(offset);
offset += 4;
player.duration = query.readFloatLE(offset);
offset += 4;
result.push(player);
}
return result;
};
public rules = async (address: string, port: string | number, timeout = 1000) => {
const query: Buffer = await this.solicit(
{ address, port: parseInt(port as string, 10), family: '' },
'V',
undefined,
timeout
);
let offset = 0;
const header: number = query.readInt32LE(offset);
if (header === -2) throw new Error('Unsupported response received.');
offset += 4;
offset += 1;
const count: number = query.readInt16LE(offset);
offset += 2;
const result: Record<string, string>[] = [];
for (let i = 0; i < count; i += 1) {
const rule: Record<string, any> = {};
rule.name = query.slice(offset, query.indexOf(0, offset));
offset += rule.name.length + 1;
rule.name = rule.name.toString();
rule.value = query.slice(offset, query.indexOf(0, offset));
offset += rule.value.length + 1;
rule.value = rule.value.toString();
result.push(rule);
}
return result;
};
}
module.exports = new SourceQuerySocket();
module.exports.SourceQuerySocket = SourceQuerySocket;