]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent: Add serve subcommand
authorMatt Joiner <anacrolix@gmail.com>
Thu, 4 Nov 2021 09:02:55 +0000 (20:02 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 4 Nov 2021 09:02:55 +0000 (20:02 +1100)
cmd/torrent/main.go
cmd/torrent/serve.go [new file with mode: 0644]

index 098e3236a6cd4c1a77c3b0087059760370cb605d..4f83762a2b16aabb7172a139cd4a6f88fc350fd0 100644 (file)
@@ -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 (file)
index 0000000..ae12eb4
--- /dev/null
@@ -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
+}