diff --git a/server/index.js b/server/index.js index 39a7e9e..110f2a6 100644 --- a/server/index.js +++ b/server/index.js @@ -576,9 +576,49 @@ chatWss.on('connection', (ws, request) => { admin: request.session.isAdminAuthenticated }; ws.send(JSON.stringify(ipMessage)); - + + const userCommands = {}; + ws.on('message', function incoming(message) { - const messageData = JSON.parse(message); + + // Anti-spam + const command = message.toString(); + const now = Date.now(); + + // Update the last message time for general spam detection + lastMessageTime = now; + + // Initialize command history for rate-limiting checks + if (!userCommands[command]) { + userCommands[command] = []; + } + + // Record the current timestamp for this command + userCommands[command].push(now); + + // Remove timestamps older than 1 second + userCommands[command] = userCommands[command].filter(timestamp => now - timestamp <= 1000); + + // If command count exceeds limit, close connection + if (userCommands[command].length > 3) { + logWarn(`User \x1b[90m${clientIp}\x1b[0m is spamming command "${command}". Connection will be terminated.`); + ws.close(1008, 'Spamming detected'); + return; + } + + + + let messageData; + + try { + messageData = JSON.parse(message); + } catch (error) { + // console.error("Failed to parse message:", error); + // Optionally, send an error response back to the client + ws.send(JSON.stringify({ error: "Invalid message format" })); + return; // Stop processing if JSON parsing fails + } + messageData.ip = clientIp; // Adding IP address to the message object const currentTime = new Date(); @@ -590,7 +630,7 @@ chatWss.on('connection', (ws, request) => { return; } - if(request.session.isAdminAuthenticated === true) { + if (request.session.isAdminAuthenticated === true) { messageData.admin = true; } @@ -611,15 +651,6 @@ chatWss.on('connection', (ws, request) => { client.send(modifiedMessage); } }); -}); - - ws.on('close', function close() { - }); -}); - -rdsWss.on('connection', (ws, request) => { - ws.on('message', function incoming(message) { - }); ws.on('close', function close() {