]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
Expose public IPv4 and IPv6 config options and use them with trackers
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net/http"
6         "net/url"
7
8         "github.com/anacrolix/dht/krpc"
9 )
10
11 // Marshalled as binary by the UDP client, so be careful making changes.
12 type AnnounceRequest struct {
13         InfoHash   [20]byte
14         PeerId     [20]byte
15         Downloaded int64
16         Left       uint64
17         Uploaded   int64
18         // Apparently this is optional. None can be used for announces done at
19         // regular intervals.
20         Event     AnnounceEvent
21         IPAddress uint32
22         Key       int32
23         NumWant   int32 // How many peer addresses are desired. -1 for default.
24         Port      uint16
25 } // 82 bytes
26
27 type AnnounceResponse struct {
28         Interval int32 // Minimum seconds the local peer should wait before next announce.
29         Leechers int32
30         Seeders  int32
31         Peers    []Peer
32 }
33
34 type AnnounceEvent int32
35
36 func (e AnnounceEvent) String() string {
37         // See BEP 3, "event".
38         return []string{"empty", "completed", "started", "stopped"}[e]
39 }
40
41 const (
42         None      AnnounceEvent = iota
43         Completed               // The local peer just completed the torrent.
44         Started                 // The local peer has just resumed this torrent.
45         Stopped                 // The local peer is leaving the swarm.
46 )
47
48 var (
49         ErrBadScheme = errors.New("unknown scheme")
50 )
51
52 type Announce struct {
53         TrackerUrl string
54         Request    AnnounceRequest
55         HostHeader string
56         UserAgent  string
57         HttpClient *http.Client
58         UdpNetwork string
59         // If the port is zero, it's assumed to be the same as the Request.Port
60         ClientIp4 krpc.NodeAddr
61         // If the port is zero, it's assumed to be the same as the Request.Port
62         ClientIp6 krpc.NodeAddr
63 }
64
65 // In an FP language with currying, what order what you put these params?
66
67 func (me Announce) Do() (res AnnounceResponse, err error) {
68         _url, err := url.Parse(me.TrackerUrl)
69         if err != nil {
70                 return
71         }
72         switch _url.Scheme {
73         case "http", "https":
74                 return announceHTTP(me, _url)
75         case "udp", "udp4", "udp6":
76                 return announceUDP(me, _url)
77         default:
78                 err = ErrBadScheme
79                 return
80         }
81 }