1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

Chat JSON and spam fix

This commit is contained in:
Amateur Audio Dude
2024-11-04 21:55:23 +11:00
committed by GitHub
parent 10d379585b
commit 0a927c9e15

View File

@@ -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() {