-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (77 loc) · 2.4 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
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
var named = require('node-named');
var dns = require('dns');
var os = require('os');
var server = named.createServer();
var interfaces = os.networkInterfaces();
module.exports = function (data) {
function domainMatchesFilters (domain) {
for (var i=0, j=data.hosts.length; i<j; i++) {
if ( typeof data.hosts[i].substring(0,1) === '/' ) {
if (domain.match(data.hosts[i])) {
return true;
}
} else {
if ( domain === data.hosts[i] ) {
return true;
}
}
}
return false;
}
var hostname = data.serverHost;
if (!hostname) {
for (var i=0; i<interfaces.en1.length; i++) {
if (interfaces.en1[i].family === 'IPv4') {
hostname = interfaces.en1[i].address;
break;
}
}
}
server.listen(53, hostname, function () {
console.log('Listening on port 53 with hostname "' + hostname + '"...');
});
server.on('query', function (query) {
var domain = query.name();
var answer;
if ( domainMatchesFilters(domain) ) {
console.log('Serving', query.name(), 'as local domain with type', query.type());
answer = new named.ARecord('10.0.1.3');
query.addAnswer(domain, answer, 300);
server.send(query);
} else {
console.log('Forwarding', query.name(), 'request of type', query.type());
dns.resolve(domain, query.type(), function (err, addresses) {
if (err) console.log('Problem getting DNS record:', err);
if ( !addresses || addresses.length < 1 ) {
console.log('Response is apparently empty: ' + typeof addresses);
server.send(query);
return;
}
switch(query.type()) {
case 'A':
answer = new named.ARecord(addresses[0]);
break;
case 'AAAA':
console.log(JSON.stringify(addresses));
answer = new named.AAAARecord(addresses[0]);
break;
case 'CNAME':
answer = new named.CnameRecord(addresses[0]);
break;
case 'MX':
var rec = addresses[0];
answer = new named.MxRecord(rec.exchange, {
priority: rec.priority
});
break;
default:
console.log(query.type(), 'WTF is this??');
server.send(query);
return;
}
query.addAnswer(domain, answer, 300);
server.send(query);
});
}
});
}