2014-09-25 16:05:52 +08:00
|
|
|
// Converts magnet URIs and info hashes into torrent metainfo files.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2017-08-18 09:14:57 +08:00
|
|
|
"net/http"
|
2014-09-25 16:05:52 +08:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2017-08-18 09:14:57 +08:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
2017-08-27 09:46:06 +08:00
|
|
|
"github.com/anacrolix/tagflag"
|
2019-08-21 18:58:40 +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/bencode"
|
2014-09-25 16:05:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-08-27 09:46:06 +08:00
|
|
|
args := struct {
|
|
|
|
tagflag.StartPos
|
|
|
|
Magnet []string
|
|
|
|
}{}
|
|
|
|
tagflag.Parse(&args)
|
2014-09-25 16:05:52 +08:00
|
|
|
cl, err := torrent.NewClient(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error creating client: %s", err)
|
|
|
|
}
|
2017-08-18 09:14:57 +08:00
|
|
|
http.HandleFunc("/torrent", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
cl.WriteStatus(w)
|
|
|
|
})
|
|
|
|
http.HandleFunc("/dht", func(w http.ResponseWriter, r *http.Request) {
|
2018-04-12 09:41:07 +08:00
|
|
|
for _, ds := range cl.DhtServers() {
|
|
|
|
ds.WriteStatus(w)
|
|
|
|
}
|
2017-08-18 09:14:57 +08:00
|
|
|
})
|
2014-09-25 16:05:52 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2017-08-27 09:46:06 +08:00
|
|
|
for _, arg := range args.Magnet {
|
2014-09-25 16:05:52 +08:00
|
|
|
t, err := cl.AddMagnet(arg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error adding magnet to client: %s", err)
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2015-04-29 22:30:19 +08:00
|
|
|
<-t.GotInfo()
|
2016-04-03 16:40:43 +08:00
|
|
|
mi := t.Metainfo()
|
2015-02-09 21:12:29 +08:00
|
|
|
t.Drop()
|
2016-09-20 16:39:07 +08:00
|
|
|
f, err := os.Create(t.Info().Name + ".torrent")
|
2014-09-25 16:05:52 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error creating torrent metainfo file: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
err = bencode.NewEncoder(f).Encode(mi)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error writing torrent metainfo file: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|