forked from 1290799223/trafficConsume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
107 lines (90 loc) · 2.66 KB
/
client.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
package client
import (
"sync"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
log "github.com/sirupsen/logrus"
"github.com/thank243/trafficConsume/common/fakefile"
"github.com/thank243/trafficConsume/infra"
"github.com/thank243/trafficConsume/storage"
)
func New(cfg *torrent.ClientConfig) (*Client, error) {
cl, err := torrent.NewClient(cfg)
if err != nil {
return nil, err
}
now := time.Now()
return &Client{
Client: cl,
totalStats: stats{createdAt: now},
fakeUploadStats: stats{createdAt: now},
fakeDownloadStats: stats{createdAt: now},
}, nil
}
func (c *Client) AddTorrents(mhs []metainfo.Hash) {
// default tracker servers
trs := []string{
"http://nyaa.tracker.wf:7777/announce",
"http://p4p.arenabg.com:1337/announce",
"udp://tracker.opentrackr.org:1337/announce",
}
var wg sync.WaitGroup
for i := range mhs {
wg.Add(1)
go func(i int) {
t, _ := c.AddTorrentInfoHash(mhs[i])
t.AddTrackers([][]string{trs})
if t.Info() == nil {
<-t.GotInfo()
}
t.DownloadAll()
wg.Done()
}(i)
}
wg.Wait()
}
func (c *Client) Monitor() {
for now := range time.Tick(time.Second * 10) {
totalBytes := c.ConnStats().BytesRead
totalSpeed := c.speed(&c.totalStats, totalBytes, now)
fakeDownSpeed, fakeUpSpeed, actPeers := c.torrentStats(now)
log.Infof("Throughput: %s, Total: ↓ %s/s, Private: ↑ %s/s - ↓ %s/s, Pieces: %d, Peers: %d, Tasks: %d",
infra.ByteCountIEC(totalBytes.Int64()),
infra.ByteCountIEC(totalSpeed), infra.ByteCountIEC(fakeUpSpeed), infra.ByteCountIEC(fakeDownSpeed),
storage.PieceCache().ItemCount(), actPeers, len(c.Torrents()))
}
}
func (c *Client) torrentStats(now time.Time) (fakeDownSpeed int64, fakeUpSpeed int64, actPeers int) {
for _, t := range c.Torrents() {
actPeers += t.Stats().ActivePeers
if t.InfoHash().String() == storage.FakeFileHash {
fakeDownSpeed = c.speed(&c.fakeDownloadStats, t.Stats().BytesRead, now)
fakeUpSpeed = c.speed(&c.fakeUploadStats, t.Stats().BytesWritten, now)
}
}
return
}
func (c *Client) speed(s *stats, nowBytes torrent.Count, now time.Time) int64 {
b := nowBytes.Int64()
speed := (b - s.bytesCount) * 1000 / now.Sub(s.createdAt).Milliseconds()
s.bytesCount = b
s.createdAt = now
return speed
}
func (c *Client) AddFakeTorrent() {
f := &fakefile.FakeFile{
Size: 1<<30 + 114514,
FillByte: 0xff,
}
t, _ := c.AddTorrent(&metainfo.MetaInfo{
InfoBytes: bencode.MustMarshal(f.BuildFakeFileInfo()),
})
trs := []string{
"http://p4p.arenabg.com:1337/announce",
"udp://tracker.opentrackr.org:1337/announce",
}
t.AddTrackers([][]string{trs})
t.DownloadAll()
}