-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorated.js
65 lines (53 loc) · 1.36 KB
/
sorated.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
'use strict';
var sorated = {};
//Store hits per minute
var hits = {};
//each minute, clean up hits store
var cleanup = setInterval(function(){
for (var x in hits){
hits[x].minute = {};
if (new Date().getMinutes() === 0){
hits[x].minute = {};
}
if (new Date().getHours() === 0){
hits[x].day = {};
}
}
}, 1000*60);
/**
* Very simple in-memory rate limiting
*
* options = {max: {minute: 10, hour: 100}}
*
*/
sorated.RateLimit = function(key, options) {
if (typeof hits[key] === 'undefined'){
hits[key] = {
minute: {},
hour: {},
day: {}
};
}
return function rateLimit(req, res, next) {
var ip = options.global ? 'global' : req.ip;
var rejected = '';
for (var prop in hits[key]){
if (typeof hits[key][prop][ip] !== "number") {
hits[key][prop][ip] = 0;
} else {
hits[key][prop][ip]++;
}
if (hits[key][prop][ip] >= options.max[prop] && !rejected) {
// 429 status = Too Many Requests (RFC 6585)
res.status(429).send('Too many requests, please try again later.');
rejected = prop;
}
}
if (rejected){
console.log('rejected because of: ' + rejected + ' via ' + ip);
} else {
next();
}
};
};
module.exports = sorated;