From 9dc89ab6dc4c6c6908fe90b1fee4c35a6a597a3b Mon Sep 17 00:00:00 2001
From: Denis Kuzmenok <denis.kuzmenok@gmail.com>
Date: Tue, 30 Oct 2018 23:32:33 +0100
Subject: [PATCH] reverting usage of proxy for http requests

---
 client.go          |  8 ++++++++
 config.go          | 11 ++++++++++-
 tracker/http.go    |  1 +
 tracker/tracker.go |  2 ++
 tracker_scraper.go |  1 +
 5 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/client.go b/client.go
index 2dba6973..14fff4f6 100644
--- a/client.go
+++ b/client.go
@@ -10,6 +10,8 @@ import (
 	"fmt"
 	"io"
 	"net"
+	"net/http"
+	"net/url"
 	"strconv"
 	"strings"
 	"time"
@@ -220,6 +222,12 @@ func NewClient(cfg *ClientConfig) (cl *Client, err error) {
 		}
 	}
 
+	if cl.config.HTTPProxy == nil && cl.config.ProxyURL != "" {
+		if fixedURL, err := url.Parse(cl.config.ProxyURL); err == nil {
+			cl.config.HTTPProxy = http.ProxyURL(fixedURL)
+		}
+	}
+
 	cl.conns, err = listenAll(cl.enabledPeerNetworks(), cl.config.ListenHost, cl.config.ListenPort, cl.config.ProxyURL, cl.firewallCallback)
 	if err != nil {
 		return
diff --git a/config.go b/config.go
index 8e5346f9..964453af 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 @@ type ClientConfig struct {
 	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 @@ type ClientConfig struct {
 	// 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 6a99b560..58d9dc11 100644
--- a/tracker/http.go
+++ b/tracker/http.go
@@ -105,6 +105,7 @@ func announceHTTP(opt Announce, _url *url.URL) (ret AnnounceResponse, err error)
 			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 bd41e6ad..a56a99b7 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 2e177bad..0a87c868 100644
--- a/tracker_scraper.go
+++ b/tracker_scraper.go
@@ -111,6 +111,7 @@ func (me *trackerScraper) announce() (ret trackerAnnounceResult) {
 	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,
-- 
2.51.0