]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/tracker.go
reverting usage of proxy for http requests
[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         HTTPProxy  func(*http.Request) (*url.URL, error)
57         ServerName string
58         UserAgent  string
59         UdpNetwork string
60         // If the port is zero, it's assumed to be the same as the Request.Port
61         ClientIp4 krpc.NodeAddr
62         // If the port is zero, it's assumed to be the same as the Request.Port
63         ClientIp6 krpc.NodeAddr
64 }
65
66 // In an FP language with currying, what order what you put these params?
67
68 func (me Announce) Do() (res AnnounceResponse, err error) {
69         _url, err := url.Parse(me.TrackerUrl)
70         if err != nil {
71                 return
72         }
73         switch _url.Scheme {
74         case "http", "https":
75                 return announceHTTP(me, _url)
76         case "udp", "udp4", "udp6":
77                 return announceUDP(me, _url)
78         default:
79                 err = ErrBadScheme
80                 return
81         }
82 }