2015-03-27 12:12:15 +08:00
|
|
|
package tracker
|
2015-03-26 14:20:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2016-05-19 22:48:46 +08:00
|
|
|
"github.com/anacrolix/missinggo/httptoo"
|
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-27 12:12:15 +08:00
|
|
|
"github.com/anacrolix/torrent/util"
|
2015-03-26 14:20:31 +08:00
|
|
|
)
|
|
|
|
|
2016-02-07 14:45:46 +08:00
|
|
|
type httpResponse struct {
|
2015-03-26 14:20:31 +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 interface{} `bencode:"peers"`
|
|
|
|
}
|
|
|
|
|
2016-02-07 14:45:46 +08:00
|
|
|
func (r *httpResponse) UnmarshalPeers() (ret []Peer, err error) {
|
2015-03-26 14:20:31 +08:00
|
|
|
s, ok := r.Peers.(string)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("unsupported peers value type: %T", r.Peers)
|
|
|
|
return
|
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
cp, err := util.UnmarshalIPv4CompactPeers([]byte(s))
|
2015-03-26 14:20:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-27 12:12:15 +08:00
|
|
|
ret = make([]Peer, 0, len(cp))
|
2015-03-26 14:20:31 +08:00
|
|
|
for _, p := range cp {
|
2015-03-27 12:12:15 +08:00
|
|
|
ret = append(ret, Peer{net.IP(p.IP[:]), int(p.Port)})
|
2015-03-26 14:20:31 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-19 22:48:46 +08:00
|
|
|
func setAnnounceParams(_url *url.URL, ar *AnnounceRequest) {
|
|
|
|
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[:]))
|
|
|
|
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 14:22:00 +08:00
|
|
|
q.Set("left", strconv.FormatUint(ar.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")
|
2015-03-27 14:22:42 +08:00
|
|
|
// According to https://wiki.vuze.com/w/Message_Stream_Encryption.
|
|
|
|
q.Set("supportcrypto", "1")
|
2016-05-19 22:48:46 +08:00
|
|
|
|
|
|
|
_url.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func announceHTTP(ar *AnnounceRequest, _url *url.URL, host string) (ret AnnounceResponse, err error) {
|
|
|
|
_url = httptoo.CopyURL(_url)
|
|
|
|
setAnnounceParams(_url, ar)
|
|
|
|
req, err := http.NewRequest("GET", _url.String(), nil)
|
|
|
|
req.Host = host
|
|
|
|
resp, err := http.DefaultClient.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
|
|
|
|
}
|
2016-02-07 14:45:46 +08:00
|
|
|
var trackerResponse httpResponse
|
2015-03-27 14:18:45 +08:00
|
|
|
err = bencode.Unmarshal(buf.Bytes(), &trackerResponse)
|
2015-03-26 14:20:31 +08:00
|
|
|
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 != "" {
|
|
|
|
err = errors.New(trackerResponse.FailureReason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret.Interval = trackerResponse.Interval
|
|
|
|
ret.Leechers = trackerResponse.Incomplete
|
|
|
|
ret.Seeders = trackerResponse.Complete
|
|
|
|
ret.Peers, err = trackerResponse.UnmarshalPeers()
|
|
|
|
return
|
|
|
|
}
|