11 "github.com/anacrolix/dht/v2/krpc"
12 "github.com/anacrolix/log"
14 "github.com/anacrolix/torrent/tracker"
17 // Announces a torrent to a tracker at regular intervals, when peers are
19 type trackerScraper struct {
22 lastAnnounce trackerAnnounceResult
24 // The slowdown argument lets us indicate if we think there should be some backpressure on
25 // access to the tracker. It doesn't necessarily have to be used.
26 done func(slowdown bool)
29 type torrentTrackerAnnouncer interface {
34 func (me trackerScraper) URL() *url.URL {
38 func (ts *trackerScraper) statusLine() string {
40 fmt.Fprintf(&w, "next ann: %v, last ann: %v",
42 na := time.Until(ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval))
52 if ts.lastAnnounce.Err != nil {
53 return ts.lastAnnounce.Err.Error()
55 if ts.lastAnnounce.Completed.IsZero() {
58 return fmt.Sprintf("%d peers", ts.lastAnnounce.NumPeers)
64 type trackerAnnounceResult struct {
67 Interval time.Duration
71 func (me *trackerScraper) getIp() (ip net.IP, err error) {
72 ips, err := net.LookupIP(me.u.Hostname())
77 err = errors.New("no ips")
80 for _, ip = range ips {
81 if me.t.cl.ipIsBlocked(ip) {
96 err = errors.New("no acceptable ips")
100 func (me *trackerScraper) trackerUrl(ip net.IP) string {
103 u.Host = net.JoinHostPort(ip.String(), u.Port())
108 // Return how long to wait before trying again. For most errors, we return 5
109 // minutes, a relatively quick turn around for DNS changes.
110 func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnnounceResult) {
112 ret.Completed = time.Now()
114 ret.Interval = time.Minute
116 // We might pass true if we got an error. Currently we don't because timing out with a
117 // reasonably long timeout is its own form of backpressure (it remains to be seen if it's
120 ip, err := me.getIp()
122 ret.Err = fmt.Errorf("error getting ip: %s", err)
126 req := me.t.announceRequest(event)
128 // The default timeout is currently 15s, and that works well as backpressure on concurrent
129 // access to the tracker.
130 //ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
132 me.t.logger.WithDefaultLevel(log.Debug).Printf("announcing to %q: %#v", me.u.String(), req)
133 res, err := tracker.Announce{
135 HTTPProxy: me.t.cl.config.HTTPProxy,
136 UserAgent: me.t.cl.config.HTTPUserAgent,
137 TrackerUrl: me.trackerUrl(ip),
139 HostHeader: me.u.Host,
140 ServerName: me.u.Hostname(),
141 UdpNetwork: me.u.Scheme,
142 ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4},
143 ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},
145 me.t.logger.WithDefaultLevel(log.Debug).Printf("announce to %q returned %#v: %v", me.u.String(), res, err)
147 ret.Err = fmt.Errorf("announcing: %w", err)
150 me.t.AddPeers(peerInfos(nil).AppendFromTracker(res.Peers))
151 ret.NumPeers = len(res.Peers)
152 ret.Interval = time.Duration(res.Interval) * time.Second
156 func (me *trackerScraper) Run() {
157 defer me.announceStopped()
158 // make sure first announce is a "started"
162 // after first announce, get back to regular "none"
169 // Make sure we don't announce for at least a minute since the last one.
170 interval := ar.Interval
171 if interval < time.Minute {
172 interval = time.Minute
176 wantPeers := me.t.wantPeersEvent.C()
177 closed := me.t.closed.C()
180 // If we want peers, reduce the interval to the minimum.
183 if interval > time.Minute {
184 interval = time.Minute
186 // Now we're at the minimum, don't trigger on it anymore.
195 // Recalculate the interval.
197 case <-time.After(time.Until(ar.Completed.Add(interval))):
202 func (me *trackerScraper) announceStopped() {
203 me.announce(tracker.Stopped)