-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod.ts
589 lines (515 loc) · 14.8 KB
/
mod.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import { writeAll } from "https://deno.land/[email protected]/streams/write_all.ts";
import { readLines } from "https://deno.land/[email protected]/io/read_lines.ts";
import { encodeHex } from "https://deno.land/[email protected]/encoding/hex.ts";
const decoder = new TextDecoder();
const encoder = new TextEncoder();
type ClientData = Record<string, any>;
interface BasePacket {
clientId?: number;
roomId?: string;
quiet?: boolean;
targetClientId?: number;
}
interface UpdateClientDataPacket extends BasePacket {
type: "UPDATE_CLIENT_DATA";
data: ClientData;
}
interface AllClientDataPacket extends BasePacket {
type: "ALL_CLIENT_DATA";
clients: ClientData[];
}
interface ServerMessagePacket extends BasePacket {
type: "SERVER_MESSAGE";
message: string;
}
interface DisableAnchorPacket extends BasePacket {
type: "DISABLE_ANCHOR";
}
interface OtherPackets extends BasePacket {
type:
| "REQUEST_SAVE_STATE"
| "PUSH_SAVE_STATE"
| "GAME_COMPLETE"
| "HEARTBEAT";
}
type Packet =
| UpdateClientDataPacket
| DisableAnchorPacket
| ServerMessagePacket
| AllClientDataPacket
| OtherPackets;
interface ServerStats {
lastStatsHeartbeat: number;
clientSHAs: Record<string, boolean>;
onlineCount: number;
gamesCompleted: number;
pid: number;
}
const port =
(Deno.env.has("PORT") && !isNaN(parseInt(Deno.env.get("PORT")!, 10)))
? parseInt(Deno.env.get("PORT")!, 10)
: 43385;
let quietMode = !!Deno.env.has("QUIET");
class Server {
private listener?: Deno.Listener;
public clients: Client[] = [];
public rooms: Room[] = [];
public stats: ServerStats = {
lastStatsHeartbeat: Date.now(),
clientSHAs: {},
onlineCount: 0,
gamesCompleted: 0,
pid: Deno.pid,
};
async start() {
await this.parseStats();
this.statsHeartbeat();
this.clientHeartbeat();
this.startServer();
}
async parseStats() {
try {
const statsString = await Deno.readTextFile("./stats.json");
this.stats = Object.assign(this.stats, JSON.parse(statsString));
this.stats.pid = Deno.pid;
this.log("Loaded stats file");
} catch (_) {
this.log("No stats file found");
}
}
async statsHeartbeat() {
try {
this.stats.lastStatsHeartbeat = Date.now();
this.stats.onlineCount = this.clients.length;
await this.saveStats();
} catch (error) {
this.log(`Error saving stats: ${error.message}`);
}
setTimeout(() => {
this.statsHeartbeat();
}, 2500);
}
async clientHeartbeat() {
try {
await Promise.all(server.clients.map((client) => {
return client.sendPacket({
type: "HEARTBEAT",
}).catch((_) => {}); // Ignore errors, client will disconnect if it's a problem
}));
} catch (error) {
this.log(`Error sending heartbeat to clients: ${error.message}`);
}
setTimeout(() => {
this.clientHeartbeat();
}, 1000 * 30);
}
async saveStats() {
try {
await Deno.writeTextFile(
"./stats.json",
JSON.stringify(this.stats, null, 4),
);
} catch (error) {
this.log(`Error saving stats: ${error.message}`);
}
}
async startServer() {
this.listener = Deno.listen({ port });
this.log(`Server Started on port ${port}`);
try {
for await (const connection of this.listener) {
try {
const client = new Client(connection, this);
this.clients.push(client);
} catch (error) {
this.log(`Error connecting client: ${error.message}`);
}
}
} catch (error) {
this.log(`Error starting server: ${error.message}`);
}
}
removeClient(client: Client) {
const index = this.clients.indexOf(client);
if (index !== -1) {
this.clients.splice(index, 1);
}
}
getOrCreateRoom(id: string) {
const room = this.rooms.find((room) => room.id === id);
if (room) {
return room;
}
const newRoom = new Room(id, this);
this.rooms.push(newRoom);
return newRoom;
}
removeRoom(room: Room) {
const index = this.rooms.indexOf(room);
if (index !== -1) {
this.rooms.splice(index, 1);
}
}
log(...data: any[]) {
console.log(`[Server]:`, ...data);
}
}
class Client {
public id: number;
public data: ClientData = {};
public connection: Deno.Conn;
public server: Server;
public room?: Room;
constructor(connection: Deno.Conn, server: Server) {
this.connection = connection;
this.server = server;
this.id = connection.rid;
// SHA256 to get a rough idea of how many unique players there are
crypto.subtle.digest(
"SHA-256",
encoder.encode((this.connection.remoteAddr as Deno.NetAddr).hostname),
)
.then((hasBuffer) => {
this.server.stats.onlineCount++;
this.server.stats.clientSHAs[encodeHex(hasBuffer)] = true;
})
.catch((error) => {
this.log(`Error hashing client: ${error.message}`);
});
this.waitForData();
this.log("Connected");
}
async waitForData() {
const buffer = new Uint8Array(1024);
let data = new Uint8Array(0);
while (true) {
let count: null | number = 0;
try {
count = await this.connection.read(buffer);
} catch (error) {
this.log(`Error reading from connection: ${error.message}`);
this.disconnect();
break;
}
if (!count) {
this.disconnect();
break;
}
// Concatenate received data with the existing data
const receivedData = buffer.subarray(0, count);
data = concatUint8Arrays(data, receivedData);
// Handle all complete packets (while loop in case multiple packets were received at once)
while (true) {
const delimiterIndex = findDelimiterIndex(data);
if (delimiterIndex === -1) {
break; // Incomplete packet, wait for more data
}
// Extract the packet
const packet = data.subarray(0, delimiterIndex);
data = data.subarray(delimiterIndex + 1);
this.handlePacket(packet);
}
}
}
handlePacket(packet: Uint8Array) {
try {
const packetString = decoder.decode(packet);
const packetObject: Packet = JSON.parse(packetString);
packetObject.clientId = this.id;
if (!packetObject.quiet && !quietMode) {
this.log(`-> ${packetObject.type} packet`);
}
if (packetObject.type === "UPDATE_CLIENT_DATA") {
this.data = packetObject.data;
}
if (packetObject.type === "GAME_COMPLETE") {
this.server.stats.gamesCompleted++;
}
if (packetObject.roomId && !this.room) {
this.server.getOrCreateRoom(packetObject.roomId).addClient(this);
}
if (!this.room) {
this.log("Not in a room, ignoring packet");
return;
}
if (packetObject.targetClientId) {
const targetClient = this.room.clients.find((client) =>
client.id === packetObject.targetClientId
);
if (targetClient) {
targetClient.sendPacket(packetObject);
} else {
this.log(`Target client ${packetObject.targetClientId} not found`);
}
return;
}
if (packetObject.type === "REQUEST_SAVE_STATE") {
if (this.room.clients.length > 1) {
this.room.requestingStateClients.push(this);
this.room.broadcastPacket(packetObject, this);
}
} else if (packetObject.type === "PUSH_SAVE_STATE") {
const roomStateRequests = this.room.requestingStateClients;
roomStateRequests.forEach((client) => {
client.sendPacket(packetObject);
});
this.room.requestingStateClients = [];
} else {
this.room.broadcastPacket(packetObject, this);
}
} catch (error) {
this.log(`Error handling packet: ${error.message}`);
}
}
async sendPacket(packetObject: Packet) {
try {
if (!packetObject.quiet && !quietMode) {
this.log(`<- ${packetObject.type} packet`);
}
const packetString = JSON.stringify(packetObject);
const packet = encoder.encode(packetString + "\0");
// Wait for writeAll to complete, if it takes longer than 30 seconds, disconnect
await Promise.race([
writeAll(this.connection, packet),
new Promise((_, reject) => {
setTimeout(() => {
reject(new Error("Timeout, took longer than 30 seconds to send"));
}, 1000 * 30);
}),
]);
} catch (error) {
this.log(`Error sending packet: ${error.message}`);
this.disconnect();
}
}
disconnect() {
try {
if (this.room) {
this.room.removeClient(this);
}
this.server.removeClient(this);
this.connection.close();
} catch (error) {
this.log(`Error disconnecting: ${error.message}`);
} finally {
this.server.stats.onlineCount--;
this.log("Disconnected");
}
}
log(message: string) {
console.log(`[Client ${this.id}]: ${message}`);
}
}
class Room {
public id: string;
public server: Server;
public clients: Client[] = [];
public requestingStateClients: Client[] = [];
constructor(id: string, server: Server) {
this.id = id;
this.server = server;
this.log("Created");
}
addClient(client: Client) {
this.log(`Adding client ${client.id}`);
this.clients.push(client);
client.room = this;
this.broadcastAllClientData();
}
removeClient(client: Client) {
this.log(`Removing client ${client.id}`);
const index = this.clients.indexOf(client);
if (index !== -1) {
this.clients.splice(index, 1);
client.room = undefined;
}
if (this.clients.length) {
this.broadcastAllClientData();
} else {
this.log("No clients left, removing room");
this.server.removeRoom(this);
}
}
broadcastAllClientData() {
if (!quietMode) {
this.log("<- ALL_CLIENT_DATA packet");
}
for (const client of this.clients) {
const packetObject = {
type: "ALL_CLIENT_DATA" as const,
roomId: this.id,
clients: this.clients.filter((c) => c !== client).map((c) => ({
clientId: c.id,
...c.data,
})),
};
client.sendPacket(packetObject);
}
}
broadcastPacket(packetObject: Packet, sender: Client) {
if (!packetObject.quiet && !quietMode) {
this.log(`<- ${packetObject.type} packet from ${sender.id}`);
}
for (const client of this.clients) {
if (client !== sender) {
client.sendPacket(packetObject);
}
}
}
log(message: string) {
console.log(`[Room ${this.id}]: ${message}`);
}
}
function concatUint8Arrays(a: Uint8Array, b: Uint8Array): Uint8Array {
const result = new Uint8Array(a.length + b.length);
result.set(a, 0);
result.set(b, a.length);
return result;
}
function findDelimiterIndex(data: Uint8Array): number {
for (let i = 0; i < data.length; i++) {
if (data[i] === 0 /* null terminator */) {
return i;
}
}
return -1;
}
const server = new Server();
server.start().catch((error) => {
console.error("Error starting server: ", error);
Deno.exit(1);
});
globalThis.addEventListener("unhandledrejection", (e) => {
console.error("Unhandled rejection at:", e.promise, "reason:", e.reason);
e.preventDefault();
Deno.exit(1);
});
function sendServerMessage(client: Client, message: string) {
return client.sendPacket({
type: "SERVER_MESSAGE",
message,
});
}
function sendDisable(client: Client, message: string) {
sendServerMessage(client, message)
.finally(() =>
client.sendPacket({
type: "DISABLE_ANCHOR",
})
);
}
async function stop(message = "Server restarting") {
await Promise.all(
server.clients.map((client) =>
sendServerMessage(client, message)
.finally(() => {
client.disconnect();
})
),
);
Deno.exit();
}
(async function processStdin() {
try {
for await (const line of readLines(Deno.stdin)) {
const [command, ...args] = line.split(" ");
switch (command) {
default:
case "help": {
console.log(
`Available commands:
help: Show this help message
stats: Print server stats
quiet: Toggle quiet mode
roomCount: Show the number of rooms
clientCount: Show the number of clients
list: List all rooms and clients
stop <message>: Stop the server
message <clientId> <message>: Send a message to a client
messageAll <message>: Send a message to all clients
disable <clientId> <message>: Disable anchor on a client
disableAll <message>: Disable anchor on all clients`,
);
break;
}
case "roomCount": {
console.log(`Room count: ${server.rooms.length}`);
break;
}
case "clientCount": {
console.log(`Client count: ${server.clients.length}`);
break;
}
case "quiet": {
quietMode = !quietMode;
console.log(`Quiet mode: ${quietMode}`);
break;
}
case "stats": {
const { clientSHAs: _, ...stats } = server.stats;
console.log(stats);
break;
}
case "list": {
for (const room of server.rooms) {
console.log(`Room ${room.id}:`);
for (const client of room.clients) {
console.log(
` Client ${client.id}: ${JSON.stringify(client.data)}`,
);
}
}
break;
}
case "disable": {
const [clientId, ...messageParts] = args;
const message = messageParts.join(" ");
const client = server.clients.find((c) =>
c.id === parseInt(clientId, 10)
);
if (client) {
sendDisable(client, message);
} else {
console.log(`Client ${clientId} not found`);
}
break;
}
case "disableAll": {
const message = args.join(" ");
for (const client of server.clients) {
sendDisable(client, message);
}
break;
}
case "message": {
const [clientId, ...messageParts] = args;
const message = messageParts.join(" ");
const client = server.clients.find((c) =>
c.id === parseInt(clientId, 10)
);
if (client) {
sendServerMessage(client, message);
} else {
console.log(`Client ${clientId} not found`);
}
break;
}
case "messageAll": {
const message = args.join(" ");
for (const client of server.clients) {
sendServerMessage(client, message);
}
break;
}
case "stop": {
const message = args.join(" ");
stop(message);
break;
}
}
}
} catch (error) {
console.error("Error reading from stdin: ", error.message);
processStdin();
}
})();