From 03a5631b07d6a9d8acd219a30d13f23dd06e1763 Mon Sep 17 00:00:00 2001
From: kimbo
Date: Sat, 14 Mar 2020 15:46:24 -0600
Subject: [PATCH 1/5] timeout param for DohResolver.query & sendDohMsg
---
lib/index.js | 35 ++++++++++++++++++++++-------------
1 file changed, 22 insertions(+), 13 deletions(-)
diff --git a/lib/index.js b/lib/index.js
index 2340ece..33ce856 100755
--- a/lib/index.js
+++ b/lib/index.js
@@ -67,16 +67,17 @@ resolver.query('example.com', 'A')
*
* IMPORTANT: If you don't provide the "Accept: application/dns-message" header, you probably won't get the response you're hoping for.
* See [RFC 8484 examples](https://tools.ietf.org/html/rfc8484#section-4.1.1) for examples of HTTPS headers for both GET and POST requests.
+ * @param timeout {number} the number of milliseconds to wait for a response before aborting the request
* @throws {MethodNotAllowedError} If the method is not allowed (i.e. if it's not "GET" or "POST"), a MethodNotAllowedError will be thrown.
* @returns {Promise
The recursion desired flag will be set, and the ID in the header will be set to zero, per the RFC (section 4.1).
-
Send a DNS message over HTTPS to url using the given request method
@@ -52,7 +52,7 @@ A super lame DNS over HTTPS stub resolver
* [DohResolver](#DohResolver)
* [new DohResolver(nameserver_url)](#new_DohResolver_new)
- * [.query(qname, qtype, method, headers)](#DohResolver+query) ⇒ Promise.<object>
+ * [.query(qname, qtype, method, headers, timeout)](#DohResolver+query) ⇒ Promise.<object>
@@ -82,7 +82,7 @@ resolver.query('example.com', 'A')
```
-### dohResolver.query(qname, qtype, method, headers) ⇒ Promise.<object>
+### dohResolver.query(qname, qtype, method, headers, timeout) ⇒ Promise.<object>
Perform a DNS lookup for the given query name and type.
**Kind**: instance method of [DohResolver](#DohResolver)
@@ -98,6 +98,7 @@ Perform a DNS lookup for the given query name and type.
| qtype | string | "A" | the type of record we're looking for (e.g. A, AAAA, TXT, MX) |
| method | string | "POST" | Must be either "GET" or "POST" |
| headers | object | | define HTTP headers to use in the DNS query IMPORTANT: If you don't provide the "Accept: application/dns-message" header, you probably won't get the response you're hoping for. See [RFC 8484 examples](https://tools.ietf.org/html/rfc8484#section-4.1.1) for examples of HTTPS headers for both GET and POST requests. |
+| timeout | number | | the number of milliseconds to wait for a response before aborting the request |
@@ -151,7 +152,7 @@ console.log(msg);
```
-## sendDohMsg(packet, url, method, headers) ⇒ Promise.<object>
+## sendDohMsg(packet, url, method, headers, timeout) ⇒ Promise.<object>
Send a DNS message over HTTPS to `url` using the given request method
**Kind**: global function
@@ -163,20 +164,21 @@ Send a DNS message over HTTPS to `url` using the given request method
| url | string | the url to send the DNS message to |
| method | string | the request method to use ("GET" or "POST") |
| headers | object | headers to send in the DNS request. The default headers for GET requests are |
+| timeout | number | the number of milliseconds to wait for a response before aborting the request |
**Example**
```js
// imports
-const {makeQuery, sendDohMsg} = require('dohjs');
+ const {makeQuery, sendDohMsg} = require('dohjs');
-const url = 'https://cloudflare-dns.com/dns-query';
-const method = 'GET';
+ const url = 'https://cloudflare-dns.com/dns-query';
+ const method = 'GET';
-// create a query message
-let msg = makeQuery('example.com', 'TXT');
+ // create a query message
+ let msg = makeQuery('example.com', 'TXT');
-// send it and print out the response to the console
-sendDohMsg(msg, url, method)
-.then(response => response.answers.forEach(ans => console.log(ans.data.toString())))
-.catch(console.error);
+ // send it and print out the response to the console
+ sendDohMsg(msg, url, method)
+ .then(response => response.answers.forEach(ans => console.log(ans.data.toString())))
+ .catch(console.error);
```
From 713b0c0d1e1021f1eaaf324085ea94ffcd84780d Mon Sep 17 00:00:00 2001
From: kimbo
Date: Sat, 14 Mar 2020 15:57:24 -0600
Subject: [PATCH 5/5] added example with timeout
---
examples/basic/DohResolver.js | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/examples/basic/DohResolver.js b/examples/basic/DohResolver.js
index fad3441..3abe6d1 100755
--- a/examples/basic/DohResolver.js
+++ b/examples/basic/DohResolver.js
@@ -11,3 +11,18 @@ resolver.query('example.com', 'A')
response.answers.forEach(ans => console.log(ans.data));
})
.catch(err => console.error(err));
+
+
+// Now we'll do the same query, but this time,
+// it's going to be a GET request, and
+// we're going to set a timeout for one second
+
+// timeout in milliseconds
+const timeout = 1000;
+
+// lookup the A records for example.com
+resolver.query('example.com', 'A', 'GET', null, timeout)
+ .then(response => {
+ response.answers.forEach(ans => console.log(ans.data));
+ })
+ .catch(err => console.error(err));