torrent/tracker/http/client.go

50 lines
943 B
Go
Raw Normal View History

2022-12-05 06:50:39 +00:00
package httpTracker
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
)
type Client struct {
hc *http.Client
url_ *url.URL
}
2023-02-18 07:35:22 +00:00
type (
ProxyFunc func(*http.Request) (*url.URL, error)
DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
)
type NewClientOpts struct {
2021-06-25 05:00:09 +00:00
Proxy ProxyFunc
DialContext DialContextFunc
2021-06-25 05:00:09 +00:00
ServerName string
AllowKeepAlive bool
}
func NewClient(url_ *url.URL, opts NewClientOpts) Client {
return Client{
url_: url_,
hc: &http.Client{
Transport: &http.Transport{
DialContext: opts.DialContext,
2022-11-15 12:22:10 +00:00
Proxy: opts.Proxy,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
ServerName: opts.ServerName,
},
// This is for S3 trackers that hold connections open.
2021-06-25 05:00:09 +00:00
DisableKeepAlives: !opts.AllowKeepAlive,
},
},
}
}
func (cl Client) Close() error {
cl.hc.CloseIdleConnections()
return nil
}