GithubHelp home page GithubHelp logo

cf-doh's Introduction

cf-doh

Typed Cloudflare DNS Over HTTPS Resolver

Motivation

This lib is a type safe resolver that uses cloudflare's DNS Over HTTP service to resolve DNS records. It is primarily useful in contexts that do not have access to libc's dns resolver (e.g. non-node javascript like v8 isolates or the browser).

Usage

npm --save install cf-doh

import { queryDNS } from "cf-doh";

const records = await queryDNS("_verification.jesseditson.com", "TXT");

records.forEach((r) => console.log(r));

Usage in node.js

If you have access to the node stdlib, you probably want to use node's dns library rather than this one, as it will use the system's native resolver. If you explicitly want to use cloudflare's DoH resolver, you can still use this library in node, but you'll need to provide a fetch implementation. JakeChampion's fetch polyfill is the preferred implmementation.

Install it in your local package.json npm install whatwg-fetch --save

Then provide it to this library:

import { queryDNS, setFetch } from "cf-doh";
import { fetch } from "whatwg-fetch";
setFetch(fetch);

// Use queryDNS normally

API

queryDNS(hostname: string, recordType: DNSRecordType | keyof typeof DNSRecordType): Promise<string[]>

Queries Cloudflare DoH for the provided hostname. If you want to query a named record, pass the hostname including it. recordType may be a DNS record type (e.g. CNAME, TXT) or the numeric value of a type as per the spec (e.g. 5, 16).

This returns an array of all values of that type found at this hostname.

If cloudflare returns a non-200 response or a Status other than NoError, this will throw with a status message or the response from cloudflare.

import { queryDNS } from "cf-doh";

const records = await queryDNS("_verification.jesseditson.com", "TXT");

records.forEach((r) => console.log(r));

queryDNSRecords(hostname: string, recordType: DNSRecordType | keyof typeof DNSRecordType): Promise<DOHResponse>

Queries Cloudflare DoH for the provided hostname. If you want to query a named record, pass the hostname including it. recordType may be a DNS record type (e.g. CNAME, TXT) or the numeric value of a type as per the spec (e.g. 5, 16).

This returns a DOHResponse, which is a typed version of the JSON returned from cloudflare: https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/make-api-requests/dns-json/

If cloudflare returns a non-200 response, this will throw.

import { queryDNSRecords, DOHStatus } from "cf-doh";

const response = await queryDNSRecords("_verification.jesseditson.com", "TXT");

if (response.status === DOHStatus.NoError) {
  const records = r.Answer.map((r) => JSON.parse(r.data));
}

Caveats and Notes

Cloudflare returns quoted strings for standardized record types, and hex values for unknown record types. This means that for most records, the string you get back will include double quotes. queryDNS uses JSON.parse() to strip these quotes before returning them, so if you're using queryDNSRecords directly, you'll need to process these values to use them as strings.

cf-doh's People

Contributors

jesseditson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.