-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (51 loc) · 2.08 KB
/
Copy pathindex.js
File metadata and controls
57 lines (51 loc) · 2.08 KB
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
// 'dgram' module is used for UDP Datagram sockets.
const dgram = require('node:dgram');
const dnsPacket = require('dns-packet');
// UDP is chosen for DNS because it supports quick, connectionless exchange of messages.
const server = dgram.createSocket('udp4');
// This should not be here, if we host our server we should store our IP addresses somewhere
const dataBase = {
'bhatiamanan.dev': {
type: 'A',
data: '1.2.3.4'
},
'blog.bhatiamanan.dev': {
type: 'CNAME',
data: 'starnode.network'
},
};
server.on('message', (msg, rinfo) => {
const request = dnsPacket.decode(msg);
const ipFromDb = dataBase[request.questions[0].name];
if (ipFromDb) {
const response = dnsPacket.encode({
type: 'response',
id: request.id,
flags: dnsPacket.AUTHORITATIVE_ANSWER,
questions: request.questions,
answers: [{
type: ipFromDb.type,
class: "IN",
name: request.questions[0].name,
data: ipFromDb.data
}]
});
server.send(response, rinfo.port, rinfo.address);
console.log(ipFromDb);
} else {
// Send an NXDOMAIN response when the requested domain does not exist in the database.
// NXDOMAIN stands for "Non-Existent Domain" and is used to indicate that the domain name queried does not exist.
const response = dnsPacket.encode({
type: 'response',
id: request.id,
flags: dnsPacket.AUTHORITATIVE_ANSWER | dnsPacket.NXDOMAIN,
questions: request.questions
});
server.send(response, rinfo.port, rinfo.address);
console.log(`No record found for ${request.questions[0].name}, sent NXDOMAIN.`);
}
});
server.bind(53, () => console.log('DNS server is running on port 53'));
// When we host this on some machine like AWS and someone wants to use this DNS server,
// we can use some domain like ns.myDnsServer.com and that user put its NS record on ns.myDnsServer.com
// then this server will be handling all the requests.