2016-05-22 20:45:08 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-07-12 14:44:06 +08:00
|
|
|
"bytes"
|
2020-12-15 12:29:45 +08:00
|
|
|
"context"
|
2016-07-12 14:44:06 +08:00
|
|
|
"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"
|
2020-04-16 10:17:18 +08:00
|
|
|
"github.com/anacrolix/log"
|
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 {
|
2021-10-29 16:34:38 +08:00
|
|
|
u url.URL
|
|
|
|
t *Torrent
|
|
|
|
lastAnnounce trackerAnnounceResult
|
|
|
|
lookupTrackerIp func(*url.URL) ([]net.IP, error)
|
2016-07-12 14:44:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:38:01 +08:00
|
|
|
type torrentTrackerAnnouncer interface {
|
|
|
|
statusLine() string
|
2020-04-28 07:13:44 +08:00
|
|
|
URL() *url.URL
|
2020-04-06 13:38:01 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 07:13:44 +08:00
|
|
|
func (me trackerScraper) URL() *url.URL {
|
|
|
|
return &me.u
|
2020-04-06 13:38:01 +08:00
|
|
|
}
|
|
|
|
|
2016-07-12 14:44:06 +08:00
|
|
|
func (ts *trackerScraper) statusLine() string {
|
|
|
|
var w bytes.Buffer
|
2020-04-28 07:13:44 +08:00
|
|
|
fmt.Fprintf(&w, "next ann: %v, last ann: %v",
|
2016-07-12 14:44:06 +08:00
|
|
|
func() string {
|
2023-05-06 11:43:30 +08:00
|
|
|
// 计算下一次 announce 的时间
|
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 {
|
2023-05-06 11:43:30 +08:00
|
|
|
// 马上可以进行下一次 announce
|
2016-07-12 14:44:06 +08:00
|
|
|
return "anytime"
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() string {
|
2023-05-06 11:43:30 +08:00
|
|
|
// 上一次 announce 出错
|
2016-07-12 14:44:06 +08:00
|
|
|
if ts.lastAnnounce.Err != nil {
|
|
|
|
return ts.lastAnnounce.Err.Error()
|
|
|
|
}
|
2023-05-06 11:43:30 +08:00
|
|
|
// 上一次 announce 从未成功完成过, 即没有成功完成的 announce
|
2016-07-14 11:08:52 +08:00
|
|
|
if ts.lastAnnounce.Completed.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
2023-05-06 11:43:30 +08:00
|
|
|
// 上一次 announce 完成时获取的peer数
|
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) {
|
2023-05-06 11:43:30 +08:00
|
|
|
trackerURL := me.u.String()
|
2021-10-28 16:13:21 +08:00
|
|
|
var ips []net.IP
|
2023-05-06 11:43:30 +08:00
|
|
|
// 内置函数查找tracker IP
|
2021-10-29 16:34:38 +08:00
|
|
|
if me.lookupTrackerIp != nil {
|
|
|
|
ips, err = me.lookupTrackerIp(&me.u)
|
2021-10-28 16:13:21 +08:00
|
|
|
} else {
|
|
|
|
// Do a regular dns lookup
|
|
|
|
ips, err = net.LookupIP(me.u.Hostname())
|
|
|
|
}
|
2018-02-19 13:19:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-05-06 11:43:30 +08:00
|
|
|
// 内置函数或dns 查询结果为空
|
2018-02-19 13:19:18 +08:00
|
|
|
if len(ips) == 0 {
|
2023-05-06 11:43:30 +08:00
|
|
|
err = fmt.Errorf("no ips(couldn't know the tracker's(%s) ip)", trackerURL)
|
2018-02-19 13:19:18 +08:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 15:24:17 +08:00
|
|
|
me.t.cl.rLock()
|
|
|
|
defer me.t.cl.rUnlock()
|
|
|
|
if me.t.cl.closed.IsSet() {
|
|
|
|
err = errors.New("client is closed")
|
|
|
|
return
|
|
|
|
}
|
2023-05-08 11:30:36 +08:00
|
|
|
if Debug {
|
|
|
|
log.Printf("dns resolve: tracker(%s),ips(%v)", trackerURL, ips)
|
|
|
|
}
|
|
|
|
|
2018-02-19 13:19:18 +08:00
|
|
|
for _, ip = range ips {
|
|
|
|
if me.t.cl.ipIsBlocked(ip) {
|
2023-05-08 11:30:36 +08:00
|
|
|
if Debug {
|
|
|
|
log.Printf("%s(%s) blocked", me.u.String(), ip.String())
|
|
|
|
}
|
2018-02-19 13:19:18 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch me.u.Scheme {
|
|
|
|
case "udp4":
|
|
|
|
if ip.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case "udp6":
|
|
|
|
if ip.To4() != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-06 11:43:30 +08:00
|
|
|
default:
|
2023-05-08 11:30:36 +08:00
|
|
|
if Debug {
|
|
|
|
log.Printf("%s", me.u.Scheme)
|
|
|
|
}
|
2018-02-19 13:19:18 +08:00
|
|
|
}
|
|
|
|
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.
|
2020-12-15 12:29:45 +08:00
|
|
|
func (me *trackerScraper) announce(ctx context.Context, 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
|
2020-12-15 12:29:45 +08:00
|
|
|
|
|
|
|
// Limit concurrent use of the same tracker URL by the Client.
|
2020-12-17 09:22:02 +08:00
|
|
|
ref := me.t.cl.activeAnnounceLimiter.GetRef(me.u.String())
|
2020-12-15 12:29:45 +08:00
|
|
|
defer ref.Drop()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ret.Err = ctx.Err()
|
|
|
|
return
|
|
|
|
case ref.C() <- struct{}{}:
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case <-ref.C():
|
|
|
|
default:
|
|
|
|
panic("should return immediately")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-06 11:43:30 +08:00
|
|
|
// tracker ip
|
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()
|
2020-12-21 06:29:20 +08:00
|
|
|
// The default timeout works well as backpressure on concurrent access to the tracker. Since
|
|
|
|
// we're passing our own Context now, we will include that timeout ourselves to maintain similar
|
|
|
|
// behavior to previously, albeit with this context now being cancelled when the Torrent is
|
|
|
|
// closed.
|
2023-05-06 11:43:30 +08:00
|
|
|
// 15s
|
2020-12-21 06:29:20 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, tracker.DefaultTrackerAnnounceTimeout)
|
|
|
|
defer cancel()
|
2020-04-16 15:20:58 +08:00
|
|
|
me.t.logger.WithDefaultLevel(log.Debug).Printf("announcing to %q: %#v", me.u.String(), req)
|
2018-02-19 13:19:18 +08:00
|
|
|
res, err := tracker.Announce{
|
2022-11-29 07:35:36 +08:00
|
|
|
Context: ctx,
|
2022-11-29 07:44:46 +08:00
|
|
|
HttpProxy: me.t.cl.config.HTTPProxy,
|
|
|
|
HttpRequestDirector: me.t.cl.config.HttpRequestDirector,
|
2022-11-29 07:35:36 +08:00
|
|
|
DialContext: me.t.cl.config.TrackerDialContext,
|
|
|
|
ListenPacket: me.t.cl.config.TrackerListenPacket,
|
|
|
|
UserAgent: me.t.cl.config.HTTPUserAgent,
|
|
|
|
TrackerUrl: me.trackerUrl(ip),
|
|
|
|
Request: req,
|
|
|
|
HostHeader: me.u.Host,
|
|
|
|
ServerName: me.u.Hostname(),
|
|
|
|
UdpNetwork: me.u.Scheme,
|
|
|
|
ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4},
|
|
|
|
ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},
|
|
|
|
Logger: me.t.logger,
|
2018-02-19 13:19:18 +08:00
|
|
|
}.Do()
|
2020-12-09 17:01:05 +08:00
|
|
|
me.t.logger.WithDefaultLevel(log.Debug).Printf("announce to %q returned %#v: %v", me.u.String(), res, err)
|
2016-05-22 20:45:08 +08:00
|
|
|
if err != nil {
|
2023-05-08 11:30:36 +08:00
|
|
|
if Debug {
|
|
|
|
log.Printf("annouce error:%v", err)
|
|
|
|
}
|
2020-10-01 09:45:47 +08:00
|
|
|
ret.Err = fmt.Errorf("announcing: %w", err)
|
2016-07-12 14:44:06 +08:00
|
|
|
return
|
2016-05-22 20:45:08 +08:00
|
|
|
}
|
2023-05-18 18:25:22 +08:00
|
|
|
// log.Printf("announce ok %s(%s)", me.trackerUrl(ip), ip)
|
2023-05-08 11:30:36 +08:00
|
|
|
|
2020-05-29 17:44:48 +08:00
|
|
|
me.t.AddPeers(peerInfos(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
|
|
|
}
|
|
|
|
|
2020-12-09 19:36:57 +08:00
|
|
|
// Returns whether we can shorten the interval, and sets notify to a channel that receives when we
|
|
|
|
// might change our mind, or leaves it if we won't.
|
|
|
|
func (me *trackerScraper) canIgnoreInterval(notify *<-chan struct{}) bool {
|
|
|
|
gotInfo := me.t.GotInfo()
|
|
|
|
select {
|
|
|
|
case <-gotInfo:
|
|
|
|
// Private trackers really don't like us announcing more than they specify. They're also
|
|
|
|
// tracking us very carefully, so it's best to comply.
|
|
|
|
private := me.t.info.Private
|
|
|
|
return private == nil || !*private
|
|
|
|
default:
|
|
|
|
*notify = gotInfo
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 20:45:08 +08:00
|
|
|
func (me *trackerScraper) Run() {
|
2019-07-17 09:56:25 +08:00
|
|
|
defer me.announceStopped()
|
2020-12-15 12:29:45 +08:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-me.t.Closed():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-01 00:11:01 +08:00
|
|
|
// make sure first announce is a "started"
|
|
|
|
e := tracker.Started
|
2020-12-15 12:29:45 +08:00
|
|
|
|
2016-05-22 20:45:08 +08:00
|
|
|
for {
|
2020-12-15 12:29:45 +08:00
|
|
|
ar := me.announce(ctx, e)
|
2019-06-01 00:11:01 +08:00
|
|
|
// 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
|
|
|
|
2020-12-09 19:36:57 +08:00
|
|
|
recalculate:
|
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()
|
|
|
|
me.t.cl.unlock()
|
|
|
|
|
2020-12-09 19:36:57 +08:00
|
|
|
// If we want peers, reduce the interval to the minimum if it's appropriate.
|
|
|
|
|
|
|
|
// A channel that receives when we should reconsider our interval. Starts as nil since that
|
|
|
|
// never receives.
|
|
|
|
var reconsider <-chan struct{}
|
2018-10-18 07:35:38 +08:00
|
|
|
select {
|
2018-10-21 15:24:22 +08:00
|
|
|
case <-wantPeers:
|
2020-12-09 19:36:57 +08:00
|
|
|
if interval > time.Minute && me.canIgnoreInterval(&reconsider) {
|
2018-10-18 07:35:38 +08:00
|
|
|
interval = time.Minute
|
|
|
|
}
|
|
|
|
default:
|
2020-12-09 19:36:57 +08:00
|
|
|
reconsider = wantPeers
|
2018-10-18 07:35:38 +08:00
|
|
|
}
|
2016-05-22 20:45:08 +08:00
|
|
|
|
|
|
|
select {
|
2021-09-02 18:53:49 +08:00
|
|
|
case <-me.t.closed.Done():
|
2016-05-22 20:45:08 +08:00
|
|
|
return
|
2020-12-09 19:36:57 +08:00
|
|
|
case <-reconsider:
|
2020-04-15 11:05:41 +08:00
|
|
|
// Recalculate the interval.
|
2020-12-09 19:36:57 +08:00
|
|
|
goto recalculate
|
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() {
|
2020-12-21 06:29:20 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), tracker.DefaultTrackerAnnounceTimeout)
|
2020-12-15 12:29:45 +08:00
|
|
|
defer cancel()
|
|
|
|
me.announce(ctx, tracker.Stopped)
|
2019-06-01 00:11:01 +08:00
|
|
|
}
|