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-29 06:11:24 +08:00
|
|
|
"net"
|
2013-11-04 21:07:28 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-07-24 15:34:06 +08:00
|
|
|
"os/signal"
|
2014-06-26 22:57:07 +08:00
|
|
|
"strings"
|
2018-07-24 15:34:06 +08:00
|
|
|
"syscall"
|
2015-03-25 14:32:42 +08:00
|
|
|
"time"
|
|
|
|
|
2020-01-04 13:26:49 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2020-02-27 13:50:14 +08:00
|
|
|
"github.com/dustin/go-humanize"
|
2019-08-22 11:59:04 +08:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/anacrolix/log"
|
|
|
|
|
2017-09-01 08:35:40 +08:00
|
|
|
"github.com/anacrolix/envpprof"
|
2015-11-05 20:21:39 +08:00
|
|
|
"github.com/anacrolix/tagflag"
|
2019-08-21 18:58:40 +08:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent"
|
2018-07-07 09:40:06 +08:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
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
|
|
|
)
|
|
|
|
|
2020-02-27 15:33:44 +08:00
|
|
|
func torrentBar(t *torrent.Torrent, pieceStates bool) {
|
2015-11-22 15:45:06 +08:00
|
|
|
go func() {
|
2020-02-27 13:50:14 +08:00
|
|
|
if t.Info() == nil {
|
|
|
|
fmt.Printf("getting info for %q\n", t.Name())
|
|
|
|
<-t.GotInfo()
|
2016-09-12 15:47:07 +08:00
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
for {
|
2020-02-27 13:50:14 +08:00
|
|
|
var completedPieces, partialPieces int
|
|
|
|
psrs := t.PieceStateRuns()
|
|
|
|
for _, r := range psrs {
|
|
|
|
if r.Complete {
|
|
|
|
completedPieces += r.Length
|
|
|
|
}
|
|
|
|
if r.Partial {
|
|
|
|
partialPieces += r.Length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf(
|
|
|
|
"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,
|
|
|
|
)
|
2020-02-27 15:33:44 +08:00
|
|
|
if pieceStates {
|
|
|
|
fmt.Println(psrs)
|
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
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
|
|
|
|
2019-08-22 11:59:04 +08:00
|
|
|
func addTorrents(client *torrent.Client) error {
|
2016-05-12 10:26:09 +08:00
|
|
|
for _, arg := range flags.Torrent {
|
2019-08-22 11:59:04 +08:00
|
|
|
t, err := func() (*torrent.Torrent, error) {
|
2014-12-01 17:37:33 +08:00
|
|
|
if strings.HasPrefix(arg, "magnet:") {
|
|
|
|
t, err := client.AddMagnet(arg)
|
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil, xerrors.Errorf("error adding magnet: %w", err)
|
2014-12-01 17:37:33 +08:00
|
|
|
}
|
2019-08-22 11:59:04 +08:00
|
|
|
return t, nil
|
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 {
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil, xerrors.Errorf("Error downloading torrent file: %s", err)
|
2016-10-25 16:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
metaInfo, err := metainfo.Load(response.Body)
|
|
|
|
defer response.Body.Close()
|
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
|
2016-10-25 16:13:06 +08:00
|
|
|
}
|
|
|
|
t, err := client.AddTorrent(metaInfo)
|
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil, xerrors.Errorf("adding torrent: %w", err)
|
2016-10-25 16:13:06 +08:00
|
|
|
}
|
2019-08-22 11:59:04 +08:00
|
|
|
return t, nil
|
2016-11-30 15:02:21 +08:00
|
|
|
} else if strings.HasPrefix(arg, "infohash:") {
|
|
|
|
t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
|
2019-08-22 11:59:04 +08:00
|
|
|
return t, nil
|
2014-12-01 17:37:33 +08:00
|
|
|
} else {
|
|
|
|
metaInfo, err := metainfo.LoadFromFile(arg)
|
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil, xerrors.Errorf("error loading torrent file %q: %s\n", arg, err)
|
2014-12-01 17:37:33 +08:00
|
|
|
}
|
|
|
|
t, err := client.AddTorrent(metaInfo)
|
2019-12-08 17:34:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("adding torrent: %w", err)
|
|
|
|
}
|
2019-08-22 11:59:04 +08:00
|
|
|
return t, nil
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
2014-12-01 17:37:33 +08:00
|
|
|
}()
|
2019-08-22 11:59:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("adding torrent for %q: %w", arg, err)
|
|
|
|
}
|
2020-02-27 13:50:14 +08:00
|
|
|
if flags.Progress {
|
2020-02-27 15:33:44 +08:00
|
|
|
torrentBar(t, flags.PieceStates)
|
2020-02-27 13:50:14 +08:00
|
|
|
}
|
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{
|
2020-02-20 13:47:37 +08:00
|
|
|
Addr: ta,
|
2015-11-05 20:21:39 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil
|
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"`
|
2020-01-03 11:51:18 +08:00
|
|
|
Addr string `help:"network listen addr"`
|
2018-02-19 13:20:31 +08:00
|
|
|
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
|
2018-06-05 22:40:03 +08:00
|
|
|
Stats *bool
|
2019-01-08 12:47:01 +08:00
|
|
|
PublicIP net.IP
|
2019-07-19 11:01:36 +08:00
|
|
|
Progress bool
|
2020-02-27 15:33:44 +08:00
|
|
|
PieceStates bool
|
2019-08-22 11:59:04 +08:00
|
|
|
Quiet bool `help:"discard client logging"`
|
2020-01-03 11:49:43 +08:00
|
|
|
Dht bool
|
2020-04-11 11:03:41 +08:00
|
|
|
TcpPeers bool
|
|
|
|
UtpPeers bool
|
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,
|
2019-07-19 11:01:36 +08:00
|
|
|
Progress: true,
|
2020-01-03 11:49:43 +08:00
|
|
|
Dht: true,
|
2020-04-16 08:26:37 +08:00
|
|
|
TcpPeers: true,
|
|
|
|
UtpPeers: true,
|
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)
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:40:03 +08:00
|
|
|
func statsEnabled() bool {
|
|
|
|
if flags.Stats == nil {
|
|
|
|
return flags.Debug
|
|
|
|
}
|
|
|
|
return *flags.Stats
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:26:49 +08:00
|
|
|
func exitSignalHandlers(notify *missinggo.SynchronizedEvent) {
|
2018-07-24 15:34:06 +08:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
for {
|
|
|
|
log.Printf("close signal received: %+v", <-c)
|
2020-01-04 13:26:49 +08:00
|
|
|
notify.Set()
|
2018-07-24 15:34:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 15:45:06 +08:00
|
|
|
func main() {
|
2019-08-22 11:59:04 +08:00
|
|
|
if err := mainErr(); err != nil {
|
|
|
|
log.Printf("error in main: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mainErr() error {
|
2020-04-10 13:27:30 +08:00
|
|
|
var flags struct {
|
|
|
|
tagflag.StartPos
|
|
|
|
Command string
|
|
|
|
Args tagflag.ExcessArgs
|
|
|
|
}
|
|
|
|
parser := tagflag.Parse(&flags, tagflag.ParseIntermixed(false))
|
|
|
|
switch flags.Command {
|
|
|
|
case "announce":
|
|
|
|
return announceErr(flags.Args, parser)
|
|
|
|
case "download":
|
|
|
|
return downloadErr(flags.Args, parser)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown command %q", flags.Command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadErr(args []string, parent *tagflag.Parser) error {
|
|
|
|
tagflag.ParseArgs(&flags, args, tagflag.Parent(parent))
|
2018-06-05 22:40:03 +08:00
|
|
|
defer envpprof.Stop()
|
2018-06-16 14:33:40 +08:00
|
|
|
clientConfig := torrent.NewDefaultClientConfig()
|
2020-04-11 11:03:41 +08:00
|
|
|
clientConfig.DisableTCP = !flags.TcpPeers
|
|
|
|
clientConfig.DisableUTP = !flags.UtpPeers
|
2020-01-04 14:16:48 +08:00
|
|
|
clientConfig.DisableAcceptRateLimiting = true
|
2020-01-03 11:49:43 +08:00
|
|
|
clientConfig.NoDHT = !flags.Dht
|
2018-06-16 14:33:40 +08:00
|
|
|
clientConfig.Debug = flags.Debug
|
|
|
|
clientConfig.Seed = flags.Seed
|
2019-01-08 12:47:01 +08:00
|
|
|
clientConfig.PublicIp4 = flags.PublicIP
|
|
|
|
clientConfig.PublicIp6 = flags.PublicIP
|
2018-02-19 13:20:31 +08:00
|
|
|
if flags.PackedBlocklist != "" {
|
|
|
|
blocklist, err := iplist.MMapPackedFile(flags.PackedBlocklist)
|
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return xerrors.Errorf("loading blocklist: %v", err)
|
2018-02-19 13:20:31 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2020-01-03 11:51:18 +08:00
|
|
|
if flags.Addr != "" {
|
|
|
|
clientConfig.SetListenAddr(flags.Addr)
|
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)
|
|
|
|
}
|
2019-08-22 11:59:04 +08:00
|
|
|
if flags.Quiet {
|
|
|
|
clientConfig.Logger = log.Discard
|
|
|
|
}
|
2015-11-22 15:45:06 +08:00
|
|
|
|
2020-01-04 13:26:49 +08:00
|
|
|
var stop missinggo.SynchronizedEvent
|
|
|
|
defer func() {
|
|
|
|
stop.Set()
|
|
|
|
}()
|
|
|
|
|
2018-06-16 14:33:40 +08:00
|
|
|
client, err := torrent.NewClient(clientConfig)
|
2015-11-22 15:45:06 +08:00
|
|
|
if err != nil {
|
2019-08-22 11:59:04 +08:00
|
|
|
return xerrors.Errorf("creating client: %v", err)
|
2015-11-22 15:45:06 +08:00
|
|
|
}
|
|
|
|
defer client.Close()
|
2020-01-04 13:26:49 +08:00
|
|
|
go exitSignalHandlers(&stop)
|
|
|
|
go func() {
|
|
|
|
<-stop.C()
|
|
|
|
client.Close()
|
|
|
|
}()
|
2018-07-24 15:34:06 +08:00
|
|
|
|
2019-07-19 11:04:10 +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)
|
|
|
|
})
|
|
|
|
addTorrents(client)
|
|
|
|
if client.WaitAll() {
|
|
|
|
log.Print("downloaded ALL the torrents")
|
|
|
|
} else {
|
2019-08-22 11:59:04 +08:00
|
|
|
return xerrors.New("y u no complete torrents?!")
|
2015-11-22 15:45:06 +08:00
|
|
|
}
|
2016-05-12 10:26:09 +08:00
|
|
|
if flags.Seed {
|
2018-06-05 22:40:03 +08:00
|
|
|
outputStats(client)
|
2020-01-04 13:26:49 +08:00
|
|
|
<-stop.C()
|
2014-05-21 15:38:09 +08:00
|
|
|
}
|
2018-06-05 22:40:03 +08:00
|
|
|
outputStats(client)
|
2019-08-22 11:59:04 +08:00
|
|
|
return nil
|
2018-06-05 22:40:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func outputStats(cl *torrent.Client) {
|
|
|
|
if !statsEnabled() {
|
|
|
|
return
|
|
|
|
}
|
2017-09-01 08:35:40 +08:00
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
fmt.Printf("%s: %s\n", kv.Key, kv.Value)
|
|
|
|
})
|
2018-06-05 22:40:03 +08:00
|
|
|
cl.WriteStatus(os.Stdout)
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|