-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.js
258 lines (245 loc) · 6.94 KB
/
dns.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
/*
* Author: SpringHack - [email protected]
* Last modified: 2022-08-09 19:41:27
* Filename: dns.js
* Description: Created by SpringHack using vim automatically.
*/
const {Packet, createServer} = require('dns2');
const {parse} = require('ip6addr');
const crypto = require('crypto');
const http = require('http');
const caList = [
['issue', 'letsencrypt.org'], ['issuewild', 'letsencrypt.org'],
['issue', 'zerossl.com'], ['issuewild', 'zerossl.com'], ['issue', 'ssl.com'],
['issuewild', 'ssl.com'], ['issue', 'digicert.com'],
['issuewild', 'digicert.com']
];
const isHex = (data) => /^[A-Fa-f0-9]{1,4}$/.test(data);
const generateIPv4 = (name) => {
const parts = name.replace(/-/g, '.').split('.');
const ipParts = [];
for (const part of parts) {
if (part.length === 8) {
const a =
isHex(part.substr(0, 2)) ? parseInt(part.substr(0, 2), 16) : NaN;
const b =
isHex(part.substr(2, 2)) ? parseInt(part.substr(2, 2), 16) : NaN;
const c =
isHex(part.substr(4, 2)) ? parseInt(part.substr(4, 2), 16) : NaN;
const d =
isHex(part.substr(6, 2)) ? parseInt(part.substr(6, 2), 16) : NaN;
if (a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255 &&
d >= 0 && d <= 255) {
return `${a}.${b}.${c}.${d}`;
}
} else {
if (/^\d+$/.test(part)) {
const ipPart = parseInt(part, 10);
if (ipPart >= 0 && ipPart <= 255) {
ipParts.push(ipPart);
}
}
}
}
if (ipParts.length < 4) {
return null;
}
return ipParts.slice(0, 4).join('.');
};
const generateIPv6 = (name) => {
const parts = name.split('.');
for (const part of parts) {
const ipParts = part.split(/-+/).map((dig) => {
if (dig === '') {
return '0';
}
return dig;
});
let isValid = !!ipParts.length && ipParts.length <= 8;
for (const ipPart of ipParts) {
const num = isHex(ipPart) ? parseInt(ipPart, 16) : NaN;
if (isNaN(num) || num > 65535) {
isValid = false;
}
}
if (isValid) {
const maybeValue = part.replace(/-/g, ':');
const maybeParts = maybeValue.split('::');
if (ipParts.length === 8 ||
(ipParts.length < 8 && maybeParts.length === 2)) {
return maybeValue;
}
}
}
return null;
};
class TxtResource {
#txtMap_ = new Map();
ensureExist(key) {
if (!this.#txtMap_.get(key)) {
this.#txtMap_.set(key, new Set());
}
}
getItem(key) {
this.ensureExist(key);
return [...this.#txtMap_.get(key)];
}
setItem(key, value) {
this.ensureExist(key);
return this.#txtMap_.get(key).add(value);
}
removeItem(key, value) {
this.ensureExist(key);
return this.#txtMap_.get(key).delete(value);
}
}
const txts = new TxtResource();
const options = {
auth: crypto.randomUUID(),
token: crypto.randomUUID(),
dnsPort: '53',
dnsAddress: '0.0.0.0',
httpPort: '5333',
httpAddress: '0.0.0.0'
};
for (let index = 0; index < process.argv.length; ++index) {
if (process.argv[index] === '--help') {
console.error(JSON.stringify(options, null, 2));
process.exit(0);
}
if (process.argv[index].startsWith('--')) {
if (index + 1 < process.argv.length &&
!process.argv[index + 1].startsWith('--')) {
options[process.argv[index].substring(2)] = process.argv[index + 1];
} else {
console.error(`illegal option=${process.argv[index].substring(2)}`);
process.exit(1);
}
}
}
const createSOARecord = (name) => {
return {
name,
type: Packet.TYPE.SOA,
class: Packet.CLASS.IN,
ttl: 300,
primary: name,
admin: 'springhack.live.cn',
serial: 2016121301,
refresh: 300,
retry: 3,
expiration: 10,
minimum: 10,
};
};
const server = createServer({
udp: true,
handle(request, send) {
const response = Packet.createResponseFromRequest(request);
for (const question of request.questions) {
const {name: originalName, type} = question;
const name = originalName.toLowerCase();
switch (type) {
case Packet.TYPE.A: {
const address = generateIPv4(name);
if (!address) {
response.authorities.push(createSOARecord(name));
break;
}
const obj = {name, type, address, ttl: 300, class: Packet.CLASS.IN};
response.answers.push(obj);
break;
}
case Packet.TYPE.AAAA: {
const address = generateIPv6(name);
if (!address) {
response.authorities.push(createSOARecord(name));
break;
}
const obj = {
name,
type,
ttl: 300,
class: Packet.CLASS.IN,
address: parse(address).toString({format: 'v6', zeroElide: false})
};
response.answers.push(obj);
break;
}
case Packet.TYPE.TXT: {
const records = txts.getItem(name);
for (const data of records) {
const obj = {name, type, data, class: Packet.CLASS.IN, ttl: 300};
response.answers.push(obj);
}
if (!response.answers.length) {
response.authorities.push(createSOARecord(name));
}
break;
}
case Packet.TYPE.CAA: {
for (const ca of caList) {
const [tag, value] = ca;
const obj = {
tag,
name,
type,
value,
flags: 0,
class: Packet.CLASS.IN,
ttl: 300
};
response.answers.push(obj);
}
break;
}
default:
response.authorities.push(createSOARecord(name));
}
}
send(response);
}
});
server.listen({
udp: {
port: parseInt(options.dnsPort),
address: options.dnsAddress,
type: 'udp4'
}
});
const httpServer = http.createServer((request, response) => {
const {url, headers} = request;
if (!url) {
return response.writeHead(500, 'not support').end();
}
if (headers[options.auth] !== options.token) {
return response.writeHead(500, 'not auth').end();
}
const urlParts = url.substring(1).split('/');
const [operation, key, value] = urlParts;
switch (operation) {
case 'get': {
if (!key) {
response.writeHead(500, 'no key').end();
}
return response.end(JSON.stringify(txts.getItem(key)));
}
case 'set': {
if (!key || !value) {
return response.writeHead(500, 'no key/value').end();
}
return response.end(JSON.stringify(txts.setItem(key, value)));
}
case 'del': {
if (!key || !value) {
return response.writeHead(500, 'no key/value').end();
}
return response.end(JSON.stringify(txts.removeItem(key, value)));
}
default: {
return response.writeHead(500, 'invalid operation').end();
}
}
});
httpServer.listen(parseInt(options.httpPort), options.httpAddress);
console.log(`start with options=${JSON.stringify(options, null, 2)}`);