dns2 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -7
- package/client/doh.js +80 -40
- package/client/google.js +1 -1
- package/client/tcp.js +43 -28
- package/client/udp.js +81 -13
- package/example/client/doh.js +18 -8
- package/index.js +27 -17
- package/lib/proxy-protocol.js +153 -0
- package/lib/reader.js +4 -2
- package/lib/writer.js +0 -1
- package/package.json +26 -12
- package/packet.js +314 -65
- package/server/dns.js +19 -2
- package/server/doh.js +9 -8
- package/server/tcp.js +72 -1
- package/server/udp.js +28 -4
- package/ts/index.d.ts +371 -0
- package/ts/tsconfig.json +13 -0
- package/ts/typings-check.ts +124 -0
- package/.eslintrc +0 -16
- package/.github/FUNDING.yml +0 -12
- package/.github/workflows/lint.js.yml +0 -17
- package/.github/workflows/node.js.yml +0 -29
- package/SECURITY.md +0 -21
- package/test/index.js +0 -413
- package/test/test.js +0 -34
package/test/index.js
DELETED
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const test = require('./test');
|
|
3
|
-
const { Packet, createDOHServer, createServer, TCPClient, DOHClient, UDPClient } = require('..');
|
|
4
|
-
const http = require('http');
|
|
5
|
-
const tcp = require('net');
|
|
6
|
-
const udp = require('dgram');
|
|
7
|
-
|
|
8
|
-
/* TODO: below is unused, either delete or use
|
|
9
|
-
const request = Buffer.from([
|
|
10
|
-
0x29, 0x64, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
11
|
-
0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x6f, 0x6f,
|
|
12
|
-
0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00,
|
|
13
|
-
0x00, 0x01, 0x00, 0x01]);
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const response = Buffer.from([
|
|
17
|
-
0x29, 0x64, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01,
|
|
18
|
-
0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77,
|
|
19
|
-
0x01, 0x7a, 0x02, 0x63, 0x6e, 0x00, 0x00, 0x01,
|
|
20
|
-
0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01,
|
|
21
|
-
0x00, 0x00, 0x01, 0x90, 0x00, 0x04, 0x36, 0xde,
|
|
22
|
-
0x3c, 0xfc ]);
|
|
23
|
-
|
|
24
|
-
test('Name#encode', function() {
|
|
25
|
-
const name = Packet.Name.encode('www.google.com');
|
|
26
|
-
const pattern = [ 3, 'w', 'w', 'w', 5, 'g', 'o', 'o', 'g', 'l', 'e', 3, 'c', 'o', 'm', '0' ];
|
|
27
|
-
assert.equal(name.length, pattern.length);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('Name#decode', function() {
|
|
31
|
-
const reader = new Packet.Reader(response, 8 * 12);
|
|
32
|
-
let name = Packet.Name.decode(reader);
|
|
33
|
-
assert.equal(name, 'www.z.cn');
|
|
34
|
-
|
|
35
|
-
reader.offset = 8 * 26;
|
|
36
|
-
name = Packet.Name.decode(reader);
|
|
37
|
-
assert.equal(reader.offset, 8 * 28);
|
|
38
|
-
assert.equal(name, 'www.z.cn');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('Header#encode', function() {
|
|
42
|
-
const header = new Packet.Header({ id: 0x2964, qr: 1 });
|
|
43
|
-
header.qdcount = 1;
|
|
44
|
-
header.ancount = 2;
|
|
45
|
-
assert.deepEqual(header.toBuffer(), Buffer.from([
|
|
46
|
-
0x29, 0x64, 0x80, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00 ]));
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('Header#parse', function() {
|
|
50
|
-
const header = Packet.Header.parse(response);
|
|
51
|
-
assert.equal(header.id, 0x2964);
|
|
52
|
-
assert.equal(header.qr, 1);
|
|
53
|
-
assert.equal(header.opcode, 0);
|
|
54
|
-
assert.equal(header.aa, 0);
|
|
55
|
-
assert.equal(header.tc, 0);
|
|
56
|
-
assert.equal(header.rd, 1);
|
|
57
|
-
assert.equal(header.z, 0);
|
|
58
|
-
assert.equal(header.rcode, 0);
|
|
59
|
-
assert.equal(header.qdcount, 1);
|
|
60
|
-
assert.equal(header.ancount, 1);
|
|
61
|
-
assert.equal(header.nscount, 0);
|
|
62
|
-
assert.equal(header.arcount, 0);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('Question#encode', function() {
|
|
66
|
-
const question = new Packet.Question({
|
|
67
|
-
name : 'google.com',
|
|
68
|
-
type : Packet.TYPE.A,
|
|
69
|
-
class : Packet.CLASS.IN,
|
|
70
|
-
});
|
|
71
|
-
//
|
|
72
|
-
assert.deepEqual(question.toBuffer(), Buffer.from([
|
|
73
|
-
0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
|
|
74
|
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
|
|
75
|
-
]));
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('Question#decode', function() {
|
|
79
|
-
const question = new Packet.Question('google.com',
|
|
80
|
-
Packet.TYPE.A, Packet.CLASS.IN);
|
|
81
|
-
assert.deepEqual(question.toBuffer(), Buffer.from([
|
|
82
|
-
0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
|
|
83
|
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
|
|
84
|
-
]));
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
test('Package#toIPv6', function() {
|
|
89
|
-
assert.equal(Packet.toIPv6([ 10756, 20034, 512, 0, 0, 0, 0, 803 ]), '2a04:4e42:200::323');
|
|
90
|
-
assert.equal(Packet.toIPv6([ 10755, 45248, 3, 208, 0, 0, 5057, 61441 ]), '2a03:b0c0:3:d0::13c1:f001');
|
|
91
|
-
assert.equal(Packet.toIPv6([ 10752, 5200, 16387, 2055, 0, 0, 0, 8206 ]), '2a00:1450:4003:807::200e');
|
|
92
|
-
assert.equal(Packet.toIPv6([ 9734, 18176, 12552, 0, 0, 0, 44098, 10984 ]), '2606:4700:3108::ac42:2ae8');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('Package#fromIPv6', function() {
|
|
96
|
-
assert.deepEqual(Packet.fromIPv6('2a04:4e42:200::323'), [
|
|
97
|
-
'2a04', '4e42', '0200', '0', '0', '0', '0', '0323' ]);
|
|
98
|
-
assert.deepEqual(Packet.fromIPv6('2a03:b0c0:3:d0::13c1:f001'), [ '2a03', 'b0c0', '0003', '00d0', '0', '0', '13c1', 'f001' ]);
|
|
99
|
-
assert.deepEqual(Packet.fromIPv6('2a00:1450:4003:807::200e'), [ '2a00', '1450', '4003', '0807', '0', '0', '0', '200e' ]);
|
|
100
|
-
assert.deepEqual(Packet.fromIPv6('2606:4700:3108::ac42:2ae8'), [ '2606', '4700', '3108', '0', '0', '0', 'ac42', '2ae8' ]);
|
|
101
|
-
assert.deepEqual(Packet.fromIPv6('::'), [ '0', '0', '0', '0', '0', '0', '0', '0' ]);
|
|
102
|
-
assert.deepEqual(Packet.fromIPv6('::2606:4700:3108'), [ '0', '0', '0', '0', '0', '2606', '4700', '3108' ]);
|
|
103
|
-
assert.deepEqual(Packet.fromIPv6('606:4700:3108::'), [ '0606', '4700', '3108', '0', '0', '0', '0', '0' ]);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('Packet#parse', function() {
|
|
107
|
-
const packet = Packet.parse(response);
|
|
108
|
-
assert.equal(packet.questions[0].name, 'www.z.cn');
|
|
109
|
-
assert.equal(packet.questions[0].type, Packet.TYPE.A);
|
|
110
|
-
assert.equal(packet.questions[0].class, Packet.CLASS.IN);
|
|
111
|
-
assert.equal(packet.answers[0].class, Packet.TYPE.A);
|
|
112
|
-
assert.equal(packet.answers[0].class, Packet.CLASS.IN);
|
|
113
|
-
assert.equal(packet.answers[0].address, '54.222.60.252');
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test('Packet#encode', function() {
|
|
117
|
-
const response = new Packet();
|
|
118
|
-
//
|
|
119
|
-
response.header.qr = 1;
|
|
120
|
-
response.answers.push({
|
|
121
|
-
name : 'lsong.org',
|
|
122
|
-
type : Packet.TYPE.A,
|
|
123
|
-
class : Packet.CLASS.IN,
|
|
124
|
-
ttl : 300,
|
|
125
|
-
address : '127.0.0.1',
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
response.answers.push({
|
|
129
|
-
name : 'lsong.org',
|
|
130
|
-
type : Packet.TYPE.AAAA,
|
|
131
|
-
class : Packet.CLASS.IN,
|
|
132
|
-
ttl : 300,
|
|
133
|
-
address : '2001:db8::ff00:42:8329',
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
response.answers.push({
|
|
137
|
-
name : 'lsong.org',
|
|
138
|
-
type : Packet.TYPE.CNAME,
|
|
139
|
-
class : Packet.CLASS.IN,
|
|
140
|
-
ttl : 300,
|
|
141
|
-
domain : 'sfo1.lsong.org',
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
response.answers.push({
|
|
145
|
-
name : 'lsong.org',
|
|
146
|
-
type : Packet.TYPE.PTR,
|
|
147
|
-
class : Packet.CLASS.IN,
|
|
148
|
-
ttl : 300,
|
|
149
|
-
domain : 'sfo1.lsong.org',
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
response.authorities.push({
|
|
153
|
-
name : 'lsong.org',
|
|
154
|
-
type : Packet.TYPE.MX,
|
|
155
|
-
class : Packet.CLASS.IN,
|
|
156
|
-
ttl : 300,
|
|
157
|
-
exchange : 'mail.lsong.org',
|
|
158
|
-
priority : 5,
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
response.authorities.push({
|
|
162
|
-
name : 'lsong.org',
|
|
163
|
-
type : Packet.TYPE.NS,
|
|
164
|
-
class : Packet.CLASS.IN,
|
|
165
|
-
ttl : 300,
|
|
166
|
-
ns : 'ns1.lsong.org',
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
response.additionals.push({
|
|
170
|
-
name : 'lsong.org',
|
|
171
|
-
type : Packet.TYPE.SOA,
|
|
172
|
-
class : Packet.CLASS.IN,
|
|
173
|
-
ttl : 300,
|
|
174
|
-
primary : 'lsong.org',
|
|
175
|
-
admin : 'admin@lsong.org',
|
|
176
|
-
serial : 2016121301,
|
|
177
|
-
refresh : 300,
|
|
178
|
-
retry : 3,
|
|
179
|
-
expiration : 10,
|
|
180
|
-
minimum : 10,
|
|
181
|
-
});
|
|
182
|
-
//
|
|
183
|
-
response.additionals.push({
|
|
184
|
-
name : 'lsong.org',
|
|
185
|
-
type : Packet.TYPE.TXT,
|
|
186
|
-
class : Packet.CLASS.IN,
|
|
187
|
-
ttl : 300,
|
|
188
|
-
data : '#v=spf1 include:_spf.google.com ~all',
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
assert.deepEqual(Packet.parse(response.toBuffer()), response);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test('Packet#encode array of character strings', function() {
|
|
195
|
-
const response = new Packet();
|
|
196
|
-
//
|
|
197
|
-
const dkim = [ 'v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsD6Th73ZDKkFAntNZDbx',
|
|
198
|
-
'Eh8VV2DSMs3re6v9/gXoT3dGcbSsuUMpfLzP5MWp4sW5cPyZxEGSiC03ZVIcCca0GRAuX9b1M0Qy25wLmPq',
|
|
199
|
-
'8eT129mhwbeX50xTaXqq63A/oDM0QOPe1IeBMfPnR9tWXxvEzZKvVbmTlMY5bf+3QHLqmaEihnGlXh2LRVZ',
|
|
200
|
-
'be2EMlYo18YM4LU/LkZKe06rxlq38W22TL7964tr7jmOZ+huXf2iLSg4nc4UzLwb2aOdOA+w4c87h+HW/L8',
|
|
201
|
-
'0548pFguF46TKc0C0egZ+oll3Y8zySYrbkVrWFrcpnrw5qDiRVHEjxqZSubSYX+16TjNcJg9QIDAQAB' ];
|
|
202
|
-
|
|
203
|
-
response.header.qr = 1;
|
|
204
|
-
response.answers.push({
|
|
205
|
-
name : 'lsong.org',
|
|
206
|
-
type : Packet.TYPE.TXT,
|
|
207
|
-
class : Packet.CLASS.IN,
|
|
208
|
-
ttl : 300,
|
|
209
|
-
data : dkim,
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
assert.equal(Packet.parse(response.toBuffer()).answers[0].data, dkim.join(''));
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
test('EDNS.ECS#encode', function() {
|
|
216
|
-
const query = new Packet.Resource.EDNS([
|
|
217
|
-
new Packet.Resource.EDNS.ECS('10.11.12.13/24'),
|
|
218
|
-
]);
|
|
219
|
-
|
|
220
|
-
const b = Packet.Resource.encode(query);
|
|
221
|
-
assert.deepEqual(b, Buffer.from([
|
|
222
|
-
0x00, 0x00, 0x29, 0x02, 0x00, 0x00, 0x00, 0x00,
|
|
223
|
-
0x00, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x08, 0x00,
|
|
224
|
-
0x01, 0x18, 0x00, 0x0a, 0x0b, 0x0c, 0x0d ]));
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test('EDNS#decode', function() {
|
|
228
|
-
const buffer = Buffer.from([ 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x18, 0x00, 0x0a, 0x0b, 0x0c, 0x0d ]);
|
|
229
|
-
const reader = new Packet.Reader(buffer);
|
|
230
|
-
const record = Packet.Resource.EDNS.decode(reader, buffer.length);
|
|
231
|
-
|
|
232
|
-
assert.equal(record.rdata.length, 1);
|
|
233
|
-
assert.equal(record.rdata[0].ednsCode, 8);
|
|
234
|
-
assert.equal(record.rdata[0].family, 1);
|
|
235
|
-
assert.equal(record.rdata[0].sourcePrefixLength, 24);
|
|
236
|
-
assert.equal(record.rdata[0].scopePrefixLength, 0);
|
|
237
|
-
assert.equal(record.rdata[0].ip, '10.11.12.13');
|
|
238
|
-
|
|
239
|
-
const query = new Packet.Resource.EDNS([
|
|
240
|
-
new Packet.Resource.EDNS.ECS('10.20.0.0/16'),
|
|
241
|
-
]);
|
|
242
|
-
const encoded = Packet.Resource.encode(query);
|
|
243
|
-
const decoded = Packet.Resource.decode(encoded);
|
|
244
|
-
delete decoded.name;
|
|
245
|
-
|
|
246
|
-
assert.deepEqual(decoded, query);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
test('EDNS#decode multiple', function() {
|
|
250
|
-
const query = new Packet.Resource.EDNS([
|
|
251
|
-
new Packet.Resource.EDNS.ECS('10.0.0.0/8'),
|
|
252
|
-
new Packet.Resource.EDNS.ECS('10.9.0.0/16'),
|
|
253
|
-
new Packet.Resource.EDNS.ECS('10.9.8.0/24'),
|
|
254
|
-
new Packet.Resource.EDNS.ECS('10.9.8.7/32'),
|
|
255
|
-
]);
|
|
256
|
-
const encoded = Packet.Resource.encode(query);
|
|
257
|
-
const decoded = Packet.Resource.decode(encoded);
|
|
258
|
-
delete decoded.name;
|
|
259
|
-
|
|
260
|
-
assert.deepEqual(decoded, query);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test('server/doh#cors - default', async function() {
|
|
264
|
-
const server = createDOHServer();
|
|
265
|
-
const { port } = await new Promise(resolve => {
|
|
266
|
-
server.on('listening', resolve);
|
|
267
|
-
server.listen();
|
|
268
|
-
});
|
|
269
|
-
const { headers } = await get(`http://localhost:${port}`);
|
|
270
|
-
assert.equal(headers['access-control-allow-origin'], '*');
|
|
271
|
-
server.server.close();
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
test('server/doh#cors - no cors', async function() {
|
|
275
|
-
const server = createDOHServer({
|
|
276
|
-
cors: false,
|
|
277
|
-
});
|
|
278
|
-
const { port } = await new Promise(resolve => {
|
|
279
|
-
server.on('listening', resolve);
|
|
280
|
-
server.listen();
|
|
281
|
-
});
|
|
282
|
-
const { headers } = await get(`http://localhost:${port}`);
|
|
283
|
-
assert.equal(headers['access-control-allow-origin'], undefined);
|
|
284
|
-
server.server.close();
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
test('server/doh#cors - cors origin', async function() {
|
|
288
|
-
const server = createDOHServer({
|
|
289
|
-
cors: 'some.domain',
|
|
290
|
-
});
|
|
291
|
-
const { port } = await new Promise(resolve => {
|
|
292
|
-
server.on('listening', resolve);
|
|
293
|
-
server.listen();
|
|
294
|
-
});
|
|
295
|
-
const { headers } = await get(`http://localhost:${port}`);
|
|
296
|
-
assert.equal(headers['access-control-allow-origin'], 'some.domain');
|
|
297
|
-
assert.equal(headers.vary, 'Origin');
|
|
298
|
-
server.server.close();
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
test('server/doh#cors - cors function', async function() {
|
|
302
|
-
const server = createDOHServer({
|
|
303
|
-
cors(domain) {
|
|
304
|
-
if (domain === 'a.domain') {
|
|
305
|
-
return true;
|
|
306
|
-
} else if (domain === 'b.domain') {
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
throw new Error(`Unexpected domain: ${domain}`);
|
|
310
|
-
},
|
|
311
|
-
});
|
|
312
|
-
const { port } = await new Promise(resolve => {
|
|
313
|
-
server.on('listening', resolve);
|
|
314
|
-
server.listen();
|
|
315
|
-
});
|
|
316
|
-
let headers = (await get(`http://localhost:${port}`, { headers: { origin: 'a.domain' } })).headers;
|
|
317
|
-
assert.equal(headers['access-control-allow-origin'], 'a.domain');
|
|
318
|
-
assert.equal(headers.vary, 'Origin');
|
|
319
|
-
headers = (await get(`http://localhost:${port}`, { headers: { origin: 'b.domain' } })).headers;
|
|
320
|
-
assert.equal(headers['access-control-allow-origin'], 'false');
|
|
321
|
-
assert.equal(headers.vary, 'Origin');
|
|
322
|
-
server.server.close();
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
test('server/all#simple-request', async() => {
|
|
326
|
-
const server = createServer({
|
|
327
|
-
doh : true,
|
|
328
|
-
tcp : true,
|
|
329
|
-
udp : true,
|
|
330
|
-
handle(request, send, _info) {
|
|
331
|
-
const [ question ] = request.questions;
|
|
332
|
-
assert.deepEqual(request.questions, [ { name: 'test.com', type: 1, class: 1 } ]);
|
|
333
|
-
const response = Packet.createResponseFromRequest(request);
|
|
334
|
-
response.answers.push({
|
|
335
|
-
name : question.name,
|
|
336
|
-
type : Packet.TYPE.TXT,
|
|
337
|
-
class : Packet.CLASS.IN,
|
|
338
|
-
ttl : 300,
|
|
339
|
-
data : [ 'Hello World' ],
|
|
340
|
-
});
|
|
341
|
-
send(response);
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
const servers = await server.listen();
|
|
345
|
-
assert.ok(servers.udp.port > 1000);
|
|
346
|
-
assert.ok(servers.tcp.port > 1000);
|
|
347
|
-
assert.ok(servers.doh.port > 1000);
|
|
348
|
-
const doh = DOHClient({ dns: `127.0.0.1:${servers.doh.port}`, http: true });
|
|
349
|
-
const tcp = TCPClient({ dns: '127.0.0.1', port: servers.tcp.port });
|
|
350
|
-
const udp = UDPClient({ dns: '127.0.0.1', port: servers.udp.port });
|
|
351
|
-
const expected = [ { name: 'test.com', ttl: 300, type: 16, class: 1, data: 'Hello World' } ];
|
|
352
|
-
assert.deepEqual((await doh('test.com')).answers, expected);
|
|
353
|
-
assert.deepEqual((await tcp('test.com')).answers, expected);
|
|
354
|
-
assert.deepEqual((await udp('test.com')).answers, expected);
|
|
355
|
-
await server.close();
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
test('server/all#invalid-request', async() => {
|
|
359
|
-
const server = createServer({
|
|
360
|
-
doh : true,
|
|
361
|
-
tcp : true,
|
|
362
|
-
udp : true,
|
|
363
|
-
handle : () => {},
|
|
364
|
-
});
|
|
365
|
-
const servers = await server.listen();
|
|
366
|
-
assert.ok(servers.udp.port > 1000);
|
|
367
|
-
assert.ok(servers.tcp.port > 1000);
|
|
368
|
-
assert.ok(servers.doh.port > 1000);
|
|
369
|
-
|
|
370
|
-
const errors = [];
|
|
371
|
-
server.on('requestError', (e) => {
|
|
372
|
-
errors.push(e);
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
const tcpSocket = tcp.connect({ port: servers.tcp.port, host: '127.0.0.1' });
|
|
376
|
-
tcpSocket.on('connect', () => tcpSocket.end('INVALID'));
|
|
377
|
-
|
|
378
|
-
const udpSocket = udp.createSocket('udp4');
|
|
379
|
-
udpSocket.send('INVALID', servers.udp.port, '127.0.0.1', () => udpSocket.close());
|
|
380
|
-
|
|
381
|
-
const dohConn = http.get(`http://127.0.0.1:${servers.doh.port}/dns-query?dns=INVALID`, {
|
|
382
|
-
headers: { accept: 'application/dns-message' },
|
|
383
|
-
}).on('error', () => {});
|
|
384
|
-
|
|
385
|
-
await Promise.all([
|
|
386
|
-
new Promise((resolve) => tcpSocket.on('close', resolve)),
|
|
387
|
-
new Promise((resolve) => udpSocket.on('close', resolve)),
|
|
388
|
-
new Promise((resolve) => dohConn.on('close', resolve)),
|
|
389
|
-
]);
|
|
390
|
-
|
|
391
|
-
assert.equal(errors.length, 3);
|
|
392
|
-
|
|
393
|
-
await server.close();
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
function get(url, options) {
|
|
397
|
-
return new Promise((resolve, reject) => {
|
|
398
|
-
try {
|
|
399
|
-
const req = http.get(url, options, res => {
|
|
400
|
-
const result = [];
|
|
401
|
-
res.on('data', data => result.push(data));
|
|
402
|
-
res.once('error', reject);
|
|
403
|
-
res.once('end', () => resolve({
|
|
404
|
-
body : Buffer.concat(result),
|
|
405
|
-
headers : res.headers,
|
|
406
|
-
}));
|
|
407
|
-
});
|
|
408
|
-
req.on('error', reject);
|
|
409
|
-
} catch (err) {
|
|
410
|
-
reject(err);
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
|
-
}
|
package/test/test.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const { inspect } = require('util');
|
|
2
|
-
|
|
3
|
-
let previous = Promise.resolve();
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* super tiny testing framework
|
|
7
|
-
*
|
|
8
|
-
* @author Liu song <hi@lsong.org>
|
|
9
|
-
* @github https://github.com/song940
|
|
10
|
-
*/
|
|
11
|
-
const test = (title, fn) => {
|
|
12
|
-
previous = previous.then(async() => {
|
|
13
|
-
try {
|
|
14
|
-
await fn();
|
|
15
|
-
console.log(color(` ✔ ${title}`, 32));
|
|
16
|
-
} catch (err) {
|
|
17
|
-
console.error(color(` ✘ ${title}`, 31));
|
|
18
|
-
console.log();
|
|
19
|
-
console.log(color(` ${err.name}: ${err.message}`, 31));
|
|
20
|
-
console.error(color(` expected: ${inspect(err.expected)}`, 32));
|
|
21
|
-
console.error(color(` actual: ${inspect(err.actual)}`, 31));
|
|
22
|
-
console.log(err.stack);
|
|
23
|
-
console.log();
|
|
24
|
-
process.exit(1);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
return previous;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
function color(str, c) {
|
|
31
|
-
return `\x1b[${c}m${str}\x1b[0m`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = test;
|