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