mirror of
https://github.com/celogeek/go-qbittorrent-sync.git
synced 2025-05-24 16:02:37 +02:00
support password file and improve check auth
This commit is contained in:
parent
5d16ff8424
commit
22e2debd3f
16
main.go
16
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 == "" ||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@ -11,6 +13,7 @@ type QBitTorrentOptions struct {
|
||||
Uri string
|
||||
Username string
|
||||
Password string
|
||||
PasswordFile string
|
||||
SyncTag string
|
||||
SyncedTag string
|
||||
}
|
||||
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user