Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Commit

Permalink
updated codebase to work with both node and browser
Browse files Browse the repository at this point in the history
  • Loading branch information
ptariche committed Apr 11, 2018
1 parent 9657aa0 commit 180e07c
Show file tree
Hide file tree
Showing 4 changed files with 1,062 additions and 703 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### Fetch with DNS over HTTPS

The point of this utility is to be able to utilize node-fetch with your DNS queries running over HTTPS from Cloudflare [1.1.1.1](https://1.1.1.1) or Google DNS over HTTPS.
The point of this utility is to be able to utilize fetch with your DNS queries running over HTTPS from Cloudflare [1.1.1.1](https://1.1.1.1) or Google DNS over HTTPS.

### Fetch Example
- [Link](./examples/fetch.js)
Expand Down Expand Up @@ -35,8 +35,3 @@ npm install --save fdoh
+ uri {String}
+ headers {Object}


### Dependencies
- [dohdec](https://github.com/hildjj/dohdec)
- [is-ip](https://github.com/sindresorhus/is-ip)
45 changes: 37 additions & 8 deletions lib/fdoh.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
const Pkg = require('./../package.json');
require('es6-promise').polyfill();
require('isomorphic-fetch');

const IsIP = require('is-ip');
const Url = require ('url');
const Dohdec = require('dohdec');
const Fetch = require('node-fetch');
const Pkg = require('./../package.json');

const IsIP = require('is-ip');
const Url = require ('url');
const Qs = require('querystring');

const Fetch = fetch;
const CT_TYPE = 'application/dns-json';
const USER_AGENT = `${Pkg.name} v${Pkg.version}`;

const TYPES = {
DNS_PROVIDERS: {
GOOGLE: 'GOOGLE',
CLOUDFLARE: 'CLOUDFLARE',
},
DNS_OVER_HTTPS: {
GOOGLE: 'https://dns.google.com/resolve'
GOOGLE: 'https://dns.google.com/resolve',
CLOUDFLARE: 'https://cloudflare-dns.com/dns-query'
}
};

Expand All @@ -24,6 +31,27 @@ class Fdoh {
return Pkg.version;
}

getDns () {
return async (name, opts) => {
opts = Object.assign({}, {
rrtype: 'A',
url: TYPES.DNS_OVER_HTTPS.CLOUDFLARE,
name: Qs.escape(name)
}, opts);

opts.rrtype = opts.rrtype.toUpperCase();

const lookup = await fetch(
`${opts.url}?ct=${CT_TYPE}&name=${opts.name}&type=${opts.rrtype}`, {
headers: {
'User-Agent': USER_AGENT
}
});

return lookup.json();
};
}

set () {
let ctx = this;
return async (uri, options = {}, provider = TYPES.DNS_PROVIDERS.CLOUDFLARE, respondWithConfig = false) => {
Expand All @@ -39,9 +67,10 @@ class Fdoh {
}
}

const getDns = ctx.getDns();
const _dnsRequest = (provider === TYPES.DNS_PROVIDERS.CLOUDFLARE) ?
await Dohdec(_url.hostname) :
await Dohdec(_url.hostname, {
await getDns(_url.hostname) :
await getDns(_url.hostname, {
url: TYPES.DNS_OVER_HTTPS[TYPES.DNS_PROVIDERS.GOOGLE]
});

Expand Down

0 comments on commit 180e07c

Please sign in to comment.