From 14f285192940452035deecad3f0fb6605b83f99f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 21 Dec 2016 15:12:14 -0800 Subject: [PATCH] pool: add ban and unban calls once again. --- lib/http/rpc.js | 14 ++++---------- lib/net/peer.js | 7 ++----- lib/net/pool.js | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 1c882875..c092a193 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -545,7 +545,7 @@ RPC.prototype.ping = co(function* ping(args) { }); RPC.prototype.setban = co(function* setban(args) { - var addr, peer; + var addr; if (args.help || args.length < 2 @@ -555,20 +555,14 @@ RPC.prototype.setban = co(function* setban(args) { } addr = toString(args[0]); - addr = IP.parseHost(addr, this.network); + addr = NetAddress.fromHostname(addr, this.network); switch (args[1]) { case 'add': - peer = this.pool.peers.get(addr.hostname); - if (peer) { - peer.ban(); - break; - } - this.pool.hosts.ban(addr.host); - this.pool.hosts.remove(addr.hostname); + this.pool.ban(addr); break; case 'remove': - this.pool.hosts.unban(addr.host); + this.pool.unban(addr); break; } diff --git a/lib/net/peer.js b/lib/net/peer.js index 9adf6e42..d39c184b 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -35,7 +35,7 @@ var VerifyResult = errors.VerifyResult; * @exports Peer * @constructor * @param {Pool} pool - * @param {NetAddress} addr + * @param {NetAddress} address * @param {net.Socket?} socket * @property {Pool} pool * @property {net.Socket?} socket @@ -2685,10 +2685,7 @@ Peer.prototype.increaseBan = function increaseBan(score) { */ Peer.prototype.ban = function ban() { - this.logger.debug('Banning peer (%s).', this.hostname); - this.pool.hosts.ban(this.host); - this.pool.hosts.remove(this.hostname); - this.destroy(); + this.pool.ban(this.address); }; /** diff --git a/lib/net/pool.js b/lib/net/pool.js index 43e4f7ec..17c532d2 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1668,6 +1668,32 @@ Pool.prototype.removePeer = function removePeer(peer) { } }; +/** + * Ban peer. + * @param {NetAddress} addr + */ + +Pool.prototype.ban = function ban(addr) { + var peer = this.peers.get(addr.hostname); + + this.logger.debug('Banning peer (%s).', addr.hostname); + + this.hosts.ban(addr.host); + this.hosts.remove(addr.hostname); + + if (peer) + peer.destroy(); +}; + +/** + * Unban peer. + * @param {NetAddress} addr + */ + +Pool.prototype.unban = function unban(addr) { + this.hosts.unban(addr.host); +}; + /** * Set the spv filter. * @param {Bloom} filter