]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
Fix TLS handshake failures with https trackers
[btrtrc.git] / tracker / tracker.go
1 package tracker
2
3 import (
4         "errors"
5         "net/url"
6
7         "github.com/anacrolix/dht/krpc"
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 uint32
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 const (
41         None      AnnounceEvent = iota
42         Completed               // The local peer just completed the torrent.
43         Started                 // The local peer has just resumed this torrent.
44         Stopped                 // The local peer is leaving the swarm.
45 )
46
47 var (
48         ErrBadScheme = errors.New("unknown scheme")
49 )
50
51 type Announce struct {
52         TrackerUrl string
53         Request    AnnounceRequest
54         HostHeader string
55         ServerName string
56         UserAgent  string
57         UdpNetwork string
58         // If the port is zero, it's assumed to be the same as the Request.Port
59         ClientIp4 krpc.NodeAddr
60         // If the port is zero, it's assumed to be the same as the Request.Port
61         ClientIp6 krpc.NodeAddr
62 }
63
64 // In an FP language with currying, what order what you put these params?
65
66 func (me Announce) Do() (res AnnounceResponse, err error) {
67         _url, err := url.Parse(me.TrackerUrl)
68         if err != nil {
69                 return
70         }
71         switch _url.Scheme {
72         case "http", "https":
73                 return announceHTTP(me, _url)
74         case "udp", "udp4", "udp6":
75                 return announceUDP(me, _url)
76         default:
77                 err = ErrBadScheme
78                 return
79         }
80 }