FedP2P/tracker/tracker.go

95 lines
2.5 KiB
Go
Raw Normal View History

package tracker
import (
"context"
"errors"
"net/http"
"net/url"
"time"
2018-02-19 13:19:18 +08:00
2019-08-10 16:46:07 +08:00
"github.com/anacrolix/dht/v2/krpc"
)
// Marshalled as binary by the UDP client, so be careful making changes.
type AnnounceRequest struct {
InfoHash [20]byte
PeerId [20]byte
Downloaded int64
2019-07-17 16:12:11 +08:00
Left int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead.
Uploaded int64
// Apparently this is optional. None can be used for announces done at
// regular intervals.
Event AnnounceEvent
IPAddress uint32
Key int32
NumWant int32 // How many peer addresses are desired. -1 for default.
Port uint16
2015-03-12 17:07:10 +08:00
} // 82 bytes
type AnnounceResponse struct {
2013-12-16 15:46:55 +08:00
Interval int32 // Minimum seconds the local peer should wait before next announce.
Leechers int32
Seeders int32
Peers []Peer
}
type AnnounceEvent int32
func (e AnnounceEvent) String() string {
// See BEP 3, "event", and https://github.com/anacrolix/torrent/issues/416#issuecomment-751427001.
return []string{"", "completed", "started", "stopped"}[e]
2015-03-26 14:20:31 +08:00
}
const (
2013-12-16 15:46:55 +08: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.
)
var (
ErrBadScheme = errors.New("unknown scheme")
)
2018-02-19 13:19:18 +08:00
type Announce struct {
TrackerUrl string
Request AnnounceRequest
HostHeader string
HTTPProxy func(*http.Request) (*url.URL, error)
ServerName string
2018-02-19 13:19:18 +08:00
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.
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.
ClientIp6 krpc.NodeAddr
Context context.Context
}
// 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) {
_url, err := url.Parse(me.TrackerUrl)
if err != nil {
return
}
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
}
switch _url.Scheme {
case "http", "https":
2018-02-19 13:19:18 +08:00
return announceHTTP(me, _url)
case "udp", "udp4", "udp6":
return announceUDP(me, _url)
default:
err = ErrBadScheme
return
}
}