net: use bdns to resolve ip address.

This commit is contained in:
Christopher Jeffrey 2017-12-10 23:15:34 -08:00
parent 76c4321448
commit fcb3a10318
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 15 additions and 140 deletions

View File

@ -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.');
};

View File

@ -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);
};

View File

@ -1260,7 +1260,7 @@ HostList.scores = {
IF: 1,
BIND: 2,
UPNP: 3,
HTTP: 3,
DNS: 3,
MANUAL: 4,
MAX: 5
};

View File

@ -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');

View File

@ -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);
}
/**

View File

@ -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",