-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (50 loc) · 1.6 KB
/
index.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
var net = require('net');
// https://wiki.vg/RCON
class RCONClient {
constructor() {
this._requests = {};
this._conn = new net.Socket();
this._offset = 0;
this._lengthBytes = [];
this._length = null;
this._conn.on('data', chunk => {
if (this._lengthBytes < 4) this._lengthBytes.push(chunk[0]);
});
}
async connect(host, port) {
if (port == null) port = 25575;
return new Promise((resolve, reject) => {
this._conn.once('error', err => reject(err));
this._conn.connect({ host, port }, () => {
resolve();
this._conn.removeAllListeners('error');
});
});
}
async login(password) {
if (typeof password == 'string') password = Buffer.from(password);
if (!Buffer.isBuffer(password)) throw new Error('Must login with string or buffer for password.');
if (password.includes(0)) throw new Error('Password cannot contain null bytes.');
let reqIDs = new Set(Object.keys(this._requests));
if (reqIDs.size > 2 ** 32 - 2) throw new Error('Too many open requests.');
let reqID;
for (reqID = 0; reqID < 2 ** 32 - 1; reqID++) {
if (!reqIDs.has('' + reqID))
break;
}
let header = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0]);
header.writeUInt32LE(password.length + 10, 0);
header.writeUInt32LE(reqID, 4);
this._conn.write(Buffer.concat([
header,
password,
Buffer.from([0, 0])
]));
return new Promise((resolve, reject) => {
this._requests[reqID] = { resolve, reject };
});
}
}
module.exports = {
RCONClient
};