2013-11-06 14:55:29 +00:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
2018-11-28 01:02:12 +00:00
|
|
|
"context"
|
2013-12-14 11:21:45 +00:00
|
|
|
"errors"
|
2018-10-30 22:32:33 +00:00
|
|
|
"net/http"
|
2013-11-06 14:55:29 +00:00
|
|
|
"net/url"
|
2020-10-01 00:45:05 +00:00
|
|
|
"time"
|
2018-02-19 05:19:18 +00:00
|
|
|
|
2019-08-10 08:46:07 +00:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2021-06-22 13:28:26 +00:00
|
|
|
trHttp "github.com/anacrolix/torrent/tracker/http"
|
|
|
|
"github.com/anacrolix/torrent/tracker/shared"
|
2021-06-22 12:36:43 +00:00
|
|
|
"github.com/anacrolix/torrent/tracker/udp"
|
2013-11-06 14:55:29 +00:00
|
|
|
)
|
|
|
|
|
2021-06-22 13:28:26 +00:00
|
|
|
const (
|
|
|
|
None = shared.None
|
|
|
|
Started = shared.Started
|
|
|
|
Stopped = shared.Stopped
|
|
|
|
Completed = shared.Completed
|
|
|
|
)
|
|
|
|
|
2021-06-22 12:36:43 +00:00
|
|
|
type AnnounceRequest = udp.AnnounceRequest
|
2013-11-06 14:55:29 +00:00
|
|
|
|
2021-06-22 13:28:26 +00:00
|
|
|
type AnnounceResponse = trHttp.AnnounceResponse
|
2013-11-06 14:55:29 +00:00
|
|
|
|
2021-06-22 13:28:26 +00:00
|
|
|
type Peer = trHttp.Peer
|
2015-03-26 06:20:31 +00:00
|
|
|
|
2021-06-22 13:28:26 +00:00
|
|
|
type AnnounceEvent = udp.AnnounceEvent
|
2013-11-06 14:55:29 +00:00
|
|
|
|
2021-11-08 03:47:01 +00:00
|
|
|
var ErrBadScheme = errors.New("unknown scheme")
|
2013-11-06 14:55:29 +00:00
|
|
|
|
2018-02-19 05:19:18 +00:00
|
|
|
type Announce struct {
|
|
|
|
TrackerUrl string
|
|
|
|
Request AnnounceRequest
|
|
|
|
HostHeader string
|
2018-10-30 22:32:33 +00:00
|
|
|
HTTPProxy func(*http.Request) (*url.URL, error)
|
2018-10-18 00:09:56 +00:00
|
|
|
ServerName string
|
2018-02-19 05:19:18 +00:00
|
|
|
UserAgent string
|
|
|
|
UdpNetwork string
|
2018-11-28 01:02:25 +00:00
|
|
|
// If the port is zero, it's assumed to be the same as the Request.Port.
|
2018-03-29 03:29:15 +00:00
|
|
|
ClientIp4 krpc.NodeAddr
|
2018-11-28 01:02:25 +00:00
|
|
|
// If the port is zero, it's assumed to be the same as the Request.Port.
|
2018-03-29 03:29:15 +00:00
|
|
|
ClientIp6 krpc.NodeAddr
|
2018-11-28 01:02:12 +00:00
|
|
|
Context context.Context
|
2013-11-06 14:55:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 22:29:20 +00:00
|
|
|
// The code *is* the documentation.
|
|
|
|
const DefaultTrackerAnnounceTimeout = 15 * time.Second
|
|
|
|
|
2018-02-19 05:19:18 +00:00
|
|
|
func (me Announce) Do() (res AnnounceResponse, err error) {
|
2021-06-24 00:39:56 +00:00
|
|
|
cl, err := NewClient(me.TrackerUrl, NewClientOpts{
|
|
|
|
Http: trHttp.NewClientOpts{
|
|
|
|
Proxy: me.HTTPProxy,
|
|
|
|
ServerName: me.ServerName,
|
|
|
|
},
|
|
|
|
UdpNetwork: me.UdpNetwork,
|
|
|
|
})
|
2013-12-16 07:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-24 00:39:56 +00:00
|
|
|
defer cl.Close()
|
2020-10-01 00:45:05 +00:00
|
|
|
if me.Context == nil {
|
|
|
|
// This is just to maintain the old behaviour that should be a timeout of 15s. Users can
|
2020-10-01 01:45:47 +00:00
|
|
|
// override it by providing their own Context. See comments elsewhere about longer timeouts
|
|
|
|
// acting as rate limiting overloaded trackers.
|
2020-12-20 22:29:20 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
|
2020-10-01 00:45:05 +00:00
|
|
|
defer cancel()
|
|
|
|
me.Context = ctx
|
|
|
|
}
|
2021-06-24 00:39:56 +00:00
|
|
|
return cl.Announce(me.Context, me.Request, trHttp.AnnounceOpt{
|
|
|
|
UserAgent: me.UserAgent,
|
|
|
|
HostHeader: me.HostHeader,
|
|
|
|
ClientIp4: me.ClientIp4.IP,
|
|
|
|
ClientIp6: me.ClientIp6.IP,
|
|
|
|
})
|
2016-02-07 07:06:13 +00:00
|
|
|
}
|