/
tracker.go
136 lines (110 loc) · 2.82 KB
/
tracker.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
126
127
128
129
130
131
132
133
134
135
136
package tracker
import (
"fmt"
"github.com/yorirou/gotorrent/client/config"
"github.com/yorirou/gotorrent/metainfo"
"github.com/yorirou/gotorrent/util"
"io/ioutil"
"log"
"net/http"
"net/url"
"sync"
"time"
)
type TrackerClientCollection struct {
clients []*trackerClient
infohash string
clientConfig *config.ClientConfig
}
func NewTrackerClientCollection(mi *metainfo.Metainfo, cc *config.ClientConfig) *TrackerClientCollection {
tcc := new(TrackerClientCollection)
tcc.infohash = mi.Info.Hash
tcc.clients = tcc.createClients(mi)
tcc.clientConfig = cc
return tcc
}
type trackerClient struct {
url string
timeout time.Duration
lastRequest time.Time
collection *TrackerClientCollection
trackerID string
}
func newTrackerClient(url string, tcc *TrackerClientCollection) *trackerClient {
tc := new(trackerClient)
tc.url = url
tc.collection = tcc
return tc
}
func (tc *TrackerClientCollection) createClients(mi *metainfo.Metainfo) []*trackerClient {
clients := make([]*trackerClient, 0)
if ann := mi.Announce; ann != "" {
clients = append(clients, newTrackerClient(ann, tc))
}
for _, annurl := range mi.AnnounceList {
clients = append(clients, newTrackerClient(annurl, tc))
}
return clients
}
func (tc *TrackerClientCollection) RequestPeers(downloaded, uploaded, left uint64) (seedernum uint32, leechernum uint32, peers *PeerPool) {
var wg sync.WaitGroup
wg.Add(len(tc.clients))
seeders := util.NewCounter()
leechers := util.NewCounter()
peers = NewPeerPool()
for _, c := range tc.clients {
go func() {
c.announce(tc.infohash, downloaded, uploaded, left, seeders, leechers, peers)
wg.Done()
}()
}
wg.Wait()
seedernum = uint32(seeders.Value())
leechernum = uint32(leechers.Value())
return
}
func (tc *trackerClient) announce(infohash string, downloaded, uploaded, left uint64, seeders *util.Counter, leechers *util.Counter, peerPool *PeerPool) {
if time.Since(tc.lastRequest) < tc.timeout {
return
}
u, err := url.Parse(tc.url)
if err != nil {
log.Print(err)
return
}
f := func(n uint64) string {
return fmt.Sprintf("%d", n)
}
q := url.Values{}
q.Add("info_hash", infohash)
q.Add("peer_id", tc.collection.clientConfig.PeerID)
q.Add("port", f(tc.collection.clientConfig.Port))
q.Add("uploaded", f(uploaded))
q.Add("downloaded", f(downloaded))
q.Add("left", f(left))
q.Add("compact", "1")
if tc.trackerID != "" {
u.Query().Add("trackerid", tc.trackerID)
}
fullurl := u.String() + "?" + q.Encode()
resp, err := http.Get(fullurl)
if err != nil {
log.Print(err)
return
}
b, errr := ioutil.ReadAll(resp.Body)
if errr != nil {
log.Print(err)
return
}
r, errp := ParseResponse(b)
if errp != nil {
log.Print(errp)
return
}
seeders.Add(uint64(r.Seeders()))
leechers.Add(uint64(r.Leechers()))
for _, p := range r.Peers {
peerPool.Add(p)
}
}