-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathclient.go
60 lines (53 loc) · 1.37 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package tracker
import (
"context"
"net"
"net/url"
"github.com/anacrolix/log"
trHttp "github.com/anacrolix/torrent/tracker/http"
"github.com/anacrolix/torrent/tracker/udp"
"github.com/anacrolix/torrent/types/infohash"
)
type Client interface {
Announce(context.Context, AnnounceRequest, AnnounceOpt) (AnnounceResponse, error)
Scrape(ctx context.Context, ihs []infohash.T) (out udp.ScrapeResponse, err error)
Close() error
}
type AnnounceOpt = trHttp.AnnounceOpt
type NewClientOpts struct {
Http trHttp.NewClientOpts
// Overrides the network in the scheme. Probably a legacy thing.
UdpNetwork string
Logger log.Logger
ListenPacket func(network, addr string) (net.PacketConn, error)
}
func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
_url, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
switch _url.Scheme {
case "http", "https":
return trHttp.NewClient(_url, opts.Http), nil
case "udp", "udp4", "udp6":
network := _url.Scheme
if opts.UdpNetwork != "" {
network = opts.UdpNetwork
}
cc, err := udp.NewConnClient(udp.NewConnClientOpts{
Network: network,
Host: _url.Host,
Logger: opts.Logger,
ListenPacket: opts.ListenPacket,
})
if err != nil {
return nil, err
}
return &udpClient{
cl: cc,
requestUri: _url.RequestURI(),
}, nil
default:
return nil, ErrBadScheme
}
}