From b212eed28204be6437e5b2001f52d7b96f6542c3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 21 Jan 2017 13:56:41 -0800 Subject: [PATCH] net: handle public ip and port better. --- etc/sample.conf | 79 ++++++++++++++++++++++++++++++++----------- lib/http/client.js | 2 +- lib/http/request.js | 50 ++++++++++----------------- lib/http/rpcclient.js | 2 +- lib/mining/miner.js | 10 ++++-- lib/net/pool.js | 24 ++++++++++--- lib/node/config.js | 5 ++- lib/node/fullnode.js | 3 ++ 8 files changed, 113 insertions(+), 62 deletions(-) diff --git a/etc/sample.conf b/etc/sample.conf index 42c42d7b..0aacb5b6 100644 --- a/etc/sample.conf +++ b/etc/sample.conf @@ -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 diff --git a/lib/http/client.js b/lib/http/client.js index 0afd5090..88fa6a59 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -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. diff --git a/lib/http/request.js b/lib/http/request.js index c14c0f8e..ee4b359e 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -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; }; /* diff --git a/lib/http/rpcclient.js b/lib/http/rpcclient.js index 230a6ce8..43da3a4e 100644 --- a/lib/http/rpcclient.js +++ b/lib/http/rpcclient.js @@ -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, diff --git a/lib/mining/miner.js b/lib/mining/miner.js index 20e74406..55784355 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -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)); diff --git a/lib/net/pool.js b/lib/net/pool.js index dcc5586b..f3e0b174 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -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; diff --git a/lib/node/config.js b/lib/node/config.js index 8851c79f..a43d2b31 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -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 diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index 5b2d8bcb..f15c7255 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -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 });