2013-11-06 14:55:29 +00:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
2013-12-14 11:21:45 +00:00
|
|
|
"errors"
|
2013-11-06 14:55:29 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2015-03-27 06:22:00 +00:00
|
|
|
// Marshalled as binary by the UDP client, so be careful making changes.
|
2013-11-06 14:55:29 +00:00
|
|
|
type AnnounceRequest struct {
|
|
|
|
InfoHash [20]byte
|
|
|
|
PeerId [20]byte
|
|
|
|
Downloaded int64
|
2015-03-27 06:22:00 +00:00
|
|
|
Left uint64
|
2013-11-06 14:55:29 +00:00
|
|
|
Uploaded int64
|
|
|
|
Event AnnounceEvent
|
|
|
|
IPAddress int32
|
|
|
|
Key int32
|
2013-12-16 07:46:55 +00:00
|
|
|
NumWant int32 // How many peer addresses are desired. -1 for default.
|
2015-03-27 06:22:00 +00:00
|
|
|
Port uint16
|
2015-03-12 09:07:10 +00:00
|
|
|
} // 82 bytes
|
2013-11-06 14:55:29 +00:00
|
|
|
|
|
|
|
type AnnounceResponse struct {
|
2013-12-16 07:46:55 +00:00
|
|
|
Interval int32 // Minimum seconds the local peer should wait before next announce.
|
2013-11-06 14:55:29 +00:00
|
|
|
Leechers int32
|
|
|
|
Seeders int32
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnnounceEvent int32
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (e AnnounceEvent) String() string {
|
2015-03-26 06:20:31 +00:00
|
|
|
// See BEP 3, "event".
|
2016-04-19 04:11:11 +00:00
|
|
|
return []string{"empty", "completed", "started", "stopped"}[e]
|
2015-03-26 06:20:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 14:55:29 +00:00
|
|
|
type Peer struct {
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2013-12-16 07:46:55 +00: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-06 14:55:29 +00:00
|
|
|
)
|
|
|
|
|
2016-02-07 07:06:13 +00:00
|
|
|
type client interface {
|
2013-12-16 07:46:55 +00:00
|
|
|
// Returns ErrNotConnected if Connect needs to be called.
|
2013-11-06 14:55:29 +00:00
|
|
|
Announce(*AnnounceRequest) (AnnounceResponse, error)
|
2013-12-14 11:21:45 +00:00
|
|
|
Connect() error
|
2014-05-20 14:52:49 +00:00
|
|
|
String() string
|
2014-11-21 05:39:56 +00:00
|
|
|
URL() string
|
2016-02-07 07:06:13 +00:00
|
|
|
Close() error
|
2013-11-06 14:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-14 11:21:45 +00:00
|
|
|
var (
|
|
|
|
ErrNotConnected = errors.New("not connected")
|
|
|
|
ErrBadScheme = errors.New("unknown scheme")
|
|
|
|
|
2016-02-07 07:06:13 +00:00
|
|
|
schemes = make(map[string]func(*url.URL) client)
|
2013-12-14 11:21:45 +00:00
|
|
|
)
|
2013-11-06 14:55:29 +00:00
|
|
|
|
2016-02-07 07:06:13 +00:00
|
|
|
func registerClientScheme(scheme string, newFunc func(*url.URL) client) {
|
2013-12-14 11:21:45 +00:00
|
|
|
schemes[scheme] = newFunc
|
2013-11-06 14:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 07:47:23 +00:00
|
|
|
// Returns ErrBadScheme if the tracker scheme isn't recognised.
|
2016-02-07 07:06:13 +00:00
|
|
|
func new(rawurl string) (cl client, err error) {
|
2013-12-16 07:47:23 +00:00
|
|
|
url_s, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newFunc, ok := schemes[url_s.Scheme]
|
2013-12-14 11:21:45 +00:00
|
|
|
if !ok {
|
|
|
|
err = ErrBadScheme
|
|
|
|
return
|
|
|
|
}
|
2013-12-16 07:47:23 +00:00
|
|
|
cl = newFunc(url_s)
|
2013-12-14 11:21:45 +00:00
|
|
|
return
|
2013-11-06 14:55:29 +00:00
|
|
|
}
|
2016-02-07 07:06:13 +00:00
|
|
|
|
|
|
|
func Announce(urlStr string, req *AnnounceRequest) (res AnnounceResponse, err error) {
|
|
|
|
cl, err := new(urlStr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cl.Close()
|
|
|
|
err = cl.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cl.Announce(req)
|
|
|
|
|
|
|
|
}
|