-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcon.ts
196 lines (165 loc) · 5.33 KB
/
rcon.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
import { concat, equals, iterateReader } from './deps.ts';
const FrameBuffer = new Uint8Array([0, 1, 0, 0]);
const PacketType = {
COMMAND: 0x02,
AUTH: 0x03,
RESPONSE_VALUE: 0x00,
RESPONSE_AUTH: 0x02,
};
class Request {
public data = new Uint8Array();
constructor(
public readonly resolve: (value: string) => void,
public readonly reject: (error: unknown) => void
) {}
}
class ResolvablePromise {
resolve!: () => void;
reject!: (error: unknown) => void;
promise = new Promise<void>((res, rej) => {
this.resolve = res;
this.reject = rej;
});
}
export interface Options {
/** Support multi-packet responses from the server. On by default.
* May be turned off in order to read responses from servers that
* don't support it, like Factorio. */
multiPacketResponses: boolean;
}
const defaultOptions: Options = {
multiPacketResponses: true
};
export class Rcon {
private authed?: ResolvablePromise;
private conn?: Deno.Conn;
private outstandingData?: Uint8Array;
private requests = new Map<number, Request>();
private options: Options;
constructor(
private host = 'localhost',
private port = 27015,
private password = '',
options: Partial<Options> = {}
) {
this.options = {...defaultOptions, ...options};
}
sendCmd(cmd: string) {
return this.send(cmd, PacketType.COMMAND);
}
private async connect() {
if (!this.conn) {
this.authed = new ResolvablePromise();
this.conn = await Deno.connect({
hostname: this.host,
port: this.port,
});
this.read().catch(() => {});
await this.sendData(
new Uint8Array([0, 0, 0, 0]),
this.password,
PacketType.AUTH
);
}
return this.authed?.promise;
}
private async read() {
const conn = this.conn!;
try {
for await (const chunk of iterateReader(conn)) {
this.readChunk(chunk);
}
} finally {
if (this.conn === conn) {
this.conn = undefined;
this.authed?.reject(new Error('connection closed'));
this.authed = undefined;
this.requests.forEach((request) =>
request.reject(new Error('connection closed'))
);
this.requests.clear();
}
}
}
disconnect() {
this.conn?.close();
this.conn = undefined;
this.authed = undefined;
this.requests.forEach((request) => request.reject(new Error('read EOF')));
this.requests.clear();
}
private readChunk(data: Uint8Array) {
if (this.outstandingData) {
data = concat(this.outstandingData, data);
this.outstandingData = undefined;
}
while (data.length) {
const dataView = new DataView(data.buffer);
const len = dataView.getInt32(0, true);
if (!len) return;
if (len >= 10 && data.length >= len + 4) {
const id = dataView.getInt32(4, true);
const type = dataView.getInt32(8, true);
const payload = data.slice(12, 12 + len - 10);
if (id !== -1) {
if (type === PacketType.RESPONSE_AUTH && id === 0) {
this.authed?.resolve();
} else if (type === PacketType.RESPONSE_VALUE) {
// Read just the body of the packet (truncate the last null byte)
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for details
const request = this.requests.get(id);
if (request) {
if (!this.options.multiPacketResponses) {
const str = new TextDecoder().decode(payload);
request.resolve(str);
this.requests.delete(id);
} else if (equals(FrameBuffer, payload)) {
const str = new TextDecoder().decode(request.data);
request.resolve(str);
this.requests.delete(id);
} else {
request.data = concat(request.data, payload);
}
}
}
} else if (id == -1) {
console.error('Authentication failed');
this.authed?.reject(new Error('authentication'));
}
data = data.slice(4 + len);
} else {
// Keep the data of the chunk if it doesn't represent a full packet
this.outstandingData = new Uint8Array(data.length);
this.outstandingData.set(data, 0);
break;
}
}
}
private async send(data: string, packetType: number) {
await this.connect();
const id = crypto.getRandomValues(new Uint8Array(4));
const idNumber = new DataView(id.buffer).getInt32(0, true);
const sendPromise = new Promise<string>((resolve, reject) => {
this.requests.set(idNumber, new Request(resolve, reject));
});
try {
await this.sendData(id, data, packetType);
await this.sendData(id, '', PacketType.RESPONSE_VALUE);
} catch (e) {
this.requests.get(idNumber)?.reject(e);
}
return sendPromise;
}
private async sendData(id: Uint8Array, data: string, packetType: number) {
const dataBuffer = new TextEncoder().encode(data);
const dataLength = dataBuffer.length;
const sendBuffer = new Uint8Array(dataLength + 14);
const view = new DataView(sendBuffer.buffer);
view.setInt32(0, dataLength + 10, true);
sendBuffer.set(id, 4);
view.setInt32(8, packetType, true);
sendBuffer.set(dataBuffer, 12);
view.setInt16(dataLength + 12, 0, true);
await this.conn?.write(sendBuffer);
}
}