]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
tracker: Allow resolving announce URL host in advance, and passing the desired Host...
[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         Event      AnnounceEvent
17         IPAddress  int32
18         Key        int32
19         NumWant    int32 // How many peer addresses are desired. -1 for default.
20         Port       uint16
21 } // 82 bytes
22
23 type AnnounceResponse struct {
24         Interval int32 // Minimum seconds the local peer should wait before next announce.
25         Leechers int32
26         Seeders  int32
27         Peers    []Peer
28 }
29
30 type AnnounceEvent int32
31
32 func (e AnnounceEvent) String() string {
33         // See BEP 3, "event".
34         return []string{"empty", "completed", "started", "stopped"}[e]
35 }
36
37 type Peer struct {
38         IP   net.IP
39         Port int
40 }
41
42 const (
43         None      AnnounceEvent = iota
44         Completed               // The local peer just completed the torrent.
45         Started                 // The local peer has just resumed this torrent.
46         Stopped                 // The local peer is leaving the swarm.
47 )
48
49 var (
50         ErrBadScheme = errors.New("unknown scheme")
51 )
52
53 func Announce(urlStr string, req *AnnounceRequest) (res AnnounceResponse, err error) {
54         return AnnounceHost(urlStr, req, "")
55 }
56
57 func AnnounceHost(urlStr string, req *AnnounceRequest, host string) (res AnnounceResponse, err error) {
58         _url, err := url.Parse(urlStr)
59         if err != nil {
60                 return
61         }
62         switch _url.Scheme {
63         case "http", "https":
64                 return announceHTTP(req, _url, host)
65         case "udp":
66                 return announceUDP(req, _url)
67         default:
68                 err = ErrBadScheme
69                 return
70         }
71 }