net: handle public ip and port better.

This commit is contained in:
Christopher Jeffrey 2017-01-21 13:56:41 -08:00
parent dc839062bf
commit b212eed282
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 113 additions and 62 deletions

View File

@ -1,66 +1,105 @@
# Sample bcoin config file (~/.bcoin/bcoin.conf)
#
# Options
network: main
#
# network: main
use-workers: true
# max-workers: 4
# worker-timeout: 5000
# sigcache-size: 50000
#
# Node
#
prefix: ~/.bcoin
db: leveldb
max-files: 64
cache-size: 100
# fast: false
fast: false
#
# Logger
#
log-level: debug
log-console: true
log-file: true
#
# Chain
# witness: true
# prune: false
#
witness: true
prune: false
use-checkpoints: true
coin-cache: 40
index-tx: false
index-address: false
#
# Mempool
# limit-free: true
# limit-free-relay: 15
# reject-absurd-fees: true
# replace-by-fee: false
#
limit-free: true
limit-free-relay: 15
reject-absurd-fees: true
replace-by-fee: false
#
# Pool
# selfish: false
headers: false
#
selfish: false
headers: true
compact: true
bip151: true
# proxy-server: localhost
# preferred-seed: seed.bitcoin.sipa.be
# no-discovery: false
# port: 8333
no-discovery: false
listen: true
max-outbound: 8
max-inbound: 30
# Websocket Proxy Server (Browser Only)
proxy-server: localhost
# Custom list of DNS seeds
# seeds: seed.bitcoin.sipa.be
# Local Host & Port (to listen on)
host: ::
# port: 8333
# Public Host & Port (to advertise to peers)
# public-host: 0.0.0.0
# public-port: 8444
# BIP151 AuthDB and Identity Key
bip150: false
identity-key: 74b4147957813b62cc8987f2b711ddb31f8cb46dcbf71502033da66053c8780a
auth-peers: ./authorized-peers
known-peers: ./known-peers
# Always try to connect to these nodes.
# nodes: 127.0.0.1,127.0.0.2
#
# Miner
# payout-address: 1111111111111111111114oLvT2
# coinbase-flags: mined by bcoin
#
coinbase-flags: mined by bcoin
# payout-address: 1111111111111111111114oLvT2,1111111111111111111114oLvT2
#
# HTTP
#
http-host: ::
# http-port: 8332
# ssl-cert: @/ssl/cert.crt
# ssl-key: @/ssl/priv.key
# http-port: 8332
# http-host: 0.0.0.0
# service-key: bikeshed
service-key: bikeshed
api-key: bikeshed
wallet-auth: false
# no-auth: false
no-auth: false

View File

@ -13,7 +13,7 @@ var RPCClient = require('./rpcclient');
var Amount = require('../btc/amount');
var util = require('../utils/util');
var co = require('../utils/co');
var request = require('./request').promise;
var request = require('./request');
/**
* BCoin HTTP client.

View File

@ -507,49 +507,35 @@ Request.prototype._onEnd = function _onEnd(err) {
* @param {String?} options.expect - Type to expect (see options.type).
* Error will be returned if the response is not of this type.
* @param {Number?} options.limit - Byte limit on response.
* @param {Function?} callback - Will return a stream if not present.
* @returns {Promise}
*/
function request(options, callback) {
var stream;
if (!callback) {
stream = new Request(options);
stream.start();
return stream;
}
function request(options) {
if (typeof options === 'string')
options = { uri: options };
options.buffer = true;
stream = new Request(options);
return new Promise(function(resolve, reject) {
var stream = new Request(options);
stream.on('error', function(err) {
callback(err);
stream.on('error', function(err) {
reject(err);
});
stream.on('end', function() {
resolve(stream);
});
stream.start();
stream.end();
});
stream.on('end', function() {
callback(null, stream, stream.body);
});
stream.start();
stream.end();
return stream;
}
request.promise = function promise(options) {
return new Promise(function(resolve, reject) {
request(options, function(err, res) {
if (err) {
reject(err);
return;
}
resolve(res);
});
});
request.stream = function _stream(options) {
var stream = new Request(options);
stream.start();
return stream;
};
/*

View File

@ -45,7 +45,7 @@ function RPCClient(options) {
*/
RPCClient.prototype.call = co(function* call(method, params) {
var res = yield request.promise({
var res = yield request({
method: 'POST',
uri: this.uri,
pool: true,

View File

@ -516,8 +516,14 @@ MinerOptions.prototype.fromOptions = function fromOptions(options) {
this.version = options.version;
}
if (options.address)
this.addresses.push(new Address(options.address));
if (options.address) {
if (Array.isArray(options.address)) {
for (i = 0; i < options.address.length; i++)
this.addresses.push(new Address(options.address[i]));
} else {
this.addresses.push(new Address(options.address));
}
}
if (options.addresses) {
assert(Array.isArray(options.addresses));

View File

@ -396,7 +396,7 @@ Pool.prototype.listen = co(function* listen() {
if (!this.options.listen)
return;
yield this.server.listen(this.address.port, '0.0.0.0');
yield this.server.listen(this.options.port, this.options.host);
});
/**
@ -3083,7 +3083,7 @@ Pool.prototype.getIP = co(function* getIP() {
throw new Error('Could not find IP.');
try {
res = yield request.promise({
res = yield request({
method: 'GET',
uri: 'http://icanhazip.com',
expect: 'txt',
@ -3112,7 +3112,7 @@ Pool.prototype.getIP2 = co(function* getIP2() {
if (request.unsupported)
throw new Error('Could not find IP.');
res = yield request.promise({
res = yield request({
method: 'GET',
uri: 'http://checkip.dyndns.org',
expect: 'html',
@ -3263,12 +3263,26 @@ PoolOptions.prototype.fromOptions = function fromOptions(options) {
if (options.host != null) {
assert(typeof options.host === 'string');
this.host = options.host;
assert(IP.version(options.host) !== -1, '`host` must be an IP.');
this.host = IP.normalize(options.host);
if (IP.isRoutable(this.host))
this.address.setHost(this.host);
}
if (options.port != null) {
assert(typeof options.port === 'number');
this.port = options.port;
this.address.setPort(this.port);
}
if (options.publicHost != null) {
assert(typeof options.publicHost === 'string');
this.address.setHost(options.publicHost);
}
if (options.publicPort != null) {
assert(typeof options.port === 'number');
this.address.setPort(options.publicPort);
}
if (options.maxOutbound != null) {
@ -3410,7 +3424,7 @@ PoolOptions.prototype.fromOptions = function fromOptions(options) {
this.requiredServices = options.requiredServices;
}
this.address.fromOptions(this);
this.address.services = this.services;
this.address.ts = this.network.now();
return this;

View File

@ -199,13 +199,16 @@ config.parseData = function parseData(data, prefix, dirname) {
options.maxOutbound = num(data.maxoutbound);
options.maxInbound = num(data.maxinbound);
options.noDiscovery = bool(data.nodiscovery);
options.publicHost = str(data.publichost);
options.publicPort = num(data.publicport);
options.host = str(data.host);
options.port = num(data.port);
options.listen = bool(data.listen);
options.knownPeers = file(data.knownpeers, prefix, dirname, 'utf8');
options.authPeers = file(data.authpeers, prefix, dirname, 'utf8');
// Miner
options.payoutAddress = str(data.payoutaddress);
options.payoutAddress = list(data.payoutaddress);
options.coinbaseFlags = str(data.coinbaseflags);
// HTTP

View File

@ -115,6 +115,9 @@ function FullNode(options) {
seeds: this.options.seeds,
nodes: this.options.nodes,
noDiscovery: this.options.noDiscovery,
publicHost: this.options.publicHost,
publicPort: this.options.publicPort,
host: this.options.host,
port: this.options.port,
listen: this.options.listen
});