-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
188 lines (148 loc) · 5.27 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
const fs = require('fs');
const chalk = require('chalk');
const Tail = require('nodejs-tail');
const { exec, execSync } = require('child_process');
const config = require('./config.json');
const Logs = require('./modules/logs.js');
var firewall = [];
var client = [];
var clientBanned = [];
var blacklist_data = "";
const tail = new Tail(config.access_log);
var runmode = "sudo";
if(config.firewall.length == 0){
console.log(Logs.error('You have not yet made a firewall configuration'));
return false;
}else{
console.log(Logs.info('Getting the firewall...'));
config.firewall.forEach(rule => {
if(rule.enabled == true){
console.log(Logs.info('Getting the firewall rule ' + rule.name + '...'));
if (firewall[rule.path] === undefined) {
firewall[rule.path] = {
ratelimit_limit: rule.ratelimit, // cdfrr
ratelimit_duration: rule.ratelimit_duration, // ddsr
ratelimit_ban: rule.ratelimit_ban, // dpqltr
client:[]
};
}
console.log(Logs.success('Firewall rule ' + rule.name + ' added !'));
}
});
if(execSync('whoami').toString().includes('root')){
console.log(Logs.success('The script will restart nginx in root mode'));
runmode = "";
}else{
console.log(Logs.warning('The script will restart nginx in sudo mode'));
}
}
function RestartNginx() {
console.log(Logs.info('Restarting nginx...'));
setTimeout(function () {
exec('/etc/init.d/nginx ' + runmode + ' reload', (err, stdout, stderr) => {
if (err) {
console.log(err);
console.log(Logs.error('Error restarting nginx'));
return;
}
});
}, 500);
}
function UpdateBannedList(){
const data = fs.readFileSync(config.blacklist_file, 'UTF-8');
const lines = data.split('\n');
change = false;
blacklist_data = data;
lines.forEach(function (item) {
if(item != ''){
let ip = item.split('deny')[1].split(';')[0].trim();
let time_to_ban = item.split('#')[1];
let date = new Date();
let time = date.getTime();
if(ip != ''){
if(time_to_ban < time){
console.log(Logs.success('The ip ' + ip + ' has been unbanned'));
blacklist_data = blacklist_data.replace(item, '').replace(/(^[ \t]*\n)/gm, "");
change = true;
}
}
}
});
fs.writeFile(config.blacklist_file, blacklist_data, 'utf8', function (err) {
if (err) return console.log(err);
});
if(change == true){
RestartNginx();
}
}
function BanIp(ip, path){
let date = new Date();
let time = date.getTime();
let time2 = time + firewall[path].ratelimit_duration * 1000;
fs.readFile(config.blacklist_file, function (err, data) {
if (err) throw err;
if(!data.includes(ip)){
if(clientBanned[ip]){
return false;
}
if(clientBanned[ip] === undefined){
clientBanned[ip] = {
count: 1,
};
setTimeout(() => {
delete clientBanned[ip];
}, firewall[path].ratelimit_duration * 1000);
}
fs.appendFile(config.blacklist_file, '\ndeny ' + ip.trim() + ' ;#' + time2, function (err) {
if (err) throw err;
});
console.log(Logs.error('The ip ' + ip + ' has been banned for ' + firewall[path].ratelimit_duration + ' seconds for the path ' + path));
UpdateBannedList();
RestartNginx();
}
});
}
function Random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function Verification(ip, path){
if(firewall[path] === undefined){
return false;
}
if(firewall[path].client[ip] === undefined){
firewall[path].client[ip] = {
count: 1,
};
setTimeout(() => {
delete firewall[path].client[ip];
}, firewall[path].ratelimit_ban * 1000);
}
firewall[path].client[ip].count += 1;
if(firewall[path].client[ip].count >= firewall[path].ratelimit_limit){
setTimeout(() => {
BanIp(ip, path);
}, Random(100, 500));
};
}
tail.on('line', (line) => {
if (line) {
const found = line.match(/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/);
const found_ip = line.split('-');
//console.log(line);
if (found) {
if ((found_ip[0] != null) && (found[8] != null)) {
Verification(found_ip[0], found[8].split('?')[0]); // ip | path |
}
}
}
});
tail.on('close', () => {
console.log(Logs.error('The script was stopped.'));
});
tail.on('error', () => {
console.log(Logs.error('The access log file is not found.'));
});
tail.watch();
UpdateBannedList();
RestartNginx();
setInterval(UpdateBannedList, 2000);