summaryrefslogtreecommitdiffstats
path: root/extra/simple-dns-server.js
blob: 38bf74f68ad16b6dab07c1eaef8d790dbd123549 (plain)
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
/*
 * Simple DNS Server
 * For testing DNS monitoring type, dev only
 */
const dns2 = require("dns2");

const { Packet } = dns2;

const server = dns2.createServer({
    udp: true
});

server.on("request", (request, send, rinfo) => {
    for (let question of request.questions) {
        console.log(question.name, type(question.type), question.class);

        const response = Packet.createResponseFromRequest(request);

        if (question.name === "existing.com") {

            if (question.type === Packet.TYPE.A) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    address: "1.2.3.4"
                });
            } else if (question.type === Packet.TYPE.AAAA) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    address: "fe80::::1234:5678:abcd:ef00",
                });
            } else if (question.type === Packet.TYPE.CNAME) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    domain: "cname1.existing.com",
                });
            } else if (question.type === Packet.TYPE.MX) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    exchange: "mx1.existing.com",
                    priority: 5
                });
            } else if (question.type === Packet.TYPE.NS) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    ns: "ns1.existing.com",
                });
            } else if (question.type === Packet.TYPE.SOA) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    primary: "existing.com",
                    admin: "admin@existing.com",
                    serial: 2021082701,
                    refresh: 300,
                    retry: 3,
                    expiration: 10,
                    minimum: 10,
                });
            } else if (question.type === Packet.TYPE.SRV) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    priority: 5,
                    weight: 5,
                    port: 8080,
                    target: "srv1.existing.com",
                });
            } else if (question.type === Packet.TYPE.TXT) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    data: "#v=spf1 include:_spf.existing.com ~all",
                });
            } else if (question.type === Packet.TYPE.CAA) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    flags: 0,
                    tag: "issue",
                    value: "ca.existing.com",
                });
            }

        }

        if (question.name === "4.3.2.1.in-addr.arpa") {
            if (question.type === Packet.TYPE.PTR) {
                response.answers.push({
                    name: question.name,
                    type: question.type,
                    class: question.class,
                    ttl: 300,
                    domain: "ptr1.existing.com",
                });
            }
        }

        send(response);
    }
});

server.on("listening", () => {
    console.log("Listening");
    console.log(server.addresses());
});

server.on("close", () => {
    console.log("server closed");
});

server.listen({
    udp: 5300
});

/**
 * Get human readable request type from request code
 * @param {number} code Request code to translate
 * @returns {string|void} Human readable request type
 */
function type(code) {
    for (let name in Packet.TYPE) {
        if (Packet.TYPE[name] === code) {
            return name;
        }
    }
}