From 22e2debd3f15e0bb6618a729b007125e5cef4f8d Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Thu, 17 Aug 2023 09:48:18 +0200 Subject: [PATCH] support password file and improve check auth --- main.go | 16 ++++++++++++++++ qbittorent.go | 19 +++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index f568a49..b850368 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "bytes" "flag" "log" + "os" "time" ) @@ -13,6 +15,7 @@ func main() { flag.StringVar(&qbitoptions.Uri, "qbittorrent-uri", "http://localhost:8080", "URI of qbittorrent") flag.StringVar(&qbitoptions.Username, "qbittorrent-username", "", "Username of qbittorrent") flag.StringVar(&qbitoptions.Password, "qbittorrent-password", "", "Password of qbittorrent") + flag.StringVar(&qbitoptions.PasswordFile, "qbittorrent-password-file", "", "Password file with the password of qbittorrent") flag.StringVar(&qbitoptions.SyncTag, "qbittorrent-sync-tag", "Sync", "Tag of qbittorrent to copy") flag.StringVar(&qbitoptions.SyncedTag, "qbittorrent-synced-tag", "", "Tag of qbittorrent when copy finished") flag.StringVar(&rsyncoptions.Hostname, "rsync-hostname", "", "Rsync host") @@ -22,6 +25,19 @@ func main() { flag.IntVar(&poolTime, "pool-time", 30, "Number of second to check new files to sync") flag.Parse() + if qbitoptions.PasswordFile != "" { + var b []byte + var ok bool + var err error + if b, err = os.ReadFile(qbitoptions.PasswordFile); err != nil { + log.Fatalf("[Qbit] Reading password file failed: %v", err) + } + if b, ok = bytes.CutSuffix(b, []byte{'\r', '\n'}); !ok { + b, _ = bytes.CutSuffix(b, []byte{'\n'}) + } + qbitoptions.Password = string(b) + } + if qbitoptions.Uri == "" || qbitoptions.Username == "" || qbitoptions.Password == "" || diff --git a/qbittorent.go b/qbittorent.go index 7aa6097..33b6c67 100644 --- a/qbittorent.go +++ b/qbittorent.go @@ -1,6 +1,8 @@ package main import ( + "bytes" + "errors" "fmt" "strings" @@ -8,11 +10,12 @@ import ( ) type QBitTorrentOptions struct { - Uri string - Username string - Password string - SyncTag string - SyncedTag string + Uri string + Username string + Password string + PasswordFile string + SyncTag string + SyncedTag string } type QBittorrentCli struct { @@ -32,7 +35,7 @@ type Torrent struct { func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) { r := resty.New().SetBaseURL(fmt.Sprintf("%s/api/v2", options.Uri)) - _, err := r. + result, err := r. R(). SetFormData(map[string]string{ "username": options.Username, @@ -44,6 +47,10 @@ func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) { return nil, err } + if !bytes.Equal(result.Body(), []byte("Ok.")) { + return nil, errors.New("auth failed") + } + cli := &QBittorrentCli{ SyncTag: options.SyncTag, SyncedTag: options.SyncedTag,