Andrea Jung
Andrea Jung

Reputation: 11

Creating and seeding a torrent in golang

I want to use the golang library https://github.com/anacrolix/torrent to create to torrent and get a magnet and seed the torrent. Below you can find the code I wrote. Yet, if I use the magnet the code generates I can not download anything not even the metainfo. Am I missing something here?

package main

import (
    "log"
    "time"
    "github.com/anacrolix/torrent"
    "github.com/anacrolix/torrent/bencode"
    "github.com/anacrolix/torrent/metainfo"
)

var builtinAnnounceList = [][]string{
    {"http://p4p.arenabg.com:1337/announce"},
    {"udp://tracker.opentrackr.org:1337/announce"},
    {"udp://tracker.openbittorrent.com:6969/announce"},
}

func main() {

    tmpComment:="Cool torrent description"
    tmpCreatedBy:="coolboys"
    tmpInfoName:="CoolInfoName"
    mi := metainfo.MetaInfo{
        AnnounceList: builtinAnnounceList,
    }

    mi.SetDefaults()
    
        mi.Comment = tmpComment

        mi.CreatedBy = tmpCreatedBy
    //}
    //mi.UrlList = args.Url//???????????
    info := metainfo.Info{
        PieceLength: 256 * 1024,
    }
    err := info.BuildFromFilePath("./TorrentFiles")//args.Root)
    if err != nil {
        log.Fatal(err)
    }

        info.Name =tmpInfoName
    mi.InfoBytes, err = bencode.Marshal(info)
    if err != nil {
        log.Fatal(err)
    }

    tmpMagnet:=mi.Magnet(nil,nil)
    log.Println("****",tmpMagnet)

    //
    cfg := torrent.NewDefaultClientConfig()
    cfg.Seed = true
    mainclient, ncerr := torrent.NewClient(cfg)
    if ncerr != nil {
        log.Println("new torrent client:", ncerr)
        return
    }
    defer mainclient.Close()    
    t, _ := mainclient.AddMagnet(tmpMagnet.String())

    for {   
        log.Println("******", t.Seeding())
        time.Sleep(8 * time.Second)

    }
}

Upvotes: 1

Views: 898

Answers (2)

Andrea Jung
Andrea Jung

Reputation: 11

I corrected the code I wrote and the correct code can be found here: enter link description here

In the original code I should not have used addmagnet as it assumes that I don't have the info available, which is why it would fail to seed.

Upvotes: 0

Sandy Cash
Sandy Cash

Reputation: 406

I think you might need to take a closer look at this example. Among other things, I don't see any invocation of t.DownloadAll() to do the actual download or mainclient.WaitAll() to tell you when the downloads are complete.

Upvotes: 0

Related Questions