mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-31 17:29:19 +02:00
Chat JSON and spam fix
This commit is contained in:
+41
-10
@@ -577,8 +577,48 @@ chatWss.on('connection', (ws, request) => {
|
|||||||
};
|
};
|
||||||
ws.send(JSON.stringify(ipMessage));
|
ws.send(JSON.stringify(ipMessage));
|
||||||
|
|
||||||
|
const userCommands = {};
|
||||||
|
|
||||||
ws.on('message', function incoming(message) {
|
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
|
messageData.ip = clientIp; // Adding IP address to the message object
|
||||||
const currentTime = new Date();
|
const currentTime = new Date();
|
||||||
|
|
||||||
@@ -617,15 +657,6 @@ chatWss.on('connection', (ws, request) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
rdsWss.on('connection', (ws, request) => {
|
|
||||||
ws.on('message', function incoming(message) {
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('close', function close() {
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
//additional web socket for using plugins
|
//additional web socket for using plugins
|
||||||
pluginsWss.on('connection', (ws, request) => {
|
pluginsWss.on('connection', (ws, request) => {
|
||||||
ws.on('message', message => {
|
ws.on('message', message => {
|
||||||
|
|||||||
Reference in New Issue
Block a user