2016-05-22 20:45:08 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-07-12 14:44:06 +08:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-02-19 13:19:18 +08:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2016-05-22 20:45:08 +08:00
|
|
|
"time"
|
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2016-05-22 20:45:08 +08:00
|
|
|
"github.com/anacrolix/torrent/tracker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Announces a torrent to a tracker at regular intervals, when peers are
|
|
|
|
// required.
|
|
|
|
type trackerScraper struct {
|
2019-07-17 09:56:25 +08:00
|
|
|
u url.URL
|
2016-07-12 14:44:06 +08:00
|
|
|
t *Torrent
|
|
|
|
lastAnnounce trackerAnnounceResult
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:38:01 +08:00
|
|
|
type torrentTrackerAnnouncer interface {
|
|
|
|
statusLine() string
|
|
|
|
URL() url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me trackerScraper) URL() url.URL {
|
|
|
|
return me.u
|
|
|
|
}
|
|
|
|
|
2016-07-12 14:44:06 +08:00
|
|
|
func (ts *trackerScraper) statusLine() string {
|
|
|
|
var w bytes.Buffer
|
|
|
|
fmt.Fprintf(&w, "%q\t%s\t%s",
|
2018-02-19 13:19:18 +08:00
|
|
|
ts.u.String(),
|
2016-07-12 14:44:06 +08:00
|
|
|
func() string {
|
2017-08-16 23:18:18 +08:00
|
|
|
na := time.Until(ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval))
|
2016-07-12 14:44:06 +08:00
|
|
|
if na > 0 {
|
2017-08-16 23:18:18 +08:00
|
|
|
na /= time.Second
|
|
|
|
na *= time.Second
|
2016-07-12 14:44:06 +08:00
|
|
|
return na.String()
|
|
|
|
} else {
|
|
|
|
return "anytime"
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() string {
|
|
|
|
if ts.lastAnnounce.Err != nil {
|
|
|
|
return ts.lastAnnounce.Err.Error()
|
|
|
|
}
|
2016-07-14 11:08:52 +08:00
|
|
|
if ts.lastAnnounce.Completed.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
2016-07-12 14:44:06 +08:00
|
|
|
return fmt.Sprintf("%d peers", ts.lastAnnounce.NumPeers)
|
2018-02-19 13:19:18 +08:00
|
|
|
}(),
|
|
|
|
)
|
2016-07-12 14:44:06 +08:00
|
|
|
return w.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type trackerAnnounceResult struct {
|
|
|
|
Err error
|
|
|
|
NumPeers int
|
|
|
|
Interval time.Duration
|
|
|
|
Completed time.Time
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
|
|
|
|
2018-02-19 13:19:18 +08: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
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
2018-02-19 13:19:18 +08:00
|
|
|
err = errors.New("no acceptable ips")
|
2016-05-22 20:45:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-19 13:19:18 +08: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 09:00:49 +08:00
|
|
|
// Return how long to wait before trying again. For most errors, we return 5
|
|
|
|
// minutes, a relatively quick turn around for DNS changes.
|
2019-06-01 00:11:01 +08:00
|
|
|
func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnnounceResult) {
|
2016-07-12 14:44:06 +08:00
|
|
|
defer func() {
|
|
|
|
ret.Completed = time.Now()
|
|
|
|
}()
|
2019-08-21 11:16:55 +08:00
|
|
|
ret.Interval = time.Minute
|
2018-02-19 13:19:18 +08:00
|
|
|
ip, err := me.getIp()
|
2016-05-23 09:00:49 +08:00
|
|
|
if err != nil {
|
2018-02-19 13:19:18 +08:00
|
|
|
ret.Err = fmt.Errorf("error getting ip: %s", err)
|
2016-07-12 14:44:06 +08:00
|
|
|
return
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
2020-04-13 17:13:59 +08:00
|
|
|
me.t.cl.rLock()
|
2019-06-01 00:11:01 +08:00
|
|
|
req := me.t.announceRequest(event)
|
2020-04-13 17:13:59 +08:00
|
|
|
me.t.cl.rUnlock()
|
2019-07-17 09:56:25 +08:00
|
|
|
//log.Printf("announcing %s %s to %q", me.t, req.Event, me.u.String())
|
2018-02-19 13:19:18 +08:00
|
|
|
res, err := tracker.Announce{
|
2018-10-31 06:32:33 +08:00
|
|
|
HTTPProxy: me.t.cl.config.HTTPProxy,
|
2018-02-19 13:19:18 +08:00
|
|
|
UserAgent: me.t.cl.config.HTTPUserAgent,
|
|
|
|
TrackerUrl: me.trackerUrl(ip),
|
|
|
|
Request: req,
|
|
|
|
HostHeader: me.u.Host,
|
2018-10-18 08:09:56 +08:00
|
|
|
ServerName: me.u.Hostname(),
|
2018-03-29 11:29:15 +08:00
|
|
|
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 13:19:18 +08:00
|
|
|
}.Do()
|
2016-05-22 20:45:08 +08:00
|
|
|
if err != nil {
|
2018-02-19 13:19:18 +08:00
|
|
|
ret.Err = fmt.Errorf("error announcing: %s", err)
|
2016-07-12 14:44:06 +08:00
|
|
|
return
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
2018-07-10 09:21:24 +08:00
|
|
|
me.t.AddPeers(Peers(nil).AppendFromTracker(res.Peers))
|
2016-07-12 14:44:06 +08:00
|
|
|
ret.NumPeers = len(res.Peers)
|
|
|
|
ret.Interval = time.Duration(res.Interval) * time.Second
|
|
|
|
return
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *trackerScraper) Run() {
|
2019-07-17 09:56:25 +08:00
|
|
|
defer me.announceStopped()
|
2019-06-01 00:11:01 +08:00
|
|
|
// make sure first announce is a "started"
|
|
|
|
e := tracker.Started
|
2016-05-22 20:45:08 +08:00
|
|
|
for {
|
2019-06-01 00:11:01 +08:00
|
|
|
ar := me.announce(e)
|
|
|
|
// after first announce, get back to regular "none"
|
|
|
|
e = tracker.None
|
2018-07-25 11:41:50 +08:00
|
|
|
me.t.cl.lock()
|
2016-07-12 14:44:06 +08:00
|
|
|
me.lastAnnounce = ar
|
2018-07-25 11:41:50 +08:00
|
|
|
me.t.cl.unlock()
|
2016-07-12 14:44:06 +08:00
|
|
|
|
2018-10-18 07:35:38 +08:00
|
|
|
wait:
|
2020-04-15 11:05:41 +08:00
|
|
|
// Make sure we don't announce for at least a minute since the last one.
|
2018-10-21 15:24:22 +08:00
|
|
|
interval := ar.Interval
|
|
|
|
if interval < time.Minute {
|
|
|
|
interval = time.Minute
|
|
|
|
}
|
2020-04-15 11:05:41 +08:00
|
|
|
|
|
|
|
me.t.cl.lock()
|
|
|
|
wantPeers := me.t.wantPeersEvent.C()
|
|
|
|
closed := me.t.closed.C()
|
|
|
|
me.t.cl.unlock()
|
|
|
|
|
|
|
|
// If we want peers, reduce the interval to the minimum.
|
2018-10-18 07:35:38 +08:00
|
|
|
select {
|
2018-10-21 15:24:22 +08:00
|
|
|
case <-wantPeers:
|
2018-10-18 07:35:38 +08:00
|
|
|
if interval > time.Minute {
|
|
|
|
interval = time.Minute
|
|
|
|
}
|
2020-04-15 11:05:41 +08:00
|
|
|
// Now we're at the minimum, don't trigger on it anymore.
|
2018-10-21 15:24:22 +08:00
|
|
|
wantPeers = nil
|
2018-10-18 07:35:38 +08:00
|
|
|
default:
|
|
|
|
}
|
2016-05-22 20:45:08 +08:00
|
|
|
|
|
|
|
select {
|
2020-04-15 11:05:41 +08:00
|
|
|
case <-closed:
|
2016-05-22 20:45:08 +08:00
|
|
|
return
|
2018-10-21 15:24:22 +08:00
|
|
|
case <-wantPeers:
|
2020-04-15 11:05:41 +08:00
|
|
|
// Recalculate the interval.
|
2018-10-18 07:35:38 +08:00
|
|
|
goto wait
|
2018-10-21 15:24:22 +08:00
|
|
|
case <-time.After(time.Until(ar.Completed.Add(interval))):
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 00:11:01 +08:00
|
|
|
|
2019-07-17 09:56:25 +08:00
|
|
|
func (me *trackerScraper) announceStopped() {
|
|
|
|
me.announce(tracker.Stopped)
|
2019-06-01 00:11:01 +08:00
|
|
|
}
|