Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
/ doh-resolver Public archive

A (multi) DNS-over-HTTPS resolver for Node.js

License

Notifications You must be signed in to change notification settings

Kikobeats/doh-resolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

doh-resolver

A DNS-over-HTTPS resolver for Node.js.

Last version Coverage Status NPM Status

Install

$ npm install doh-resolver --save

Usage

It can be used as dns.resolve4 and/or dns.resolve6 drop-in replacement:

const DoHResolver = require('doh-resolver')
const resolver = new DoHResolver({ servers: ['1.1.1.1', '8.8.8.8'] })

resolver.resolve4('example.com', (error, addresses) => {
  if (error) throw error
  // Handle the result
})

When you specify more than one server, the result will be the fastest HTTP request into resolve.

Customize the request using your favorite HTTP client:

const DoHResolver = require('doh-resolver')
const { promisify } = require('util')

const resolver = new DoHResolver({
  servers: ['1.1.1.1', '8.8.8.8'],
  get: promisify(require('simple-get'))
})

Combine it with cacheable-lookup for caching lookups respecting TTL:

const CacheableLookup = require('cacheable-lookup')
const DoHResolver = require('doh-resolver')
const https = require('https')

const resolver = new DoHResolver({
  servers: ['1.1.1.1', '8.8.8.8'],
  onError: cb => cb(null, [])
})

const cacheable = new CacheableLookup({ resolver })

https.get('https://example.com', { lookup: cacheable.lookup }, response => {
  // Handle the response here
})

You can store the result via keyv to maximize cache HIT:

const CacheableLookup = require('cacheable-lookup')
const DoHResolver = require('doh-resolver')
const KeyvMulti = require('@keyvhq/multi')
const KeyvRedis = require('@keyvhq/redis')
const Keyv = require('@keyvhq/core')
const https = require('https')

const cache = new Keyv({
  store: new KeyvMulti({
    local: new Map(),
    remote: new KeyvRedis()
  })
})

const resolver = new DoHResolver({
  servers: ['1.1.1.1', '8.8.8.8'],
  onError: cb => cb(null, [])
})

const cacheable = new CacheableLookup({ resolver, cache })

https.get('https://example.com', { lookup: cacheable.lookup }, response => {
  // Handle the response here
})

If you want to debug timings, you can pass your own logger:

const DoHResolver = require('doh-resolver')

const resolver = new DoHResolver({
  servers: ['1.1.1.1', '8.8.8.8'],
  logger: require('debug-logfmt')('doh-resolver')
})

License

doh-resolver © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats