From ad0c33a50c96f096e9817de792092cc48987c723 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 18 Oct 2018 11:09:56 +1100 Subject: [PATCH] Fix TLS handshake failures with https trackers Fixes #276 --- cmd/tracker-announce/main.go | 2 -- config.go | 13 ------------- tracker/http.go | 17 ++++++++++++++++- tracker/tracker.go | 3 +-- tracker_scraper.go | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/tracker-announce/main.go b/cmd/tracker-announce/main.go index f6bede88..e038d872 100644 --- a/cmd/tracker-announce/main.go +++ b/cmd/tracker-announce/main.go @@ -3,7 +3,6 @@ package main import ( "log" "math" - "net/http" "net/url" "strings" "sync" @@ -85,7 +84,6 @@ func announces(uri string, ar tracker.AnnounceRequest) (ret []announceResult) { a := tracker.Announce{ Request: ar, TrackerUrl: uri, - HttpClient: http.DefaultClient, } if u.Scheme == "udp" { a.UdpNetwork = "udp4" diff --git a/config.go b/config.go index 117f640a..8e5346f9 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,7 @@ package torrent import ( - "crypto/tls" "net" - "net/http" "time" "github.com/anacrolix/dht" @@ -79,8 +77,6 @@ type ClientConfig struct { // Perform logging and any other behaviour that will help debug. Debug bool `help:"enable debugging"` - // For querying HTTP trackers. - TrackerHttpClient *http.Client // HTTPUserAgent changes default UserAgent for HTTP requests HTTPUserAgent string // Updated occasionally to when there's been some changes to client @@ -129,15 +125,6 @@ func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { func NewDefaultClientConfig() *ClientConfig { return &ClientConfig{ - TrackerHttpClient: &http.Client{ - Timeout: time.Second * 15, - Transport: &http.Transport{ - Dial: (&net.Dialer{ - Timeout: 15 * time.Second, - }).Dial, - TLSHandshakeTimeout: 15 * time.Second, - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }}, HTTPUserAgent: DefaultHTTPUserAgent, ExtendedHandshakeClientVersion: "go.torrent dev 20150624", Bep20: "-GT0001-", diff --git a/tracker/http.go b/tracker/http.go index 16182f0e..6a99b560 100644 --- a/tracker/http.go +++ b/tracker/http.go @@ -2,11 +2,14 @@ package tracker import ( "bytes" + "crypto/tls" "fmt" "io" + "net" "net/http" "net/url" "strconv" + "time" "github.com/anacrolix/dht/krpc" "github.com/anacrolix/missinggo/httptoo" @@ -96,7 +99,19 @@ func announceHTTP(opt Announce, _url *url.URL) (ret AnnounceResponse, err error) req, err := http.NewRequest("GET", _url.String(), nil) req.Header.Set("User-Agent", opt.UserAgent) req.Host = opt.HostHeader - resp, err := opt.HttpClient.Do(req) + resp, err := (&http.Client{ + Timeout: time.Second * 15, + Transport: &http.Transport{ + Dial: (&net.Dialer{ + Timeout: 15 * time.Second, + }).Dial, + TLSHandshakeTimeout: 15 * time.Second, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + ServerName: opt.ServerName, + }, + }, + }).Do(req) if err != nil { return } diff --git a/tracker/tracker.go b/tracker/tracker.go index 928ab777..bd41e6ad 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -2,7 +2,6 @@ package tracker import ( "errors" - "net/http" "net/url" "github.com/anacrolix/dht/krpc" @@ -53,8 +52,8 @@ type Announce struct { TrackerUrl string Request AnnounceRequest HostHeader string + ServerName string UserAgent string - HttpClient *http.Client UdpNetwork string // If the port is zero, it's assumed to be the same as the Request.Port ClientIp4 krpc.NodeAddr diff --git a/tracker_scraper.go b/tracker_scraper.go index eefa92e8..a9187022 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -111,11 +111,11 @@ func (me *trackerScraper) announce() (ret trackerAnnounceResult) { req := me.t.announceRequest() me.t.cl.unlock() res, err := tracker.Announce{ - HttpClient: me.t.cl.config.TrackerHttpClient, UserAgent: me.t.cl.config.HTTPUserAgent, TrackerUrl: me.trackerUrl(ip), Request: req, HostHeader: me.u.Host, + ServerName: me.u.Hostname(), UdpNetwork: me.u.Scheme, ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4}, ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},