2020-04-27 16:46:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-08 17:19:25 +00:00
|
|
|
"fmt"
|
2020-04-27 16:46:23 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/v2/filecache"
|
2020-11-08 17:19:25 +00:00
|
|
|
t "github.com/anacrolix/torrent"
|
2020-04-27 16:46:23 +00:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2021-03-01 16:43:28 +00:00
|
|
|
"github.com/distribyted/distribyted/config"
|
2021-04-04 17:24:58 +00:00
|
|
|
"github.com/distribyted/distribyted/fs"
|
2021-03-01 16:43:28 +00:00
|
|
|
"github.com/distribyted/distribyted/fuse"
|
|
|
|
"github.com/distribyted/distribyted/http"
|
|
|
|
"github.com/distribyted/distribyted/stats"
|
|
|
|
"github.com/distribyted/distribyted/torrent"
|
2021-03-01 18:04:59 +00:00
|
|
|
"github.com/distribyted/distribyted/webdav"
|
2021-03-10 09:54:56 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-08 17:19:25 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-04-27 16:46:23 +00:00
|
|
|
)
|
|
|
|
|
2021-03-10 09:54:56 +00:00
|
|
|
func init() {
|
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
const (
|
2020-11-13 11:06:33 +00:00
|
|
|
configFlag = "config"
|
|
|
|
fuseAllowOther = "fuse-allow-other"
|
|
|
|
portFlag = "http-port"
|
2021-03-01 18:04:59 +00:00
|
|
|
webDAVPortFlag = "webdav-port"
|
2020-11-08 17:19:25 +00:00
|
|
|
)
|
2020-07-14 11:55:08 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
func main() {
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "distribyted",
|
|
|
|
Usage: "Torrent client with on-demand file downloading as a filesystem.",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: configFlag,
|
2020-11-14 15:28:50 +00:00
|
|
|
Value: "./distribyted-data/config/config.yaml",
|
2020-11-08 17:19:25 +00:00
|
|
|
EnvVars: []string{"DISTRIBYTED_CONFIG"},
|
|
|
|
Usage: "YAML file containing distribyted configuration.",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: portFlag,
|
|
|
|
Value: 4444,
|
|
|
|
EnvVars: []string{"DISTRIBYTED_HTTP_PORT"},
|
2021-03-01 18:04:59 +00:00
|
|
|
Usage: "HTTP port for web interface.",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: webDAVPortFlag,
|
|
|
|
Value: 36911,
|
|
|
|
EnvVars: []string{"DISTRIBYTED_WEBDAV_PORT"},
|
|
|
|
Usage: "Port used for WebDAV interface.",
|
2020-11-13 11:06:33 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: fuseAllowOther,
|
|
|
|
Value: false,
|
|
|
|
EnvVars: []string{"DISTRIBYTED_FUSE_ALLOW_OTHER"},
|
|
|
|
Usage: "Allow other users to access all fuse mountpoints. You need to add user_allow_other flag to /etc/fuse.conf file.",
|
2020-11-08 17:19:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Action: func(c *cli.Context) error {
|
2021-03-01 18:04:59 +00:00
|
|
|
err := load(c.String(configFlag), c.Int(portFlag), c.Int(webDAVPortFlag), c.Bool(fuseAllowOther))
|
2020-11-08 17:19:25 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
|
|
|
|
HideHelpCommand: true,
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Fatal().Err(err).Msg("problem starting application")
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
func newCache(folder string) (*filecache.Cache, error) {
|
|
|
|
if err := os.MkdirAll(folder, 0744); err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating metadata folder: %w", err)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
return filecache.NewCache(folder)
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error {
|
2020-11-08 17:19:25 +00:00
|
|
|
ch := config.NewHandler(configPath)
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
conf, err := ch.Get()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading configuration: %w", err)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
fc, err := newCache(conf.Torrent.MetadataFolder)
|
2020-04-27 16:46:23 +00:00
|
|
|
if err != nil {
|
2020-11-08 17:19:25 +00:00
|
|
|
return fmt.Errorf("error creating cache: %w", err)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st := storage.NewResourcePieces(fc.AsResourceProvider())
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
c, err := torrent.NewClient(st, conf.Torrent)
|
2020-04-27 16:46:23 +00:00
|
|
|
if err != nil {
|
2020-11-08 17:19:25 +00:00
|
|
|
return fmt.Errorf("error starting torrent client: %w", err)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:18 +00:00
|
|
|
ss := stats.NewTorrent()
|
2021-03-01 18:04:59 +00:00
|
|
|
|
|
|
|
th := torrent.NewHandler(c, ss)
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
mh := fuse.NewHandler(fuseAllowOther || conf.Fuse.AllowOther, conf.Fuse.Path)
|
2020-05-02 12:06:18 +00:00
|
|
|
|
2020-04-27 16:46:23 +00:00
|
|
|
sigChan := make(chan os.Signal)
|
2021-01-13 13:31:46 +00:00
|
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
2020-04-27 16:46:23 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
2021-03-01 18:04:59 +00:00
|
|
|
tryClose(c, mh)
|
2020-04-27 16:46:23 +00:00
|
|
|
}()
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
ch.OnReload(func(c *config.Root, ef config.EventFunc) error {
|
|
|
|
ef("unmounting filesystems")
|
2021-04-04 17:24:58 +00:00
|
|
|
mh.Unmount()
|
2021-03-01 18:04:59 +00:00
|
|
|
th.RemoveAll()
|
2020-05-08 14:37:43 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
ef(fmt.Sprintf("setting cache size to %d MB", c.Torrent.GlobalCacheSize))
|
|
|
|
fc.SetCapacity(c.Torrent.GlobalCacheSize * 1024 * 1024)
|
2020-05-18 17:42:23 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
for _, mp := range c.Routes {
|
|
|
|
ef(fmt.Sprintf("loading %v with %d torrents...", mp.Name, len(mp.Torrents)))
|
|
|
|
if err := th.Load(mp.Name, mp.Torrents); err != nil {
|
|
|
|
return fmt.Errorf("error loading route %v: %w", mp.Name, err)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
2021-04-04 17:24:58 +00:00
|
|
|
ef(fmt.Sprintf("%v loaded", mp.Name))
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
return mh.Mount(th.Fileststems(), ef)
|
2021-03-01 18:04:59 +00:00
|
|
|
|
2020-05-08 14:37:43 +00:00
|
|
|
})
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
if err := ch.Reload(nil); err != nil {
|
|
|
|
return fmt.Errorf("error reloading configuration: %w", err)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
2020-08-02 19:38:53 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
defer func() {
|
2021-03-01 18:04:59 +00:00
|
|
|
tryClose(c, mh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if conf.WebDAV != nil {
|
2021-04-04 17:24:58 +00:00
|
|
|
port = webDAVPort
|
|
|
|
if port == 0 {
|
|
|
|
port = conf.WebDAV.Port
|
2021-03-01 18:04:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
cfs, err := fs.NewContainerFs(th.Fileststems())
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error adding files to webDAV")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:50:18 +00:00
|
|
|
if err := webdav.NewWebDAVServer(cfs, port, conf.WebDAV.User, conf.WebDAV.Pass); err != nil {
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Error().Err(err).Msg("error starting webDAV")
|
2021-03-01 18:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-04 17:24:58 +00:00
|
|
|
|
|
|
|
log.Warn().Msg("webDAV configuration not found!")
|
2020-11-08 17:19:25 +00:00
|
|
|
}()
|
|
|
|
|
2021-01-02 19:09:05 +00:00
|
|
|
err = http.New(fc, ss, ch, port)
|
|
|
|
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Error().Err(err).Msg("error initializing HTTP server")
|
2021-01-02 19:09:05 +00:00
|
|
|
|
|
|
|
return err
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
2020-07-27 09:20:19 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
func tryClose(c *t.Client, mountService *fuse.Handler) {
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Info().Msg("closing torrent client...")
|
2020-07-27 09:20:19 +00:00
|
|
|
c.Close()
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Info().Msg("unmounting fuse filesystem...")
|
2021-04-04 17:24:58 +00:00
|
|
|
mountService.Unmount()
|
2020-07-27 09:20:19 +00:00
|
|
|
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Info().Msg("exiting")
|
2020-07-27 09:20:19 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|