From 0d36ea4c5d9c00c537e34502974ff620ca88ce42 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 11 Jul 2025 00:12:12 +1000 Subject: [PATCH] Add DialForPeerConns config option so you can disable peer conns --- client.go | 21 ++++++++++++++++++--- config.go | 3 +++ issue211_test.go | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 8f856827..442b7138 100644 --- a/client.go +++ b/client.go @@ -325,11 +325,12 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) { // Check for panics. cl.LocalPort() - for _, _s := range sockets { - s := _s // Go is fucking retarded. + for _, s := range sockets { cl.onClose = append(cl.onClose, func() { go s.Close() }) if peerNetworkEnabled(parseNetworkString(s.Addr().Network()), cl.config) { - cl.dialers = append(cl.dialers, s) + if cl.config.DialForPeerConns { + cl.dialers = append(cl.dialers, s) + } cl.listeners = append(cl.listeners, s) if cl.config.AcceptPeerConnections { go cl.acceptConnections(s) @@ -390,6 +391,7 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) { cl.webseedRequestTimer = time.AfterFunc(webseedRequestUpdateTimerInterval, cl.updateWebseedRequestsTimerFunc) + err = cl.checkConfig() return } @@ -1927,3 +1929,16 @@ func (cl *Client) underWebSeedHttpRequestLimit(key webseedHostKeyHandle) bool { panicif.Zero(key) return cl.numWebSeedRequests[key] < defaultRequestsPerWebseedHost } + +// Check for bad arrangements. This is a candidate for an error state check method. +func (cl *Client) checkConfig() error { + if cl.config.DownloadRateLimiter.Limit() == 0 { + if len(cl.dialers) != 0 { + return errors.New("download rate limit is zero, but dialers are set") + } + if len(cl.listeners) != 0 && cl.config.AcceptPeerConnections { + return errors.New("download rate limit is zero, but listening for peer connections") + } + } + return nil +} diff --git a/config.go b/config.go index e0727865..55b72ade 100644 --- a/config.go +++ b/config.go @@ -171,6 +171,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 + // Use dialers to obtain connections to regular peers. + DialForPeerConns bool // Whether to accept peer connections at all. AcceptPeerConnections bool // Whether a Client should want conns without delegating to any attached Torrents. This is @@ -239,6 +241,7 @@ func NewDefaultClientConfig() *ClientConfig { CryptoProvides: mse.AllSupportedCrypto, ListenPort: 42069, Extensions: defaultPeerExtensionBytes(), + DialForPeerConns: true, AcceptPeerConnections: true, MaxUnverifiedBytes: 64 << 20, DialRateLimiter: rate.NewLimiter(10, 10), diff --git a/issue211_test.go b/issue211_test.go index 914db687..d8c541da 100644 --- a/issue211_test.go +++ b/issue211_test.go @@ -20,6 +20,7 @@ func TestDropTorrentWithMmapStorageWhileHashing(t *testing.T) { // Ensure the data is present when the torrent is added, and not obtained // over the network as the test runs. cfg.DownloadRateLimiter = rate.NewLimiter(0, 0) + cfg.DialForPeerConns = false cl, err := NewClient(cfg) require.NoError(t, err) defer cl.Close() -- 2.51.0