forked from anacrolix/torrent
/
main.go
185 lines (176 loc) · 4.54 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Downloads torrents from the command-line.
package main
import (
"expvar"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/anacrolix/dht"
"github.com/anacrolix/envpprof"
"github.com/anacrolix/tagflag"
"github.com/dustin/go-humanize"
"github.com/gosuri/uiprogress"
"golang.org/x/time/rate"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/storage"
)
func torrentBar(t *torrent.Torrent) {
bar := uiprogress.AddBar(1)
bar.AppendCompleted()
bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
select {
case <-t.GotInfo():
default:
return "getting info"
}
if t.Seeding() {
return "seeding"
} else if t.BytesCompleted() == t.Info().TotalLength() {
return "completed"
} else {
return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength())))
}
})
bar.PrependFunc(func(*uiprogress.Bar) string {
return t.Name()
})
go func() {
<-t.GotInfo()
tl := int(t.Info().TotalLength())
if tl == 0 {
bar.Set(1)
return
}
bar.Total = tl
for {
bc := t.BytesCompleted()
bar.Set(int(bc))
time.Sleep(time.Second)
}
}()
}
func addTorrents(client *torrent.Client) {
for _, arg := range flags.Torrent {
t := func() *torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
}
return t
} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
response, err := http.Get(arg)
if err != nil {
log.Fatalf("Error downloading torrent file: %s", err)
}
metaInfo, err := metainfo.Load(response.Body)
defer response.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
} else if strings.HasPrefix(arg, "infohash:") {
t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
return t
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
}
}()
torrentBar(t)
t.AddPeers(func() (ret []torrent.Peer) {
for _, ta := range flags.TestPeer {
ret = append(ret, torrent.Peer{
IP: ta.IP,
Port: ta.Port,
})
}
return
}())
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
}
}
var flags = struct {
Mmap bool `help:"memory-map torrent data"`
TestPeer []*net.TCPAddr `help:"addresses of some starting peers"`
Seed bool `help:"seed after download is complete"`
Addr *net.TCPAddr `help:"network listen addr"`
UploadRate tagflag.Bytes `help:"max piece bytes to send per second"`
DownloadRate tagflag.Bytes `help:"max bytes per second down from peers"`
Debug bool
tagflag.StartPos
Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
}{
UploadRate: -1,
DownloadRate: -1,
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
tagflag.Parse(&flags)
clientConfig := torrent.Config{
DHTConfig: dht.ServerConfig{
StartingNodes: dht.GlobalBootstrapAddrs,
},
}
if flags.Mmap {
clientConfig.DefaultStorage = storage.NewMMap("")
}
if flags.Addr != nil {
clientConfig.ListenAddr = flags.Addr.String()
}
if flags.Seed {
clientConfig.Seed = true
}
if flags.UploadRate != -1 {
clientConfig.UploadRateLimiter = rate.NewLimiter(rate.Limit(flags.UploadRate), 256<<10)
}
if flags.DownloadRate != -1 {
clientConfig.DownloadRateLimiter = rate.NewLimiter(rate.Limit(flags.DownloadRate), 1<<20)
}
client, err := torrent.NewClient(&clientConfig)
if err != nil {
log.Fatalf("error creating client: %s", err)
}
defer client.Close()
// Write status on the root path on the default HTTP muxer. This will be
// bound to localhost somewhere if GOPPROF is set, thanks to the envpprof
// import.
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w)
})
uiprogress.Start()
addTorrents(client)
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
log.Fatal("y u no complete torrents?!")
}
if flags.Seed {
select {}
}
expvar.Do(func(kv expvar.KeyValue) {
fmt.Printf("%s: %s\n", kv.Key, kv.Value)
})
envpprof.Stop()
}