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