Skip to content

Commit 6775754

Browse files
committed
Refactor DOHClient URL completion
1 parent 3adb7e9 commit 6775754

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

client/doh.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const Packet = require('../packet');
66
const protocols = {
77
'http:' : http.get,
88
'https:' : https.get,
9+
/**
10+
* HTTP/2 GET
11+
*
12+
* @param {string | URL} url
13+
* @param {http.RequestOptions} options
14+
* @param {(res: http.IncomingMessage) => void} done
15+
*/
916
'h2:' : (url, options, done) => {
1017
const urlObj = new URL(url);
1118
const client = http2.connect(url.replace('h2:', 'https:'));
@@ -34,19 +41,21 @@ const protocols = {
3441
};
3542

3643
const makeRequest = (url, query) => new Promise((resolve, reject) => {
37-
const index = url.indexOf('://');
38-
if (index === -1) url = `https://${url}`;
44+
if (!url.includes('/')) { // pure IP or pure domain like "1.0.0.1" or "dns.google"
45+
url += '/dns-query';
46+
}
47+
if (!url.includes('://')) url = `https://${url}`;
3948
const u = new URL(url);
49+
4050
// The DNS query is included in a single variable named “dns” in the
4151
// query component of the request URI. The value of the “dns” variable
4252
// is the content of the DNS request message, encoded with base64url
4353
// [RFC4648](https://datatracker.ietf.org/doc/html/rfc8484#section-4.1).
44-
const searchParams = u.searchParams;
45-
searchParams.set('dns', query);
46-
u.search = searchParams.toString();
54+
u.searchParams.set('dns', query);
55+
4756
const get = protocols[u.protocol];
4857
if (!get) throw new Error(`Unsupported protocol: ${u.protocol}, must be specified (http://, https:// or h2://)`);
49-
const req = get(u.toString(), { headers: { accept: 'application/dns-message' } }, resolve);
58+
const req = get(u, { headers: { accept: 'application/dns-message' } }, resolve);
5059
if (req) req.on('error', reject);
5160
});
5261

example/client/doh.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ const { DOHClient } = require('../..');
2020
DOHClient({
2121
dns: 'https://1.0.0.1/dns-query',
2222
})('cdnjs.com', 'NS').then(console.log);
23+
DOHClient({
24+
dns: '1.0.0.1',
25+
})('cdnjs.com', 'NS').then(console.log);

0 commit comments

Comments
 (0)