From ec2b1b2073df1eaa39e14aa81a775665dea5b47d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 11 Mar 2022 13:33:34 +1100 Subject: [PATCH] Support banning webseeds --- client.go | 1 + peer-impl.go | 3 +++ peerconn.go | 4 ++++ torrent.go | 3 +-- webseed-peer.go | 7 +++++-- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 86c83e20..0d158395 100644 --- a/client.go +++ b/client.go @@ -1493,6 +1493,7 @@ func (cl *Client) banPeerIP(ip net.IP) { t.iterPeers(func(p *Peer) { if p.remoteIp().Equal(ip) { t.logger.Levelf(log.Warning, "dropping peer %v with banned ip %v", p, ip) + // Should this be a close? p.drop() } }) diff --git a/peer-impl.go b/peer-impl.go index e29fb439..1b9cf978 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -20,7 +20,10 @@ type peerImpl interface { connectionFlags() string onClose() onGotInfo(*metainfo.Info) + // Drop connection. This may be a no-op if there is no connection. drop() + // Rebuke the peer + ban() String() string connStatusString() string diff --git a/peerconn.go b/peerconn.go index bc16ae6c..6615d79c 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1590,6 +1590,10 @@ func (cn *PeerConn) drop() { cn.t.dropConnection(cn) } +func (cn *PeerConn) ban() { + cn.t.cl.banPeerIP(cn.remoteIp()) +} + func (cn *Peer) netGoodPiecesDirtied() int64 { return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64() } diff --git a/torrent.go b/torrent.go index e148d38b..1c197f9b 100644 --- a/torrent.go +++ b/torrent.go @@ -2020,8 +2020,7 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) { if len(bannableTouchers) >= 1 { c := bannableTouchers[0] - t.cl.banPeerIP(c.remoteIp()) - c.drop() + c.ban() } } t.onIncompletePiece(piece) diff --git a/webseed-peer.go b/webseed-peer.go index 9b1cba22..0cdf4cac 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -114,10 +114,13 @@ func (ws *webseedPeer) connectionFlags() string { return "WS" } -// TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could -// return bool if this is even possible, and if it isn't, skip to the next drop candidate. +// Maybe this should drop all existing connections, or something like that. func (ws *webseedPeer) drop() {} +func (cn *webseedPeer) ban() { + cn.peer.close() +} + func (ws *webseedPeer) handleUpdateRequests() { // Because this is synchronous, webseed peers seem to get first dibs on newly prioritized // pieces. -- 2.48.1