client.go | 8 ++++++++ config.go | 11 ++++++++++- tracker/http.go | 1 + tracker/tracker.go | 2 ++ tracker_scraper.go | 1 + diff --git a/client.go b/client.go index 2dba6973c516cdbbcd5a131c5b895c2dd506fc9f..14fff4f64da6fd8b66895f1cb7f0967b936951a0 100644 --- a/client.go +++ b/client.go @@ -10,6 +10,8 @@ "errors" "fmt" "io" "net" + "net/http" + "net/url" "strconv" "strings" "time" @@ -217,6 +219,12 @@ o := copy(cl.peerID[:], cfg.Bep20) _, err = rand.Read(cl.peerID[o:]) if err != nil { panic("error generating peer id") + } + } + + if cl.config.HTTPProxy == nil && cl.config.ProxyURL != "" { + if fixedURL, err := url.Parse(cl.config.ProxyURL); err == nil { + cl.config.HTTPProxy = http.ProxyURL(fixedURL) } } diff --git a/config.go b/config.go index 8e5346f9e38d09798e869e5318b77937032d135e..964453af536c78176fe89ce55edbcf8dbc265bf6 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,8 @@ package torrent import ( "net" + "net/http" + "net/url" "time" "github.com/anacrolix/dht" @@ -67,7 +69,8 @@ EncryptionPolicy // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed. - // Example of setting: "socks5://demo:demo@192.168.99.100:1080" + // Examples: socks5://demo:demo@192.168.99.100:1080 + // http://proxy.domain.com:3128 ProxyURL string IPBlocklist iplist.Ranger @@ -77,6 +80,12 @@ DisableIPv4Peers bool // Perform logging and any other behaviour that will help debug. Debug bool `help:"enable debugging"` + // HTTPProxy defines proxy for HTTP requests. + // Format: func(*Request) (*url.URL, error), + // or result of http.ProxyURL(HTTPProxy). + // By default, it is composed from ClientConfig.ProxyURL, + // if not set explicitly in ClientConfig struct + HTTPProxy func(*http.Request) (*url.URL, error) // HTTPUserAgent changes default UserAgent for HTTP requests HTTPUserAgent string // Updated occasionally to when there's been some changes to client diff --git a/tracker/http.go b/tracker/http.go index 6a99b560ec969fe52f4931c543c4897900045c72..58d9dc1144585b0f3b00dcce7e806a68b9932b7a 100644 --- a/tracker/http.go +++ b/tracker/http.go @@ -105,6 +105,7 @@ Transport: &http.Transport{ Dial: (&net.Dialer{ Timeout: 15 * time.Second, }).Dial, + Proxy: opt.HTTPProxy, TLSHandshakeTimeout: 15 * time.Second, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, diff --git a/tracker/tracker.go b/tracker/tracker.go index bd41e6adea4f27d11d21b9f109cd98c904b260eb..a56a99b74adb221ffa48f830f3eb374b5efde7df 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -2,6 +2,7 @@ package tracker import ( "errors" + "net/http" "net/url" "github.com/anacrolix/dht/krpc" @@ -52,6 +53,7 @@ type Announce struct { TrackerUrl string Request AnnounceRequest HostHeader string + HTTPProxy func(*http.Request) (*url.URL, error) ServerName string UserAgent string UdpNetwork string diff --git a/tracker_scraper.go b/tracker_scraper.go index 2e177badea2b9c904471895249bf6280dac5896b..0a87c868662ca464f9df98b483c91f61623c1426 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -111,6 +111,7 @@ me.t.cl.lock() req := me.t.announceRequest() me.t.cl.unlock() res, err := tracker.Announce{ + HTTPProxy: me.t.cl.config.HTTPProxy, UserAgent: me.t.cl.config.HTTPUserAgent, TrackerUrl: me.trackerUrl(ip), Request: req,