-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (77 loc) · 2.24 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
//var conf = require("./conf.js");
var spawn = require('child_process').spawn;
var logParser = require('./lib/logParser.js');
var ddos = new (require('./lib/ddos.js'));
var iptables =require('./lib/iptables.js');
var accessLogs = [];
var child, args;
var Q = require('q');
var php = require('phplike/module');
if (process.argc < 3) {
console.log("Example: sudo node index.js conf.js");
process.exit();
}
if (process.argv[2].indexOf('/') != 0) {
process.argv[2] = './' + process.argv[2];
}
var config = require(process.argv[2]);
if (config.blockRule) {
ddos.setRule(config.blockRule);
}
config.logFiles.map(function (file) {
accessLogs.push(file);
});
args = ["-f"];
for (var index in accessLogs) {
args.push(accessLogs[index]);
}
iptables.init();
// tail -f file1 file2 ...
child = spawn ('tail', args);
child.stdout.on ('data', function (data)
{
var passedPaths = config.skipPaths, skipIps = ["127.0.0.1"];
var ret = logParser(data.toString());
if (!ret) return "";
var promise = [];
if (config.skipIps) {
skipIps = config.skipIps;
}
ret.map(function (req) {
for (var i in passedPaths) {
if (req.path.indexOf(passedPaths[i]) === 0) {
return "";
}
}
for (var i in skipIps) {
if (req.ip.indexOf(skipIps[i]) === 0) {
console.log("test skip");
return "";
}
}
//ddos.isReject(req);
if (req.protocol.toLowerCase() === "post") {
promise.push(function () {
var deferred = Q.defer();
ddos.shouldReject(req)
.then(function (ret) {
if (ret === true) {
iptables.reject(req.ip).then(function (rejRet) {
if (rejRet === true) console.log('Block this ip: ' + req.ip);
});
}
deferred.resolve(ret);
});
return deferred.promise;
});
}
});
promise.reduce(function(qp, func) {
return qp.then(func);
}, Q());
console.log ( ret);
});
child.stderr.on ('data', function (data)
{
console.log ('Error: ' + data.toString());
});