/
main.go
369 lines (339 loc) · 9.03 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Downloads torrents from the command-line.
package main
import (
"expvar"
"fmt"
"io"
stdLog "log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/alexflint/go-arg"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/torrent/bencode"
"github.com/davecgh/go-spew/spew"
"github.com/dustin/go-humanize"
"golang.org/x/xerrors"
"github.com/anacrolix/log"
"github.com/anacrolix/envpprof"
"github.com/anacrolix/tagflag"
"golang.org/x/time/rate"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/storage"
)
func torrentBar(t *torrent.Torrent, pieceStates bool) {
go func() {
if t.Info() == nil {
fmt.Printf("getting info for %q\n", t.Name())
<-t.GotInfo()
}
var lastLine string
for {
var completedPieces, partialPieces int
psrs := t.PieceStateRuns()
for _, r := range psrs {
if r.Complete {
completedPieces += r.Length
}
if r.Partial {
partialPieces += r.Length
}
}
line := fmt.Sprintf(
"downloading %q: %s/%s, %d/%d pieces completed (%d partial)\n",
t.Name(),
humanize.Bytes(uint64(t.BytesCompleted())),
humanize.Bytes(uint64(t.Length())),
completedPieces,
t.NumPieces(),
partialPieces,
)
if line != lastLine {
lastLine = line
os.Stdout.WriteString(line)
}
if pieceStates {
fmt.Println(psrs)
}
time.Sleep(time.Second)
}
}()
}
type stringAddr string
func (stringAddr) Network() string { return "" }
func (me stringAddr) String() string { return string(me) }
func resolveTestPeers(addrs []string) (ret []torrent.PeerInfo) {
for _, ta := range flags.TestPeer {
ret = append(ret, torrent.PeerInfo{
Addr: stringAddr(ta),
})
}
return
}
func addTorrents(client *torrent.Client) error {
testPeers := resolveTestPeers(flags.TestPeer)
for _, arg := range flags.Torrent {
t, err := func() (*torrent.Torrent, error) {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
return nil, xerrors.Errorf("error adding magnet: %w", err)
}
return t, nil
} else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
response, err := http.Get(arg)
if err != nil {
return nil, xerrors.Errorf("Error downloading torrent file: %s", err)
}
metaInfo, err := metainfo.Load(response.Body)
defer response.Body.Close()
if err != nil {
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
return nil, xerrors.Errorf("adding torrent: %w", err)
}
return t, nil
} else if strings.HasPrefix(arg, "infohash:") {
t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
return t, nil
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
return nil, xerrors.Errorf("adding torrent: %w", err)
}
return t, nil
}
}()
if err != nil {
return xerrors.Errorf("adding torrent for %q: %w", arg, err)
}
if flags.Progress {
torrentBar(t, flags.PieceStates)
}
t.AddPeers(testPeers)
go func() {
<-t.GotInfo()
if len(flags.File) == 0 {
t.DownloadAll()
} else {
for _, f := range t.Files() {
for _, fileArg := range flags.File {
if f.DisplayPath() == fileArg {
f.Download()
}
}
}
}
}()
}
return nil
}
var flags struct {
Debug bool
Stats *bool
*DownloadCmd `arg:"subcommand:download"`
*ListFilesCmd `arg:"subcommand:list-files"`
*SpewBencodingCmd `arg:"subcommand:spew-bencoding"`
}
type SpewBencodingCmd struct {
}
//DownloadCmd: &DownloadCmd{
// UploadRate: -1,
// DownloadRate: -1,
// Progress: true,
// Dht: true,
//
// TcpPeers: true,
// UtpPeers: true,
// Webtorrent: true,
//
// Ipv4: true,
// Ipv6: true,
// Pex: true,
//},
type DownloadCmd struct {
Mmap bool `help:"memory-map torrent data"`
TestPeer []string `help:"addresses of some starting peers"`
Seed bool `help:"seed after download is complete"`
Addr string `help:"network listen addr"`
UploadRate tagflag.Bytes `help:"max piece bytes to send per second" default:"-1"`
DownloadRate tagflag.Bytes `help:"max bytes per second down from peers" default:"-1"`
PackedBlocklist string
PublicIP net.IP
Progress bool `default:"true"`
PieceStates bool
Quiet bool `help:"discard client logging"`
Dht bool `default:"true"`
TcpPeers bool `default:"true"`
UtpPeers bool `default:"true"`
Webtorrent bool `default:"true"`
DisableWebseeds bool
Ipv4 bool `default:"true"`
Ipv6 bool `default:"true"`
Pex bool `default:"true"`
File []string
Torrent []string `arity:"+" help:"torrent file path or magnet uri" arg:"positional"`
}
type ListFilesCmd struct {
TorrentPath string `arg:"positional"`
}
func stdoutAndStderrAreSameFile() bool {
fi1, _ := os.Stdout.Stat()
fi2, _ := os.Stderr.Stat()
return os.SameFile(fi1, fi2)
}
func statsEnabled() bool {
if flags.Stats == nil {
return flags.Debug
}
return *flags.Stats
}
func exitSignalHandlers(notify *missinggo.SynchronizedEvent) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for {
log.Printf("close signal received: %+v", <-c)
notify.Set()
}
}
func main() {
if err := mainErr(); err != nil {
log.Printf("error in main: %v", err)
os.Exit(1)
}
}
func mainErr() error {
stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile)
p := arg.MustParse(&flags)
switch {
//case :
// return announceErr(flags.Args, parser)
case flags.DownloadCmd != nil:
return downloadErr()
case flags.ListFilesCmd != nil:
mi, err := metainfo.LoadFromFile(flags.ListFilesCmd.TorrentPath)
if err != nil {
return fmt.Errorf("loading from file %q: %v", flags.ListFilesCmd.TorrentPath, err)
}
info, err := mi.UnmarshalInfo()
if err != nil {
return fmt.Errorf("unmarshalling info from metainfo at %q: %v", flags.ListFilesCmd.TorrentPath, err)
}
for _, f := range info.UpvertedFiles() {
fmt.Println(f.DisplayPath(&info))
}
return nil
case flags.SpewBencodingCmd != nil:
d := bencode.NewDecoder(os.Stdin)
for i := 0; ; i++ {
var v interface{}
err := d.Decode(&v)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("decoding message index %d: %w", i, err)
}
spew.Dump(v)
}
return nil
default:
p.Fail(fmt.Sprintf("unexpected subcommand: %v", p.Subcommand()))
panic("unreachable")
}
}
func downloadErr() error {
defer envpprof.Stop()
clientConfig := torrent.NewDefaultClientConfig()
clientConfig.DisableWebseeds = flags.DisableWebseeds
clientConfig.DisableTCP = !flags.TcpPeers
clientConfig.DisableUTP = !flags.UtpPeers
clientConfig.DisableIPv4 = !flags.Ipv4
clientConfig.DisableIPv6 = !flags.Ipv6
clientConfig.DisableAcceptRateLimiting = true
clientConfig.NoDHT = !flags.Dht
clientConfig.Debug = flags.Debug
clientConfig.Seed = flags.Seed
clientConfig.PublicIp4 = flags.PublicIP
clientConfig.PublicIp6 = flags.PublicIP
clientConfig.DisablePEX = !flags.Pex
clientConfig.DisableWebtorrent = !flags.Webtorrent
if flags.PackedBlocklist != "" {
blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
if err != nil {
return xerrors.Errorf("loading blocklist: %v", err)
}
defer blocklist.Close()
clientConfig.IPBlocklist = blocklist
}
if flags.Mmap {
clientConfig.DefaultStorage = storage.NewMMap("")
}
if flags.Addr != "" {
clientConfig.SetListenAddr(flags.Addr)
}
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)
}
if flags.Quiet {
clientConfig.Logger = log.Discard
}
var stop missinggo.SynchronizedEvent
defer func() {
stop.Set()
}()
client, err := torrent.NewClient(clientConfig)
if err != nil {
return xerrors.Errorf("creating client: %v", err)
}
defer client.Close()
go exitSignalHandlers(&stop)
go func() {
<-stop.C()
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)
})
err = addTorrents(client)
if err != nil {
return fmt.Errorf("adding torrents: %w", err)
}
defer outputStats(client)
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
return xerrors.New("y u no complete torrents?!")
}
if flags.Seed {
outputStats(client)
<-stop.C()
}
return nil
}
func outputStats(cl *torrent.Client) {
if !statsEnabled() {
return
}
expvar.Do(func(kv expvar.KeyValue) {
fmt.Printf("%s: %s\n", kv.Key, kv.Value)
})
cl.WriteStatus(os.Stdout)
}