12 "github.com/anacrolix/dht/v2/krpc"
13 "github.com/anacrolix/log"
14 trHttp "github.com/anacrolix/torrent/tracker/http"
15 "github.com/anacrolix/torrent/tracker/shared"
16 "github.com/anacrolix/torrent/tracker/udp"
21 Started = shared.Started
22 Stopped = shared.Stopped
23 Completed = shared.Completed
26 type AnnounceRequest = udp.AnnounceRequest
28 type AnnounceResponse = trHttp.AnnounceResponse
30 type Peer = trHttp.Peer
32 type AnnounceEvent = udp.AnnounceEvent
34 var ErrBadScheme = errors.New("unknown scheme")
36 type Announce struct {
38 Request AnnounceRequest
40 HTTPProxy func(*http.Request) (*url.URL, error)
41 DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
42 ListenPacket func(network, addr string) (net.PacketConn, error)
46 // If the port is zero, it's assumed to be the same as the Request.Port.
47 ClientIp4 krpc.NodeAddr
48 // If the port is zero, it's assumed to be the same as the Request.Port.
49 ClientIp6 krpc.NodeAddr
50 Context context.Context
54 // The code *is* the documentation.
55 const DefaultTrackerAnnounceTimeout = 15 * time.Second
57 func (me Announce) Do() (res AnnounceResponse, err error) {
58 cl, err := NewClient(me.TrackerUrl, NewClientOpts{
59 Http: trHttp.NewClientOpts{
61 DialContext: me.DialContext,
62 ServerName: me.ServerName,
64 UdpNetwork: me.UdpNetwork,
65 Logger: me.Logger.WithContextValue(fmt.Sprintf("tracker client for %q", me.TrackerUrl)),
66 ListenPacket: me.ListenPacket,
72 if me.Context == nil {
73 // This is just to maintain the old behaviour that should be a timeout of 15s. Users can
74 // override it by providing their own Context. See comments elsewhere about longer timeouts
75 // acting as rate limiting overloaded trackers.
76 ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
80 return cl.Announce(me.Context, me.Request, trHttp.AnnounceOpt{
81 UserAgent: me.UserAgent,
82 HostHeader: me.HostHeader,
83 ClientIp4: me.ClientIp4.IP,
84 ClientIp6: me.ClientIp6.IP,