From fcb3a10318c893f90719e55fde23bc2f4f369314 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 10 Dec 2017 23:15:34 -0800 Subject: [PATCH] net: use bdns to resolve ip address. --- lib/net/external-browser.js | 29 ----------- lib/net/external.js | 95 ------------------------------------- lib/net/hostlist.js | 2 +- lib/net/index.js | 1 - lib/net/pool.js | 27 ++++++----- package.json | 1 - 6 files changed, 15 insertions(+), 140 deletions(-) delete mode 100644 lib/net/external-browser.js delete mode 100644 lib/net/external.js diff --git a/lib/net/external-browser.js b/lib/net/external-browser.js deleted file mode 100644 index 17fbb76a..00000000 --- a/lib/net/external-browser.js +++ /dev/null @@ -1,29 +0,0 @@ -/*! - * external.js - external ip address discovery for bcoin - * Copyright (c) 2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -const external = exports; - -/** - * Attempt to retrieve external IP from icanhazip.com. - * @method - * @returns {Promise} - */ - -external.getIPv4 = async function getIPv4() { - throw new Error('Could not find IP.'); -}; - -/** - * Attempt to retrieve external IP from icanhazip.com. - * @method - * @returns {Promise} - */ - -external.getIPv6 = async function getIPv6() { - throw new Error('Could not find IP.'); -}; diff --git a/lib/net/external.js b/lib/net/external.js deleted file mode 100644 index 1035629a..00000000 --- a/lib/net/external.js +++ /dev/null @@ -1,95 +0,0 @@ -/*! - * external.js - external ip address discovery for bcoin - * Copyright (c) 2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -const brq = require('brq'); -const IP = require('binet'); - -/** - * @exports net/external - */ - -const external = exports; - -/** - * Attempt to retrieve external IP from icanhazip.com. - * @method - * @returns {Promise} - */ - -external.getIPv4 = async function getIPv4() { - try { - const res = await brq({ - method: 'GET', - url: 'http://ipv4.icanhazip.com', - expect: 'txt', - timeout: 2000 - }); - - const str = res.text().trim(); - const raw = IP.toBuffer(str); - - if (!IP.isIPv4(raw)) - throw new Error('Could not find IPv4.'); - - return IP.toString(raw); - } catch (e) { - return await external.getIPv42(); - } -}; - -/** - * Attempt to retrieve external IP from dyndns.org. - * @method - * @ignore - * @returns {Promise} - */ - -external.getIPv42 = async function getIPv42() { - const res = await brq({ - method: 'GET', - url: 'http://checkip.dyndns.org', - expect: 'html', - timeout: 2000 - }); - - const match = /IP Address:\s*([0-9a-f.:]+)/i.exec(res.text()); - - if (!match) - throw new Error('Could not find IPv4.'); - - const str = match[1]; - const raw = IP.toBuffer(str); - - if (!IP.isIPv4(raw)) - throw new Error('Could not find IPv4.'); - - return IP.toString(raw); -}; - -/** - * Attempt to retrieve external IP from icanhazip.com. - * @method - * @returns {Promise} - */ - -external.getIPv6 = async function getIPv6() { - const res = await brq({ - method: 'GET', - url: 'http://ipv6.icanhazip.com', - expect: 'txt', - timeout: 2000 - }); - - const str = res.text().trim(); - const raw = IP.toBuffer(str); - - if (!IP.isIPv6(raw)) - throw new Error('Could not find IPv6.'); - - return IP.toString(raw); -}; diff --git a/lib/net/hostlist.js b/lib/net/hostlist.js index a5b9e213..9dd869fa 100644 --- a/lib/net/hostlist.js +++ b/lib/net/hostlist.js @@ -1260,7 +1260,7 @@ HostList.scores = { IF: 1, BIND: 2, UPNP: 3, - HTTP: 3, + DNS: 3, MANUAL: 4, MAX: 5 }; diff --git a/lib/net/index.js b/lib/net/index.js index 8233a22d..14be16e2 100644 --- a/lib/net/index.js +++ b/lib/net/index.js @@ -14,7 +14,6 @@ exports.BIP150 = require('./bip150'); exports.BIP151 = require('./bip151'); exports.bip152 = require('./bip152'); exports.common = require('./common'); -exports.external = require('./external'); exports.Framer = require('./framer'); exports.HostList = require('./hostlist'); exports.NetAddress = require('./netaddress'); diff --git a/lib/net/pool.js b/lib/net/pool.js index 5d22a047..be5caf14 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -26,7 +26,6 @@ const BIP151 = require('./bip151'); const BIP152 = require('./bip152'); const Network = require('../protocol/network'); const Peer = require('./peer'); -const external = require('./external'); const List = require('../utils/list'); const HostList = require('./hostlist'); const InvItem = require('../primitives/invitem'); @@ -506,7 +505,7 @@ class Pool extends EventEmitter { } /** - * Attempt to discover external IP via HTTP. + * Attempt to discover external IP via DNS. * @returns {Promise} */ @@ -517,7 +516,7 @@ class Pool extends EventEmitter { if (!this.options.listen) return; - // Never hit an HTTP server if + // Never hit a DNS server if // we're using an outbound proxy. if (this.options.proxy) return; @@ -526,27 +525,29 @@ class Pool extends EventEmitter { if (this.hosts.local.size > 0) return; - let host4; + let host4 = null; + try { - host4 = await external.getIPv4(); + host4 = await dns.getIPv4(2000); } catch (e) { - this.logger.debug('Could not find external IPv4 (http).'); + this.logger.debug('Could not find external IPv4 (dns).'); this.logger.debug(e); } - if (host4 && this.hosts.addLocal(host4, port, scores.HTTP)) - this.logger.info('External IPv4 found (http): %s.', host4); + if (host4 && this.hosts.addLocal(host4, port, scores.DNS)) + this.logger.info('External IPv4 found (dns): %s.', host4); + + let host6 = null; - let host6; try { - host6 = await external.getIPv6(); + host6 = await dns.getIPv6(2000); } catch (e) { - this.logger.debug('Could not find external IPv6 (http).'); + this.logger.debug('Could not find external IPv6 (dns).'); this.logger.debug(e); } - if (host6 && this.hosts.addLocal(host6, port, scores.HTTP)) - this.logger.info('External IPv6 found (http): %s.', host6); + if (host6 && this.hosts.addLocal(host6, port, scores.DNS)) + this.logger.info('External IPv6 found (dns): %s.', host6); } /** diff --git a/package.json b/package.json index 8b4f0a06..1ce27766 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "blgr": "^0.0.1", "bmutex": "^0.0.1", "bn.js": "^4.11.8", - "brq": "^0.0.1", "bsock": "^0.0.1", "bsocks": "^0.0.1", "bstring": "^0.0.1",