This commit is contained in:
royalcat 2025-01-20 05:18:15 +03:00
parent a89b9e7303
commit 832f5b9710
33 changed files with 900 additions and 1086 deletions

View file

@ -7,9 +7,9 @@ import (
"time"
"git.kmsign.ru/royalcat/tstor/pkg/qbittorrent"
"github.com/creativecreature/sturdyc"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/royalcat/btrgo/btrsync"
"github.com/viccon/sturdyc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)

View file

@ -0,0 +1,66 @@
package qbittorrent
import (
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
)
type Config struct {
DataFolder string `koanf:"data_folder,omitempty"`
MetadataFolder string `koanf:"metadata_folder,omitempty"`
}
var defaultConfig = Config{
DataFolder: "./qbittorrent/data",
MetadataFolder: "./qbittorrent/metadata",
}
func loadConfig(koanf *koanf.Koanf) (Config, error) {
if err := koanf.Load(structs.Provider(defaultConf, "koanf"), nil); err != nil {
return Config{}, err
}
var config Config
if err := koanf.Unmarshal("", &config); err != nil {
return Config{}, err
}
return config, nil
}
// var defaultRoutes = []Route{
// {
// Name: "multimedia",
// Torrents: []Torrent{
// {
// MagnetURI: "magnet:?xt=urn:btih:c9e15763f722f23e98a29decdfae341b98d53056&dn=Cosmos+Laundromat&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fcosmos-laundromat.torrent",
// },
// {
// MagnetURI: "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent",
// },
// {
// MagnetURI: "magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent",
// },
// {
// MagnetURI: "magnet:?xt=urn:btih:209c8226b299b308beaf2b9cd3fb49212dbd13ec&dn=Tears+of+Steel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Ftears-of-steel.torrent",
// },
// {
// MagnetURI: "magnet:?xt=urn:btih:a88fda5954e89178c372716a6a78b8180ed4dad3&dn=The+WIRED+CD+-+Rip.+Sample.+Mash.+Share&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fwired-cd.torrent",
// },
// },
// },
// }
// var defaultServers = []Server{
// {
// Name: "server",
// Path: "server",
// Trackers: []string{
// "wss://tracker.btorrent.xyz",
// "wss://tracker.openwebtorrent.com",
// "http://p4p.arenabg.com:1337/announce",
// "udp://tracker.opentrackr.org:1337/announce",
// "udp://open.tracker.cl:1337/announce",
// "http://openbittorrent.com:80/announce",
// },
// },
// }

View file

@ -14,7 +14,7 @@ import (
"git.kmsign.ru/royalcat/tstor/pkg/qbittorrent"
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
"git.kmsign.ru/royalcat/tstor/src/config"
"git.kmsign.ru/royalcat/tstor/src/daemon"
"git.kmsign.ru/royalcat/tstor/src/logwrap"
"git.kmsign.ru/royalcat/tstor/src/vfs"
"github.com/anacrolix/torrent/metainfo"
@ -22,6 +22,7 @@ import (
infohash_v2 "github.com/anacrolix/torrent/types/infohash-v2"
mapset "github.com/deckarep/golang-set/v2"
"github.com/iceber/iouring-go"
"github.com/knadh/koanf/v2"
"github.com/royalcat/ctxio"
"go.opentelemetry.io/otel"
)
@ -52,12 +53,20 @@ WebUI\LocalHostAuth=false
WebUI\Password_PBKDF2="@ByteArray(qef5I4wZBkDG+PP6/5mQwA==:LoTmorQM/QM5RHI4+dOiu6xfAz9xak6fhR4ZGpRtJF3JNCGG081Yrtva4G71kXz//ODUuWQKTLlrZPuIDvzqUQ==)"
`
func NewDaemon(conf config.QBittorrent) (*Daemon, error) {
ctx := context.Background()
log := rlog.Component("qbittorrent")
const DaemonName = "qbittorrent"
binPath := conf.MetadataFolder + "/qbittorrent-nox"
err := downloadLatestQbitRelease(ctx, binPath)
var _ daemon.DaemonConstructor = NewDaemon
func NewDaemon(ctx context.Context, koanf *koanf.Koanf) (daemon.Daemon, error) {
log := rlog.Component(DaemonName)
config, err := loadConfig(koanf)
if err != nil {
return nil, err
}
binPath := config.MetadataFolder + "/qbittorrent-nox"
err = downloadLatestQbitRelease(ctx, binPath)
if err != nil {
return nil, err
}
@ -66,26 +75,26 @@ func NewDaemon(conf config.QBittorrent) (*Daemon, error) {
outLog := logwrap.NewSlogWriter(ctx, slog.LevelInfo, daemonLog.Slog())
errLog := logwrap.NewSlogWriter(ctx, slog.LevelError, daemonLog.Slog())
_, err = os.Stat(conf.MetadataFolder + "/profile/qBittorrent/config/qBittorrent.conf")
_, err = os.Stat(config.MetadataFolder + "/profile/qBittorrent/config/qBittorrent.conf")
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(conf.MetadataFolder+"/profile/qBittorrent/config", 0744)
err = os.MkdirAll(config.MetadataFolder+"/profile/qBittorrent/config", 0744)
if err != nil {
return nil, err
}
err = os.WriteFile(conf.MetadataFolder+"/profile/qBittorrent/config/qBittorrent.conf", []byte(defaultConf), 0644)
err = os.WriteFile(config.MetadataFolder+"/profile/qBittorrent/config/qBittorrent.conf", []byte(defaultConf), 0644)
if err != nil {
return nil, err
}
}
err = os.MkdirAll(conf.DataFolder, 0744)
err = os.MkdirAll(config.DataFolder, 0744)
if err != nil {
return nil, err
}
const port = 25436
proc, err := runQBittorrent(binPath, conf.MetadataFolder+"/profile", port, outLog, errLog)
proc, err := runQBittorrent(binPath, config.MetadataFolder+"/profile", port, outLog, errLog)
if err != nil {
return nil, err
}
@ -109,7 +118,7 @@ func NewDaemon(conf config.QBittorrent) (*Daemon, error) {
time.Sleep(time.Second)
}
dataDir, err := filepath.Abs(conf.DataFolder)
dataDir, err := filepath.Abs(config.DataFolder)
if err != nil {
return nil, err
}
@ -129,15 +138,23 @@ func NewDaemon(conf config.QBittorrent) (*Daemon, error) {
return &Daemon{
qb: qb,
proc: proc,
dataDir: conf.DataFolder,
dataDir: config.DataFolder,
ur: ur,
sourceFiles: make(map[string]string),
registeredTorrents: mapset.NewSet[string](),
client: wrapClient(qb),
log: rlog.Component("qbittorrent"),
log: rlog.Component(DaemonName),
}, nil
}
func (d *Daemon) Name() string {
return DaemonName
}
func (d *Daemon) Extensions() []string {
return []string{".torrent"}
}
func (d *Daemon) Close(ctx context.Context) error {
err := d.proc.Signal(os.Interrupt)
if err != nil {
@ -156,7 +173,7 @@ func torrentDataPath(dataDir string, ih string) (string, error) {
return filepath.Abs(path.Join(dataDir, ih))
}
func (fs *Daemon) GetTorrentFS(ctx context.Context, sourcePath string, file vfs.File) (vfs.Filesystem, error) {
func (fs *Daemon) GetFS(ctx context.Context, sourcePath string, file vfs.File) (vfs.Filesystem, error) {
ctx, span := trace.Start(ctx, "GetTorrentFS")
defer span.End()

View file

@ -0,0 +1,3 @@
module github.com/royalcat/tstor/daemons/qbittorrent
go 1.23.5

View file

@ -0,0 +1,10 @@
package main
import "git.kmsign.ru/royalcat/tstor/daemons/qbittorrent"
func main() {
}
const DaemonName = qbittorrent.DaemonName
var NewDaemon = qbittorrent.NewDaemon