2013-11-07 01:55:29 +11:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
2018-11-28 12:02:12 +11:00
|
|
|
"context"
|
2013-12-14 22:21:45 +11:00
|
|
|
"errors"
|
2018-10-30 23:32:33 +01:00
|
|
|
"net/http"
|
2013-11-07 01:55:29 +11:00
|
|
|
"net/url"
|
2020-10-01 10:45:05 +10:00
|
|
|
"time"
|
2018-02-19 16:19:18 +11:00
|
|
|
|
2019-08-10 18:46:07 +10:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2013-11-07 01:55:29 +11:00
|
|
|
)
|
|
|
|
|
2015-03-27 17:22:00 +11:00
|
|
|
// Marshalled as binary by the UDP client, so be careful making changes.
|
2013-11-07 01:55:29 +11:00
|
|
|
type AnnounceRequest struct {
|
|
|
|
InfoHash [20]byte
|
|
|
|
PeerId [20]byte
|
|
|
|
Downloaded int64
|
2019-07-17 18:12:11 +10:00
|
|
|
Left int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
|
2013-11-07 01:55:29 +11:00
|
|
|
Uploaded int64
|
2016-05-22 22:42:37 +10:00
|
|
|
// Apparently this is optional. None can be used for announces done at
|
|
|
|
// regular intervals.
|
|
|
|
Event AnnounceEvent
|
2018-02-21 13:33:12 +11:00
|
|
|
IPAddress uint32
|
2016-05-22 22:42:37 +10:00
|
|
|
Key int32
|
|
|
|
NumWant int32 // How many peer addresses are desired. -1 for default.
|
|
|
|
Port uint16
|
2015-03-12 20:07:10 +11:00
|
|
|
} // 82 bytes
|
2013-11-07 01:55:29 +11:00
|
|
|
|
|
|
|
type AnnounceResponse struct {
|
2013-12-16 18:46:55 +11:00
|
|
|
Interval int32 // Minimum seconds the local peer should wait before next announce.
|
2013-11-07 01:55:29 +11:00
|
|
|
Leechers int32
|
|
|
|
Seeders int32
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnnounceEvent int32
|
|
|
|
|
2016-04-18 23:11:11 -05:00
|
|
|
func (e AnnounceEvent) String() string {
|
2020-12-30 11:43:48 +11:00
|
|
|
// See BEP 3, "event", and https://github.com/anacrolix/torrent/issues/416#issuecomment-751427001.
|
|
|
|
return []string{"", "completed", "started", "stopped"}[e]
|
2015-03-26 17:20:31 +11:00
|
|
|
}
|
|
|
|
|
2013-11-07 01:55:29 +11:00
|
|
|
const (
|
2013-12-16 18:46:55 +11:00
|
|
|
None AnnounceEvent = iota
|
|
|
|
Completed // The local peer just completed the torrent.
|
|
|
|
Started // The local peer has just resumed this torrent.
|
|
|
|
Stopped // The local peer is leaving the swarm.
|
2013-11-07 01:55:29 +11:00
|
|
|
)
|
|
|
|
|
2013-12-14 22:21:45 +11:00
|
|
|
var (
|
2016-05-09 12:20:35 +10:00
|
|
|
ErrBadScheme = errors.New("unknown scheme")
|
2013-12-14 22:21:45 +11:00
|
|
|
)
|
2013-11-07 01:55:29 +11:00
|
|
|
|
2018-02-19 16:19:18 +11:00
|
|
|
type Announce struct {
|
|
|
|
TrackerUrl string
|
|
|
|
Request AnnounceRequest
|
|
|
|
HostHeader string
|
2018-10-30 23:32:33 +01:00
|
|
|
HTTPProxy func(*http.Request) (*url.URL, error)
|
2018-10-18 11:09:56 +11:00
|
|
|
ServerName string
|
2018-02-19 16:19:18 +11:00
|
|
|
UserAgent string
|
|
|
|
UdpNetwork string
|
2018-11-28 12:02:25 +11:00
|
|
|
// If the port is zero, it's assumed to be the same as the Request.Port.
|
2018-03-29 14:29:15 +11:00
|
|
|
ClientIp4 krpc.NodeAddr
|
2018-11-28 12:02:25 +11:00
|
|
|
// If the port is zero, it's assumed to be the same as the Request.Port.
|
2018-03-29 14:29:15 +11:00
|
|
|
ClientIp6 krpc.NodeAddr
|
2018-11-28 12:02:12 +11:00
|
|
|
Context context.Context
|
2013-11-07 01:55:29 +11:00
|
|
|
}
|
|
|
|
|
2020-12-21 09:29:20 +11:00
|
|
|
// The code *is* the documentation.
|
|
|
|
const DefaultTrackerAnnounceTimeout = 15 * time.Second
|
|
|
|
|
2018-02-19 16:19:18 +11:00
|
|
|
func (me Announce) Do() (res AnnounceResponse, err error) {
|
|
|
|
_url, err := url.Parse(me.TrackerUrl)
|
2013-12-16 18:47:23 +11:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-01 10:45:05 +10: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 11:45:47 +10:00
|
|
|
// override it by providing their own Context. See comments elsewhere about longer timeouts
|
|
|
|
// acting as rate limiting overloaded trackers.
|
2020-12-21 09:29:20 +11:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout)
|
2020-10-01 10:45:05 +10:00
|
|
|
defer cancel()
|
|
|
|
me.Context = ctx
|
|
|
|
}
|
2016-05-20 00:48:46 +10:00
|
|
|
switch _url.Scheme {
|
|
|
|
case "http", "https":
|
2018-02-19 16:19:18 +11:00
|
|
|
return announceHTTP(me, _url)
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
return announceUDP(me, _url)
|
2016-05-20 00:48:46 +10:00
|
|
|
default:
|
2013-12-14 22:21:45 +11:00
|
|
|
err = ErrBadScheme
|
|
|
|
return
|
|
|
|
}
|
2016-02-07 18:06:13 +11:00
|
|
|
}
|