mirror of
https://github.com/coredns/coredns.git
synced 2025-11-21 19:32:21 -05:00
@@ -1,48 +0,0 @@
|
||||
# torrent
|
||||
|
||||
## Name
|
||||
|
||||
*torrent* - use BitTorrent to disseminate zone data.
|
||||
|
||||
## Description
|
||||
|
||||
The *torrent* plugin uses the BitTorrent protocol to disseminate zone data. Multiple peers can
|
||||
connect and down- and upload the data. A couple of nodes can be `seed` only meaning they will update
|
||||
the torrent when their zone data changes. Non-`seed` peers will write received data back into the
|
||||
zonefile - once the torrent is fully downloaded.
|
||||
|
||||
## Syntax
|
||||
|
||||
The simplest syntax is for a peer wanting to receive the zone data:
|
||||
|
||||
~~~ txt
|
||||
torrent DBFILE
|
||||
~~~
|
||||
|
||||
* **DBFILE** the zone database file to torrent. If the path is relative, the path from the
|
||||
*root* plugin will be prepended to it.
|
||||
|
||||
For peers seeding the torrent use this, slightly expanded, syntax
|
||||
|
||||
~~~ txt
|
||||
torrent DBFILE {
|
||||
dht
|
||||
// directory (temp storage)
|
||||
// port for dth, etc.
|
||||
}
|
||||
~~~
|
||||
|
||||
* `dth` starts a DTH server on address
|
||||
|
||||
## Examples
|
||||
|
||||
~~~ txt
|
||||
example.org {
|
||||
file db.example.org
|
||||
torrent db.example.org
|
||||
}
|
||||
~~~
|
||||
|
||||
## Also See
|
||||
|
||||
## Bugs
|
||||
@@ -1,75 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
rtorrent "github.com/cenkalti/rain/torrent"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
|
||||
"github.com/caddyserver/caddy"
|
||||
)
|
||||
|
||||
func init() { plugin.Register("torrent", setup) }
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
tor, err := parse(c)
|
||||
if err != nil {
|
||||
return plugin.Error("torrent", err)
|
||||
}
|
||||
|
||||
c.OnStartup(func() error {
|
||||
err := tor.Do()
|
||||
return err
|
||||
})
|
||||
c.OnShutdown(func() error {
|
||||
close(tor.stop)
|
||||
return nil
|
||||
})
|
||||
|
||||
// Don't call AddPlugin, *sign* is not a plugin.
|
||||
return nil
|
||||
}
|
||||
|
||||
func parse(c *caddy.Controller) (*Torrent, error) {
|
||||
t := &Torrent{stop: make(chan struct{})}
|
||||
config := dnsserver.GetConfig(c)
|
||||
|
||||
for c.Next() {
|
||||
if !c.NextArg() {
|
||||
return nil, c.ArgErr()
|
||||
}
|
||||
dbfile := c.Val()
|
||||
if !filepath.IsAbs(dbfile) && config.Root != "" {
|
||||
dbfile = filepath.Join(config.Root, dbfile)
|
||||
}
|
||||
t.dbfile = dbfile
|
||||
|
||||
for c.NextBlock() {
|
||||
switch c.Val() {
|
||||
case "dht":
|
||||
t.dht = true
|
||||
default:
|
||||
return nil, c.Errf("unknown property '%s'", c.Val())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
exp *Torrent
|
||||
}{
|
||||
{`torrent testdata/db.miek.nl {
|
||||
dht
|
||||
}`,
|
||||
false,
|
||||
&Torrent{dbfile: "testdata/db.miek.nl", dht: true},
|
||||
},
|
||||
{`torrent testdata/db.miek.nl`,
|
||||
false,
|
||||
&Torrent{dbfile: "testdata/db.miek.nl"},
|
||||
},
|
||||
// errors
|
||||
{`torrent db.example.org {
|
||||
bla
|
||||
}`,
|
||||
true,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for i, tc := range tests {
|
||||
c := caddy.NewTestController("dns", tc.input)
|
||||
tor, err := parse(c)
|
||||
|
||||
if err == nil && tc.shouldErr {
|
||||
t.Fatalf("Test %d expected errors, but got no error", i)
|
||||
}
|
||||
if err != nil && !tc.shouldErr {
|
||||
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
|
||||
}
|
||||
if tc.shouldErr {
|
||||
continue
|
||||
}
|
||||
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.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
17
plugin/torrent/testdata/db.miek.nl
vendored
@@ -1,17 +0,0 @@
|
||||
$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
|
||||
@@ -1,9 +0,0 @@
|
||||
package torrent
|
||||
|
||||
// Torrent contains the file data that needs to be torrented.
|
||||
type Torrent struct {
|
||||
dbfile string
|
||||
dht bool
|
||||
|
||||
stop chan struct{}
|
||||
}
|
||||
Reference in New Issue
Block a user