torrent/tracker_scraper.go

159 lines
3.3 KiB
Go
Raw Normal View History

package torrent
import (
"bytes"
"errors"
"fmt"
2018-02-19 05:19:18 +00:00
"net"
"net/url"
"time"
"github.com/anacrolix/dht/krpc"
"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 {
2018-02-19 05:19:18 +00:00
u url.URL
// Causes the trackerScraper to stop running.
stop missinggo.Event
t *Torrent
lastAnnounce trackerAnnounceResult
}
func (ts *trackerScraper) statusLine() string {
var w bytes.Buffer
fmt.Fprintf(&w, "%q\t%s\t%s",
2018-02-19 05:19:18 +00:00
ts.u.String(),
func() string {
na := time.Until(ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval))
if na > 0 {
na /= time.Second
na *= time.Second
return na.String()
} else {
return "anytime"
}
}(),
func() string {
if ts.lastAnnounce.Err != nil {
return ts.lastAnnounce.Err.Error()
}
if ts.lastAnnounce.Completed.IsZero() {
return "never"
}
return fmt.Sprintf("%d peers", ts.lastAnnounce.NumPeers)
2018-02-19 05:19:18 +00:00
}(),
)
return w.String()
}
type trackerAnnounceResult struct {
Err error
NumPeers int
Interval time.Duration
Completed time.Time
}
2018-02-19 05:19:18 +00:00
func (me *trackerScraper) getIp() (ip net.IP, err error) {
ips, err := net.LookupIP(me.u.Hostname())
if err != nil {
return
}
if len(ips) == 0 {
err = errors.New("no ips")
return
}
for _, ip = range ips {
if me.t.cl.ipIsBlocked(ip) {
continue
}
switch me.u.Scheme {
case "udp4":
if ip.To4() == nil {
continue
}
case "udp6":
if ip.To4() != nil {
continue
}
}
return
}
2018-02-19 05:19:18 +00:00
err = errors.New("no acceptable ips")
return
}
2018-02-19 05:19:18 +00:00
func (me *trackerScraper) trackerUrl(ip net.IP) string {
u := me.u
if u.Port() != "" {
u.Host = net.JoinHostPort(ip.String(), u.Port())
}
return u.String()
}
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.
func (me *trackerScraper) announce() (ret trackerAnnounceResult) {
defer func() {
ret.Completed = time.Now()
}()
ret.Interval = 5 * time.Minute
2018-02-19 05:19:18 +00:00
ip, err := me.getIp()
2016-05-23 01:00:49 +00:00
if err != nil {
2018-02-19 05:19:18 +00:00
ret.Err = fmt.Errorf("error getting ip: %s", err)
return
}
2018-07-25 03:41:50 +00:00
me.t.cl.lock()
req := me.t.announceRequest()
2018-07-25 03:41:50 +00:00
me.t.cl.unlock()
2018-02-19 05:19:18 +00:00
res, err := tracker.Announce{
HttpClient: me.t.cl.config.TrackerHttpClient,
2018-02-19 05:19:18 +00:00
UserAgent: me.t.cl.config.HTTPUserAgent,
TrackerUrl: me.trackerUrl(ip),
Request: req,
HostHeader: me.u.Host,
UdpNetwork: me.u.Scheme,
ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4},
ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},
2018-02-19 05:19:18 +00:00
}.Do()
if err != nil {
2018-02-19 05:19:18 +00:00
ret.Err = fmt.Errorf("error announcing: %s", err)
return
}
2018-07-10 01:21:24 +00:00
me.t.AddPeers(Peers(nil).AppendFromTracker(res.Peers))
ret.NumPeers = len(res.Peers)
ret.Interval = time.Duration(res.Interval) * time.Second
return
}
func (me *trackerScraper) Run() {
for {
select {
2018-07-25 03:41:50 +00:00
case <-me.t.closed.LockedChan(me.t.cl.locker()):
return
2018-07-25 03:41:50 +00:00
case <-me.stop.LockedChan(me.t.cl.locker()):
return
2018-07-25 03:41:50 +00:00
case <-me.t.wantPeersEvent.LockedChan(me.t.cl.locker()):
}
ar := me.announce()
2018-07-25 03:41:50 +00:00
me.t.cl.lock()
me.lastAnnounce = ar
2018-07-25 03:41:50 +00:00
me.t.cl.unlock()
2017-11-07 05:11:59 +00:00
intervalChan := time.After(time.Until(ar.Completed.Add(ar.Interval)))
select {
2018-07-25 03:41:50 +00:00
case <-me.t.closed.LockedChan(me.t.cl.locker()):
return
2018-07-25 03:41:50 +00:00
case <-me.stop.LockedChan(me.t.cl.locker()):
return
case <-intervalChan:
}
}
}