From 612f6a987c32c51e6075efa6d2dc9e94d73a4669 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 15 May 2017 03:22:09 -0700 Subject: [PATCH] rpc: better errors. --- lib/http/rpc.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 298d90bb..b23e3b8c 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -272,7 +272,7 @@ RPC.prototype.addNode = co(function* addNode(args, help) { addr = this.pool.hosts.addNode(node); ; // fall through case 'onetry': - addr = NetAddress.fromHostname(node, this.network); + addr = parseNetAddress(node, this.network); if (!this.pool.peers.get(addr.hostname)) { peer = this.pool.createOutbound(addr); @@ -296,7 +296,7 @@ RPC.prototype.disconnectNode = co(function* disconnectNode(args, help) { if (help || args.length !== 1) throw new RPCError(errs.MISC_ERROR, 'disconnectnode "node"'); - addr = IP.fromHostname(addr, this.network.port); + addr = parseIP(addr, this.network); peer = this.pool.peers.get(addr.hostname); if (peer) @@ -316,7 +316,7 @@ RPC.prototype.getAddedNodeInfo = co(function* getAddedNodeInfo(args, help) { throw new RPCError(errs.MISC_ERROR, 'getaddednodeinfo ( "node" )'); if (args.length === 1) - target = IP.fromHostname(addr, this.network.port); + target = parseIP(addr, this.network); for (i = 0; i < hosts.nodes.length; i++) { node = hosts.nodes[i]; @@ -460,12 +460,7 @@ RPC.prototype.setBan = co(function* setBan(args, help) { 'setban "ip(/netmask)" "add|remove" (bantime) (absolute)'); } - try { - addr = NetAddress.fromHostname(addr, this.network); - } catch (e) { - throw new RPCError(errs.CLIENT_INVALID_IP_OR_SUBNET, - 'Invalid IP or subnet.'); - } + addr = parseNetAddress(addr, this.network); switch (action) { case 'add': @@ -2810,6 +2805,24 @@ function parseSecret(raw, network) { } } +function parseIP(addr, network) { + try { + return IP.fromHostname(addr, network.port); + } catch (e) { + throw new RPCError(errs.CLIENT_INVALID_IP_OR_SUBNET, + 'Invalid IP address or subnet.'); + } +} + +function parseNetAddress(addr, network) { + try { + return NetAddress.fromHostname(addr, network); + } catch (e) { + throw new RPCError(errs.CLIENT_INVALID_IP_OR_SUBNET, + 'Invalid IP address or subnet.'); + } +} + function toDifficulty(bits) { var shift = (bits >>> 24) & 0xff; var diff = 0x0000ffff / (bits & 0x00ffffff);