-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
Copy pathtracker.go
89 lines (77 loc) · 2.49 KB
/
tracker.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tracker
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/anacrolix/dht/v2/krpc"
"github.com/anacrolix/log"
trHttp "github.com/anacrolix/torrent/tracker/http"
"github.com/anacrolix/torrent/tracker/shared"
"github.com/anacrolix/torrent/tracker/udp"
)
const (
None = shared.None
Started = shared.Started
Stopped = shared.Stopped
Completed = shared.Completed
)
type AnnounceRequest = udp.AnnounceRequest
type AnnounceResponse = trHttp.AnnounceResponse
type Peer = trHttp.Peer
type AnnounceEvent = udp.AnnounceEvent
var ErrBadScheme = errors.New("unknown scheme")
type Announce struct {
TrackerUrl string
Request AnnounceRequest
HostHeader string
HttpProxy func(*http.Request) (*url.URL, error)
HttpRequestDirector func(*http.Request) error
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
ListenPacket func(network, addr string) (net.PacketConn, error)
ServerName string
UserAgent string
UdpNetwork string
// If the port is zero, it's assumed to be the same as the Request.Port.
ClientIp4 krpc.NodeAddr
// If the port is zero, it's assumed to be the same as the Request.Port.
ClientIp6 krpc.NodeAddr
Context context.Context
Logger log.Logger
}
// The code *is* the documentation.
const DefaultTrackerAnnounceTimeout = 15 * time.Second
func (me Announce) Do() (res AnnounceResponse, err error) {
cl, err := NewClient(me.TrackerUrl, NewClientOpts{
Http: trHttp.NewClientOpts{
Proxy: me.HttpProxy,
DialContext: me.DialContext,
ServerName: me.ServerName,
},
UdpNetwork: me.UdpNetwork,
Logger: me.Logger.WithContextValue(fmt.Sprintf("tracker client for %q", me.TrackerUrl)),
ListenPacket: me.ListenPacket,
})
if err != nil {
return
}
defer cl.Close()
if me.Context == nil {
// This is just to maintain the old behaviour that should be a timeout of 15s. Users can
// override it by providing their own Context. See comments elsewhere about longer timeouts
// acting as rate limiting overloaded trackers.
ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
defer cancel()
me.Context = ctx
}
return cl.Announce(me.Context, me.Request, trHttp.AnnounceOpt{
UserAgent: me.UserAgent,
HostHeader: me.HostHeader,
ClientIp4: me.ClientIp4.IP,
ClientIp6: me.ClientIp6.IP,
HttpRequestDirector: me.HttpRequestDirector,
})
}