Skip to content

Commit 39cb53f

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

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

client/doh.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ const protocols = {
3434
};
3535

3636
const makeRequest = (url, query) => new Promise((resolve, reject) => {
37-
const index = url.indexOf('://');
38-
if (index === -1) url = `https://${url}`;
37+
if (!url.includes('/')) { // pure IP or pure domain like "1.0.0.1" or "dns.google"
38+
url += '/dns-query';
39+
}
40+
if (!url.includes('://')) url = `https://${url}`;
3941
const u = new URL(url);
42+
4043
// The DNS query is included in a single variable named “dns” in the
4144
// query component of the request URI. The value of the “dns” variable
4245
// is the content of the DNS request message, encoded with base64url
4346
// [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();
47+
u.searchParams.set('dns', query);
48+
4749
const get = protocols[u.protocol];
4850
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);
51+
const req = get(u.href, { headers: { accept: 'application/dns-message' } }, resolve);
5052
if (req) req.on('error', reject);
5153
});
5254

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)