-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns-server.js
154 lines (128 loc) · 4.57 KB
/
dns-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
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
154
const dgram = require('dgram');
const DNSMessage = require('./dns-parser');
// A Domain to Ip Record (A Record)
class DNSRecord {
constructor(name, data, type = 1, clss = 1, dataType = "ip") {
this.name = name;
this.type = type;
this.class = clss;
this.data = data;
this.dataType = dataType;
}
getId() {
return this.name + "|" + this.type + "|" + this.class;
}
}
// DNSServer
class DNSServer {
constructor(remoteServerInfo = null, serverInfo = { address: '0.0.0.0', port: 53 }, timeout = 1000) {
this.remoteServerInfo = remoteServerInfo;
this.serverInfo = serverInfo;
this.timeout = timeout;
this.records = {};
this.requests = {};
// Create a UDP server
this.server = dgram.createSocket('udp4');
// When server receive message
this.server.on('message', (msg, info) => {
var dns = new DNSMessage();
// Try to Parse it
try {
dns.fromBuffer(msg);
}
catch (err) {
console.warn("Parse Message Error!");
console.warn(err);
return;
}
// DNS Message is a Query
if (dns.header.qr == 0) {
// Try to response it
dns = this.response(dns, info);
if (dns != null) {
var buf = dns.toBuffer();
this.server.send(buf, info.port, info.address);
}
return dns;
}
// DNS Message is an Answer
else {
// Check if it is from the Remote Server
if (info.address == remoteServerInfo.address && info.port == remoteServerInfo.port) {
// Find the Corresponding Query and Respond
var rinfo = this.requests[dns.header.id];
rinfo.hasRespond = true;
dns.header.ra = 0;
dns.header.id = rinfo.id;
dns.header.nscount = 0;
dns.header.arcount = 0;
if (dns.header.ancount == 0) {
dns.header.rcode = 3;
}
this.server.send(dns.toBuffer(), rinfo.port, rinfo.address);
}
return dns;
}
});
// Start the Server
this.server.bind({ address: this.serverInfo.address, port: this.serverInfo.port }, (err) => {
if (!err) {
console.log("DNS Server Start!");
}
else {
console.log(err);
}
});
}
response(dns, rinfo) {
// Only Respond the DNS Message that has only ONE Query
if (dns.header.qdcount == 1) {
var question = dns.questions[0];
var record = this.records[question.getId()];
// Local Record Exists
if (record) {
dns.header.qr = 1;
if(record.dataType == 'ip' && record.data == "0.0.0.0")
{
dns.header.nscount = 0;
dns.header.arcount = 0;
dns.rcode = 3;
}
else
{
dns.header.nscount = 0;
dns.header.arcount = 0;
dns.answer = dns.constructAnswer(record.data, record.dataType);
}
}
// Local Record Missing
else {
if (this.remoteServerInfo) {
//console.log(dns.header.id);
rinfo.id = dns.header.id;
dns.header.id = Buffer.alloc(2);
dns.header.id[0] = Math.round(Math.random() * 256);
dns.header.id[1] = Math.round(Math.random() * 256);
this.requests[dns.header.id] = rinfo;
var buf = dns.toBuffer();
this.server.send(buf, this.remoteServerInfo.port, this.remoteServerInfo.address);
return null;
}
else {
dns.header.rcode = 3;
}
}
}
else {
dns.header.rcode = 1;
}
return dns;
}
addRecord(record) {
this.records[record.getId()] = record;
}
}
module.exports = {
DNSServer: DNSServer,
DNSRecord: DNSRecord
}