client.go 943 B

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