]> Sergey Matveev's repositories - btrtrc.git/blob - internal/cmd/issue-908/main.go
Add seeder and files for issue #908
[btrtrc.git] / internal / cmd / issue-908 / main.go
1 package main
2
3 import (
4         "fmt"
5         "github.com/davecgh/go-spew/spew"
6         "log"
7         "net/http"
8         "os"
9
10         "github.com/anacrolix/torrent"
11         "github.com/anacrolix/torrent/bencode"
12         "github.com/anacrolix/torrent/metainfo"
13 )
14
15 func main() {
16         cfg := torrent.NewDefaultClientConfig()
17         cfg.Seed = true
18         cfg.Debug = true
19         cfg.NoDefaultPortForwarding = true
20         cfg.DisableIPv6 = true
21
22         cl, err := torrent.NewClient(cfg)
23         if err != nil {
24                 log.Fatal(err)
25         }
26         fmt.Printf("%x\n", cl.PeerID())
27         defer cl.Close()
28         http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
29                 cl.WriteStatus(w)
30         })
31         go http.ListenAndServe(":8080", nil)
32
33         filePath := "testdata"
34         fi, err := os.Stat(filePath)
35         if err != nil {
36                 panic(err)
37         }
38         totalLength := fi.Size()
39         if err != nil {
40                 log.Fatal(err)
41         }
42         pieceLength := metainfo.ChoosePieceLength(totalLength)
43         info := metainfo.Info{
44                 PieceLength: pieceLength,
45         }
46         err = info.BuildFromFilePath(filePath)
47         if err != nil {
48                 log.Fatal(err)
49         }
50         for _, fi := range info.Files {
51                 log.Printf("added %q", fi.Path)
52         }
53         mi := &metainfo.MetaInfo{
54                 InfoBytes: bencode.MustMarshal(info),
55         }
56         spew.Dump(info)
57         torrentFile := mi
58         torrentFile.Announce = ""
59
60         // Add the torrent to the client
61         tor, err := cl.AddTorrent(torrentFile)
62         if err != nil {
63                 log.Fatal(err)
64         }
65
66         // Wait for the torrent to be ready
67         <-tor.GotInfo()
68
69         hash := tor.InfoHash()
70         fmt.Printf("%v\n", tor.Metainfo().Magnet(&hash, tor.Info()))
71
72         // Announce the torrent to DHT
73         for _, _ds := range cl.DhtServers() {
74                 ds := _ds
75                 done, _, err := tor.AnnounceToDht(ds)
76                 if err != nil {
77                         log.Fatal(err)
78                 }
79                 for c := range done {
80                         fmt.Println("++++++++++++++++++++++++", c)
81                 }
82         }
83         select {}
84 }