From: Matt Joiner Date: Thu, 4 Nov 2021 09:02:55 +0000 (+1100) Subject: cmd/torrent: Add serve subcommand X-Git-Tag: v1.36.0~3 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=eaeb38b18c6a8eb0bcdf0feab0e2dbf6ad424917;p=btrtrc.git cmd/torrent: Add serve subcommand --- diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 098e3236..4f83762a 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -109,6 +109,7 @@ func mainErr() error { fmt.Printf("Torrent version prefix: %q\n", version.DefaultBep20Prefix) return nil }), + args.Subcommand("serve", serve, args.Help("creates and seeds a torrent from a filepath")), ) return nil } diff --git a/cmd/torrent/serve.go b/cmd/torrent/serve.go new file mode 100644 index 00000000..ae12eb49 --- /dev/null +++ b/cmd/torrent/serve.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "net/http" + "path/filepath" + + "github.com/anacrolix/args" + "github.com/anacrolix/log" + "github.com/anacrolix/torrent" + "github.com/anacrolix/torrent/bencode" + "github.com/anacrolix/torrent/metainfo" + "github.com/anacrolix/torrent/storage" +) + +func serve(ctx args.SubCmdCtx) error { + var filePath string + ctx.Parse(args.Pos("filePath", &filePath)) + ctx.Defer(func() error { + cfg := torrent.NewDefaultClientConfig() + cfg.Seed = true + cl, err := torrent.NewClient(cfg) + if err != nil { + return fmt.Errorf("new torrent client: %w", err) + } + defer cl.Close() + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + cl.WriteStatus(w) + }) + info := metainfo.Info{ + PieceLength: 1 << 18, + } + err = info.BuildFromFilePath(filePath) + if err != nil { + return fmt.Errorf("building info from path %q: %w", filePath, err) + } + for _, fi := range info.Files { + log.Printf("added %q", fi.Path) + } + mi := metainfo.MetaInfo{ + InfoBytes: bencode.MustMarshal(info), + } + pc, err := storage.NewDefaultPieceCompletionForDir(".") + if err != nil { + return fmt.Errorf("new piece completion: %w", err) + } + defer pc.Close() + ih := mi.HashInfoBytes() + to, _ := cl.AddTorrentOpt(torrent.AddTorrentOpts{ + InfoHash: ih, + Storage: storage.NewFileOpts(storage.NewFileClientOpts{ + ClientBaseDir: filePath, + FilePathMaker: func(opts storage.FilePathMakerOpts) string { + return filepath.Join(opts.File.Path...) + }, + TorrentDirMaker: nil, + PieceCompletion: pc, + }), + }) + defer to.Drop() + err = to.MergeSpec(&torrent.TorrentSpec{ + InfoBytes: mi.InfoBytes, + Trackers: [][]string{{}}, + }) + if err != nil { + return fmt.Errorf("setting trackers: %w", err) + } + fmt.Println(ih) + select {} + }) + return nil +}