Play some more with torrenting

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2020-07-27 15:55:31 +02:00
parent ef33425487
commit 37e6038788
12 changed files with 277 additions and 19 deletions

View File

@@ -26,13 +26,13 @@ For peers seeding the torrent use this, slightly expanded, syntax
~~~ txt
torrent DBFILE {
seed
dht
// directory (temp storage)
// port for dth, etc.
}
~~~
* `seed` tells *torrent* to seed content from **DBFILE** to the peers, it will _never_ write to
**DBFILE**. When `seed` is _not_ specified **DBFILE** will be written to once the entire torrent
is downloaded.
* `dth` starts a DTH server on address
## Examples

75
plugin/torrent/info.go Normal file
View File

@@ -0,0 +1,75 @@
package torrent
import (
"crypto/sha1"
"io"
"os"
"strings"
"github.com/zeebo/bencode"
)
const pieceLength = 2048 * 10
// pieces will hash the file in path on 256kb boundaries and return the (sha1) hashes.
func pieces(path string) (int, string, error) {
f, err := os.Open(path)
if err != nil {
return 0, "", err
}
hashes := "" // concatenated string of hash (strings)
buf := make([]byte, 2048)
h := sha1.New()
chunk := 0
length := 0
n, err := f.Read(buf)
for err != nil {
h.Write(buf[:n])
chunk++
length += n
if chunk > 10 {
chunk = 0
hashes += string(h.Sum(nil))
h = sha1.New()
}
n, err = f.Read(buf)
}
if n > 0 {
length += n
h.Write(buf[:n])
hashes += string(h.Sum(nil))
}
return length, hashes, nil
}
// Info is the torrent meta data for a single file.
type Info struct {
Pieces string `bencode:"pieces"`
PieceLength int `bencode:"piece length"`
Length int `bencode:"length"`
Name string `bencode:"name"`
}
// TorrentInfo contains the meta data for this torrent.
type TorrentInfo struct {
Nodes []string `bencode:"nodes"`
Info Info `bencode:"info"`
}
func NewTorrentInfo(path string) (*TorrentInfo, error) {
length, pieces, err := pieces(path)
if err != nil {
return nil, err
}
i := Info{Pieces: pieces, PieceLength: 2048 * 10, Length: length, Name: path}
return &TorrentInfo{Nodes: []string{}, Info: i}, nil
}
func (t *TorrentInfo) ToReader() io.Reader {
s, err := bencode.EncodeString(t)
if err != nil {
return nil
}
return strings.NewReader(s)
}

View File

@@ -1,25 +1,49 @@
package torrent
import (
"io/ioutil"
"log"
"path/filepath"
"time"
rtorrent "github.com/cenkalti/rain/torrent"
)
func (t *Torrent) StartSession() error {
s, err := rtorrent.NewSession(torrent.DefaultConfig)
func (t *Torrent) Do() error {
dc := rtorrent.DefaultConfig
dc.DHTEnabled = t.dht
dc.RPCEnabled = false
dc.DHTBootstrapNodes = []string{"127.0.0.1:7246"} // its a me
td, err := ioutil.TempDir("", "example")
if err != nil {
return err
}
dc.DataDir = td
dc.Database = filepath.Join(td, "session.db")
s, err := rtorrent.NewSession(dc)
if err != nil {
return err
}
// Add magnet link
tor, _ := ses.AddURI(magnetLink, nil)
ti, err := NewTorrentInfo("plugin/torrent/testdata/db.miek.nl")
if err != nil {
return err
}
tor, err := s.AddTorrent(ti.ToReader(), nil)
if err != nil {
return err
}
// mag, _ := tor.Magnet()
go s.StartAll()
// Watch the progress
for range time.Tick(time.Second) {
s := tor.Stats()
log.Printf("Status: %s, Downloaded: %d, Peers: %d", s.Status.String(), s.Bytes.Completed, s.Peers.Total)
}
return nil
}

View File

@@ -14,12 +14,12 @@ func init() { plugin.Register("torrent", setup) }
func setup(c *caddy.Controller) error {
tor, err := parse(c)
if err != nil {
return plugin.Error("sign", err)
return plugin.Error("torrent", err)
}
c.OnStartup(func() error {
// go tor.do()
return nil
err := tor.Do()
return err
})
c.OnShutdown(func() error {
close(tor.stop)
@@ -31,7 +31,7 @@ func setup(c *caddy.Controller) error {
}
func parse(c *caddy.Controller) (*Torrent, error) {
t := &Torrent{}
t := &Torrent{stop: make(chan struct{})}
config := dnsserver.GetConfig(c)
for c.Next() {
@@ -46,8 +46,8 @@ func parse(c *caddy.Controller) (*Torrent, error) {
for c.NextBlock() {
switch c.Val() {
case "seed":
t.seed = true
case "dht":
t.dht = true
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}

View File

@@ -13,10 +13,10 @@ func TestParse(t *testing.T) {
exp *Torrent
}{
{`torrent testdata/db.miek.nl {
seed
dht
}`,
false,
&Torrent{dbfile: "testdata/db.miek.nl", seed: true},
&Torrent{dbfile: "testdata/db.miek.nl", dht: true},
},
{`torrent testdata/db.miek.nl`,
false,
@@ -46,8 +46,8 @@ func TestParse(t *testing.T) {
if x := tor.dbfile; x != tc.exp.dbfile {
t.Errorf("Test %d expected %s as dbfile, got %s", i, tc.exp.dbfile, x)
}
if x := tor.seed; x != tc.exp.seed {
t.Errorf("Test %d expected %T as seed, got %T", i, tc.exp.seed, x)
if x := tor.dth; x != tc.exp.dth {
t.Errorf("Test %d expected %T as seed, got %T", i, tc.exp.dth, x)
}
}
}

17
plugin/torrent/testdata/db.miek.nl vendored Normal file
View File

@@ -0,0 +1,17 @@
$TTL 30M
$ORIGIN miek.nl.
@ IN SOA linode.atoom.net. miek.miek.nl. ( 1282630060 4H 1H 7D 4H )
IN NS linode.atoom.net.
IN MX 1 aspmx.l.google.com.
IN AAAA 2a01:7e00::f03c:91ff:fe79:234c
IN DNSKEY 257 3 13 sfzRg5nDVxbeUc51su4MzjgwpOpUwnuu81SlRHqJuXe3SOYOeypR69tZ52XLmE56TAmPHsiB8Rgk+NTpf0o1Cw==
a IN AAAA 2a01:7e00::f03c:91ff:fe79:234c
www IN CNAME a
bla IN NS ns1.bla.com.
ns3.blaaat.miek.nl. IN AAAA ::1 ; non-glue, should be signed.
; in baliwick nameserver that requires glue, should not be signed
bla IN NS ns2.bla.miek.nl.
ns2.bla.miek.nl. IN A 127.0.0.1

View File

@@ -3,7 +3,7 @@ package torrent
// Torrent contains the file data that needs to be torrented.
type Torrent struct {
dbfile string
seed bool
dht bool
stop chan struct{}
}