support password file and improve check auth

This commit is contained in:
Celogeek 2023-08-17 09:48:18 +02:00
parent 5d16ff8424
commit 22e2debd3f
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
2 changed files with 29 additions and 6 deletions

16
main.go
View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"log" "log"
"os"
"time" "time"
) )
@ -13,6 +15,7 @@ func main() {
flag.StringVar(&qbitoptions.Uri, "qbittorrent-uri", "http://localhost:8080", "URI of qbittorrent") flag.StringVar(&qbitoptions.Uri, "qbittorrent-uri", "http://localhost:8080", "URI of qbittorrent")
flag.StringVar(&qbitoptions.Username, "qbittorrent-username", "", "Username of qbittorrent") flag.StringVar(&qbitoptions.Username, "qbittorrent-username", "", "Username of qbittorrent")
flag.StringVar(&qbitoptions.Password, "qbittorrent-password", "", "Password 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.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(&qbitoptions.SyncedTag, "qbittorrent-synced-tag", "", "Tag of qbittorrent when copy finished")
flag.StringVar(&rsyncoptions.Hostname, "rsync-hostname", "", "Rsync host") 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.IntVar(&poolTime, "pool-time", 30, "Number of second to check new files to sync")
flag.Parse() 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 == "" || if qbitoptions.Uri == "" ||
qbitoptions.Username == "" || qbitoptions.Username == "" ||
qbitoptions.Password == "" || qbitoptions.Password == "" ||

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"bytes"
"errors"
"fmt" "fmt"
"strings" "strings"
@ -8,11 +10,12 @@ import (
) )
type QBitTorrentOptions struct { type QBitTorrentOptions struct {
Uri string Uri string
Username string Username string
Password string Password string
SyncTag string PasswordFile string
SyncedTag string SyncTag string
SyncedTag string
} }
type QBittorrentCli struct { type QBittorrentCli struct {
@ -32,7 +35,7 @@ type Torrent struct {
func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) { func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) {
r := resty.New().SetBaseURL(fmt.Sprintf("%s/api/v2", options.Uri)) r := resty.New().SetBaseURL(fmt.Sprintf("%s/api/v2", options.Uri))
_, err := r. result, err := r.
R(). R().
SetFormData(map[string]string{ SetFormData(map[string]string{
"username": options.Username, "username": options.Username,
@ -44,6 +47,10 @@ func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) {
return nil, err return nil, err
} }
if !bytes.Equal(result.Body(), []byte("Ok.")) {
return nil, errors.New("auth failed")
}
cli := &QBittorrentCli{ cli := &QBittorrentCli{
SyncTag: options.SyncTag, SyncTag: options.SyncTag,
SyncedTag: options.SyncedTag, SyncedTag: options.SyncedTag,