forked from iriscouch/dnsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.js
executable file
·266 lines (212 loc) · 7.81 KB
/
nameserver.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const tld = require('tldjs');
const to = require('./to')
a = {
"name": "nodetopia.com",
"ttl": 3600,
"admin": "hostmaster.nodetopia.com",
"serial": 2003080800,
"refresh": 172800,
"retry": 900,
"expiration": 1209600,
"minimum": 86400
}
class Nameserver {
constructor(store, console) {
this.store = store;
this.console = console;
}
async handler(req, res) {
let start = Date.now();
let self = this;
let question = req.question[0];
req.question.forEach(function (q) {
q.name = q.name.toLowerCase()
})
let { name, type } = question;
if (this[type]) {
try {
await this[type](req, res)
} catch (err) {
console.log('handler', err)
}
} else {
try {
await this.handleAll(req, res)
} catch (err) {
console.log('handler', err)
}
}
res.end()
req.question.forEach(function (q) {
//self.console.log('%s:%s/%s %s %s %j %j', req.connection.remoteAddress, req.connection.remotePort, req.connection.type, q.name, q.type, res.answer, res.additional)
self.console.log('%s:%s/%s %s %s', req.connection.remoteAddress, req.connection.remotePort, req.connection.type, q.name, q.type, res.answer.length, res.additional.length, Date.now() - start)
})
}
handleAll(req, res) {
let self = this;
return new Promise(async function (resolve, reject) {
let [err, zones] = await to(self.store.getAnswerList(req.question))
if (err || !zones) {
return reject(err || new Error('no zone'));
}
for (let zone of zones) {
res.answer.push(zone);
}
resolve();
})
}
AAAA(req, res) {
let self = this;
let question = req.question[0];
let { name } = question;
return new Promise(async function (resolve, reject) {
let [err, zones] = await to(self.store.getAnswerList(req.question))
if (err || !zones) {
return reject(err || new Error('no zone'));
}
for (let zone of zones) {
res.answer.push({
name: zone.name,
type: 'AAAA',
data: Nameserver.expandIPv6Address(zone.data),
ttl: zone.ttl
});
}
resolve();
})
}
MX(req, res) {
let self = this;
let question = req.question[0];
let { name } = question;
return new Promise(async function (resolve, reject) {
let [err, zones] = await to(self.store.getAnswerList(req.question))
if (err || !zones) {
return reject(err || new Error('no zone'));
}
for (let zone of zones) {
res.answer.push({
name: zone.name,
type: 'MX',
data: [zone.priority, zone.data],
ttl: zone.ttl
});
}
resolve();
})
}
SOA(req, res) {
let self = this;
let question = req.question[0];
let { name } = question;
return new Promise(async function (resolve, reject) {
let [err, soa] = await to(self.store.getSOAList(tld.getDomain(name.replace('*', ''))))
if (err) {
return reject(err);
}
res.answer.push({
'class': 'IN'
, 'type': 'SOA'
, 'name': name
, 'data': {
'mname': name
, 'rname': soa.admin
, 'serial': soa.serial
, 'refresh': soa.refresh
, 'retry': soa.retry
, 'expire': soa.expiration
, 'ttl': soa.ttl
}
});
resolve()
})
}
NS(req, res) {
let self = this;
let question = req.question[0];
let { name } = question;
return new Promise(async function (resolve, reject) {
let [err, nameservers] = await to(self.store.getNSList(tld.getDomain(name.replace('*', ''))))
if (err) {
return reject(err);
}
let promises = [];
nameservers.forEach(function (ns) {
res.answer.push({
type: 'NS',
name: name,
data: ns.data,
ttl: ns.ttl
});
promises.push(self.store.getDnsFromHostType(ns.data, 'A'))
promises.push(self.store.getDnsFromHostType(ns.data, 'AAAA'))
});
try {
let result = await Promise.all(promises)
result.forEach(function (result) {
for (let record of result) {
if (record.type === 'AAAA') {
res.additional.push({
type: 'AAAA',
name: record.name,
data: Nameserver.expandIPv6Address(record.data),
ttl: record.ttl
});
} else {
res.additional.push({
type: 'A',
name: record.name,
data: record.data,
ttl: record.ttl
});
}
}
})
resolve();
} catch (err) {
reject(err)
}
});
}
static expandIPv6Address(address) {
var fullAddress = "";
var expandedAddress = "";
var validGroupCount = 8;
var validGroupSize = 4;
var ipv4 = "";
var extractIpv4 = /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/;
var validateIpv4 = /((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})/;
// look for embedded ipv4
if (validateIpv4.test(address)) {
groups = address.match(extractIpv4);
for (var i = 1; i < groups.length; i++) {
ipv4 += ("00" + (parseInt(groups[i], 10).toString(16))).slice(-2) + (i == 2 ? ":" : "");
}
address = address.replace(extractIpv4, ipv4);
}
if (address.indexOf("::") === -1) // All eight groups are present.
fullAddress = address;
else // Consecutive groups of zeroes have been collapsed with "::".
{
var sides = address.split("::");
var groupsPresent = 0;
for (var i = 0; i < sides.length; i++) {
groupsPresent += sides[i].split(":").length;
}
fullAddress += sides[0] + ":";
for (var i = 0; i < validGroupCount - groupsPresent; i++) {
fullAddress += "0000:";
}
fullAddress += sides[1];
}
var groups = fullAddress.split(":");
for (var i = 0; i < validGroupCount; i++) {
while (groups[i].length < validGroupSize) {
groups[i] = "0" + groups[i];
}
expandedAddress += (i !== validGroupCount - 1) ? groups[i] + ":" : groups[i];
}
return expandedAddress;
}
}
module.exports = Nameserver;