2015-03-27 04:12:15 +00:00
|
|
|
package tracker
|
2015-03-26 06:20:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2018-02-11 04:13:00 +00:00
|
|
|
"github.com/anacrolix/dht/krpc"
|
2016-05-19 14:48:46 +00:00
|
|
|
"github.com/anacrolix/missinggo/httptoo"
|
|
|
|
|
2015-04-27 04:55:01 +00:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-26 06:20:31 +00:00
|
|
|
)
|
|
|
|
|
2016-02-07 06:45:46 +00:00
|
|
|
type httpResponse struct {
|
2018-02-12 13:23:07 +00:00
|
|
|
FailureReason string `bencode:"failure reason"`
|
|
|
|
Interval int32 `bencode:"interval"`
|
|
|
|
TrackerId string `bencode:"tracker id"`
|
|
|
|
Complete int32 `bencode:"complete"`
|
|
|
|
Incomplete int32 `bencode:"incomplete"`
|
|
|
|
Peers Peers `bencode:"peers"`
|
|
|
|
// BEP 7
|
|
|
|
Peers6 krpc.CompactIPv6NodeAddrs `bencode:"peers6"`
|
2015-03-26 06:20:31 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:23:07 +00:00
|
|
|
type Peers []Peer
|
|
|
|
|
|
|
|
func (me *Peers) UnmarshalBencode(b []byte) (err error) {
|
|
|
|
var _v interface{}
|
|
|
|
err = bencode.Unmarshal(b, &_v)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch v := _v.(type) {
|
2016-11-22 04:40:46 +00:00
|
|
|
case string:
|
2018-02-12 13:23:07 +00:00
|
|
|
vars.Add("http responses with string peers", 1)
|
|
|
|
var cnas krpc.CompactIPv4NodeAddrs
|
|
|
|
err = cnas.UnmarshalBinary([]byte(v))
|
2016-11-22 04:40:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-12 13:23:07 +00:00
|
|
|
for _, cp := range cnas {
|
|
|
|
*me = append(*me, Peer{
|
2016-11-22 04:40:46 +00:00
|
|
|
IP: cp.IP[:],
|
|
|
|
Port: int(cp.Port),
|
|
|
|
})
|
|
|
|
}
|
2015-03-26 06:20:31 +00:00
|
|
|
return
|
2016-11-22 04:40:46 +00:00
|
|
|
case []interface{}:
|
2018-02-12 13:23:07 +00:00
|
|
|
vars.Add("http responses with list peers", 1)
|
2016-11-22 04:40:46 +00:00
|
|
|
for _, i := range v {
|
|
|
|
var p Peer
|
|
|
|
p.fromDictInterface(i.(map[string]interface{}))
|
2018-02-12 13:23:07 +00:00
|
|
|
*me = append(*me, p)
|
2016-11-22 04:40:46 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
2018-02-12 13:23:07 +00:00
|
|
|
vars.Add("http responses with unhandled peers type", 1)
|
|
|
|
err = fmt.Errorf("unsupported type: %T", _v)
|
2015-03-26 06:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:48:46 +00:00
|
|
|
func setAnnounceParams(_url *url.URL, ar *AnnounceRequest) {
|
|
|
|
q := _url.Query()
|
2016-02-14 20:15:51 +00:00
|
|
|
|
2015-03-26 06:20:31 +00:00
|
|
|
q.Set("info_hash", string(ar.InfoHash[:]))
|
|
|
|
q.Set("peer_id", string(ar.PeerId[:]))
|
|
|
|
q.Set("port", fmt.Sprintf("%d", ar.Port))
|
|
|
|
q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10))
|
|
|
|
q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10))
|
2015-03-27 06:22:00 +00:00
|
|
|
q.Set("left", strconv.FormatUint(ar.Left, 10))
|
2015-03-27 04:12:15 +00:00
|
|
|
if ar.Event != None {
|
2015-03-26 06:20:31 +00:00
|
|
|
q.Set("event", ar.Event.String())
|
|
|
|
}
|
|
|
|
// http://stackoverflow.com/questions/17418004/why-does-tracker-server-not-understand-my-request-bittorrent-protocol
|
|
|
|
q.Set("compact", "1")
|
2018-02-13 00:18:23 +00:00
|
|
|
// According to https://wiki.vuze.com/w/Message_Stream_Encryption. TODO:
|
|
|
|
// Take EncryptionPolicy or something like it as a parameter.
|
2015-03-27 06:22:42 +00:00
|
|
|
q.Set("supportcrypto", "1")
|
2016-05-19 14:48:46 +00:00
|
|
|
|
|
|
|
_url.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
|
2017-12-28 08:47:51 +00:00
|
|
|
func announceHTTP(cl *http.Client, userAgent string, ar *AnnounceRequest, _url *url.URL, host string) (ret AnnounceResponse, err error) {
|
2016-05-19 14:48:46 +00:00
|
|
|
_url = httptoo.CopyURL(_url)
|
|
|
|
setAnnounceParams(_url, ar)
|
|
|
|
req, err := http.NewRequest("GET", _url.String(), nil)
|
2017-12-28 08:47:51 +00:00
|
|
|
req.Header.Set("User-Agent", userAgent)
|
2016-05-19 14:48:46 +00:00
|
|
|
req.Host = host
|
2017-11-07 18:14:13 +00:00
|
|
|
resp, err := cl.Do(req)
|
2015-03-26 06:20:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-05-19 14:48:46 +00:00
|
|
|
var buf bytes.Buffer
|
2015-03-26 06:20:31 +00:00
|
|
|
io.Copy(&buf, resp.Body)
|
2015-03-27 06:18:45 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("response from tracker: %s: %s", resp.Status, buf.String())
|
|
|
|
return
|
|
|
|
}
|
2016-02-07 06:45:46 +00:00
|
|
|
var trackerResponse httpResponse
|
2015-03-27 06:18:45 +00:00
|
|
|
err = bencode.Unmarshal(buf.Bytes(), &trackerResponse)
|
2015-03-26 06:20:31 +00:00
|
|
|
if err != nil {
|
2015-03-27 06:18:45 +00:00
|
|
|
err = fmt.Errorf("error decoding %q: %s", buf.Bytes(), err)
|
2015-03-26 06:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if trackerResponse.FailureReason != "" {
|
|
|
|
err = errors.New(trackerResponse.FailureReason)
|
|
|
|
return
|
|
|
|
}
|
2018-02-12 13:23:07 +00:00
|
|
|
vars.Add("successful http announces", 1)
|
2015-03-26 06:20:31 +00:00
|
|
|
ret.Interval = trackerResponse.Interval
|
|
|
|
ret.Leechers = trackerResponse.Incomplete
|
|
|
|
ret.Seeders = trackerResponse.Complete
|
2018-02-12 13:23:07 +00:00
|
|
|
if len(trackerResponse.Peers) != 0 {
|
|
|
|
vars.Add("http responses with nonempty peers key", 1)
|
|
|
|
}
|
|
|
|
ret.Peers = trackerResponse.Peers
|
|
|
|
if len(trackerResponse.Peers6) != 0 {
|
|
|
|
vars.Add("http responses with nonempty peers6 key", 1)
|
|
|
|
}
|
|
|
|
for _, na := range trackerResponse.Peers6 {
|
|
|
|
ret.Peers = append(ret.Peers, Peer{
|
|
|
|
IP: na.IP,
|
|
|
|
Port: na.Port,
|
|
|
|
})
|
|
|
|
}
|
2015-03-26 06:20:31 +00:00
|
|
|
return
|
|
|
|
}
|