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

banlist fix

This commit is contained in:
NoobishSVK
2024-11-03 20:29:10 +01:00
parent ae803ff155
commit b0db24577b

View File

@@ -356,6 +356,10 @@ wss.on('connection', (ws, request) => {
const output = serverConfig.xdrd.wirelessConnection ? client : serialport; const output = serverConfig.xdrd.wirelessConnection ? client : serialport;
let clientIp = request.headers['x-forwarded-for'] || request.connection.remoteAddress; let clientIp = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
if (serverConfig.webserver.banlist?.includes(clientIp)) {
return;
}
if (clientIp.includes(',')) { if (clientIp.includes(',')) {
clientIp = clientIp.split(',')[0].trim(); clientIp = clientIp.split(',')[0].trim();
} }
@@ -403,18 +407,29 @@ wss.on('connection', (ws, request) => {
// Anti-spam tracking for each client // Anti-spam tracking for each client
const userCommands = {}; const userCommands = {};
let lastMessageTime = Date.now();
ws.on('message', (message) => { ws.on('message', (message) => {
const command = message.toString(); const command = message.toString();
const now = Date.now();
logDebug(`Command received from \x1b[90m${clientIp}\x1b[0m: ${command}`); logDebug(`Command received from \x1b[90m${clientIp}\x1b[0m: ${command}`);
// Anti-spam check: initialize command history for this client if not existing // Detect extremely fast spamming (more than 1 message in under 10ms)
if (now - lastMessageTime < 10) {
logWarn(`User \x1b[90m${clientIp}\x1b[0m is likely a bot or script spamming. Connection will be terminated immediately.`);
ws.close(1008, 'Bot-like behavior detected');
return;
}
// Update the last message time
lastMessageTime = now;
// Initialize command history for rate-limiting checks
if (!userCommands[command]) { if (!userCommands[command]) {
userCommands[command] = []; userCommands[command] = [];
} }
// Record the current timestamp for this command // Record the current timestamp for this command
const now = Date.now();
userCommands[command].push(now); userCommands[command].push(now);
// Remove timestamps older than 1 second // Remove timestamps older than 1 second