2015-03-27 12:12:15 +08:00
|
|
|
package tracker
|
2015-03-26 14:20:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-18 08:09:56 +08:00
|
|
|
"crypto/tls"
|
2015-03-26 14:20:31 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-07-17 16:12:11 +08:00
|
|
|
"math"
|
2018-10-18 08:09:56 +08:00
|
|
|
"net"
|
2015-03-26 14:20:31 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2018-10-18 08:09:56 +08:00
|
|
|
"time"
|
2015-03-26 14:20:31 +08:00
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2016-05-19 22:48:46 +08:00
|
|
|
"github.com/anacrolix/missinggo/httptoo"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-26 14:20:31 +08:00
|
|
|
)
|
|
|
|
|
2019-11-20 13:31:54 +08:00
|
|
|
type HttpResponse struct {
|
2018-02-12 21:23:07 +08: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 14:20:31 +08:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:23:07 +08: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 12:40:46 +08:00
|
|
|
case string:
|
2018-02-12 21:23:07 +08:00
|
|
|
vars.Add("http responses with string peers", 1)
|
|
|
|
var cnas krpc.CompactIPv4NodeAddrs
|
|
|
|
err = cnas.UnmarshalBinary([]byte(v))
|
2016-11-22 12:40:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-12 21:23:07 +08:00
|
|
|
for _, cp := range cnas {
|
|
|
|
*me = append(*me, Peer{
|
2016-11-22 12:40:46 +08:00
|
|
|
IP: cp.IP[:],
|
|
|
|
Port: int(cp.Port),
|
|
|
|
})
|
|
|
|
}
|
2015-03-26 14:20:31 +08:00
|
|
|
return
|
2016-11-22 12:40:46 +08:00
|
|
|
case []interface{}:
|
2018-02-12 21:23:07 +08:00
|
|
|
vars.Add("http responses with list peers", 1)
|
2016-11-22 12:40:46 +08:00
|
|
|
for _, i := range v {
|
|
|
|
var p Peer
|
2019-11-15 07:22:59 +08:00
|
|
|
p.FromDictInterface(i.(map[string]interface{}))
|
2018-02-12 21:23:07 +08:00
|
|
|
*me = append(*me, p)
|
2016-11-22 12:40:46 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
2018-02-12 21:23:07 +08:00
|
|
|
vars.Add("http responses with unhandled peers type", 1)
|
|
|
|
err = fmt.Errorf("unsupported type: %T", _v)
|
2015-03-26 14:20:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 13:19:18 +08:00
|
|
|
func setAnnounceParams(_url *url.URL, ar *AnnounceRequest, opts Announce) {
|
2016-05-19 22:48:46 +08:00
|
|
|
q := _url.Query()
|
2016-02-15 04:15:51 +08:00
|
|
|
|
2015-03-26 14:20:31 +08:00
|
|
|
q.Set("info_hash", string(ar.InfoHash[:]))
|
|
|
|
q.Set("peer_id", string(ar.PeerId[:]))
|
2018-08-13 18:27:42 +08:00
|
|
|
// AFAICT, port is mandatory, and there's no implied port key.
|
2015-03-26 14:20:31 +08:00
|
|
|
q.Set("port", fmt.Sprintf("%d", ar.Port))
|
|
|
|
q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10))
|
|
|
|
q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10))
|
2019-07-17 16:12:11 +08:00
|
|
|
|
|
|
|
// The AWS S3 tracker returns "400 Bad Request: left(-1) was not in the valid range 0 -
|
|
|
|
// 9223372036854775807" if left is out of range, or "500 Internal Server Error: Internal Server
|
|
|
|
// Error" if omitted entirely.
|
|
|
|
left := ar.Left
|
|
|
|
if left < 0 {
|
|
|
|
left = math.MaxInt64
|
|
|
|
}
|
|
|
|
q.Set("left", strconv.FormatInt(left, 10))
|
|
|
|
|
2015-03-27 12:12:15 +08:00
|
|
|
if ar.Event != None {
|
2015-03-26 14:20:31 +08: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 08:18:23 +08:00
|
|
|
// According to https://wiki.vuze.com/w/Message_Stream_Encryption. TODO:
|
|
|
|
// Take EncryptionPolicy or something like it as a parameter.
|
2015-03-27 14:22:42 +08:00
|
|
|
q.Set("supportcrypto", "1")
|
2018-02-19 13:19:18 +08:00
|
|
|
if opts.ClientIp4.IP != nil {
|
|
|
|
q.Set("ipv4", opts.ClientIp4.String())
|
|
|
|
}
|
|
|
|
if opts.ClientIp6.IP != nil {
|
|
|
|
q.Set("ipv6", opts.ClientIp6.String())
|
|
|
|
}
|
2016-05-19 22:48:46 +08:00
|
|
|
_url.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
|
2018-02-19 13:19:18 +08:00
|
|
|
func announceHTTP(opt Announce, _url *url.URL) (ret AnnounceResponse, err error) {
|
2016-05-19 22:48:46 +08:00
|
|
|
_url = httptoo.CopyURL(_url)
|
2018-02-19 13:19:18 +08:00
|
|
|
setAnnounceParams(_url, &opt.Request, opt)
|
2016-05-19 22:48:46 +08:00
|
|
|
req, err := http.NewRequest("GET", _url.String(), nil)
|
2018-02-19 13:19:18 +08:00
|
|
|
req.Header.Set("User-Agent", opt.UserAgent)
|
|
|
|
req.Host = opt.HostHeader
|
2018-11-28 09:02:12 +08:00
|
|
|
if opt.Context != nil {
|
|
|
|
req = req.WithContext(opt.Context)
|
|
|
|
}
|
2018-10-18 08:09:56 +08:00
|
|
|
resp, err := (&http.Client{
|
|
|
|
Timeout: time.Second * 15,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 15 * time.Second,
|
|
|
|
}).Dial,
|
2018-10-31 06:32:33 +08:00
|
|
|
Proxy: opt.HTTPProxy,
|
2018-10-18 08:09:56 +08:00
|
|
|
TLSHandshakeTimeout: 15 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: opt.ServerName,
|
|
|
|
},
|
2020-04-28 07:15:06 +08:00
|
|
|
DisableKeepAlives: true,
|
2018-10-18 08:09:56 +08:00
|
|
|
},
|
|
|
|
}).Do(req)
|
2015-03-26 14:20:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-05-19 22:48:46 +08:00
|
|
|
var buf bytes.Buffer
|
2015-03-26 14:20:31 +08:00
|
|
|
io.Copy(&buf, resp.Body)
|
2015-03-27 14:18:45 +08:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("response from tracker: %s: %s", resp.Status, buf.String())
|
|
|
|
return
|
|
|
|
}
|
2019-11-20 13:31:54 +08:00
|
|
|
var trackerResponse HttpResponse
|
2015-03-27 14:18:45 +08:00
|
|
|
err = bencode.Unmarshal(buf.Bytes(), &trackerResponse)
|
2018-08-13 18:24:15 +08:00
|
|
|
if _, ok := err.(bencode.ErrUnusedTrailingBytes); ok {
|
|
|
|
err = nil
|
|
|
|
} else if err != nil {
|
2015-03-27 14:18:45 +08:00
|
|
|
err = fmt.Errorf("error decoding %q: %s", buf.Bytes(), err)
|
2015-03-26 14:20:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if trackerResponse.FailureReason != "" {
|
2018-08-13 18:28:02 +08:00
|
|
|
err = fmt.Errorf("tracker gave failure reason: %q", trackerResponse.FailureReason)
|
2015-03-26 14:20:31 +08:00
|
|
|
return
|
|
|
|
}
|
2018-02-12 21:23:07 +08:00
|
|
|
vars.Add("successful http announces", 1)
|
2015-03-26 14:20:31 +08:00
|
|
|
ret.Interval = trackerResponse.Interval
|
|
|
|
ret.Leechers = trackerResponse.Incomplete
|
|
|
|
ret.Seeders = trackerResponse.Complete
|
2018-02-12 21:23:07 +08: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 14:20:31 +08:00
|
|
|
return
|
|
|
|
}
|