You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/middleware/ratelimit.js

36 lines
789 B
JavaScript

'use strict';
var winston = require('winston');
var ratelimit = {};
var allowedCallsPerSecond = 20;
ratelimit.isFlooding = function(socket) {
socket.callsPerSecond = socket.callsPerSecond || 0;
socket.elapsedTime = socket.elapsedTime || 0;
socket.lastCallTime = socket.lastCallTime || Date.now();
++socket.callsPerSecond;
var now = Date.now();
socket.elapsedTime += now - socket.lastCallTime;
if (socket.callsPerSecond > allowedCallsPerSecond && socket.elapsedTime < 1000) {
winston.warn('Flooding detected! Calls : ' + socket.callsPerSecond + ', Duration : ' + socket.elapsedTime);
return true;
}
if (socket.elapsedTime >= 1000) {
socket.elapsedTime = 0;
socket.callsPerSecond = 0;
}
socket.lastCallTime = now;
return false;
};
module.exports = ratelimit;