From 677575465231aec45bceff444dfb6a88f73726e1 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 22 Aug 2025 20:12:32 +0800 Subject: [PATCH] Refactor DOHClient URL completion --- client/doh.js | 21 +++++++++++++++------ example/client/doh.js | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/client/doh.js b/client/doh.js index c9abcb1..bb612be 100644 --- a/client/doh.js +++ b/client/doh.js @@ -6,6 +6,13 @@ const Packet = require('../packet'); const protocols = { 'http:' : http.get, 'https:' : https.get, + /** + * HTTP/2 GET + * + * @param {string | URL} url + * @param {http.RequestOptions} options + * @param {(res: http.IncomingMessage) => void} done + */ 'h2:' : (url, options, done) => { const urlObj = new URL(url); const client = http2.connect(url.replace('h2:', 'https:')); @@ -34,19 +41,21 @@ const protocols = { }; const makeRequest = (url, query) => new Promise((resolve, reject) => { - const index = url.indexOf('://'); - if (index === -1) url = `https://${url}`; + if (!url.includes('/')) { // pure IP or pure domain like "1.0.0.1" or "dns.google" + url += '/dns-query'; + } + if (!url.includes('://')) url = `https://${url}`; const u = new URL(url); + // The DNS query is included in a single variable named “dns” in the // query component of the request URI. The value of the “dns” variable // is the content of the DNS request message, encoded with base64url // [RFC4648](https://datatracker.ietf.org/doc/html/rfc8484#section-4.1). - const searchParams = u.searchParams; - searchParams.set('dns', query); - u.search = searchParams.toString(); + u.searchParams.set('dns', query); + const get = protocols[u.protocol]; if (!get) throw new Error(`Unsupported protocol: ${u.protocol}, must be specified (http://, https:// or h2://)`); - const req = get(u.toString(), { headers: { accept: 'application/dns-message' } }, resolve); + const req = get(u, { headers: { accept: 'application/dns-message' } }, resolve); if (req) req.on('error', reject); }); diff --git a/example/client/doh.js b/example/client/doh.js index 7c6a9db..bb064d4 100644 --- a/example/client/doh.js +++ b/example/client/doh.js @@ -20,3 +20,6 @@ const { DOHClient } = require('../..'); DOHClient({ dns: 'https://1.0.0.1/dns-query', })('cdnjs.com', 'NS').then(console.log); +DOHClient({ + dns: '1.0.0.1', +})('cdnjs.com', 'NS').then(console.log);