feat(clients): add support for qBittorrent 4.4.0+ (#558)

* refactor: move client to go-qbittorrent

* refactor: move client to go-qbittorrent

* feat(downloadclient): cache qbittorrent client

* feat(downloadclient): update qbit

* feat(downloadclient): client test and remove pkg qbit

* feat(downloadclient): update pkg qbit

* fix(release): method

* feat(release): make GetCachedClient concurrent safe

* feat(release): add additional tests for buildLegacyHost

* feat(release): remove branching

* chore: update pkg autobrr/go-qbittorrent to v.1.2.0
This commit is contained in:
ze0s 2022-12-10 19:25:04 +01:00 committed by GitHub
parent 6ad4abe296
commit 29da2416ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 379 additions and 1764 deletions

View file

@ -2,73 +2,29 @@ package action
import (
"context"
"strings"
"time"
"github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/qbittorrent"
"github.com/autobrr/go-qbittorrent"
)
const ReannounceMaxAttempts = 50
const ReannounceInterval = 7000
func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]string, error) {
func (s *service) qbittorrent(ctx context.Context, action domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action qBittorrent: %v", action.Name)
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
return nil, errors.Wrap(err, "error finding client: %v", action.ClientID)
}
c := s.clientSvc.GetCachedClient(ctx, action.ClientID)
if client == nil {
return nil, errors.New("could not find client by id: %v", action.ClientID)
}
qbtSettings := qbittorrent.Settings{
Name: client.Name,
Hostname: client.Host,
Port: uint(client.Port),
Username: client.Username,
Password: client.Password,
TLS: client.TLS,
TLSSkipVerify: client.TLSSkipVerify,
}
// setup sub logger adapter which is compatible with *log.Logger
qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel)
// only set basic auth if enabled
if client.Settings.Basic.Auth {
qbtSettings.BasicAuth = client.Settings.Basic.Auth
qbtSettings.Basic.Username = client.Settings.Basic.Username
qbtSettings.Basic.Password = client.Settings.Basic.Password
}
qbt := qbittorrent.NewClient(qbtSettings)
// only login if we have a password
if qbtSettings.Password != "" {
if err = qbt.Login(); err != nil {
return nil, errors.Wrap(err, "could not log into client: %v at %v", client.Name, client.Host)
}
}
rejections, err := s.qbittorrentCheckRulesCanDownload(action, client, qbt)
rejections, err := s.qbittorrentCheckRulesCanDownload(ctx, action, c.Dc, c.Qbt)
if err != nil {
return nil, errors.Wrap(err, "error checking client rules: %v", action.Name)
}
if rejections != nil {
if len(rejections) > 0 {
return rejections, nil
}
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
return nil, errors.Wrap(err, "error downloading torrent file for release: %v", release.TorrentName)
}
}
@ -83,38 +39,41 @@ func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]s
s.log.Trace().Msgf("action qBittorrent options: %+v", options)
if err = qbt.AddTorrentFromFile(release.TorrentTmpFile, options); err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
if err = c.Qbt.AddTorrentFromFileCtx(ctx, release.TorrentTmpFile, options); err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, c.Dc.Name)
}
if !action.Paused && !action.ReAnnounceSkip && release.TorrentHash != "" {
if err := s.reannounceTorrent(qbt, action, release.TorrentHash); err != nil {
opts := qbittorrent.ReannounceOptions{
Interval: int(action.ReAnnounceInterval),
MaxAttempts: int(action.ReAnnounceMaxAttempts),
DeleteOnFailure: action.ReAnnounceDelete,
}
if err := c.Qbt.ReannounceTorrentWithRetry(ctx, opts, release.TorrentHash); err != nil {
return nil, errors.Wrap(err, "could not reannounce torrent: %v", release.TorrentHash)
}
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, client.Name)
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, c.Dc.Name)
return nil, nil
}
func (s *service) prepareQbitOptions(action domain.Action, m domain.Macro) (map[string]string, error) {
opts := &qbittorrent.TorrentAddOptions{}
opts.Paused = false
if action.Paused {
opts.Paused = BoolPointer(true)
opts.Paused = true
}
if action.SkipHashCheck {
opts.SkipHashCheck = BoolPointer(true)
opts.SkipHashCheck = true
}
if action.ContentLayout != "" {
if action.ContentLayout == domain.ActionContentLayoutSubfolderCreate {
layout := qbittorrent.ContentLayoutSubfolderCreate
opts.ContentLayout = &layout
opts.ContentLayout = qbittorrent.ContentLayoutSubfolderCreate
} else if action.ContentLayout == domain.ActionContentLayoutSubfolderNone {
layout := qbittorrent.ContentLayoutSubfolderNone
opts.ContentLayout = &layout
opts.ContentLayout = qbittorrent.ContentLayoutSubfolderNone
}
// if ORIGINAL then leave empty
}
@ -125,8 +84,8 @@ func (s *service) prepareQbitOptions(action domain.Action, m domain.Macro) (map[
return nil, errors.Wrap(err, "could not parse savepath macro: %v", action.SavePath)
}
opts.SavePath = &actionArgs
opts.AutoTMM = BoolPointer(false)
opts.SavePath = actionArgs
opts.AutoTMM = false
}
if action.Category != "" {
// parse and replace values in argument string before continuing
@ -135,7 +94,7 @@ func (s *service) prepareQbitOptions(action domain.Action, m domain.Macro) (map[
return nil, errors.Wrap(err, "could not parse category macro: %v", action.Category)
}
opts.Category = &categoryArgs
opts.Category = categoryArgs
}
if action.Tags != "" {
// parse and replace values in argument string before continuing
@ -144,34 +103,30 @@ func (s *service) prepareQbitOptions(action domain.Action, m domain.Macro) (map[
return nil, errors.Wrap(err, "could not parse tags macro: %v", action.Tags)
}
opts.Tags = &tagsArgs
opts.Tags = tagsArgs
}
if action.LimitUploadSpeed > 0 {
opts.LimitUploadSpeed = &action.LimitUploadSpeed
opts.LimitUploadSpeed = action.LimitUploadSpeed
}
if action.LimitDownloadSpeed > 0 {
opts.LimitDownloadSpeed = &action.LimitDownloadSpeed
opts.LimitDownloadSpeed = action.LimitDownloadSpeed
}
if action.LimitRatio > 0 {
opts.LimitRatio = &action.LimitRatio
opts.LimitRatio = action.LimitRatio
}
if action.LimitSeedTime > 0 {
opts.LimitSeedTime = &action.LimitSeedTime
opts.LimitSeedTime = action.LimitSeedTime
}
return opts.Prepare(), nil
}
func BoolPointer(b bool) *bool {
return &b
}
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
func (s *service) qbittorrentCheckRulesCanDownload(ctx context.Context, action domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
// check for active downloads and other rules
if client.Settings.Rules.Enabled && !action.IgnoreRules {
activeDownloads, err := qbt.GetTorrentsActiveDownloads()
activeDownloads, err := qbt.GetTorrentsActiveDownloadsCtx(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not fetch active downloads")
}
@ -179,11 +134,11 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client
// make sure it's not set to 0 by default
if client.Settings.Rules.MaxActiveDownloads > 0 {
// if max active downloads reached, check speed and if lower than threshold add anyways
// if max active downloads reached, check speed and if lower than threshold add anyway
if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads {
if client.Settings.Rules.IgnoreSlowTorrents {
// check speeds of downloads
info, err := qbt.GetTransferInfo()
info, err := qbt.GetTransferInfoCtx(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not get transfer info")
}
@ -210,109 +165,3 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client
return nil, nil
}
func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action, hash string) error {
announceOK := false
attempts := 0
interval := ReannounceInterval
if action.ReAnnounceInterval > 0 {
interval = int(action.ReAnnounceInterval)
}
maxAttempts := ReannounceMaxAttempts
if action.ReAnnounceMaxAttempts > 0 {
maxAttempts = int(action.ReAnnounceMaxAttempts)
}
for attempts < maxAttempts {
s.log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts)
// add delay for next run
time.Sleep(time.Duration(interval) * time.Second)
trackers, err := qb.GetTorrentTrackers(hash)
if err != nil {
return errors.Wrap(err, "could not get trackers for torrent with hash: %v", hash)
}
if trackers == nil {
attempts++
continue
}
s.log.Trace().Msgf("qBittorrent - run re-announce %v attempt: %v trackers (%+v)", hash, attempts, trackers)
// check if status not working or something else
working := isTrackerStatusOK(trackers)
if working {
s.log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
announceOK = true
// if working lets return
return nil
}
s.log.Trace().Msgf("qBittorrent - not working yet, lets re-announce %v attempt: %v", hash, attempts)
err = qb.ReAnnounceTorrents([]string{hash})
if err != nil {
return errors.Wrap(err, "could not re-announce torrent with hash: %v", hash)
}
attempts++
}
// delete on failure to reannounce
if !announceOK && action.ReAnnounceDelete {
s.log.Debug().Msgf("qBittorrent - re-announce for %v took too long, deleting torrent", hash)
err := qb.DeleteTorrents([]string{hash}, false)
if err != nil {
return errors.Wrap(err, "could not delete torrent with hash: %v", hash)
}
}
return nil
}
// Check if status not working or something else
// https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-trackers
//
// 0 Tracker is disabled (used for DHT, PeX, and LSD)
// 1 Tracker has not been contacted yet
// 2 Tracker has been contacted and is working
// 3 Tracker is updating
// 4 Tracker has been contacted, but it is not working (or doesn't send proper replies)
func isTrackerStatusOK(trackers []qbittorrent.TorrentTracker) bool {
for _, tracker := range trackers {
if tracker.Status == qbittorrent.TrackerStatusDisabled {
continue
}
// check for certain messages before the tracker status to catch ok status with unreg msg
if isUnregistered(tracker.Message) {
return false
}
if tracker.Status == qbittorrent.TrackerStatusOK {
return true
}
}
return false
}
func isUnregistered(msg string) bool {
words := []string{"unregistered", "not registered", "not found", "not exist"}
msg = strings.ToLower(msg)
for _, v := range words {
if strings.Contains(msg, v) {
return true
}
}
return false
}

View file

@ -2,6 +2,7 @@ package action
import (
"bytes"
"context"
"crypto/tls"
"io"
"net/http"
@ -14,7 +15,7 @@ import (
"github.com/autobrr/autobrr/pkg/errors"
)
func (s *service) RunAction(action *domain.Action, release domain.Release) ([]string, error) {
func (s *service) RunAction(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error) {
var (
err error
@ -40,13 +41,13 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
err = s.watchFolder(*action, release)
case domain.ActionTypeWebhook:
err = s.webhook(*action, release)
err = s.webhook(ctx, *action, release)
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
rejections, err = s.deluge(*action, release)
case domain.ActionTypeQbittorrent:
rejections, err = s.qbittorrent(*action, release)
rejections, err = s.qbittorrent(ctx, *action, release)
case domain.ActionTypeRTorrent:
rejections, err = s.rtorrent(*action, release)
@ -86,12 +87,12 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
}
payload := &domain.NotificationPayload{
Event: domain.NotificationEventPushApproved,
ReleaseName: release.TorrentName,
Filter: release.Filter.Name,
Indexer: release.Indexer,
InfoHash: release.TorrentHash,
Event: domain.NotificationEventPushApproved,
ReleaseName: release.TorrentName,
Filter: release.Filter.Name,
Indexer: release.Indexer,
InfoHash: release.TorrentHash,
Size: release.Size,
Status: domain.ReleasePushStatusApproved,
Action: action.Name,
@ -208,10 +209,10 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro
return nil
}
func (s *service) webhook(action domain.Action, release domain.Release) error {
func (s *service) webhook(ctx context.Context, action domain.Action, release domain.Release) error {
// if webhook data contains TorrentPathName or TorrentDataRawBytes, lets download the torrent file
if release.TorrentTmpFile == "" && (strings.Contains(action.WebhookData, "TorrentPathName") || strings.Contains(action.WebhookData, "TorrentDataRawBytes")) {
if err := release.DownloadTorrentFile(); err != nil {
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
return errors.Wrap(err, "webhook: could not download torrent file for release: %v", release.TorrentName)
}
}
@ -245,7 +246,7 @@ func (s *service) webhook(action domain.Action, release domain.Release) error {
client := http.Client{Transport: t, Timeout: 15 * time.Second}
req, err := http.NewRequest(http.MethodPost, action.WebhookHost, bytes.NewBufferString(dataArgs))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, action.WebhookHost, bytes.NewBufferString(dataArgs))
if err != nil {
return errors.Wrap(err, "could not build request for webhook")
}

View file

@ -7,7 +7,6 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/download_client"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/qbittorrent"
"github.com/asaskevich/EventBus"
"github.com/dcarbone/zadapters/zstdlog"
@ -21,12 +20,7 @@ type Service interface {
DeleteByFilterID(ctx context.Context, filterID int) error
ToggleEnabled(actionID int) error
RunAction(action *domain.Action, release domain.Release) ([]string, error)
}
type qbitKey struct {
I int // type
N string // name
RunAction(ctx context.Context, action *domain.Action, release domain.Release) ([]string, error)
}
type service struct {
@ -35,17 +29,14 @@ type service struct {
repo domain.ActionRepo
clientSvc download_client.Service
bus EventBus.Bus
qbitClients map[qbitKey]qbittorrent.Client
}
func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
s := &service{
log: log.With().Str("module", "action").Logger(),
repo: repo,
clientSvc: clientSvc,
bus: bus,
qbitClients: map[qbitKey]qbittorrent.Client{},
log: log.With().Str("module", "action").Logger(),
repo: repo,
clientSvc: clientSvc,
bus: bus,
}
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)

View file

@ -1,6 +1,12 @@
package domain
import "context"
import (
"context"
"fmt"
"net/url"
"github.com/autobrr/go-qbittorrent"
)
type DownloadClientRepo interface {
List(ctx context.Context) ([]DownloadClient, error)
@ -24,6 +30,11 @@ type DownloadClient struct {
Settings DownloadClientSettings `json:"settings,omitempty"`
}
type DownloadClientCached struct {
Dc *DownloadClient
Qbt *qbittorrent.Client
}
type DownloadClientSettings struct {
APIKey string `json:"apikey,omitempty"`
Basic BasicAuth `json:"basic,omitempty"`
@ -57,3 +68,48 @@ const (
DownloadClientTypeWhisparr DownloadClientType = "WHISPARR"
DownloadClientTypeReadarr DownloadClientType = "READARR"
)
func (c DownloadClient) BuildLegacyHost() string {
if c.Type == DownloadClientTypeQbittorrent {
return c.qbitBuildLegacyHost()
}
return ""
}
// qbitBuildLegacyHost exists to support older configs
func (c DownloadClient) qbitBuildLegacyHost() string {
// parse url
u, _ := url.Parse(c.Host)
// reset Opaque
u.Opaque = ""
// set scheme
scheme := "http"
if c.TLS {
scheme = "https"
}
u.Scheme = scheme
// if host is empty lets use one from settings
if u.Host == "" {
u.Host = c.Host
}
// reset Path
if u.Host == u.Path {
u.Path = ""
}
// handle ports
if c.Port > 0 {
if c.Port == 80 || c.Port == 443 {
// skip for regular http and https
} else {
u.Host = fmt.Sprintf("%v:%v", u.Host, c.Port)
}
}
// make into new string and return
return u.String()
}

View file

@ -0,0 +1,155 @@
package domain
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDownloadClient_qbitBuildLegacyHost(t *testing.T) {
type fields struct {
ID int
Name string
Type DownloadClientType
Enabled bool
Host string
Port int
TLS bool
TLSSkipVerify bool
Username string
Password string
Settings DownloadClientSettings
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "build_url_1",
fields: fields{
Host: "https://qbit.domain.ltd",
Port: 0,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
},
want: "https://qbit.domain.ltd",
},
{
name: "build_url_2",
fields: fields{
Host: "http://qbit.domain.ltd",
Port: 0,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
},
want: "http://qbit.domain.ltd",
},
{
name: "build_url_3",
fields: fields{
Host: "https://qbit.domain.ltd:8080",
Port: 0,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
},
want: "https://qbit.domain.ltd:8080",
},
{
name: "build_url_4",
fields: fields{
Host: "qbit.domain.ltd:8080",
Port: 0,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
},
want: "http://qbit.domain.ltd:8080",
},
{
name: "build_url_5",
fields: fields{
Host: "qbit.domain.ltd",
Port: 8080,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
},
want: "http://qbit.domain.ltd:8080",
},
{
name: "build_url_6",
fields: fields{
Host: "qbit.domain.ltd",
Port: 443,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
},
want: "https://qbit.domain.ltd",
},
{
name: "build_url_7",
fields: fields{
Host: "qbit.domain.ltd",
Port: 10200,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
},
want: "http://qbit.domain.ltd:10200",
},
{
name: "build_url_8",
fields: fields{
Host: "https://domain.ltd/qbittorrent",
Port: 0,
Username: "",
Password: "",
TLS: true,
TLSSkipVerify: false,
},
want: "https://domain.ltd/qbittorrent",
},
{
name: "build_url_9",
fields: fields{
Host: "127.0.0.1",
Port: 8080,
Username: "",
Password: "",
TLS: false,
TLSSkipVerify: false,
},
want: "http://127.0.0.1:8080",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := DownloadClient{
ID: tt.fields.ID,
Name: tt.fields.Name,
Type: tt.fields.Type,
Enabled: tt.fields.Enabled,
Host: tt.fields.Host,
Port: tt.fields.Port,
TLS: tt.fields.TLS,
TLSSkipVerify: tt.fields.TLSSkipVerify,
Username: tt.fields.Username,
Password: tt.fields.Password,
Settings: tt.fields.Settings,
}
assert.Equalf(t, tt.want, c.qbitBuildLegacyHost(), "qbitBuildLegacyHost()")
})
}
}

View file

@ -273,7 +273,15 @@ func (r *Release) ParseSizeBytesString(size string) {
r.Size = s
}
func (r *Release) DownloadTorrentFileCtx(ctx context.Context) error {
return r.downloadTorrentFile(ctx)
}
func (r *Release) DownloadTorrentFile() error {
return r.downloadTorrentFile(context.Background())
}
func (r *Release) downloadTorrentFile(ctx context.Context) error {
if r.TorrentURL == "" {
return errors.New("download_file: url can't be empty")
} else if r.TorrentTmpFile != "" {
@ -294,7 +302,7 @@ func (r *Release) DownloadTorrentFile() error {
Timeout: time.Second * 45,
}
req, err := http.NewRequest(http.MethodGet, r.TorrentURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.TorrentURL, nil)
if err != nil {
return errors.Wrap(err, "error downloading file")
}

View file

@ -7,21 +7,21 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/lidarr"
"github.com/autobrr/autobrr/pkg/qbittorrent"
"github.com/autobrr/autobrr/pkg/radarr"
"github.com/autobrr/autobrr/pkg/readarr"
"github.com/autobrr/autobrr/pkg/sonarr"
"github.com/autobrr/autobrr/pkg/whisparr"
"github.com/autobrr/go-qbittorrent"
delugeClient "github.com/gdm85/go-libdeluge"
"github.com/hekmon/transmissionrpc/v2"
"github.com/mrobinsn/go-rtorrent/rtorrent"
)
func (s *service) testConnection(client domain.DownloadClient) error {
func (s *service) testConnection(ctx context.Context, client domain.DownloadClient) error {
switch client.Type {
case domain.DownloadClientTypeQbittorrent:
return s.testQbittorrentConnection(client)
return s.testQbittorrentConnection(ctx, client)
case domain.DownloadClientTypeDelugeV1, domain.DownloadClientTypeDelugeV2:
return s.testDelugeConnection(client)
@ -51,32 +51,28 @@ func (s *service) testConnection(client domain.DownloadClient) error {
}
}
func (s *service) testQbittorrentConnection(client domain.DownloadClient) error {
qbtSettings := qbittorrent.Settings{
Hostname: client.Host,
Port: uint(client.Port),
func (s *service) testQbittorrentConnection(ctx context.Context, client domain.DownloadClient) error {
qbtSettings := qbittorrent.Config{
Host: client.BuildLegacyHost(),
Username: client.Username,
Password: client.Password,
TLS: client.TLS,
TLSSkipVerify: client.TLSSkipVerify,
Log: s.subLogger,
}
// only set basic auth if enabled
if client.Settings.Basic.Auth {
qbtSettings.BasicAuth = client.Settings.Basic.Auth
qbtSettings.Basic.Username = client.Settings.Basic.Username
qbtSettings.Basic.Password = client.Settings.Basic.Password
qbtSettings.BasicUser = client.Settings.Basic.Username
qbtSettings.BasicPass = client.Settings.Basic.Password
}
qbt := qbittorrent.NewClient(qbtSettings)
if err := qbt.Login(); err != nil {
if err := qbt.LoginCtx(ctx); err != nil {
return errors.Wrap(err, "error logging into client: %v", client.Host)
}
_, err := qbt.GetTorrents()
if err != nil {
if _, err := qbt.GetTorrentsCtx(ctx, qbittorrent.TorrentFilterOptions{Filter: qbittorrent.TorrentFilterAll}); err != nil {
return errors.Wrap(err, "error getting torrents: %v", client.Host)
}

View file

@ -4,10 +4,12 @@ import (
"context"
"errors"
"log"
"sync"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/go-qbittorrent"
"github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
)
@ -18,19 +20,27 @@ type Service interface {
Store(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Update(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Delete(ctx context.Context, clientID int) error
Test(client domain.DownloadClient) error
Test(ctx context.Context, client domain.DownloadClient) error
GetCachedClient(ctx context.Context, clientId int32) *domain.DownloadClientCached
}
type service struct {
log zerolog.Logger
repo domain.DownloadClientRepo
subLogger *log.Logger
qbitClients map[int32]*domain.DownloadClientCached
m sync.RWMutex
}
func NewService(log logger.Logger, repo domain.DownloadClientRepo) Service {
s := &service{
log: log.With().Str("module", "download_client").Logger(),
repo: repo,
qbitClients: map[int32]*domain.DownloadClientCached{},
m: sync.RWMutex{},
}
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)
@ -91,19 +101,29 @@ func (s *service) Update(ctx context.Context, client domain.DownloadClient) (*do
return nil, err
}
if client.Type == domain.DownloadClientTypeQbittorrent {
s.m.Lock()
delete(s.qbitClients, int32(client.ID))
s.m.Unlock()
}
return c, err
}
func (s *service) Delete(ctx context.Context, clientID int) error {
err := s.repo.Delete(ctx, clientID)
if err != nil {
if err := s.repo.Delete(ctx, clientID); err != nil {
s.log.Error().Err(err).Msgf("could not delete download client: %v", clientID)
return err
}
s.m.Lock()
delete(s.qbitClients, int32(clientID))
s.m.Unlock()
return nil
}
func (s *service) Test(client domain.DownloadClient) error {
func (s *service) Test(ctx context.Context, client domain.DownloadClient) error {
// basic validation of client
if client.Host == "" {
return errors.New("validation error: no host")
@ -112,10 +132,61 @@ func (s *service) Test(client domain.DownloadClient) error {
}
// test
if err := s.testConnection(client); err != nil {
if err := s.testConnection(ctx, client); err != nil {
s.log.Error().Err(err).Msg("client connection test error")
return err
}
return nil
}
func (s *service) GetCachedClient(ctx context.Context, clientId int32) *domain.DownloadClientCached {
// check if client exists in cache
s.m.RLock()
cached, ok := s.qbitClients[clientId]
s.m.RUnlock()
if ok {
return cached
}
// get client for action
client, err := s.FindByID(ctx, clientId)
if err != nil {
return nil
}
if client == nil {
return nil
}
qbtSettings := qbittorrent.Config{
Host: client.BuildLegacyHost(),
Username: client.Username,
Password: client.Password,
TLSSkipVerify: client.TLSSkipVerify,
}
// setup sub logger adapter which is compatible with *log.Logger
qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel)
// only set basic auth if enabled
if client.Settings.Basic.Auth {
qbtSettings.BasicUser = client.Settings.Basic.Username
qbtSettings.BasicPass = client.Settings.Basic.Password
}
qc := &domain.DownloadClientCached{
Dc: client,
Qbt: qbittorrent.NewClient(qbtSettings),
}
cached = qc
s.m.Lock()
s.qbitClients[clientId] = cached
s.m.Unlock()
return cached
}

View file

@ -17,7 +17,7 @@ type downloadClientService interface {
Store(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Update(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error)
Delete(ctx context.Context, clientID int) error
Test(client domain.DownloadClient) error
Test(ctx context.Context, client domain.DownloadClient) error
}
type downloadClientHandler struct {
@ -77,8 +77,7 @@ func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
return
}
err := h.service.Test(data)
if err != nil {
if err := h.service.Test(r.Context(), data); err != nil {
h.encoder.Error(w, err)
return
}

View file

@ -86,6 +86,8 @@ func (s *service) Process(release *domain.Release) {
return
}
ctx := context.Background()
// TODO check in config for "Save all releases"
// TODO cross-seed check
// TODO dupe checks
@ -133,8 +135,7 @@ func (s *service) Process(release *domain.Release) {
// save release here to only save those with rejections from actions instead of all releases
if release.ID == 0 {
release.FilterStatus = domain.ReleaseStatusFilterApproved
err = s.Store(context.Background(), release)
if err != nil {
if err = s.Store(ctx, release); err != nil {
l.Error().Err(err).Msgf("release.Process: error writing release to database: %+v", release)
return
}
@ -166,7 +167,7 @@ func (s *service) Process(release *domain.Release) {
continue
}
rejections, err = s.actionSvc.RunAction(a, *release)
rejections, err = s.actionSvc.RunAction(ctx, a, *release)
if err != nil {
l.Error().Stack().Err(err).Msgf("release.Process: error running actions for filter: %v", release.Filter.Name)
continue