config.go | 1 + torrent.go | 14 ++++++++++++++ diff --git a/config.go b/config.go index f438cbce8e93a27804d24c60f723836d506882b6..899a2f062f5deb55df1ccc95c76f9bec894bf9db 100644 --- a/config.go +++ b/config.go @@ -110,6 +110,7 @@ PublicIp4 net.IP PublicIp6 net.IP DisableAcceptRateLimiting bool + dropDuplicatePeerIds bool } func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { diff --git a/torrent.go b/torrent.go index 62c61a10edcdf0a8afa9ffabe8b81adb1044a88f..3259dcc4d9db45ad542b1beab0a99612a03653b3 100644 --- a/torrent.go +++ b/torrent.go @@ -1503,6 +1503,20 @@ }() if t.closed.IsSet() { return errors.New("torrent closed") } + for c0 := range t.conns { + if c.PeerID != c0.PeerID { + continue + } + if !t.cl.config.dropDuplicatePeerIds { + continue + } + if left, ok := c.hasPreferredNetworkOver(c0); ok && left { + c0.Close() + t.deleteConnection(c0) + } else { + return errors.New("existing connection preferred") + } + } if !t.wantConns() { return errors.New("don't want conns") }