From 0b8950f4bc46063591b5ffb933e26f717f9f6336 Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 30 Dec 2024 13:24:09 -0800 Subject: [PATCH] fix ip processing bug: ip was sometimes an array --- src/server/Archive.ts | 2 -- src/server/Server.ts | 31 ++++++++++++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/server/Archive.ts b/src/server/Archive.ts index 501922d58..094b033c6 100644 --- a/src/server/Archive.ts +++ b/src/server/Archive.ts @@ -166,10 +166,8 @@ function anonymizeIPv6(ipv6: string): string | null { } function anonymizeIP(ip: string): string | null { - console.log(`anonymize got ip: ${ip}`) const ipv4Result = anonymizeIPv4(ip); if (ipv4Result) { - console.log(`got ipv4 result: ${ipv4Result}`) return ipv4Result; } diff --git a/src/server/Server.ts b/src/server/Server.ts index 31e8e3e2b..71c7981d2 100644 --- a/src/server/Server.ts +++ b/src/server/Server.ts @@ -122,19 +122,19 @@ app.get('/private_lobby/:id', (req, res) => { wss.on('connection', (ws, req) => { ws.on('message', (message: string) => { - const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message)) - slog({ - logKey: 'websocket_msg', - msg: 'server received websocket message', - data: clientMsg, - severity: LogSeverity.Debug - }) - if (clientMsg.type == "join") { - const forwarded = req.headers['x-forwarded-for'] - const ip = Array.isArray(forwarded) - ? forwarded[0] // Get the first IP if it's an array - : forwarded || req.socket.remoteAddress; - + try { + const clientMsg: ClientMessage = ClientMessageSchema.parse(JSON.parse(message)) + slog({ + logKey: 'websocket_msg', + msg: 'server received websocket message', + data: clientMsg, + severity: LogSeverity.Debug + }) + if (clientMsg.type == "join") { + const forwarded = req.headers['x-forwarded-for'] + const ip = Array.isArray(forwarded) + ? forwarded[0] // Get the first IP if it's an array + : forwarded || req.socket.remoteAddress; const username = clientMsg.username; const { isValid, error } = validateUsername(username); if (!isValid) { @@ -147,8 +147,9 @@ wss.on('connection', (ws, req) => { })); return; } - - // If username is valid, add the client + if (Array.isArray(ip)) { + ip = ip[0] + } gm.addClient( new Client( clientMsg.clientID,