Skip to content

Commit

Permalink
Fix TLS handshake failures with https trackers
Browse files Browse the repository at this point in the history
Fixes #276
  • Loading branch information
anacrolix committed Oct 18, 2018
1 parent 5e3f989 commit ad0c33a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
2 changes: 0 additions & 2 deletions cmd/tracker-announce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"log"
"math"
"net/http"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -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"
Expand Down
13 changes: 0 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package torrent

import (
"crypto/tls"
"net"
"net/http"
"time"

"github.com/anacrolix/dht"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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-",
Expand Down
17 changes: 16 additions & 1 deletion tracker/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tracker

import (
"errors"
"net/http"
"net/url"

"github.com/anacrolix/dht/krpc"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tracker_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit ad0c33a

Please sign in to comment.