From a90ae5aed4aea44dc1ecdd3b9568d7af9ba2d9fc Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Fri, 12 Nov 2021 14:16:14 +1100
Subject: [PATCH] Pass HTTP Proxy config into webseeding HTTP client

---
 client.go  |  8 +++++++-
 torrent.go | 10 +---------
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/client.go b/client.go
index 30ab0cd6..cb988201 100644
--- a/client.go
+++ b/client.go
@@ -82,6 +82,7 @@ type Client struct {
 	websocketTrackers websocketTrackers
 
 	activeAnnounceLimiter limiter.Instance
+	webseedHttpClient     *http.Client
 }
 
 type ipStr string
@@ -199,9 +200,14 @@ func (cl *Client) init(cfg *ClientConfig) {
 	cl.torrents = make(map[metainfo.Hash]*Torrent)
 	cl.dialRateLimiter = rate.NewLimiter(10, 10)
 	cl.activeAnnounceLimiter.SlotsPerKey = 2
-
 	cl.event.L = cl.locker()
 	cl.ipBlockList = cfg.IPBlocklist
+	cl.webseedHttpClient = &http.Client{
+		Transport: &http.Transport{
+			Proxy:           cfg.HTTPProxy,
+			MaxConnsPerHost: 10,
+		},
+	}
 }
 
 func NewClient(cfg *ClientConfig) (cl *Client, err error) {
diff --git a/torrent.go b/torrent.go
index b9ecb0f6..441f613c 100644
--- a/torrent.go
+++ b/torrent.go
@@ -8,7 +8,6 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"net/http"
 	"net/url"
 	"sort"
 	"strings"
@@ -2180,12 +2179,6 @@ func (t *Torrent) callbacks() *Callbacks {
 	return &t.cl.config.Callbacks
 }
 
-var WebseedHttpClient = &http.Client{
-	Transport: &http.Transport{
-		MaxConnsPerHost: 10,
-	},
-}
-
 func (t *Torrent) addWebSeed(url string) {
 	if t.cl.config.DisableWebseeds {
 		return
@@ -2212,8 +2205,7 @@ func (t *Torrent) addWebSeed(url string) {
 			callbacks:       t.callbacks(),
 		},
 		client: webseed.Client{
-			// Consider a MaxConnsPerHost in the transport for this, possibly in a global Client.
-			HttpClient: WebseedHttpClient,
+			HttpClient: t.cl.webseedHttpClient,
 			Url:        url,
 		},
 		activeRequests: make(map[Request]webseed.Request, maxRequests),
-- 
2.51.0