-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (52 loc) · 1.59 KB
/
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
//Consts
const dns = require('native-dns');
const app_dns = dns.createServer();
const google_dns = '8.8.8.8';
const port_dns = 53;
const dns_blocked = require('./conf.json');
//Logs triggered by various server events
app_dns.on('listening', function(){
console.log('server listening on', app_dns.address());
});
app_dns.on('close', function(){
console.log('server closed', app_dns.address());
});
app_dns.on('error', function(err, buff, req, res) {
console.error(err.stack);
});
app_dns.on('socketError', function(err, socket) {
console.error(err);
});
//Now for the fun part!
app_dns.on('request', function(request, response){
var question = request.question[0];
if (dns_blocked.blocked.indexOf(question.name) != -1){
// lets end processing here and send an empty answer response,
// since this host is in our block list
response.answer = [];
return response.send();
}
// ok, the requested host isn't blocked so lets retrieve a result
var forward = dns.Request({
question: question,
server: {
address: google_dns,
type: 'udp',
port: port_dns
},
cache: false
});
// when we get answers back, add them to the response
forward.on('message', function(err, msg) {
msg.answer.forEach(function(a){
response.answer.push(a);
});
});
// when the forwarded request finishes, send off our actual response
forward.on('end', function(){
response.send();
});
// begin the forward
forward.send();
});
app_dns.serve(port_dns);