Support banning webseeds

This commit is contained in:
Matt Joiner 2022-03-11 13:33:34 +11:00
parent 162cef5d1b
commit ec2b1b2073
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
5 changed files with 14 additions and 4 deletions

View File

@ -1493,6 +1493,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
t.iterPeers(func(p *Peer) { t.iterPeers(func(p *Peer) {
if p.remoteIp().Equal(ip) { if p.remoteIp().Equal(ip) {
t.logger.Levelf(log.Warning, "dropping peer %v with banned ip %v", p, ip) t.logger.Levelf(log.Warning, "dropping peer %v with banned ip %v", p, ip)
// Should this be a close?
p.drop() p.drop()
} }
}) })

View File

@ -20,7 +20,10 @@ type peerImpl interface {
connectionFlags() string connectionFlags() string
onClose() onClose()
onGotInfo(*metainfo.Info) onGotInfo(*metainfo.Info)
// Drop connection. This may be a no-op if there is no connection.
drop() drop()
// Rebuke the peer
ban()
String() string String() string
connStatusString() string connStatusString() string

View File

@ -1590,6 +1590,10 @@ func (cn *PeerConn) drop() {
cn.t.dropConnection(cn) cn.t.dropConnection(cn)
} }
func (cn *PeerConn) ban() {
cn.t.cl.banPeerIP(cn.remoteIp())
}
func (cn *Peer) netGoodPiecesDirtied() int64 { func (cn *Peer) netGoodPiecesDirtied() int64 {
return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64() return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64()
} }

View File

@ -2020,8 +2020,7 @@ func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
if len(bannableTouchers) >= 1 { if len(bannableTouchers) >= 1 {
c := bannableTouchers[0] c := bannableTouchers[0]
t.cl.banPeerIP(c.remoteIp()) c.ban()
c.drop()
} }
} }
t.onIncompletePiece(piece) t.onIncompletePiece(piece)

View File

@ -114,10 +114,13 @@ func (ws *webseedPeer) connectionFlags() string {
return "WS" return "WS"
} }
// TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could // Maybe this should drop all existing connections, or something like that.
// return bool if this is even possible, and if it isn't, skip to the next drop candidate.
func (ws *webseedPeer) drop() {} func (ws *webseedPeer) drop() {}
func (cn *webseedPeer) ban() {
cn.peer.close()
}
func (ws *webseedPeer) handleUpdateRequests() { func (ws *webseedPeer) handleUpdateRequests() {
// Because this is synchronous, webseed peers seem to get first dibs on newly prioritized // Because this is synchronous, webseed peers seem to get first dibs on newly prioritized
// pieces. // pieces.