2016-05-22 12:45:08 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-07-12 06:44:06 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-05-22 12:45:08 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Announces a torrent to a tracker at regular intervals, when peers are
|
|
|
|
// required.
|
|
|
|
type trackerScraper struct {
|
|
|
|
url string
|
|
|
|
// Causes the trackerScraper to stop running.
|
2016-07-12 06:44:06 +00:00
|
|
|
stop missinggo.Event
|
|
|
|
t *Torrent
|
|
|
|
lastAnnounce trackerAnnounceResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *trackerScraper) statusLine() string {
|
|
|
|
var w bytes.Buffer
|
|
|
|
fmt.Fprintf(&w, "%q\t%s\t%s",
|
|
|
|
ts.url,
|
|
|
|
func() string {
|
2017-08-16 15:18:18 +00:00
|
|
|
na := time.Until(ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval))
|
2016-07-12 06:44:06 +00:00
|
|
|
if na > 0 {
|
2017-08-16 15:18:18 +00:00
|
|
|
na /= time.Second
|
|
|
|
na *= time.Second
|
2016-07-12 06:44:06 +00:00
|
|
|
return na.String()
|
|
|
|
} else {
|
|
|
|
return "anytime"
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() string {
|
|
|
|
if ts.lastAnnounce.Err != nil {
|
|
|
|
return ts.lastAnnounce.Err.Error()
|
|
|
|
}
|
2016-07-14 03:08:52 +00:00
|
|
|
if ts.lastAnnounce.Completed.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
2016-07-12 06:44:06 +00:00
|
|
|
return fmt.Sprintf("%d peers", ts.lastAnnounce.NumPeers)
|
|
|
|
}())
|
|
|
|
return w.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type trackerAnnounceResult struct {
|
|
|
|
Err error
|
|
|
|
NumPeers int
|
|
|
|
Interval time.Duration
|
|
|
|
Completed time.Time
|
2016-05-22 12:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func trackerToTorrentPeers(ps []tracker.Peer) (ret []Peer) {
|
|
|
|
ret = make([]Peer, 0, len(ps))
|
|
|
|
for _, p := range ps {
|
|
|
|
ret = append(ret, Peer{
|
|
|
|
IP: p.IP,
|
|
|
|
Port: p.Port,
|
|
|
|
Source: peerSourceTracker,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-23 01:00:49 +00:00
|
|
|
// Return how long to wait before trying again. For most errors, we return 5
|
|
|
|
// minutes, a relatively quick turn around for DNS changes.
|
2016-07-12 06:44:06 +00:00
|
|
|
func (me *trackerScraper) announce() (ret trackerAnnounceResult) {
|
|
|
|
defer func() {
|
|
|
|
ret.Completed = time.Now()
|
|
|
|
}()
|
|
|
|
ret.Interval = 5 * time.Minute
|
2016-05-22 12:45:08 +00:00
|
|
|
blocked, urlToUse, host, err := me.t.cl.prepareTrackerAnnounceUnlocked(me.url)
|
2016-05-23 01:00:49 +00:00
|
|
|
if err != nil {
|
2016-07-12 06:44:06 +00:00
|
|
|
ret.Err = err
|
|
|
|
return
|
2016-05-23 01:00:49 +00:00
|
|
|
}
|
2016-05-22 12:45:08 +00:00
|
|
|
if blocked {
|
2016-07-12 06:44:06 +00:00
|
|
|
ret.Err = errors.New("blocked by IP")
|
|
|
|
return
|
2016-05-22 12:45:08 +00:00
|
|
|
}
|
|
|
|
me.t.cl.mu.Lock()
|
|
|
|
req := me.t.announceRequest()
|
|
|
|
me.t.cl.mu.Unlock()
|
2017-12-28 08:47:51 +00:00
|
|
|
res, err := tracker.AnnounceHost(me.t.cl.config.HTTP, me.t.cl.config.HTTPUserAgent, urlToUse, &req, host)
|
2016-05-22 12:45:08 +00:00
|
|
|
if err != nil {
|
2016-07-12 06:44:06 +00:00
|
|
|
ret.Err = err
|
|
|
|
return
|
2016-05-22 12:45:08 +00:00
|
|
|
}
|
|
|
|
me.t.AddPeers(trackerToTorrentPeers(res.Peers))
|
2016-07-12 06:44:06 +00:00
|
|
|
ret.NumPeers = len(res.Peers)
|
|
|
|
ret.Interval = time.Duration(res.Interval) * time.Second
|
|
|
|
return
|
2016-05-22 12:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *trackerScraper) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-me.t.closed.LockedChan(&me.t.cl.mu):
|
|
|
|
return
|
|
|
|
case <-me.stop.LockedChan(&me.t.cl.mu):
|
|
|
|
return
|
|
|
|
case <-me.t.wantPeersEvent.LockedChan(&me.t.cl.mu):
|
|
|
|
}
|
|
|
|
|
2016-07-12 06:44:06 +00:00
|
|
|
ar := me.announce()
|
|
|
|
me.t.cl.mu.Lock()
|
|
|
|
me.lastAnnounce = ar
|
|
|
|
me.t.cl.mu.Unlock()
|
|
|
|
|
2017-11-07 05:11:59 +00:00
|
|
|
intervalChan := time.After(time.Until(ar.Completed.Add(ar.Interval)))
|
2016-05-22 12:45:08 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-me.t.closed.LockedChan(&me.t.cl.mu):
|
|
|
|
return
|
|
|
|
case <-me.stop.LockedChan(&me.t.cl.mu):
|
|
|
|
return
|
|
|
|
case <-intervalChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|