2015-04-06 21:04:18 +08:00
|
|
|
// Downloads torrents from the command-line.
|
2013-09-26 17:49:15 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-01 08:35:40 +08:00
|
|
|
"expvar"
|
2013-11-04 21:07:28 +08:00
|
|
|
"fmt"
|
2013-09-26 17:49:15 +08:00
|
|
|
"log"
|
2013-09-29 06:11:24 +08:00
|
|
|
"net"
|
2013-11-04 21:07:28 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2014-06-26 22:57:07 +08:00
|
|
|
"strings"
|
2015-03-25 14:32:42 +08:00
|
|
|
"time"
|
|
|
|
|
2018-02-19 13:20:31 +08:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
|
|
|
|
2017-09-01 08:35:40 +08:00
|
|
|
"github.com/anacrolix/envpprof"
|
2015-11-05 20:21:39 +08:00
|
|
|
"github.com/anacrolix/tagflag"
|
2015-03-26 14:18:08 +08:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-11-22 15:45:06 +08:00
|
|
|
"github.com/gosuri/uiprogress"
|
2016-10-10 14:29:39 +08:00
|
|
|
"golang.org/x/time/rate"
|
2014-03-20 13:58:09 +08:00
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent"
|
2015-04-28 13:24:17 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2016-03-28 18:57:04 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2013-09-26 17:49:15 +08:00
|
|
|
)
|
|
|
|
|
2018-01-25 10:12:46 +08:00
|
|
|
var progress = uiprogress.New()
|
|
|
|
|
2016-04-03 16:40:43 +08:00
|
|
|
func torrentBar(t *torrent.Torrent) {
|
2018-01-25 10:12:46 +08:00
|
|
|
bar := progress.AddBar(1)
|
2015-11-22 15:45:06 +08:00
|
|
|
bar.AppendCompleted()
|
|
|
|
bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
|
|
|
|
select {
|
|
|
|
case <-t.GotInfo():
|
|
|
|
default:
|
|
|
|
return "getting info"
|
2015-03-25 14:32:42 +08:00
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
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())))
|
2015-03-25 14:32:42 +08:00
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
})
|
|
|
|
bar.PrependFunc(func(*uiprogress.Bar) string {
|
2016-01-06 09:19:49 +08:00
|
|
|
return t.Name()
|
2015-11-22 15:45:06 +08:00
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
<-t.GotInfo()
|
2016-09-12 15:47:07 +08:00
|
|
|
tl := int(t.Info().TotalLength())
|
|
|
|
if tl == 0 {
|
|
|
|
bar.Set(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bar.Total = tl
|
2015-11-22 15:45:06 +08:00
|
|
|
for {
|
|
|
|
bc := t.BytesCompleted()
|
|
|
|
bar.Set(int(bc))
|
|
|
|
time.Sleep(time.Second)
|
2015-10-23 09:42:19 +08:00
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
}()
|
|
|
|
}
|
2015-03-25 12:41:15 +08:00
|
|
|
|
2015-11-22 15:45:06 +08:00
|
|
|
func addTorrents(client *torrent.Client) {
|
2016-05-12 10:26:09 +08:00
|
|
|
for _, arg := range flags.Torrent {
|
2016-04-03 16:40:43 +08:00
|
|
|
t := func() *torrent.Torrent {
|
2014-12-01 17:37:33 +08:00
|
|
|
if strings.HasPrefix(arg, "magnet:") {
|
|
|
|
t, err := client.AddMagnet(arg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error adding magnet: %s", err)
|
|
|
|
}
|
|
|
|
return t
|
2016-10-25 16:13:06 +08:00
|
|
|
} 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
|
2016-11-30 15:02:21 +08:00
|
|
|
} else if strings.HasPrefix(arg, "infohash:") {
|
|
|
|
t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
|
|
|
|
return t
|
2014-12-01 17:37:33 +08:00
|
|
|
} else {
|
|
|
|
metaInfo, err := metainfo.LoadFromFile(arg)
|
|
|
|
if err != nil {
|
2015-11-05 20:21:39 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
|
|
|
|
os.Exit(1)
|
2014-12-01 17:37:33 +08:00
|
|
|
}
|
|
|
|
t, err := client.AddTorrent(metaInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return t
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
2014-12-01 17:37:33 +08:00
|
|
|
}()
|
2015-11-22 15:45:06 +08:00
|
|
|
torrentBar(t)
|
2016-05-12 10:26:09 +08:00
|
|
|
t.AddPeers(func() (ret []torrent.Peer) {
|
|
|
|
for _, ta := range flags.TestPeer {
|
2015-11-05 20:21:39 +08:00
|
|
|
ret = append(ret, torrent.Peer{
|
|
|
|
IP: ta.IP,
|
|
|
|
Port: ta.Port,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}())
|
2014-12-01 17:37:33 +08:00
|
|
|
go func() {
|
2015-04-29 22:30:19 +08:00
|
|
|
<-t.GotInfo()
|
2014-12-01 17:37:33 +08:00
|
|
|
t.DownloadAll()
|
|
|
|
}()
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
}
|
|
|
|
|
2016-10-10 14:29:39 +08:00
|
|
|
var flags = struct {
|
2018-02-19 13:20:31 +08:00
|
|
|
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
|
|
|
|
PackedBlocklist string
|
2016-04-18 19:30:28 +08:00
|
|
|
tagflag.StartPos
|
2016-05-12 10:26:09 +08:00
|
|
|
Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
|
2016-10-10 14:29:39 +08:00
|
|
|
}{
|
|
|
|
UploadRate: -1,
|
|
|
|
DownloadRate: -1,
|
2015-11-22 15:45:06 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 10:12:46 +08:00
|
|
|
func stdoutAndStderrAreSameFile() bool {
|
|
|
|
fi1, _ := os.Stdout.Stat()
|
|
|
|
fi2, _ := os.Stderr.Stat()
|
|
|
|
return os.SameFile(fi1, fi2)
|
|
|
|
}
|
|
|
|
|
2015-11-22 15:45:06 +08:00
|
|
|
func main() {
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
2016-05-12 10:26:09 +08:00
|
|
|
tagflag.Parse(&flags)
|
2017-08-28 18:54:37 +08:00
|
|
|
clientConfig := torrent.Config{
|
2018-01-25 10:11:32 +08:00
|
|
|
Debug: flags.Debug,
|
|
|
|
Seed: flags.Seed,
|
2017-08-28 18:54:37 +08:00
|
|
|
}
|
2018-02-19 13:20:31 +08:00
|
|
|
if flags.PackedBlocklist != "" {
|
|
|
|
blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading blocklist: %s", err)
|
|
|
|
}
|
|
|
|
defer blocklist.Close()
|
|
|
|
clientConfig.IPBlocklist = blocklist
|
|
|
|
}
|
2016-05-12 10:26:09 +08:00
|
|
|
if flags.Mmap {
|
2016-03-28 18:57:04 +08:00
|
|
|
clientConfig.DefaultStorage = storage.NewMMap("")
|
2015-03-25 14:32:42 +08:00
|
|
|
}
|
2016-05-12 10:26:09 +08:00
|
|
|
if flags.Addr != nil {
|
|
|
|
clientConfig.ListenAddr = flags.Addr.String()
|
2016-04-19 15:20:14 +08:00
|
|
|
}
|
2016-10-10 14:29:39 +08:00
|
|
|
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)
|
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
|
|
|
|
client, err := torrent.NewClient(&clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error creating client: %s", err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
2016-05-12 10:26:09 +08:00
|
|
|
// 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.
|
2015-11-22 15:45:06 +08:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
client.WriteStatus(w)
|
|
|
|
})
|
2018-01-25 10:12:46 +08:00
|
|
|
if stdoutAndStderrAreSameFile() {
|
|
|
|
log.SetOutput(progress.Bypass())
|
|
|
|
}
|
|
|
|
progress.Start()
|
2015-11-22 15:45:06 +08:00
|
|
|
addTorrents(client)
|
|
|
|
if client.WaitAll() {
|
|
|
|
log.Print("downloaded ALL the torrents")
|
|
|
|
} else {
|
|
|
|
log.Fatal("y u no complete torrents?!")
|
|
|
|
}
|
2016-05-12 10:26:09 +08:00
|
|
|
if flags.Seed {
|
2014-05-21 15:38:09 +08:00
|
|
|
select {}
|
|
|
|
}
|
2017-09-01 08:35:40 +08:00
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
fmt.Printf("%s: %s\n", kv.Key, kv.Value)
|
|
|
|
})
|
|
|
|
envpprof.Stop()
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|