How to Use DoHjs for DNS Over HTTPS Lookups

Sep 29, 2023 | Programming

In our increasingly web-driven world, ensuring secure and reliable DNS lookups is more important than ever. Enter DoHjs, a powerful JavaScript library designed for DNS over HTTPS (DoH) lookups in web applications. This guide will take you through the installation, quickstart, and troubleshooting tips, making your experience user-friendly and straightforward.

Why DoHjs

The purpose of DoHjs is well captured in the Internet standard document for DNS over HTTPS ([RFC 8484](https://tools.ietf.org/html/rfc8484)): it allows web applications to safely access DNS information by leveraging existing browser APIs while being compliant with Cross-Origin Resource Sharing (CORS) protocols.

Features

  • Fully compliant DNS over HTTPS client implementation
  • Supports GET and POST wire format queries
  • Command-line DNS over HTTPS lookup tool
  • Web interface to try DoHjs
  • CORS proxy to overcome CORS errors associated with DoH (source code available here)

Installation

If you’re not using npm, feel free to skip to the quickstart section.

bash
npm install dohjs

If you want to use the command line tool dohjs for issuing DoH lookups, install it globally (or use npx):

bash
npm install -g dohjs

NOTE: The above command may need to be run as root (learn how to fix permissions errors here).

Quickstart

A simple way to get started is by including doh.js in your HTML file. You can include it from jsdelivr or your local installation:

html





If your project follows a more node.js style (for instance, using browserify), you can utilize require() as shown:

javascript
const doh = require('dohjs');

Now let’s create an analogy to understand the lookup process better:

Imagine you’re at a restaurant (your application) and you want to find out what dishes are available (DNS records). You tell the waiter (DoHjs) your order by specifying the dish (the domain name, like example.com). The waiter communicates with the kitchen chefs (the DNS servers) to fetch the latest menu items (the corresponding IP addresses), bringing back the information so you can enjoy your meal (access your website).

Here’s how you would perform a DoH lookup using DoHjs:

javascript
// Create your stub resolver
const resolver = new doh.DohResolver('https://1.1.1.1/dns-query');

// Lookup the A records for example.com and log the IP addresses to the console
resolver.query('example.com', 'A')
    .then(response => {
        response.answers.forEach(ans => console.log(ans.data));
    })
    .catch(err => console.error(err));

Examples

Checkout our examples to see DoHjs in action. To view the examples, run npm start. Your browser will open up the index of your current directory, from where you can navigate to the examples and open your desired HTML file.

Docs

API documentation for DoHjs can be found in docs/README.md. Documentation for the DoHjs CLI is available in docs/cli.md.

Tests

To run tests, clone the repository, and run:

bash
npm test

CORS Issues

You may encounter some CORS errors when sending DoH queries. Here are a few ways to circumvent those issues:

  • Use a CORS proxy. The option to use a CORS proxy is available if you try it out at dohjs.org.
  • Disable CORS when launching your browser. This approach sometimes works (e.g., google-chrome --user-data-dir=/tmp/asdf --disable-web-security).
  • Run your own DoH server that sets the Access-Control-Allow-Origin header appropriately (e.g., Access-Control-Allow-Origin: * to allow everyone).

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

License

The library is available under the MIT License (see LICENSE).

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox