-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.js
153 lines (130 loc) · 5.69 KB
/
setup.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
const { exec, execSync } = require('child_process');
const config = require('./config.json');
const Logs = require('./modules/logs.js');
var setup = {
RestartNginx: () => {
console.log(Logs.info('Restarting nginx...'));
execSync('whoami').toString().includes('root') ? execSync('service nginx restart') : execSync('sudo service nginx restart');
},
HasModule: (module) => {
try {
require.resolve(module);
return true;
} catch(e) {
return false;
}
},
InstallModules: (module) => {
if(!setup.HasModule(module)){
console.log(Logs.info('Installing module ' + module + '...'));
exec('npm install ' + module, (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
if(stdout.includes('added')) {
console.log(Logs.success('Module ' + module + ' installed !'));
}
});
}
},
}
function WriteIncludeBlacklistFile() {
console.log(Logs.info('Writing include blacklist file...'));
const fs = require('fs');
const chalk = require('chalk');
const blacklist = fs.readFileSync('/etc/nginx/nginx.conf', 'utf8');
const blacklist_array = blacklist.split('sendfile on;');
fs.writeFileSync('/etc/nginx/nginx.conf', blacklist_array[0] + 'sendfile on;\ninclude ' + config.blacklist_file + ';\n' + blacklist_array[1]);
console.log(Logs.success('Include blacklist file written !'));
setup.RestartNginx();
}
function SetupBlacklistFile(){
const fs = require('fs');
const chalk = require('chalk');
if(process.platform != "linux"){
console.log(Logs.error('The installation did not work because you must be under linux.'));
return false;
}
if (!fs.existsSync(config.blacklist_file)) {
console.log(Logs.info('Creating blacklist file...'));
fs.writeFileSync(config.blacklist_file, '');
console.log(Logs.success('Blacklist file created !'));
WriteIncludeBlacklistFile();
}
}
if(process.argv[2] == '--firewalladd') {
// récupérer le json et ajouter l'ip dans le fichier et sauvegarder en json
const fs = require('fs');
const chalk = require('chalk');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function Add() {
fs.readFile('./config.json', 'utf8', function readFileCallback(err, data){
if (err){
console.log(err);
}
else {
const json = JSON.parse(data);
rl.question('Name of the rule (firewall1):', (name) => {
json.firewall.forEach((rule) => {
if(rule.name == name) {
console.log(Logs.error('The name is already used !'));
rl.close();
process.exit();
}
});
rl.question('Entire path (/example/test.php):', (path) => {
rl.question('X times to be ratelimit (5):', (ratelimit) => {
rl.question('Time of the ban in second (90):', (time) => {
rl.question('Time in seconds to refresh the ip table (10):', (refresh) => {
json.firewall.push({
"name": name,
"enabled": true,
"path": path,
"ratelimit": ratelimit,
"ratelimit_duration": time,
"ratelimit_ban": refresh
});
rl.question('Do you want to save the rule ? (y/n):', (answer) => {
if(answer == 'y') {
fs.writeFileSync('./config.json', JSON.stringify(json, null, 4));
console.clear();
console.log(Logs.success('The rule ' + name + ' was well added !'));
rl.question('Do you want to add another rule ? (y/n):', (another) => {
if(another == 'y') {
Add();
} else {
console.clear();
console.log(Logs.info('Good Bye !'));
rl.close();
}
});
} else {
rl.close();
console.clear();
}
});
});
});
});
});
});
}
});
}
Add();
} else if(process.argv[2] == '--help') {
console.log(Logs.info('Usage:') + " node setup.js [--help --setup --firewalladd]");
} else if(process.argv[2] == '--setup') {
config.modules.forEach(module => {
console.log(Logs.info('Start installing module ' + module + '...'));
InstallModule(module);
});
}
if( !process.argv[2] ){
console.log(Logs.info('Usage:') + " node setup.js [--help --setup --firewalladd]");
}