1
Fork 0
This repository has been archived on 2022-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
web-drs-backend/server.node.ts

104 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as fs from "fs";
import * as https from "https";
import { WebSocketServer } from "ws";
import * as crypto from "crypto";
2022-01-08 20:23:03 +01:00
import type {
AckMessage,
IdResponseMessage,
TextMessage,
} from "./lib/ServerMessage";
2022-01-07 17:36:19 +01:00
import {
2022-01-07 18:59:25 +01:00
isServerMessage,
isTextMessage,
MessageType,
} from "./lib/ServerMessage";
2022-01-08 20:22:43 +01:00
const SALT = crypto.randomBytes(2048).toString("hex");
2022-01-07 17:36:19 +01:00
const port = 8085;
2022-01-07 17:36:19 +01:00
const timeout = 15000;
const httpsServer = https.createServer({
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem"),
});
const webSocketServer = new WebSocketServer({ server: httpsServer });
2022-01-07 17:36:19 +01:00
console.log("listening on port: " + port);
function hash(str: string) {
return crypto.createHash("SHAKE256").update(str, "utf-8").digest("hex");
}
async function handleTextMessage(message: TextMessage, from: string) {
for (const to of webSocketServer.clients) {
to.send(
JSON.stringify(
Object.assign({}, message, {
author: from,
})
)
);
2022-01-07 17:36:19 +01:00
}
}
2022-01-07 17:36:19 +01:00
2022-01-08 20:23:03 +01:00
async function handleCloseConnection(id: string) {
activeConnections.delete(id);
}
const activeConnections = new Set<string>();
let nextId = 0;
webSocketServer.on("connection", function connection(socket) {
const close = (reason: string, code: number = 1000) => {
socket.send(
JSON.stringify({
type: MessageType.ACK,
date: Date.now(),
__ctx: `closing connection. reason: ${reason}`,
} as AckMessage)
);
socket.close(code, `closing connection. reason: ${reason}`);
2022-01-08 20:23:03 +01:00
handleCloseConnection(authorId);
};
2022-01-07 18:59:25 +01:00
2022-01-08 20:23:03 +01:00
const authorId: string = hash(nextId++ + SALT);
socket.on("message", function (rawMessage: string) {
2022-01-07 18:59:25 +01:00
const message = JSON.parse(rawMessage);
if (!isServerMessage(message)) {
console.error(`Unexpected message received from client \`${message}\``);
2022-01-08 20:23:03 +01:00
return;
2022-01-07 18:59:25 +01:00
}
2022-01-08 20:23:03 +01:00
2022-01-07 18:59:25 +01:00
if (isTextMessage(message)) {
2022-01-08 20:23:03 +01:00
if (message.author === authorId) {
handleTextMessage(message, authorId);
}
2022-01-07 18:59:25 +01:00
}
2022-01-08 20:23:03 +01:00
2022-01-07 17:36:19 +01:00
clearTimeout(closeTimeout);
closeTimeout = setTimeout(close, timeout);
});
socket.on("close", function close() {
console.log("closed a connection");
});
2022-01-08 20:23:03 +01:00
console.log("new client connected! ID:", authorId);
socket.send(
JSON.stringify({
2022-01-08 20:23:03 +01:00
type: MessageType.ID_RESPONSE,
__ctx: "connected successfully",
2022-01-08 20:23:03 +01:00
date: Date.now(),
authorId,
} as IdResponseMessage)
);
2022-01-08 20:23:03 +01:00
activeConnections.add(authorId);
2022-01-07 17:36:19 +01:00
let closeTimeout = setTimeout(close, timeout);
});
httpsServer.listen(port);