-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib.mjs
283 lines (263 loc) · 7.48 KB
/
lib.mjs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import dns from 'dns'
import dgram from 'dgram'
import { DNSSocket } from '@leichtgewicht/dns-socket'
import * as codec from '@leichtgewicht/ip-codec'
import { base64URL } from '@leichtgewicht/base64-codec'
import https from 'https'
import http from 'http'
import * as common from './common.mjs'
import fs from 'fs'
import * as path from 'path'
import { Buffer } from 'buffer'
const { AbortError, HTTPStatusError, TimeoutError, UDP4Endpoint, UDP6Endpoint, URL } = common
// Node 6 support
const writeFile = (path, data) => new Promise(
(resolve, reject) => fs.writeFile(path, data, err => { err ? reject(err) : resolve() })
)
const readFile = (path, opts) => new Promise(
(resolve, reject) => fs.readFile(path, opts, (err, data) => { err ? reject(err) : resolve(data) })
)
const mkdir = path => new Promise(
(resolve, reject) => fs.mkdir(path, err => { err ? reject(err) : resolve() })
)
const stat = path => new Promise(
(resolve, reject) => fs.stat(path, (err, stats) => { err ? reject(err) : resolve(stats) })
)
const filename = decodeURI(import.meta.url).replace(/^file:\/\/(\/(\w+:))?/, '$2').replace(/\//g, path.sep)
const contentType = 'application/dns-message'
let socket4
let socket6
function clearSocketMaybe (socket) {
if (socket.inflight === 0) {
socket.destroy()
if (socket === socket4) {
socket4 = null
} else {
socket6 = null
}
}
}
const MAX_32BIT_INT = 2147483647
function getSocket (protocol) {
if (protocol === 'udp4:') {
if (!socket4) {
socket4 = new DNSSocket({ timeout: MAX_32BIT_INT, timeoutChecks: MAX_32BIT_INT, retries: 0, socket: dgram.createSocket('udp4') })
}
return socket4
}
if (!socket6) {
socket6 = new DNSSocket({ timeout: MAX_32BIT_INT, timeoutChecks: MAX_32BIT_INT, retries: 0, socket: dgram.createSocket('udp6') })
}
return socket6
}
export function queryDns (endpoint, query, timeout, signal) {
return new Promise((resolve, reject) => {
const socket = getSocket(endpoint.protocol)
if (endpoint.pk) {
// TODO: add dnscrypt support to @leichtgewicht/dns-socket
return reject(new Error('dnscrypt servers currently not supported'))
}
const done = (err, res) => {
if (signal) {
signal.removeEventListener('abort', onAbort)
}
clearSocketMaybe(socket)
clearTimeout(t)
if (err) return reject(err)
resolve(res)
}
const requestId = socket.query(query, endpoint.port, endpoint.ipv4 || endpoint.ipv6, (err, res) => {
// Done for sturdier tests, some DNS servers return very, very fast.
setTimeout(done, 10, err, res)
})
const t = setTimeout(onTimeout, timeout)
if (signal) {
signal.addEventListener('abort', onAbort)
}
function onAbort () {
done(new AbortError())
socket.cancel(requestId)
clearSocketMaybe(socket)
}
function onTimeout () {
done(new TimeoutError(timeout))
socket.cancel(requestId)
clearSocketMaybe(socket)
}
})
}
function requestRaw (url, method, body, timeout, abortSignal, headers) {
return new Promise((resolve, reject) => {
let timer
const client = url.protocol === 'https:' ? https : http
let finish = (error, data, response) => {
finish = null
clearTimeout(timer)
if (abortSignal) {
abortSignal.removeEventListener('abort', onabort)
}
if (error) {
if (response) {
resolve({
error,
response
})
} else {
reject(error)
}
} else {
resolve({
data,
response
})
}
}
const target = new URL(url)
if (method === 'GET' && body) {
target.search = '?dns=' + base64URL.decode(body)
}
const req = client.request(
{
hostname: target.hostname,
port: target.port || (target.protocol === 'https:' ? 443 : 80),
path: `${target.pathname}${target.search}`,
method,
headers
},
onresponse
)
if (abortSignal) {
abortSignal.addEventListener('abort', onabort)
}
req.on('error', onerror)
if (method === 'POST') {
req.end(Buffer.from(body))
} else {
req.end()
}
resetTimeout()
function onabort () {
req.destroy(new AbortError())
}
function onresponse (res) {
if (res.statusCode !== 200) {
const error = new HTTPStatusError(target.toString(), res.statusCode, method)
finish(error, null, res)
res.destroy(error)
return
}
const result = []
res.on('error', onerror)
res.on('data', data => {
resetTimeout()
result.push(data)
})
res.on('end', onclose)
res.on('close', onclose)
function onclose () {
if (finish !== null) {
finish(null, Buffer.concat(result), res)
}
}
}
function onerror (error) {
if (finish !== null) {
if (error instanceof Error) {
finish(error)
} else {
finish(error ? new Error(error) : new Error('Unknown Error.'))
}
}
}
function resetTimeout () {
clearTimeout(timer)
timer = setTimeout(ontimeout, timeout)
}
function ontimeout () {
req.destroy(new TimeoutError(timeout))
}
})
}
export function request (url, method, packet, timeout, abortSignal) {
const headers = {
Accept: contentType
}
if (method === 'POST') {
headers['Content-Type'] = contentType
headers['Content-Length'] = packet.byteLength
}
return requestRaw(url, method, packet, timeout, abortSignal, headers)
}
function loadCache (cache, cachePath) {
if (!cachePath) {
return Promise.resolve()
}
return stat(cachePath).then(function (stats) {
const time = stats.mtime.getTime()
if (stats.isFile && time > cache.maxTime) {
return readFile(cachePath, 'utf8').then(function (raw) {
const data = JSON.parse(raw)
return { time, data }
})
}
}).catch(noop)
}
function storeCache (folder, cachePath, data) {
if (!cachePath) {
return Promise.resolve(null)
}
return mkdir(folder)
.catch(function () {}) // mkdir is okay to fail!
.then(function () {
return writeFile(cachePath, data)
})
.then(function () {
return stat(cachePath)
})
.then(
function (stat) {
return stat.mtime.getTime()
},
function () {
return null
}
)
}
function noop () {}
export function loadJSON (url, cache, timeout, abortSignal) {
const folder = path.join(filename, '..', '.cache')
const cachePath = cache ? path.join(folder, cache.name) : null
return loadCache(cache, cachePath)
.then(function (cached) {
if (cached) {
return cached
}
return requestRaw(url, 'GET', null, timeout, abortSignal)
.then(function (response) {
if (response.error) {
return Promise.reject(response.error)
}
const data = response.data
return storeCache(folder, cachePath, data).then(function (time) {
return {
time,
data: JSON.parse(data.toString())
}
})
})
})
}
export function processResolvers (resolvers) {
return resolvers.concat(
dns.getServers()
.map((host, index) => {
const name = `local#${index}`
return {
name,
endpoint: codec.familyOf(host) === 1
? new UDP4Endpoint({ protocol: 'udp4:', ipv4: host })
: new UDP6Endpoint({ protocol: 'udp6:', ipv6: host })
}
})
)
}