dns2 2.0.0 → 2.0.3
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/.eslintrc +16 -0
- package/.github/FUNDING.yml +12 -12
- package/.github/workflows/lint.js.yml +17 -0
- package/.github/workflows/node.js.yml +29 -30
- package/LICENSE +18 -18
- package/README.md +227 -196
- package/SECURITY.md +21 -21
- package/client/doh.js +50 -42
- package/client/google.js +27 -27
- package/client/tcp.js +34 -34
- package/client/udp.js +39 -39
- package/example/client/doh.js +12 -12
- package/example/client/google.js +7 -8
- package/example/client/tcp-custom-dns.js +10 -10
- package/example/client/tcp.js +12 -12
- package/example/client/udp-custom-dns.js +11 -11
- package/example/client/udp-default.js +8 -8
- package/example/client/udp-subnet.js +11 -11
- package/example/client/udp.js +8 -8
- package/example/server/dns.js +46 -0
- package/example/server/doh.js +30 -28
- package/example/server/secret.key +28 -28
- package/example/server/server.crt +22 -22
- package/example/server/tcp.js +19 -19
- package/example/server/udp.js +23 -23
- package/index.js +97 -89
- package/lib/reader.js +53 -55
- package/lib/writer.js +42 -42
- package/package.json +42 -34
- package/packet.js +873 -857
- package/server/dns.js +94 -0
- package/server/doh.js +146 -143
- package/server/index.js +31 -24
- package/server/tcp.js +34 -26
- package/server/udp.js +47 -41
- package/test/index.js +395 -244
- package/test/test.js +34 -27
- package/.travis.yml +0 -4
package/test/index.js
CHANGED
|
@@ -1,244 +1,395 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
const test = require('./test');
|
|
3
|
-
const Packet = require('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
0x00,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
assert.equal(name, 'www.z.cn');
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
assert.equal(header.
|
|
52
|
-
assert.equal(header.
|
|
53
|
-
assert.equal(header.
|
|
54
|
-
assert.equal(header.
|
|
55
|
-
assert.equal(header.
|
|
56
|
-
assert.equal(header.
|
|
57
|
-
assert.equal(header.
|
|
58
|
-
assert.equal(header.
|
|
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
|
-
assert.equal(packet.questions[0].
|
|
91
|
-
assert.equal(packet.
|
|
92
|
-
assert.equal(packet.
|
|
93
|
-
assert.equal(packet.answers[0].
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
response.header.qr = 1;
|
|
102
|
-
response.answers.push({
|
|
103
|
-
name: 'lsong.org',
|
|
104
|
-
type: Packet.TYPE.A,
|
|
105
|
-
class: Packet.CLASS.IN,
|
|
106
|
-
ttl: 300,
|
|
107
|
-
address: '127.0.0.1'
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
response.answers.push({
|
|
111
|
-
name: 'lsong.org',
|
|
112
|
-
type: Packet.TYPE.AAAA,
|
|
113
|
-
class: Packet.CLASS.IN,
|
|
114
|
-
ttl: 300,
|
|
115
|
-
address: '2001:db8
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
response.answers.push({
|
|
119
|
-
name: 'lsong.org',
|
|
120
|
-
type: Packet.TYPE.CNAME,
|
|
121
|
-
class: Packet.CLASS.IN,
|
|
122
|
-
ttl: 300,
|
|
123
|
-
domain: 'sfo1.lsong.org'
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
response.answers.push({
|
|
127
|
-
name: 'lsong.org',
|
|
128
|
-
type: Packet.TYPE.PTR,
|
|
129
|
-
class: Packet.CLASS.IN,
|
|
130
|
-
ttl: 300,
|
|
131
|
-
domain: 'sfo1.lsong.org'
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
response.authorities.push({
|
|
135
|
-
name: 'lsong.org',
|
|
136
|
-
type: Packet.TYPE.MX,
|
|
137
|
-
class: Packet.CLASS.IN,
|
|
138
|
-
ttl: 300,
|
|
139
|
-
exchange: 'mail.lsong.org',
|
|
140
|
-
priority: 5
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
response.authorities.push({
|
|
144
|
-
name: 'lsong.org',
|
|
145
|
-
type: Packet.TYPE.NS,
|
|
146
|
-
class: Packet.CLASS.IN,
|
|
147
|
-
ttl: 300,
|
|
148
|
-
ns: 'ns1.lsong.org',
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
response.additionals.push({
|
|
152
|
-
name: 'lsong.org',
|
|
153
|
-
type: Packet.TYPE.SOA,
|
|
154
|
-
class: Packet.CLASS.IN,
|
|
155
|
-
ttl: 300,
|
|
156
|
-
primary: 'lsong.org',
|
|
157
|
-
admin: 'admin@lsong.org',
|
|
158
|
-
serial: 2016121301,
|
|
159
|
-
refresh: 300,
|
|
160
|
-
retry: 3,
|
|
161
|
-
expiration: 10,
|
|
162
|
-
minimum: 10
|
|
163
|
-
});
|
|
164
|
-
//
|
|
165
|
-
response.additionals.push({
|
|
166
|
-
name: 'lsong.org',
|
|
167
|
-
type: Packet.TYPE.TXT,
|
|
168
|
-
class: Packet.CLASS.IN,
|
|
169
|
-
ttl: 300,
|
|
170
|
-
data: '#v=spf1 include:_spf.google.com ~all'
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
assert.deepEqual(Packet.parse(response.toBuffer()), response);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
'
|
|
182
|
-
'
|
|
183
|
-
'
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
response.
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
0x00, 0x00,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
assert.equal(record.rdata.
|
|
216
|
-
assert.equal(record.rdata[0].
|
|
217
|
-
assert.equal(record.rdata[0].
|
|
218
|
-
assert.equal(record.rdata[0].
|
|
219
|
-
assert.equal(record.rdata[0].
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
new Packet.Resource.EDNS.ECS('10.
|
|
235
|
-
new Packet.Resource.EDNS.ECS('10.9.
|
|
236
|
-
new Packet.Resource.EDNS.ECS('10.9.8.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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('Packet#parse', function() {
|
|
89
|
+
const packet = Packet.parse(response);
|
|
90
|
+
assert.equal(packet.questions[0].name, 'www.z.cn');
|
|
91
|
+
assert.equal(packet.questions[0].type, Packet.TYPE.A);
|
|
92
|
+
assert.equal(packet.questions[0].class, Packet.CLASS.IN);
|
|
93
|
+
assert.equal(packet.answers[0].class, Packet.TYPE.A);
|
|
94
|
+
assert.equal(packet.answers[0].class, Packet.CLASS.IN);
|
|
95
|
+
assert.equal(packet.answers[0].address, '54.222.60.252');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('Packet#encode', function() {
|
|
99
|
+
const response = new Packet();
|
|
100
|
+
//
|
|
101
|
+
response.header.qr = 1;
|
|
102
|
+
response.answers.push({
|
|
103
|
+
name : 'lsong.org',
|
|
104
|
+
type : Packet.TYPE.A,
|
|
105
|
+
class : Packet.CLASS.IN,
|
|
106
|
+
ttl : 300,
|
|
107
|
+
address : '127.0.0.1',
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
response.answers.push({
|
|
111
|
+
name : 'lsong.org',
|
|
112
|
+
type : Packet.TYPE.AAAA,
|
|
113
|
+
class : Packet.CLASS.IN,
|
|
114
|
+
ttl : 300,
|
|
115
|
+
address : '2001:db8::ff00:42:8329',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
response.answers.push({
|
|
119
|
+
name : 'lsong.org',
|
|
120
|
+
type : Packet.TYPE.CNAME,
|
|
121
|
+
class : Packet.CLASS.IN,
|
|
122
|
+
ttl : 300,
|
|
123
|
+
domain : 'sfo1.lsong.org',
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
response.answers.push({
|
|
127
|
+
name : 'lsong.org',
|
|
128
|
+
type : Packet.TYPE.PTR,
|
|
129
|
+
class : Packet.CLASS.IN,
|
|
130
|
+
ttl : 300,
|
|
131
|
+
domain : 'sfo1.lsong.org',
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
response.authorities.push({
|
|
135
|
+
name : 'lsong.org',
|
|
136
|
+
type : Packet.TYPE.MX,
|
|
137
|
+
class : Packet.CLASS.IN,
|
|
138
|
+
ttl : 300,
|
|
139
|
+
exchange : 'mail.lsong.org',
|
|
140
|
+
priority : 5,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
response.authorities.push({
|
|
144
|
+
name : 'lsong.org',
|
|
145
|
+
type : Packet.TYPE.NS,
|
|
146
|
+
class : Packet.CLASS.IN,
|
|
147
|
+
ttl : 300,
|
|
148
|
+
ns : 'ns1.lsong.org',
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
response.additionals.push({
|
|
152
|
+
name : 'lsong.org',
|
|
153
|
+
type : Packet.TYPE.SOA,
|
|
154
|
+
class : Packet.CLASS.IN,
|
|
155
|
+
ttl : 300,
|
|
156
|
+
primary : 'lsong.org',
|
|
157
|
+
admin : 'admin@lsong.org',
|
|
158
|
+
serial : 2016121301,
|
|
159
|
+
refresh : 300,
|
|
160
|
+
retry : 3,
|
|
161
|
+
expiration : 10,
|
|
162
|
+
minimum : 10,
|
|
163
|
+
});
|
|
164
|
+
//
|
|
165
|
+
response.additionals.push({
|
|
166
|
+
name : 'lsong.org',
|
|
167
|
+
type : Packet.TYPE.TXT,
|
|
168
|
+
class : Packet.CLASS.IN,
|
|
169
|
+
ttl : 300,
|
|
170
|
+
data : '#v=spf1 include:_spf.google.com ~all',
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
assert.deepEqual(Packet.parse(response.toBuffer()), response);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('Packet#encode array of character strings', function() {
|
|
177
|
+
const response = new Packet();
|
|
178
|
+
//
|
|
179
|
+
const dkim = [ 'v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsD6Th73ZDKkFAntNZDbx',
|
|
180
|
+
'Eh8VV2DSMs3re6v9/gXoT3dGcbSsuUMpfLzP5MWp4sW5cPyZxEGSiC03ZVIcCca0GRAuX9b1M0Qy25wLmPq',
|
|
181
|
+
'8eT129mhwbeX50xTaXqq63A/oDM0QOPe1IeBMfPnR9tWXxvEzZKvVbmTlMY5bf+3QHLqmaEihnGlXh2LRVZ',
|
|
182
|
+
'be2EMlYo18YM4LU/LkZKe06rxlq38W22TL7964tr7jmOZ+huXf2iLSg4nc4UzLwb2aOdOA+w4c87h+HW/L8',
|
|
183
|
+
'0548pFguF46TKc0C0egZ+oll3Y8zySYrbkVrWFrcpnrw5qDiRVHEjxqZSubSYX+16TjNcJg9QIDAQAB' ];
|
|
184
|
+
|
|
185
|
+
response.header.qr = 1;
|
|
186
|
+
response.answers.push({
|
|
187
|
+
name : 'lsong.org',
|
|
188
|
+
type : Packet.TYPE.TXT,
|
|
189
|
+
class : Packet.CLASS.IN,
|
|
190
|
+
ttl : 300,
|
|
191
|
+
data : dkim,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
assert.equal(Packet.parse(response.toBuffer()).answers[0].data, dkim.join(''));
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('EDNS.ECS#encode', function() {
|
|
198
|
+
const query = new Packet.Resource.EDNS([
|
|
199
|
+
new Packet.Resource.EDNS.ECS('10.11.12.13/24'),
|
|
200
|
+
]);
|
|
201
|
+
|
|
202
|
+
const b = Packet.Resource.encode(query);
|
|
203
|
+
assert.deepEqual(b, Buffer.from([
|
|
204
|
+
0x00, 0x00, 0x29, 0x02, 0x00, 0x00, 0x00, 0x00,
|
|
205
|
+
0x00, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x08, 0x00,
|
|
206
|
+
0x01, 0x18, 0x00, 0x0a, 0x0b, 0x0c, 0x0d ]));
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('EDNS#decode', function() {
|
|
210
|
+
const buffer = Buffer.from([ 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x18, 0x00, 0x0a, 0x0b, 0x0c, 0x0d ]);
|
|
211
|
+
const reader = new Packet.Reader(buffer);
|
|
212
|
+
const record = Packet.Resource.EDNS.decode(reader, buffer.length);
|
|
213
|
+
|
|
214
|
+
assert.equal(record.rdata.length, 1);
|
|
215
|
+
assert.equal(record.rdata[0].ednsCode, 8);
|
|
216
|
+
assert.equal(record.rdata[0].family, 1);
|
|
217
|
+
assert.equal(record.rdata[0].sourcePrefixLength, 24);
|
|
218
|
+
assert.equal(record.rdata[0].scopePrefixLength, 0);
|
|
219
|
+
assert.equal(record.rdata[0].ip, '10.11.12.13');
|
|
220
|
+
|
|
221
|
+
const query = new Packet.Resource.EDNS([
|
|
222
|
+
new Packet.Resource.EDNS.ECS('10.20.0.0/16'),
|
|
223
|
+
]);
|
|
224
|
+
const encoded = Packet.Resource.encode(query);
|
|
225
|
+
const decoded = Packet.Resource.decode(encoded);
|
|
226
|
+
delete decoded.name;
|
|
227
|
+
|
|
228
|
+
assert.deepEqual(decoded, query);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('EDNS#decode multiple', function() {
|
|
232
|
+
const query = new Packet.Resource.EDNS([
|
|
233
|
+
new Packet.Resource.EDNS.ECS('10.0.0.0/8'),
|
|
234
|
+
new Packet.Resource.EDNS.ECS('10.9.0.0/16'),
|
|
235
|
+
new Packet.Resource.EDNS.ECS('10.9.8.0/24'),
|
|
236
|
+
new Packet.Resource.EDNS.ECS('10.9.8.7/32'),
|
|
237
|
+
]);
|
|
238
|
+
const encoded = Packet.Resource.encode(query);
|
|
239
|
+
const decoded = Packet.Resource.decode(encoded);
|
|
240
|
+
delete decoded.name;
|
|
241
|
+
|
|
242
|
+
assert.deepEqual(decoded, query);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('server/doh#cors - default', async function() {
|
|
246
|
+
const server = createDOHServer();
|
|
247
|
+
const { port } = await new Promise(resolve => {
|
|
248
|
+
server.on('listening', resolve);
|
|
249
|
+
server.listen();
|
|
250
|
+
});
|
|
251
|
+
const { headers } = await get(`http://localhost:${port}`);
|
|
252
|
+
assert.equal(headers['access-control-allow-origin'], '*');
|
|
253
|
+
server.server.close();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('server/doh#cors - no cors', async function() {
|
|
257
|
+
const server = createDOHServer({
|
|
258
|
+
cors: false,
|
|
259
|
+
});
|
|
260
|
+
const { port } = await new Promise(resolve => {
|
|
261
|
+
server.on('listening', resolve);
|
|
262
|
+
server.listen();
|
|
263
|
+
});
|
|
264
|
+
const { headers } = await get(`http://localhost:${port}`);
|
|
265
|
+
assert.equal(headers['access-control-allow-origin'], undefined);
|
|
266
|
+
server.server.close();
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test('server/doh#cors - cors origin', async function() {
|
|
270
|
+
const server = createDOHServer({
|
|
271
|
+
cors: 'some.domain',
|
|
272
|
+
});
|
|
273
|
+
const { port } = await new Promise(resolve => {
|
|
274
|
+
server.on('listening', resolve);
|
|
275
|
+
server.listen();
|
|
276
|
+
});
|
|
277
|
+
const { headers } = await get(`http://localhost:${port}`);
|
|
278
|
+
assert.equal(headers['access-control-allow-origin'], 'some.domain');
|
|
279
|
+
assert.equal(headers.vary, 'Origin');
|
|
280
|
+
server.server.close();
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test('server/doh#cors - cors function', async function() {
|
|
284
|
+
const server = createDOHServer({
|
|
285
|
+
cors(domain) {
|
|
286
|
+
if (domain === 'a.domain') {
|
|
287
|
+
return true;
|
|
288
|
+
} else if (domain === 'b.domain') {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
throw new Error(`Unexpected domain: ${domain}`);
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
const { port } = await new Promise(resolve => {
|
|
295
|
+
server.on('listening', resolve);
|
|
296
|
+
server.listen();
|
|
297
|
+
});
|
|
298
|
+
let headers = (await get(`http://localhost:${port}`, { headers: { origin: 'a.domain' } })).headers;
|
|
299
|
+
assert.equal(headers['access-control-allow-origin'], 'a.domain');
|
|
300
|
+
assert.equal(headers.vary, 'Origin');
|
|
301
|
+
headers = (await get(`http://localhost:${port}`, { headers: { origin: 'b.domain' } })).headers;
|
|
302
|
+
assert.equal(headers['access-control-allow-origin'], 'false');
|
|
303
|
+
assert.equal(headers.vary, 'Origin');
|
|
304
|
+
server.server.close();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test('server/all#simple-request', async() => {
|
|
308
|
+
const server = createServer({
|
|
309
|
+
doh : true,
|
|
310
|
+
tcp : true,
|
|
311
|
+
udp : true,
|
|
312
|
+
handle(request, send, _info) {
|
|
313
|
+
const [ question ] = request.questions;
|
|
314
|
+
assert.deepEqual(request.questions, [ { name: 'test.com', type: 1, class: 1 } ]);
|
|
315
|
+
const response = Packet.createResponseFromRequest(request);
|
|
316
|
+
response.answers.push({
|
|
317
|
+
name : question.name,
|
|
318
|
+
type : Packet.TYPE.TXT,
|
|
319
|
+
class : Packet.CLASS.IN,
|
|
320
|
+
ttl : 300,
|
|
321
|
+
data : [ 'Hello World' ],
|
|
322
|
+
});
|
|
323
|
+
send(response);
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
const servers = await server.listen();
|
|
327
|
+
assert.ok(servers.udp.port > 1000);
|
|
328
|
+
assert.ok(servers.tcp.port > 1000);
|
|
329
|
+
assert.ok(servers.doh.port > 1000);
|
|
330
|
+
const doh = DOHClient({ dns: `127.0.0.1:${servers.doh.port}`, http: true });
|
|
331
|
+
const tcp = TCPClient({ dns: '127.0.0.1', port: servers.tcp.port });
|
|
332
|
+
const udp = UDPClient({ dns: '127.0.0.1', port: servers.udp.port });
|
|
333
|
+
const expected = [ { name: 'test.com', ttl: 300, type: 16, class: 1, data: 'Hello World' } ];
|
|
334
|
+
assert.deepEqual((await doh('test.com')).answers, expected);
|
|
335
|
+
assert.deepEqual((await tcp('test.com')).answers, expected);
|
|
336
|
+
assert.deepEqual((await udp('test.com')).answers, expected);
|
|
337
|
+
await server.close();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('server/all#invalid-request', async() => {
|
|
341
|
+
const server = createServer({
|
|
342
|
+
doh : true,
|
|
343
|
+
tcp : true,
|
|
344
|
+
udp : true,
|
|
345
|
+
handle : () => {},
|
|
346
|
+
});
|
|
347
|
+
const servers = await server.listen();
|
|
348
|
+
assert.ok(servers.udp.port > 1000);
|
|
349
|
+
assert.ok(servers.tcp.port > 1000);
|
|
350
|
+
assert.ok(servers.doh.port > 1000);
|
|
351
|
+
|
|
352
|
+
const errors = [];
|
|
353
|
+
server.on('requestError', (e) => {
|
|
354
|
+
errors.push(e);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const tcpSocket = tcp.connect({ port: servers.tcp.port, host: '127.0.0.1' });
|
|
358
|
+
tcpSocket.on('connect', () => tcpSocket.end('INVALID'));
|
|
359
|
+
|
|
360
|
+
const udpSocket = udp.createSocket('udp4');
|
|
361
|
+
udpSocket.send('INVALID', servers.udp.port, '127.0.0.1', () => udpSocket.close());
|
|
362
|
+
|
|
363
|
+
const dohConn = http.get(`http://127.0.0.1:${servers.doh.port}/dns-query?dns=INVALID`, {
|
|
364
|
+
headers: { accept: 'application/dns-message' },
|
|
365
|
+
}).on('error', () => {});
|
|
366
|
+
|
|
367
|
+
await Promise.all([
|
|
368
|
+
new Promise((resolve) => tcpSocket.on('close', resolve)),
|
|
369
|
+
new Promise((resolve) => udpSocket.on('close', resolve)),
|
|
370
|
+
new Promise((resolve) => dohConn.on('close', resolve)),
|
|
371
|
+
]);
|
|
372
|
+
|
|
373
|
+
assert.equal(errors.length, 3);
|
|
374
|
+
|
|
375
|
+
await server.close();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
function get(url, options) {
|
|
379
|
+
return new Promise((resolve, reject) => {
|
|
380
|
+
try {
|
|
381
|
+
const req = http.get(url, options, res => {
|
|
382
|
+
const result = [];
|
|
383
|
+
res.on('data', data => result.push(data));
|
|
384
|
+
res.once('error', reject);
|
|
385
|
+
res.once('end', () => resolve({
|
|
386
|
+
body : Buffer.concat(result),
|
|
387
|
+
headers : res.headers,
|
|
388
|
+
}));
|
|
389
|
+
});
|
|
390
|
+
req.on('error', reject);
|
|
391
|
+
} catch (err) {
|
|
392
|
+
reject(err);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|