]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/http/client.go
Rename tracker/http package
[btrtrc.git] / tracker / http / client.go
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 ProxyFunc func(*http.Request) (*url.URL, error)
17 type DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
18
19 type NewClientOpts struct {
20         Proxy          ProxyFunc
21         DialContext    DialContextFunc
22         ServerName     string
23         AllowKeepAlive bool
24 }
25
26 func NewClient(url_ *url.URL, opts NewClientOpts) Client {
27         return Client{
28                 url_: url_,
29                 hc: &http.Client{
30                         Transport: &http.Transport{
31                                 DialContext: opts.DialContext,
32                                 Proxy:       opts.Proxy,
33                                 TLSClientConfig: &tls.Config{
34                                         InsecureSkipVerify: true,
35                                         ServerName:         opts.ServerName,
36                                 },
37                                 // This is for S3 trackers that hold connections open.
38                                 DisableKeepAlives: !opts.AllowKeepAlive,
39                         },
40                 },
41         }
42 }
43
44 func (cl Client) Close() error {
45         cl.hc.CloseIdleConnections()
46         return nil
47 }