Uses HTTP/2 to comply with RFC 8484 - section 5.2.
This commit is contained in:
parent
584fa75842
commit
83b4cb269f
1 changed files with 35 additions and 42 deletions
77
src/doh.js
77
src/doh.js
|
|
@ -1,7 +1,5 @@
|
|||
import dnsPacket from 'dns-packet';
|
||||
import https from 'https';
|
||||
import http from 'http';
|
||||
import base32Encode from 'base32-encode';
|
||||
import http2 from 'http2';
|
||||
|
||||
/**
|
||||
* A super lame DNS over HTTPS stub resolver
|
||||
|
|
@ -63,56 +61,51 @@ function makeQuery(qname, qtype = 'TXT') {
|
|||
*/
|
||||
function sendDohMsg(packet, url, timeout) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transport = url.startsWith('https://') ? https : http;
|
||||
const client = http2.connect(url);
|
||||
const buf = dnsPacket.encode(packet);
|
||||
let requestOptions;
|
||||
const headers = {
|
||||
'Accept': 'application/dns-message',
|
||||
'User-Agent': 'domain-forward/1.x'
|
||||
};
|
||||
const dnsQueryParam = buf.toString('base64').toString('utf-8').replace(/=/g, '');
|
||||
url = `${url}?dns=${dnsQueryParam}`;
|
||||
url = new URL(url);
|
||||
requestOptions = {
|
||||
method: 'GET',
|
||||
hostname: url.hostname,
|
||||
port: url.port || 443,
|
||||
path: url.pathname + url.search,
|
||||
headers: headers
|
||||
};
|
||||
const req = client.request({
|
||||
":path": url.pathname + url.search
|
||||
});
|
||||
let data;
|
||||
let timer;
|
||||
const request = transport.request(requestOptions, (response) => {
|
||||
response.on('data', (d) => {
|
||||
if (!data) {
|
||||
data = d;
|
||||
} else {
|
||||
data = Buffer.concat([data, d]);
|
||||
}
|
||||
});
|
||||
response.on('end', () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
try {
|
||||
const decoded = dnsPacket.decode(data);
|
||||
resolve(decoded);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
request.on('error', (err) => {
|
||||
request.destroy();
|
||||
return reject(err);
|
||||
});
|
||||
if (timeout) {
|
||||
timer = setTimeout(() => {
|
||||
request.destroy();
|
||||
req.destroy();
|
||||
return reject(new Error(`Query timed out after ${timeout} milliseconds of inactivity`));
|
||||
}, timeout);
|
||||
}
|
||||
request.end()
|
||||
/*req.on('response', (headers, flags) => {
|
||||
for (const name in headers) {
|
||||
console.log(`${name}: ${headers[name]}`);
|
||||
}
|
||||
});*/
|
||||
req.on('data', chunk => {
|
||||
if (!data) {
|
||||
data = chunk;
|
||||
} else {
|
||||
data = Buffer.concat([data, chunk]);
|
||||
}
|
||||
});
|
||||
req.on('end', () => {
|
||||
client.close();
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
try {
|
||||
const decoded = dnsPacket.decode(data);
|
||||
resolve(decoded);
|
||||
} catch (err) {
|
||||
return reject(err);
|
||||
}
|
||||
});
|
||||
req.on('error', (err) => {
|
||||
req.destroy();
|
||||
return reject(err);
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue