This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rule.js
150 lines (113 loc) · 3.12 KB
/
rule.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
141
142
143
144
145
146
147
148
149
150
const HTTP_TOO_MANY_REQUESTS = 429;
const DEFAULT_WEIGHT = 1;
const DEFAULT_MAX_WEIGHT = 10;
const DEFAULT_ERROR_CODE = HTTP_TOO_MANY_REQUESTS;
const DEFAULT_QUEUE_SIZE = 0; // No queue.
const DEFAULT_ERROR_DATA = 'Not so fast!';
class Rule {
static get DEFAULT_WEIGHT() {
return DEFAULT_WEIGHT;
}
static get DEFAULT_MAX_WEIGHT() {
return DEFAULT_MAX_WEIGHT;
}
static get DEFAULT_ERROR_CODE() {
return DEFAULT_ERROR_CODE;
}
static get DEFAULT_QUEUE_SIZE() {
return DEFAULT_QUEUE_SIZE;
}
static get DEFAULT_ERROR_DATA() {
return DEFAULT_ERROR_DATA;
}
constructor(rule = {}, options = {}) {
if (rule === null) {
rule = {};
}
if (options === null) {
options = {};
}
const {string, regexp} = rule;
this.string = string;
this.regexp = regexp;
this.weight = _getNumber('weight', rule, options, DEFAULT_WEIGHT);
this.maxWeight = _getNumber('maxWeight', rule, options, DEFAULT_MAX_WEIGHT);
this.errorCode = _getNumber('errorCode', rule, options, DEFAULT_ERROR_CODE);
this.queueSize = _getNumber('queueSize', rule, options, DEFAULT_QUEUE_SIZE);
this.errorData = _getNonUndefined('errorData', rule, options, DEFAULT_ERROR_DATA);
this.logFunction = _getFunction('logFunction', rule, options);
this.users = new Map();
}
use(ip, path, ddos, next) {
let user = this.users.get(ip);
if (user) {
user.weight += this.weight;
} else {
user = {
weight: this.weight,
queue: []
};
this.users.set(ip, user);
}
if (user.weight <= this.maxWeight) {
if (typeof next === 'function') {
next();
}
return;
}
if (typeof this.logFunction === 'function') {
this.logFunction(ip, path, user.weight, this.maxWeight, this.regexp || this.string);
}
if (user.queue.length < this.queueSize) {
user.queue.push(next);
} else if (typeof ddos === 'function') {
ddos(this.errorCode, this.errorData);
}
}
check() {
this.users.forEach((user, ip) => {
user.weight -= this.maxWeight;
const count = this.maxWeight - user.weight;
if ((count > 0) && (user.queue.length > 0)) {
user.queue
.splice(0, count)
.filter(next => typeof next === 'function')
.forEach(next => setImmediate(next));
user.weight += count;
}
if (user.weight <= 0) {
this.users.delete(ip);
}
});
}
}
function _getNumber(name, rule, options, defValue) {
const value = Number(rule[name]) || Number(options[name]);
if (isNaN(value) || (value < 0)) {
return defValue;
}
return value;
}
function _getNonUndefined(name, rule, options, defValue) {
let value = rule[name];
if (value !== undefined) {
return value;
}
value = options[name];
if (value !== undefined) {
return value;
}
return defValue;
}
function _getFunction(name, rule, options, defValue) {
let value = rule[name];
if (typeof value === 'function') {
return value;
}
value = options[name];
if (typeof value === 'function') {
return value;
}
return defValue;
}
module.exports = Rule;