fix(download-clients): qbit nil logger panic (#344)

The logger wasn't set, so it was always nil. Change to always initialize and override if one is passed.
This commit is contained in:
Ludvig Lundgren 2022-07-08 22:12:19 +02:00 committed by GitHub
parent 33e3691737
commit 401c93a657
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 39 deletions

View file

@ -15,11 +15,24 @@ import (
"strings"
"time"
"github.com/autobrr/autobrr/pkg/errors"
"golang.org/x/net/publicsuffix"
publicsuffix "golang.org/x/net/publicsuffix"
"github.com/autobrr/autobrr/pkg/errors"
)
type Client interface {
Login() error
GetTorrents() ([]Torrent, error)
GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error)
GetTorrentsActiveDownloads() ([]Torrent, error)
GetTorrentsRaw() (string, error)
GetTorrentTrackers(hash string) ([]TorrentTracker, error)
AddTorrentFromFile(file string, options map[string]string) error
DeleteTorrents(hashes []string, deleteFiles bool) error
ReAnnounceTorrents(hashes []string) error
GetTransferInfo() (*TransferInfo, error)
}
var (
backoffSchedule = []time.Duration{
5 * time.Second,
@ -29,15 +42,16 @@ var (
timeout = 60 * time.Second
)
type Client struct {
type client struct {
Name string
settings Settings
http *http.Client
Log *log.Logger
log *log.Logger
}
type Settings struct {
Name string
Hostname string
Port uint
Username string
@ -55,20 +69,23 @@ type Basic struct {
Password string
}
func NewClient(s Settings) *Client {
c := &Client{
settings: s,
func NewClient(settings Settings) Client {
c := &client{
settings: settings,
Name: settings.Name,
log: log.New(io.Discard, "", log.LstdFlags),
}
if s.Log == nil {
c.Log = log.New(io.Discard, "qbittorrent", log.LstdFlags)
// override logger if we pass one
if settings.Log != nil {
c.log = settings.Log
}
//store cookies in jar
jarOptions := &cookiejar.Options{PublicSuffixList: publicsuffix.List}
jar, err := cookiejar.New(jarOptions)
if err != nil {
c.Log.Println("new client cookie error")
c.log.Println("new client cookie error")
}
c.http = &http.Client{
@ -93,7 +110,7 @@ func NewClient(s Settings) *Client {
return c
}
func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *client) get(endpoint string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response
@ -117,7 +134,7 @@ func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, e
break
}
c.Log.Printf("qbit GET failed: retrying attempt %d - %v\n", i, reqUrl)
c.log.Printf("qbit GET failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
@ -129,7 +146,7 @@ func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, e
return resp, nil
}
func (c *Client) post(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *client) post(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
@ -164,7 +181,7 @@ func (c *Client) post(endpoint string, opts map[string]string) (*http.Response,
break
}
c.Log.Printf("qbit POST failed: retrying attempt %d - %v\n", i, reqUrl)
c.log.Printf("qbit POST failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
@ -176,7 +193,7 @@ func (c *Client) post(endpoint string, opts map[string]string) (*http.Response,
return resp, nil
}
func (c *Client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) {
func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) {
// add optional parameters that the user wants
form := url.Values{}
if opts != nil {
@ -210,7 +227,7 @@ func (c *Client) postBasic(endpoint string, opts map[string]string) (*http.Respo
return resp, nil
}
func (c *Client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
func (c *client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) {
var err error
var resp *http.Response
@ -279,7 +296,7 @@ func (c *Client) postFile(endpoint string, fileName string, opts map[string]stri
break
}
c.Log.Printf("qbit POST file failed: retrying attempt %d - %v\n", i, reqUrl)
c.log.Printf("qbit POST file failed: retrying attempt %d - %v\n", i, reqUrl)
time.Sleep(backoff)
}
@ -291,7 +308,7 @@ func (c *Client) postFile(endpoint string, fileName string, opts map[string]stri
return resp, nil
}
func (c *Client) setCookies(cookies []*http.Cookie) {
func (c *client) setCookies(cookies []*http.Cookie) {
cookieURL, _ := url.Parse(buildUrl(c.settings, ""))
c.http.Jar.SetCookies(cookieURL, cookies)

View file

@ -12,7 +12,7 @@ import (
)
// Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication
func (c *Client) Login() error {
func (c *client) Login() error {
opts := map[string]string{
"username": c.settings.Username,
"password": c.settings.Password,
@ -50,10 +50,12 @@ func (c *Client) Login() error {
return errors.New("bad credentials")
}
c.log.Printf("logged into client: %v", c.Name)
return nil
}
func (c *Client) GetTorrents() ([]Torrent, error) {
func (c *client) GetTorrents() ([]Torrent, error) {
resp, err := c.get("torrents/info", nil)
if err != nil {
@ -76,7 +78,7 @@ func (c *Client) GetTorrents() ([]Torrent, error) {
return torrents, nil
}
func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
opts := map[string]string{
"filter": string(filter),
}
@ -102,7 +104,7 @@ func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
return torrents, nil
}
func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) {
var filter = TorrentFilterDownloading
opts := map[string]string{
@ -139,7 +141,7 @@ func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
return res, nil
}
func (c *Client) GetTorrentsRaw() (string, error) {
func (c *client) GetTorrentsRaw() (string, error) {
resp, err := c.get("torrents/info", nil)
if err != nil {
return "", errors.Wrap(err, "could not get torrents raw")
@ -155,7 +157,7 @@ func (c *Client) GetTorrentsRaw() (string, error) {
return string(data), nil
}
func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
opts := map[string]string{
"hash": hash,
}
@ -169,10 +171,11 @@ func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
c.Log.Printf("get torrent trackers error dump response: %v\n", string(dump))
//c.log.Printf("get torrent trackers error dump response: %v\n", string(dump))
return nil, errors.Wrap(err, "could not dump response for hash: %v", hash)
}
c.Log.Printf("get torrent trackers response dump: %v\n", string(dump))
c.log.Printf("get torrent trackers response dump: %q", dump)
if resp.StatusCode == http.StatusNotFound {
return nil, nil
@ -185,7 +188,7 @@ func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
return nil, errors.Wrap(err, "could not read body")
}
c.Log.Printf("get torrent trackers body: %v\n", string(body))
c.log.Printf("get torrent trackers body: %v\n", string(body))
var trackers []TorrentTracker
err = json.Unmarshal(body, &trackers)
@ -197,7 +200,7 @@ func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) {
}
// AddTorrentFromFile add new torrent from torrent file
func (c *Client) AddTorrentFromFile(file string, options map[string]string) error {
func (c *client) AddTorrentFromFile(file string, options map[string]string) error {
res, err := c.postFile("torrents/add", file, options)
if err != nil {
@ -211,7 +214,7 @@ func (c *Client) AddTorrentFromFile(file string, options map[string]string) erro
return nil
}
func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error {
func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
@ -232,7 +235,7 @@ func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error {
return nil
}
func (c *Client) ReAnnounceTorrents(hashes []string) error {
func (c *client) ReAnnounceTorrents(hashes []string) error {
// Add hashes together with | separator
hv := strings.Join(hashes, "|")
opts := map[string]string{
@ -251,7 +254,7 @@ func (c *Client) ReAnnounceTorrents(hashes []string) error {
return nil
}
func (c *Client) GetTransferInfo() (*TransferInfo, error) {
func (c *client) GetTransferInfo() (*TransferInfo, error) {
resp, err := c.get("transfer/info", nil)
if err != nil {
return nil, errors.Wrap(err, "could not get transfer info")