This repository was archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
148 lines (116 loc) · 4.79 KB
/
controller.js
File metadata and controls
148 lines (116 loc) · 4.79 KB
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
const { EventEmitter } = require('events');
const fs = require('fs');
const path = require('path');
const Util = require('./Util');
class Controller extends EventEmitter {
constructor(options) {
super();
this.settings = {};
this.banned_ips = [];
this.request_count = {};
if ('file_path' in options) {
if (typeof(options.file_path) == 'string') {
this.settings.file_path = options.file_path + '/ipbans.txt';
}
else throw new Error('Invalid ban_threshold_count');
}
else this.settings.file_path = path.parse(process.argv[1]).dir + '/ipbans.txt';
if ('ban_threshold_count' in options) {
if (typeof(options.ban_threshold_count) == 'number') {
this.settings.max_requests = Math.abs(options.ban_threshold_count);
}
else throw new Error('Invalid ban_threshold_count');
}
else this.settings.max_requests = 30;
if ('ban_threshold_time' in options) {
if (typeof(options.ban_threshold_time) == 'number') {
this.settings.time_window = Math.abs(options.ban_threshold_time);
}
else throw new Error('Invalid ban_threshold_time');
}
else this.settings.time_window = 10;
if ('whitelisted_ips' in options) {
if (Array.isArray(options.whitelisted_ips)) {
this.settings.whitelisted_ips = options.whitelisted_ips.filter(x => typeof(x) == 'string');
}
else throw new Error('Invalid whitelisted_ips');
}
else this.settings.whitelisted_ips = [];
if ('response' in options) {
if (!Util.IsObject(options.response)) {
throw new Error('Invalid respond_to_banned_ips');
}
this.settings.response = {
code: options.response.code && typeof(options.response.code) == 'number' ? options.response.code : 403,
ctype: options.response.ctype && typeof(options.response.ctype) == 'string' ? options.response.ctype : 'text/plain',
body: options.response.body && typeof(options.response.body) == 'string' ? options.response.body : 'You are banned from accessing this server',
};
if (fs.existsSync(this.settings.response.body)) {
this.settings.response.body = fs.readFileSync(this.settings.response.body).toString();
}
}
this._middleware = (req, res, next) => {
const IP = Util.IPFromRequest(req);
if (this.settings.whitelisted_ips.includes(IP)) return next();
if (this.banned_ips.includes(IP)) {
this.emit('request_denied', req);
if (this.settings.response) {
res.setHeader('Content-Type', this.settings.response.ctype);
res.status(this.settings.response.code).send(this.settings.response.body);
}
return;
}
if (IP in this.request_count) this.request_count[IP]++;
else this.request_count[IP] = 1;
next();
};
this._ban = ip => {
if (this.banned_ips.includes(ip)) return false;
this.emit('ip_banned', ip);
this.banned_ips.push(ip);
this._save();
return true;
};
this._unban = ip => {
if (!this.banned_ips.includes(ip)) return false;
this.emit('ip_unbanned', ip);
this.banned_ips.splice(this.banned_ips.indexOf(ip), 1);
this._save();
return true;
};
this._load = () => {
if (fs.existsSync(this.settings.file_path)) {
this.banned_ips = fs.readFileSync(this.settings.file_path).toString().split('\n').map(x => x.trim());
}
else fs.writeFileSync(this.settings.file_path, '');
};
this._save = () => {
fs.writeFileSync(this.settings.file_path, this.banned_ips.join('\n'));
};
this._check = () => {
for (let ip in this.request_count) {
if (this.request_count[ip] >= this.settings.max_requests) {
this._ban(ip);
}
}
this.request_count = {};
};
this._load();
setInterval(() => this._check(), this.settings.time_window * 1000);
}
get middleware() {
return this._middleware;
}
ban(ip) {
if (!ip || !Util.IsIPAddress(ip)) return;
return this._ban(ip);
}
unban(ip) {
if (!ip || !Util.IsIPAddress(ip)) return;
return this._unban(ip);
}
load() {
this._load();
}
}
module.exports = Controller;