1
Fork 0

Use uuid for authorId

This commit is contained in:
Tobias Berger 2022-01-14 13:33:03 +01:00
parent 1d04c80e0f
commit eed42381b8
Signed by: toby
GPG key ID: 2D05EFAB764D6A88

View file

@ -16,8 +16,6 @@ import {
MessageType, MessageType,
} from "./lib/ServerMessage"; } from "./lib/ServerMessage";
const SALT = crypto.randomBytes(2048).toString("hex");
const port = 8085; const port = 8085;
const timeout = 15000; const timeout = 15000;
@ -29,10 +27,6 @@ const webSocketServer = new WebSocketServer({ server: httpsServer });
console.log("listening on port: " + port); 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) { async function handleTextMessage(message: TextMessage, from: string) {
for (const to of webSocketServer.clients) { for (const to of webSocketServer.clients) {
to.send( to.send(
@ -50,7 +44,6 @@ async function handleCloseConnection(id: string) {
} }
const activeConnections = new Set<string>(); const activeConnections = new Set<string>();
let nextId = 0;
webSocketServer.on("connection", function connection(socket) { webSocketServer.on("connection", function connection(socket) {
const close = (reason: string, code: number = 1000) => { const close = (reason: string, code: number = 1000) => {
@ -65,12 +58,12 @@ webSocketServer.on("connection", function connection(socket) {
handleCloseConnection(authorId); handleCloseConnection(authorId);
}; };
const authorId: string = hash(nextId++ + SALT); const authorId: string = crypto.randomUUID();
socket.on("message", function (rawMessage: string) { socket.on("message", function (rawMessage: string) {
const message = JSON.parse(rawMessage); const message = JSON.parse(rawMessage);
if (!isServerMessage(message)) { if (!isServerMessage(message)) {
console.error(`Unexpected message received from client \`${message}\``); console.error(`Unexpected message received from client "${authorId}": \`${message}\``);
return; return;
} }