forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_scraper.go
125 lines (113 loc) · 2.71 KB
/
tracker_scraper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package torrent
import (
"bytes"
"errors"
"fmt"
"time"
"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 {
url string
// 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",
ts.url,
func() string {
// return ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval).Format("2006-01-02 15:04:05 -0700 MST")
na := ts.lastAnnounce.Completed.Add(ts.lastAnnounce.Interval).Sub(time.Now())
na /= time.Second
na *= time.Second
if na > 0 {
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)
}())
return w.String()
}
type trackerAnnounceResult struct {
Err error
NumPeers int
Interval time.Duration
Completed time.Time
}
func trackerToTorrentPeers(ps []tracker.Peer) (ret []Peer) {
ret = make([]Peer, 0, len(ps))
for _, p := range ps {
ret = append(ret, Peer{
IP: p.IP,
Port: p.Port,
Source: peerSourceTracker,
})
}
return
}
// 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
blocked, urlToUse, host, err := me.t.cl.prepareTrackerAnnounceUnlocked(me.url)
if err != nil {
ret.Err = err
return
}
if blocked {
ret.Err = errors.New("blocked by IP")
return
}
me.t.cl.mu.Lock()
req := me.t.announceRequest()
me.t.cl.mu.Unlock()
res, err := tracker.AnnounceHost(urlToUse, &req, host)
if err != nil {
ret.Err = err
return
}
me.t.AddPeers(trackerToTorrentPeers(res.Peers))
ret.NumPeers = len(res.Peers)
ret.Interval = time.Duration(res.Interval) * time.Second
return
}
func (me *trackerScraper) Run() {
for {
select {
case <-me.t.closed.LockedChan(&me.t.cl.mu):
return
case <-me.stop.LockedChan(&me.t.cl.mu):
return
case <-me.t.wantPeersEvent.LockedChan(&me.t.cl.mu):
}
ar := me.announce()
me.t.cl.mu.Lock()
me.lastAnnounce = ar
me.t.cl.mu.Unlock()
intervalChan := time.After(ar.Completed.Add(ar.Interval).Sub(time.Now()))
select {
case <-me.t.closed.LockedChan(&me.t.cl.mu):
return
case <-me.stop.LockedChan(&me.t.cl.mu):
return
case <-intervalChan:
}
}
}