5 "github.com/davecgh/go-spew/spew"
10 "github.com/anacrolix/torrent"
11 "github.com/anacrolix/torrent/bencode"
12 "github.com/anacrolix/torrent/metainfo"
16 cfg := torrent.NewDefaultClientConfig()
19 cfg.NoDefaultPortForwarding = true
20 cfg.DisableIPv6 = true
22 cl, err := torrent.NewClient(cfg)
26 fmt.Printf("%x\n", cl.PeerID())
28 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
31 go http.ListenAndServe(":8080", nil)
33 filePath := "testdata"
34 fi, err := os.Stat(filePath)
38 totalLength := fi.Size()
42 pieceLength := metainfo.ChoosePieceLength(totalLength)
43 info := metainfo.Info{
44 PieceLength: pieceLength,
46 err = info.BuildFromFilePath(filePath)
50 for _, fi := range info.Files {
51 log.Printf("added %q", fi.Path)
53 mi := &metainfo.MetaInfo{
54 InfoBytes: bencode.MustMarshal(info),
58 torrentFile.Announce = ""
60 // Add the torrent to the client
61 tor, err := cl.AddTorrent(torrentFile)
66 // Wait for the torrent to be ready
69 hash := tor.InfoHash()
70 fmt.Printf("%v\n", tor.Metainfo().Magnet(&hash, tor.Info()))
72 // Announce the torrent to DHT
73 for _, _ds := range cl.DhtServers() {
75 done, _, err := tor.AnnounceToDht(ds)
80 fmt.Println("++++++++++++++++++++++++", c)