8 "github.com/anacrolix/args"
9 "github.com/anacrolix/log"
10 "github.com/anacrolix/torrent"
11 "github.com/anacrolix/torrent/bencode"
12 "github.com/anacrolix/torrent/metainfo"
13 "github.com/anacrolix/torrent/storage"
16 func serve(ctx args.SubCmdCtx) error {
18 ctx.Parse(args.Pos("filePath", &filePath))
19 ctx.Defer(func() error {
20 cfg := torrent.NewDefaultClientConfig()
22 cl, err := torrent.NewClient(cfg)
24 return fmt.Errorf("new torrent client: %w", err)
27 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
30 info := metainfo.Info{
33 err = info.BuildFromFilePath(filePath)
35 return fmt.Errorf("building info from path %q: %w", filePath, err)
37 for _, fi := range info.Files {
38 log.Printf("added %q", fi.Path)
40 mi := metainfo.MetaInfo{
41 InfoBytes: bencode.MustMarshal(info),
43 pc, err := storage.NewDefaultPieceCompletionForDir(".")
45 return fmt.Errorf("new piece completion: %w", err)
48 ih := mi.HashInfoBytes()
49 to, _ := cl.AddTorrentOpt(torrent.AddTorrentOpts{
51 Storage: storage.NewFileOpts(storage.NewFileClientOpts{
52 ClientBaseDir: filePath,
53 FilePathMaker: func(opts storage.FilePathMakerOpts) string {
54 return filepath.Join(opts.File.Path...)
61 err = to.MergeSpec(&torrent.TorrentSpec{
62 InfoBytes: mi.InfoBytes,
63 Trackers: [][]string{{
64 `wss://tracker.btorrent.xyz`,
65 `wss://tracker.openwebtorrent.com`,
66 "http://p4p.arenabg.com:1337/announce",
67 "udp://tracker.opentrackr.org:1337/announce",
68 "udp://tracker.openbittorrent.com:6969/announce",
72 return fmt.Errorf("setting trackers: %w", err)
74 fmt.Printf("%v: %v\n", to, to.Metainfo().Magnet(&ih, &info))