From a01b451857e2e0880d5315dbdd94547519b73cd7 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 18 Jun 2021 14:59:20 +1000 Subject: [PATCH] Add ClientConfig.AcceptPeerConnections --- client.go | 10 +++++++--- config.go | 11 +++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index a8d93319..dc244807 100644 --- a/client.go +++ b/client.go @@ -248,7 +248,9 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) { if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) { cl.dialers = append(cl.dialers, s) cl.listeners = append(cl.listeners, s) - go cl.acceptConnections(s) + if cl.config.AcceptPeerConnections { + go cl.acceptConnections(s) + } } } @@ -322,12 +324,14 @@ func (cl *Client) Listeners() []Listener { // yourself. func (cl *Client) AddListener(l Listener) { cl.listeners = append(cl.listeners, l) - go cl.acceptConnections(l) + if cl.config.AcceptPeerConnections { + go cl.acceptConnections(l) + } } func (cl *Client) firewallCallback(net.Addr) bool { cl.rLock() - block := !cl.wantConns() + block := !cl.wantConns() || !cl.config.AcceptPeerConnections cl.rUnlock() if block { torrent.Add("connections firewalled", 1) diff --git a/config.go b/config.go index b687f05d..b952ca95 100644 --- a/config.go +++ b/config.go @@ -133,6 +133,8 @@ type ClientConfig struct { // bit of a special case, since a peer could also be useless if they're just not interested, or // we don't intend to obtain all of a torrent's data. DropMutuallyCompletePeers bool + // Whether to accept peer connections at all. + AcceptPeerConnections bool // OnQuery hook func DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool) @@ -180,10 +182,11 @@ func NewDefaultClientConfig() *ClientConfig { Preferred: true, RequirePreferred: false, }, - CryptoSelector: mse.DefaultCryptoSelector, - CryptoProvides: mse.AllSupportedCrypto, - ListenPort: 42069, - Extensions: defaultPeerExtensionBytes(), + CryptoSelector: mse.DefaultCryptoSelector, + CryptoProvides: mse.AllSupportedCrypto, + ListenPort: 42069, + Extensions: defaultPeerExtensionBytes(), + AcceptPeerConnections: true, } //cc.ConnTracker.SetNoMaxEntries() //cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 }