-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (205 loc) · 6.46 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sqlite from 'better-sqlite3';
import exitHook from 'exit-hook';
import { getIP, getXForwardedFor } from './trackingValues.js';
const OPTIONS_DEFAULT = {
byIP: true,
byXForwardedFor: false,
abuseMessageText: 'Access temporarily restricted.',
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
abusePoints: 1,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
abusePointsMax: 10,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expiryMillis: 5 * 60 * 1000, // 5 minutes
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
clearIntervalMillis: 60 * 60 * 1000 // 1 hour
};
Object.freeze(OPTIONS_DEFAULT);
const TABLENAME_IP = 'AbusePoints_IP';
const TABLENAME_XFORWARDEDFOR = 'AbusePoints_XForwardedFor';
const TABLECOLUMNS_CREATE = '(trackingValue TEXT, expiryTimeMillis INT UNSIGNED, abusePoints TINYINT UNSIGNED)';
const TABLECOLUMNS_INSERT = '(trackingValue, expiryTimeMillis, abusePoints)';
let options = OPTIONS_DEFAULT;
// eslint-disable-next-line @typescript-eslint/init-declarations
let database;
// eslint-disable-next-line @typescript-eslint/init-declarations
let clearAbuseIntervalFunction;
/**
* Cleans up handler.
*/
export function shutdown() {
try {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (clearAbuseIntervalFunction !== undefined) {
clearInterval(clearAbuseIntervalFunction);
}
}
catch {
// ignore
}
try {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (database !== undefined) {
database.close();
}
}
catch {
// ignore
}
}
function initializeDatabase() {
if (database !== undefined) {
return;
}
database = sqlite(':memory:');
database
.prepare(`CREATE TABLE IF NOT EXISTS ${TABLENAME_IP} ${TABLECOLUMNS_CREATE}`)
.run();
database
.prepare(`CREATE TABLE IF NOT EXISTS ${TABLENAME_XFORWARDEDFOR} ${TABLECOLUMNS_CREATE}`)
.run();
}
/**
* Initializes the middleware.
* @param optionsUser - The options.
* @returns The Express middleware.
*/
export function initialize(optionsUser) {
options = { ...OPTIONS_DEFAULT, ...optionsUser };
if (database === undefined) {
initializeDatabase();
clearAbuseIntervalFunction = setInterval(clearExpiredAbuse, options.clearIntervalMillis);
exitHook(() => {
shutdown();
});
}
return abuseCheckHandler;
}
function clearExpiredAbuse() {
if (options.byIP && database !== undefined) {
database
.prepare(`DELETE FROM ${TABLENAME_IP} WHERE expiryTimeMillis <= ?`)
.run(Date.now());
}
if (options.byXForwardedFor && database !== undefined) {
database
.prepare(`DELETE FROM ${TABLENAME_XFORWARDEDFOR} WHERE expiryTimeMillis <= ?`)
.run(Date.now());
}
}
function getAbusePoints(tableName, trackingValue) {
const row = database
?.prepare(`select sum(abusePoints) as abusePointsSum
from ${tableName}
where trackingValue = ?
and expiryTimeMillis > ?`)
.get(trackingValue, Date.now());
return row?.abusePointsSum ?? 0;
}
function clearAbusePoints(tableName, trackingValue) {
database
?.prepare(`DELETE FROM ${tableName} WHERE trackingValue = ?`)
.run(trackingValue);
}
/**
* Clears all abuse records from a requestor, expired or not.
* @param request - The Express request.
*/
export function clearAbuse(request) {
if (options.byIP) {
const ipAddress = getIP(request);
if (ipAddress !== '') {
clearAbusePoints(TABLENAME_IP, ipAddress);
}
}
if (options.byXForwardedFor) {
const ipAddress = getXForwardedFor(request);
if (ipAddress !== '') {
clearAbusePoints(TABLENAME_XFORWARDEDFOR, ipAddress);
}
}
}
/**
* Checks if the current requestor is considered from an abusive source.
* @param request - The Express request.
* @returns `true` if the requestor is considered an abusive source.
*/
export function isAbuser(request) {
if (options.byIP) {
const ipAddress = getIP(request);
if (ipAddress !== '') {
const abusePoints = getAbusePoints(TABLENAME_IP, ipAddress);
if (abusePoints >= options.abusePointsMax) {
return true;
}
}
}
if (options.byXForwardedFor) {
const ipAddress = getXForwardedFor(request);
if (ipAddress !== '') {
const abusePoints = getAbusePoints(TABLENAME_XFORWARDEDFOR, ipAddress);
if (abusePoints >= options.abusePointsMax) {
return true;
}
}
}
return false;
}
/**
* Adds a new abuse record.
* @param request - The Express request.
* @param abusePoints - The number of abuse points to apply.
* @param expiryMillis - The length of time in milliseconds until the abuse points expire.
*/
export function recordAbuse(request, abusePoints = options.abusePoints, expiryMillis = options.expiryMillis) {
const expiryTimeMillis = Date.now() + expiryMillis;
if (options.byIP) {
const ipAddress = getIP(request);
if (ipAddress !== '') {
database
?.prepare(`INSERT INTO ${TABLENAME_IP} ${TABLECOLUMNS_INSERT} VALUES (?, ?, ?)`)
.run(ipAddress, expiryTimeMillis, abusePoints);
}
}
if (options.byXForwardedFor) {
const ipAddress = getXForwardedFor(request);
if (ipAddress !== '') {
database
?.prepare(`INSERT INTO ${TABLENAME_XFORWARDEDFOR} ${TABLECOLUMNS_INSERT} VALUES (?, ?, ?)`)
.run(ipAddress, expiryTimeMillis, abusePoints);
}
}
}
/**
* Middleware handler function
* @param request - The Express request.
* @param response - The Express response.
* @param next - The Express next function.
*/
function abuseCheckHandler(request, response, next) {
const isRequestAbuser = isAbuser(request);
if (isRequestAbuser) {
response.status(403).send(options.abuseMessageText);
response.end();
}
else {
next();
}
}
/**
* Middleware setup function.
* @param optionsUser - The options.
* @returns - The middleware handler function.
*/
export function abuseCheck(optionsUser) {
initialize(optionsUser);
return abuseCheckHandler;
}
export default {
initialize,
shutdown,
recordAbuse,
isAbuser,
clearAbuse,
abuseCheck
};