2020-07-27 10:15:49 +02:00
|
|
|
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 {
|
2020-07-27 15:55:31 +02:00
|
|
|
return plugin.Error("torrent", err)
|
2020-07-27 10:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.OnStartup(func() error {
|
2020-07-27 15:55:31 +02:00
|
|
|
err := tor.Do()
|
|
|
|
|
return err
|
2020-07-27 10:15:49 +02:00
|
|
|
})
|
|
|
|
|
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) {
|
2020-07-27 15:55:31 +02:00
|
|
|
t := &Torrent{stop: make(chan struct{})}
|
2020-07-27 10:15:49 +02:00
|
|
|
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() {
|
2020-07-27 15:55:31 +02:00
|
|
|
case "dht":
|
|
|
|
|
t.dht = true
|
2020-07-27 10:15:49 +02:00
|
|
|
default:
|
|
|
|
|
return nil, c.Errf("unknown property '%s'", c.Val())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
|
}
|