using node.js, the net module for building a tcp server which can hande http requests.
I would like to prevent dos attacks so what I have done is somthing like this:
Total.js Framework: https://github.com/totaljs/modules/blob/master/ddos/ddos.js
var counter = 0;
var ip = {};
var ban = {};
var ban_length = 0;
var interval = 0;
exports.install = function () {
framework.onRequest = function (req, res) {
if (ban_length > 0 && ban[req.ip]) {
req.connection.destroy();
return true
}
var count = (ip[req.ip] || 0) + 1;
ip[req.ip] = count;
if (count === 1) counter++;
if (count < exports.options.maximum) return false;
ban[req.ip] = exports.options.minutes + 1;
ban_length++;
return true
};
setInterval(function () {
interval++;
var keys;
var length;
var count;
if (ban_length > 0 && interval % 60 === 0) {
keys = Object.keys(ban);
length = keys.length;
count = 0;
for (var i = 0; i < length; i++) {
var key = keys[i];
if (ban[key]-- > 0) continue;
ban_length--;
delete ban[key]
}
if (ban_length < 0) ban_length = 0
}
if (counter <= 0) return;
keys = Object.keys(ip);
length = keys.length;
counter = length;
for (var i = 0; i < length; i++) {
var key = keys[i];
var count = ip[key]--;
if (count > 0) continue;
counter--;
delete ip[key]
}
if (counter < 0) counter = 0
}, 1e3)
};
exports.usage = function () {
return {
bans: ban_length
}
};
exports.options = {
maximum: 1e3,
minutes: 5
};