]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/http/client.go
Extract protocol agnostic tracker Client
[btrtrc.git] / tracker / http / client.go
1 package http
2
3 import (
4         "crypto/tls"
5         "net/http"
6         "net/url"
7 )
8
9 type Client struct {
10         hc   *http.Client
11         url_ *url.URL
12 }
13
14 type ProxyFunc func(*http.Request) (*url.URL, error)
15
16 type NewClientOpts struct {
17         Proxy      ProxyFunc
18         ServerName string
19 }
20
21 func NewClient(url_ *url.URL, opts NewClientOpts) Client {
22         return Client{
23                 url_: url_,
24                 hc: &http.Client{
25                         Transport: &http.Transport{
26                                 Proxy: opts.Proxy,
27                                 TLSClientConfig: &tls.Config{
28                                         InsecureSkipVerify: true,
29                                         ServerName:         opts.ServerName,
30                                 },
31                                 // This is for S3 trackers that hold connections open.
32                                 DisableKeepAlives: true,
33                         },
34                 },
35         }
36 }
37
38 func (cl Client) Close() error {
39         cl.hc.CloseIdleConnections()
40         return nil
41 }