-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddos.js
60 lines (60 loc) · 1.38 KB
/
ddos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Generated by LiveScript 1.5.0
(function(){
var iptable, now, getType, ban, reject, setup;
iptable = {
'static': {},
api: {}
};
now = function(){
return new Date().getTime();
};
getType = function(url){
switch (false) {
case url.indexOf('/api') !== 0:
return 'api';
default:
return 'static';
}
};
ban = function(config, c){
c.stops = 0;
return c.ban = now() + config.ban;
};
reject = function(req, c){
c.stops += 1;
return req.socket.destroy();
};
setup = function(config){
return function(req, res, next){
var type, table, ip, c, ref$, lastUpdate, diff;
type = getType(req.originalUrl);
table = iptable[type];
ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
c = table[ip] = (ref$ = table[ip]) != null
? ref$
: {
lastUpdate: null,
stops: 0,
ban: null
};
if (c.stops > 3) {
ban(config, c);
}
if (c.ban != null && c.ban > now()) {
return req.socket.destroy();
}
c.ban = null;
lastUpdate = c.lastUpdate;
c.lastUpdate = now();
if (lastUpdate === null) {
return next();
}
diff = now() - lastUpdate;
if (diff < config[type]) {
return reject(req, c);
}
return next();
};
};
module.exports = setup;
}).call(this);