github.com/anacrolix/torrent@v1.61.0/tracker/http/client.go (about)

     1  package httpTracker
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"net"
     7  	"net/http"
     8  	"net/url"
     9  )
    10  
    11  type Client struct {
    12  	hc   *http.Client
    13  	url_ *url.URL
    14  }
    15  
    16  type (
    17  	ProxyFunc       func(*http.Request) (*url.URL, error)
    18  	DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
    19  )
    20  
    21  type NewClientOpts struct {
    22  	Proxy          ProxyFunc
    23  	DialContext    DialContextFunc
    24  	ServerName     string
    25  	AllowKeepAlive bool
    26  }
    27  
    28  func NewClient(url_ *url.URL, opts NewClientOpts) Client {
    29  	return Client{
    30  		url_: url_,
    31  		hc: &http.Client{
    32  			Transport: &http.Transport{
    33  				DialContext: opts.DialContext,
    34  				Proxy:       opts.Proxy,
    35  				TLSClientConfig: &tls.Config{
    36  					InsecureSkipVerify: true,
    37  					ServerName:         opts.ServerName,
    38  				},
    39  				// This is for S3 trackers that hold connections open.
    40  				DisableKeepAlives: !opts.AllowKeepAlive,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func (cl Client) Close() error {
    47  	cl.hc.CloseIdleConnections()
    48  	return nil
    49  }