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 // Returns whether we can shorten the interval, and sets notify to a channel that receives when we
157 // might change our mind, or leaves it if we won't.
158 func (me *trackerScraper) canIgnoreInterval(notify *<-chan struct{}) bool {
159 gotInfo := me.t.GotInfo()
162 // Private trackers really don't like us announcing more than they specify. They're also
163 // tracking us very carefully, so it's best to comply.
164 private := me.t.info.Private
165 return private == nil || !*private
172 func (me *trackerScraper) Run() {
173 defer me.announceStopped()
174 // make sure first announce is a "started"
178 // after first announce, get back to regular "none"
185 // Make sure we don't announce for at least a minute since the last one.
186 interval := ar.Interval
187 if interval < time.Minute {
188 interval = time.Minute
192 wantPeers := me.t.wantPeersEvent.C()
193 closed := me.t.closed.C()
196 // If we want peers, reduce the interval to the minimum if it's appropriate.
198 // A channel that receives when we should reconsider our interval. Starts as nil since that
200 var reconsider <-chan struct{}
203 if interval > time.Minute && me.canIgnoreInterval(&reconsider) {
204 interval = time.Minute
207 reconsider = wantPeers
214 // Recalculate the interval.
216 case <-time.After(time.Until(ar.Completed.Add(interval))):
221 func (me *trackerScraper) announceStopped() {
222 me.announce(tracker.Stopped)