Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions client/doh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:'));
Expand Down Expand Up @@ -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);
});

Expand Down
3 changes: 3 additions & 0 deletions example/client/doh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);