dns-query
Node & Browser tested, Non-JSON DNS over HTTPS (and DNS) fetching with minimal dependencies.
DNS over HTTPS (DoH) is protocol designed for performing remote Domain Name System
resolution over HTTPS. Requests are made of HTTP to increase user security and privacy.
See DNS over HTTPS for more
information.
This package provides simple function to make DoH queries both in node and the browser.
Important Note before getting started
By default dns-query
uses well-known public dns-over-https servers to execute
queries! These servers come with caveats, please look at ./endpoints.md
for more information.
DNS support
Node.js's dns support is limited, primary example being: lookupTXT
does not support ttl
results. For that reason, when using dns-query
in node you can also specify dns
endpoints.
JavaScript API
const { query, endpoints: defaultEndpoints } = require('dns-query')
const { cloudflare, google, opendns } = defaultEndpoints
let endpoints
endpoints = 'doh'
endpoints = 'dns'
endpoints = [cloudflare, google, opendns]
endpoints = ['cloudflare', 'google', 'opendns']
endpoints = ['https://cloudflare-dns.com/dns-query']
endpoints = [{ host: 'cloudflare-dns.com' }]
try {
const { answers } = await query({
questions: [
{type: 'A', name: 'google.com'},
{type: 'A', name: 'twitter.com'}
]
}, {
endpoints,
retry: 3,
timeout: 4000,
signal,
})
} catch (error) {
switch (error.code) {
case 'HTTP_STATUS':
case 'RESPONSE_ERR':
case 'ABORT_ERR':
default:
}
}
CLI
You can install dns-query
as a command-line tool using npm i dns-query -g
or by running npx dns-query
.
$ dns-query <options> <input>
Execute a dns query over https.
Examples:
$ dns-query --json -e google \
'{ "questions": [{ "type": "A", "name": "google.com" }] }'
$ echo '{ "questions": [{ "type": "A", "name": "google.com" }] }' \
| dns-query --stdin --endpoint cloudflare
--help, -h ....... Show this help
--version, -v .... Show the version
--json ........... --type=json
--base64 ......... --type=base64
--binary ......... --type=binary
--type ........... Input type. Options: json, base64, binary; Default: json
--out ............ Output type. Defaults to the input --type.
--stdin .......... Get <input> from stdin instead of cli arguments
--endpoint, -e ... Use a specific endpoint. Can be either the name of a known
endpoint, a json object or an url. By default uses one of the known endpoints.
If multiple are provided, one at random will be used.
--endpoints ...... Lists all known endpoints as json.
--retry .......... Number of retries to do in case a request fails, default: 3
--timeout ........ Timeout for the request in milliseconds, default: 30000
Endpoints
For an endpoint to work, it needs to satisfy this interface:
interface EndpointProps {
protocol?: 'http:' | 'https:' | 'udp4:' | 'udp6:';
host: string;
path?: string;
port?: number;
log?: boolean;
cors?: boolean;
filter?: boolean;
docs?: string;
location?: string;
method?: 'post' | 'Post' | 'POST' | 'get' | 'Get' | 'GET';
}
String endpoints
Instead of passing an object you can also pass a string. If the string matches the name
of one of the endpoints, that endpoint will be used, else it needs
to be a url, with a possible [post]
or [get]
suffix to indicate the method.
Examples:
foo.com
→ { host: 'foo.com' }
http://bar.com:81/query [post]
→
{ host: 'bar.com', path: '/query', port: 81, method: 'post', protocol: 'http:' }
Note: If no path is given, such as foo.com
, the path will be assumed as /dns-query
, but
if a path is given such as foo.com/
it will assume that path /
!
To specify DNS endpoints you need to prefix them using udp:
(or udp4:
, udp6
)
udp://1.1.1.1
→ { host: '1.1.1.1', protocol: 'udp4' }
See Also
License
MIT