16 "github.com/anacrolix/missinggo/httptoo"
17 "github.com/anacrolix/torrent/bencode"
18 "github.com/anacrolix/torrent/tracker/shared"
19 "github.com/anacrolix/torrent/tracker/udp"
20 "github.com/anacrolix/torrent/version"
23 var vars = expvar.NewMap("tracker/http")
25 func setAnnounceParams(_url *url.URL, ar *AnnounceRequest, opts AnnounceOpt) {
28 q.Set("key", strconv.FormatInt(int64(ar.Key), 10))
29 q.Set("info_hash", string(ar.InfoHash[:]))
30 q.Set("peer_id", string(ar.PeerId[:]))
31 // AFAICT, port is mandatory, and there's no implied port key.
32 q.Set("port", fmt.Sprintf("%d", ar.Port))
33 q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10))
34 q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10))
36 // The AWS S3 tracker returns "400 Bad Request: left(-1) was not in the valid range 0 -
37 // 9223372036854775807" if left is out of range, or "500 Internal Server Error: Internal Server
38 // Error" if omitted entirely.
43 q.Set("left", strconv.FormatInt(left, 10))
45 if ar.Event != shared.None {
46 q.Set("event", ar.Event.String())
48 // http://stackoverflow.com/questions/17418004/why-does-tracker-server-not-understand-my-request-bittorrent-protocol
50 // According to https://wiki.vuze.com/w/Message_Stream_Encryption. TODO:
51 // Take EncryptionPolicy or something like it as a parameter.
52 q.Set("supportcrypto", "1")
53 doIp := func(versionKey string, ip net.IP) {
57 ipString := ip.String()
58 q.Set(versionKey, ipString)
59 // Let's try listing them. BEP 3 mentions having an "ip" param, and BEP 7 says we can list
60 // addresses for other address-families, although it's not encouraged.
63 doIp("ipv4", opts.ClientIp4)
64 doIp("ipv6", opts.ClientIp6)
65 // We're operating purely on query-escaped strings, where + would have already been encoded to
66 // %2B, and + has no other special meaning. See https://github.com/anacrolix/torrent/issues/534.
67 qstr := strings.ReplaceAll(q.Encode(), "+", "%20")
69 // Some private trackers require the original query param to be in the first position.
70 if _url.RawQuery != "" {
71 _url.RawQuery += "&" + qstr
77 type AnnounceOpt struct {
84 type AnnounceRequest = udp.AnnounceRequest
86 func (cl Client) Announce(ctx context.Context, ar AnnounceRequest, opt AnnounceOpt) (ret AnnounceResponse, err error) {
87 _url := httptoo.CopyURL(cl.url_)
88 setAnnounceParams(_url, &ar, opt)
89 req, err := http.NewRequestWithContext(ctx, http.MethodGet, _url.String(), nil)
90 userAgent := opt.UserAgent
92 userAgent = version.DefaultHttpUserAgent
95 req.Header.Set("User-Agent", userAgent)
97 req.Host = opt.HostHeader
98 resp, err := cl.hc.Do(req)
102 defer resp.Body.Close()
104 io.Copy(&buf, resp.Body)
105 if resp.StatusCode != 200 {
106 err = fmt.Errorf("response from tracker: %s: %s", resp.Status, buf.String())
109 var trackerResponse HttpResponse
110 err = bencode.Unmarshal(buf.Bytes(), &trackerResponse)
111 if _, ok := err.(bencode.ErrUnusedTrailingBytes); ok {
113 } else if err != nil {
114 err = fmt.Errorf("error decoding %q: %s", buf.Bytes(), err)
117 if trackerResponse.FailureReason != "" {
118 err = fmt.Errorf("tracker gave failure reason: %q", trackerResponse.FailureReason)
121 vars.Add("successful http announces", 1)
122 ret.Interval = trackerResponse.Interval
123 ret.Leechers = trackerResponse.Incomplete
124 ret.Seeders = trackerResponse.Complete
125 if len(trackerResponse.Peers) != 0 {
126 vars.Add("http responses with nonempty peers key", 1)
128 ret.Peers = trackerResponse.Peers
129 if len(trackerResponse.Peers6) != 0 {
130 vars.Add("http responses with nonempty peers6 key", 1)
132 for _, na := range trackerResponse.Peers6 {
133 ret.Peers = append(ret.Peers, Peer{
141 type AnnounceResponse struct {
142 Interval int32 // Minimum seconds the local peer should wait before next announce.