]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
tracker: Support the original http response peers format
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net"
6         "net/url"
7 )
8
9 // Marshalled as binary by the UDP client, so be careful making changes.
10 type AnnounceRequest struct {
11         InfoHash   [20]byte
12         PeerId     [20]byte
13         Downloaded int64
14         Left       uint64
15         Uploaded   int64
16         // Apparently this is optional. None can be used for announces done at
17         // regular intervals.
18         Event     AnnounceEvent
19         IPAddress int32
20         Key       int32
21         NumWant   int32 // How many peer addresses are desired. -1 for default.
22         Port      uint16
23 } // 82 bytes
24
25 type AnnounceResponse struct {
26         Interval int32 // Minimum seconds the local peer should wait before next announce.
27         Leechers int32
28         Seeders  int32
29         Peers    []Peer
30 }
31
32 type AnnounceEvent int32
33
34 func (e AnnounceEvent) String() string {
35         // See BEP 3, "event".
36         return []string{"empty", "completed", "started", "stopped"}[e]
37 }
38
39 type Peer struct {
40         IP   net.IP
41         Port int
42         ID   []byte
43 }
44
45 const (
46         None      AnnounceEvent = iota
47         Completed               // The local peer just completed the torrent.
48         Started                 // The local peer has just resumed this torrent.
49         Stopped                 // The local peer is leaving the swarm.
50 )
51
52 var (
53         ErrBadScheme = errors.New("unknown scheme")
54 )
55
56 func Announce(urlStr string, req *AnnounceRequest) (res AnnounceResponse, err error) {
57         return AnnounceHost(urlStr, req, "")
58 }
59
60 func AnnounceHost(urlStr string, req *AnnounceRequest, host string) (res AnnounceResponse, err error) {
61         _url, err := url.Parse(urlStr)
62         if err != nil {
63                 return
64         }
65         switch _url.Scheme {
66         case "http", "https":
67                 return announceHTTP(req, _url, host)
68         case "udp":
69                 return announceUDP(req, _url)
70         default:
71                 err = ErrBadScheme
72                 return
73         }
74 }