This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrbl-server.js
95 lines (88 loc) · 3.32 KB
/
rbl-server.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
var dns = require('dns')
, db = require('./list.json')
, nDNS = require('native-dns')
, server = nDNS.createServer()
, winston = require('winston')
, redis = require("redis")
, config = require("./config.json")
, async = require('async')
;
redisClient = redis.createClient();
var log = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ timestamp: true, colorize: true }),
new (winston.transports.File)({ filename: config.logfile, timestamp: true })
]
});
server.on('request', function (request, response) {
/* redisClient.get(request.question[0].name, function (err, data) {
if(data) {
data = JSON.parse(data);
console.log(data);
if(data.answer.length < 1) {
response.header.rcode = nDNS.consts.NAME_TO_RCODE.NOTFOUND;
response.send();
}
else {
response.answer.push(data.answer);
response.answer.push(data.additional);
response.send();
}
}
else {
*/
ip = request.question[0].name.replace(config.hostBase, '');
async.each(db, function (rbl, callback) {
dns.resolve(ip + rbl.dns, function (err, domain) {
if(err) {
callback(null);
}
else {
response.answer.push(nDNS.A({
name: request.question[0].name,
address: config.standardResponse,
ttl: 300,
}));
response.answer.push(nDNS.TXT({
name: request.question[0].name,
data: rbl.dns,
ttl: 300,
}));
callback(null);
}
});
},
function (err) {
if(response.answer.length < 1) {
response.header.rcode = nDNS.consts.NAME_TO_RCODE.NOTFOUND;
response.send();
res = {};
res.answer = response.answer;
res.answer.push(response.additional);
redisClient.set(request.question[0].name, JSON.stringify(res), function (err, data) {
if (err) { log.error('REDIS: ' + err); }
redisClient.expire(request.question[0].name, 1800);
});
}
else {
response.send();
res = {};
res.answer = response.answer;
res.answer.push(response.additional);
redisClient.set(request.question[0].name, JSON.stringify(res), function (err, data) {
if (err) { log.error('REDIS: ' + err); }
redisClient.expire(request.question[0].name, 1800);
});
}
});
//}
//});
log.info(request.address.address + '\t' + request.question[0].name);
});
server.on('error', function ( err, buff, req, res) {
log.error('DNS' + err);
});
redisClient.on('error', function (err) {
log.error('REDIS: ' + err);
});
server.serve(config.listenPort, config.listenHost);