-
Notifications
You must be signed in to change notification settings - Fork 23
/
core.js
140 lines (115 loc) · 3.13 KB
/
core.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Sikka._rebuildStats = function() {
var stats = {
perIp: {},
perSession: {},
perHuman: {}
};
return stats;
};
Sikka.stats = {};
Sikka._blackList = {};
Sikka._humanTokens = {};
Sikka._getIp = function _getIp(headers, remoteAddress) {
var xForwardedFor = headers['x-forwarded-for'];
if(xForwardedFor) {
var firstIp = xForwardedFor.split(",")[0];
return firstIp;
} else {
return remoteAddress;
}
};
Sikka._updateStats = function _updateStats(ip, sessionId, humanToken) {
Sikka._ensureStats('perIp', ip, 1000 * 5);
Sikka._ensureStats('perSession', sessionId, 1000 * 5);
Sikka._ensureStats('perHuman', humanToken, 1000 * 5);
Sikka._incStat('perIp', ip);
Sikka._incStat('perSession', sessionId);
Sikka._incStat('perHuman', humanToken);
var blocked = false;
if(Sikka._rateExceeds("perIp", ip, Config.rateLimits.perIp)) {
console.log("Sikka: IP Blocked.", ip);
Sikka._blockIpFor(ip, Config.times.blockIpFor);
blocked = true;
}
if(Sikka._isValidHuman(humanToken)) {
var perHumanMaxRate = Config.rateLimits.perHuman;
if(Sikka._rateExceeds("perHuman", humanToken, perHumanMaxRate)) {
console.log("Sikka: Human Blocked", humanToken);
Sikka._deleteHuman(humanToken);
blocked = true;
} else {
blocked = false;
}
} else if(Config.onlyForHumans) {
blocked = true;
}
return blocked;
};
Sikka._rateExceeds = function(type, key, maxValue) {
var stats = Sikka._getStat(type, key);
if(!stats) {
return false;
}
var timeDiffSecs = (Date.now() - stats.startedAt) / 1000;
if(timeDiffSecs < 1) {
return false;
}
var rate = (stats.count) / timeDiffSecs;
return rate > maxValue;
}
Sikka._blockIpFor = function(ip, millis) {
millis = millis || 1000 * 60;
if(Sikka._blackList[ip]) {
clearTimeout(Sikka._blackList[ip]);
}
Sikka._blackList[ip] = setTimeout(function() {
delete Sikka._blackList[ip];
}, millis);
};
Sikka._isBlocked = function(ip) {
return !!Sikka._blackList[ip];
};
Sikka._addHumanFor = function(token, millis) {
if(Sikka._humanTokens[token]) {
clearTimeout(Sikka._humanTokens[token]);
}
millis = millis || 1000 * 60 * 60;
Sikka._humanTokens[token] = setTimeout(function() {
delete Sikka._humanTokens[token];
}, millis);
};
Sikka._deleteHuman = function(token) {
if(Sikka._humanTokens[token]) {
clearTimeout(Sikka._humanTokens[token]);
}
delete Sikka._humanTokens[token];
};
Sikka._isValidHuman = function(humanToken) {
return !!Sikka._humanTokens[humanToken];
};
Sikka._ensureStats = function(type, key, resetMillis) {
resetMillis = resetMillis || 1000 * 5;
if(!Sikka.stats[type]) {
Sikka.stats[type] = {};
}
if(!Sikka.stats[type][key]) {
Sikka.stats[type][key] = {
startedAt: Date.now(),
count: 0
};
setTimeout(function() {
delete Sikka.stats[type][key];
}, resetMillis);
}
};
Sikka._incStat = function(type, key, value) {
value = value || 1;
Sikka.stats[type][key].count += value;
};
Sikka._getStat = function(type, key) {
if(Sikka.stats[type] && Sikka.stats[type][key]) {
return Sikka.stats[type][key];
} else {
return null;
}
};