Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial implementation #1

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ dist

# TernJS port file
.tern-port
package-lock.json
40 changes: 40 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
language: node_js
cache: npm
stages:
- check
- test
- cov

node_js:
- 'lts/*'
- 'stable'

os:
- linux
- osx

script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

jobs:
include:
- stage: check
script:
- npm run lint

- stage: test
name: chrome
addons:
chrome: stable
script:
- npx aegir test -t browser

- stage: test
name: firefox
addons:
firefox: latest
script:
- npx aegir test -t browser -- --browsers FirefoxHeadless

notifications:
email: false
191 changes: 189 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,189 @@
# dns-over-http-client
DNS over HTTP client
# dns-over-http-resolver

[![Build Status](https://travis-ci.org/vasco-santos/dns-over-http-resolver.svg?branch=master)](https://travis-ci.org/vasco-santos/dns-over-http-resolver)
[![dependencies Status](https://david-dm.org/vasco-santos/dns-over-http-resolver/status.svg)](https://david-dm.org/vasco-santos/dns-over-http-resolver)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

> DNS over HTTP resolver

Isomorphic DNS over HTTP resolver using fetch.

API based on [Node.js' dns promises API](https://nodejs.org/dist/latest-v14.x/docs/api/dns.html#dns_dns_promises_api), allowing the native `dns` module to be used if available when relying on this API.

## Install

```sh
npm i dns-over-http-resolver
```

## Usage

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')
```

Use Node.js' dns promises API if available.
```js
const { Resolver } = require('dns').promises || require('dns-over-http-resolver')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example isn't going to work. I'd just specify that users can use require('dns').promises in Node.js in lieu of this module.

```

[Cloudflare](https://cloudflare-dns.com/dns-query) and [Google](https://dns.google/resolve) DNS servers are used by default. They can be replaced via the API.

## API

### resolve(hostname, rrType)

Uses the DNS protocol to resolve the given host name into a DNS record.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| hostname | `string` | host name to resolve |
| [rrType] | `string` | resource record type (default: 'A') |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<Array<string>>` | returns a Promise resolving a DNS record according to its type |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'
const recordType = 'TXT'

const dnsRecord = await resolver.resolve(hostname, recordType)
```

### resolve4(hostname)

Uses the DNS protocol to resolve the given host name into IPv4 addresses.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| hostname | `string` | host name to resolve |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<Array<string>>` | returns a Promise resolving IPv4 addresses |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolve4(hostname) // ['216.58.212.142']
```

### resolve6(hostname)

Uses the DNS protocol to resolve the given host name into IPv6 addresses.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| hostname | `string` | host name to resolve |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<Array<string>>` | returns a Promise resolving IPv6 addresses |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolve6(hostname) // ['2a00:1450:4001:801::200e']
```

### resolveTxt(hostname)

Uses the DNS protocol to resolve the given host name into a Text Record.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| hostname | `string` | host name to resolve |

#### Returns

| Type | Description |
|------|-------------|
| `Promise<Array<Array<string>>>` | returns a Promise resolving a Text Record |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolveTxt(hostname) // [['v=spf1 -all']]
```

### getServers()

Get an array of the IP addresses currently configured for DNS resolution.
These addresses are formatted according to RFC 5952. It can include a custom port.

#### Returns

| Type | Description |
|------|-------------|
| `Array<string>` | returns array of DNS servers used |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')

const resolver = new DnsOverHttpResolver()
const servers = resolver.getServers()
```

### setServers(servers)

Sets the IP address and port of servers to be used when performing DNS resolution.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| servers | `Array<string>` | Array of RFC 5952 formatted addresses. |

#### Example

```js
const DnsOverHttpResolver = require('dns-over-http-resolver')

const resolver = new DnsOverHttpResolver()
resolver.setServers(['https://cloudflare-dns.com/dns-query'])
```

## Contribute

Feel free to dive in! [Open an issue](https://github.com/vasco-santos/dns-over-http-resolver/issues/new) or submit PRs.

## License

[MIT](LICENSE) © Vasco Santos
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "dns-over-http-resolver",
"version": "0.0.0",
"description": "DNS over HTTP resolver",
"main": "src/index.js",
"author": "Vasco Santos",
"scripts": {
"test": "aegir test -t node -t browser",
"test:browser": "aegir test -t browser",
"test:node": "aegir test -t node",
"lint": "aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release -t node -t browser --type major --docs",
"build": "aegir build"
},
"files": [
"src",
"dist"
],
"devDependencies": {
"aegir": "^27.0.0",
"ipfs-utils": "^4.0.0",
"sinon": "^9.2.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vasco-santos/dns-over-http-resolver.git"
},
"keywords": [
"doh",
"dns",
"http"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/vasco-santos/dns-over-http-resolver/issues"
},
"homepage": "https://github.com/vasco-santos/dns-over-http-resolver#readme",
"dependencies": {
"debug": "^4.2.0",
"native-fetch": "^2.0.1"
}
}
Loading