|
|
@@ -5,12 +5,12 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
- "log"
|
|
|
"net/http"
|
|
|
"net/http/cookiejar"
|
|
|
"net/url"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
+
|
|
|
+ "git.aetherial.dev/aeth/gluetun-qbitt-sidecar/pkg/shared"
|
|
|
)
|
|
|
|
|
|
/*
|
|
|
@@ -52,63 +52,11 @@ func makeListenPortRequest(port int) (string, error) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-type server struct {
|
|
|
- proto string // http or https
|
|
|
- host string // just the domain name, like 'qbit.local.lan' or whatever
|
|
|
- port int // port that the web ui listens on
|
|
|
-}
|
|
|
-
|
|
|
-func (s server) format() string {
|
|
|
- return fmt.Sprintf("%s://%s:%v", s.proto, s.host, s.port)
|
|
|
-}
|
|
|
-
|
|
|
-func (s server) formatWith(path string) string {
|
|
|
- return fmt.Sprintf("%s%s", s.format(), path)
|
|
|
-}
|
|
|
-
|
|
|
type QbittorrentClient struct {
|
|
|
- server server
|
|
|
+ server shared.Server
|
|
|
client *http.Client // plumbing
|
|
|
}
|
|
|
|
|
|
-// takes string s and returns the host and port
|
|
|
-func splitServerPort(s string) (string, int) {
|
|
|
- splitPort := strings.Split(s, ":")
|
|
|
- if len(splitPort) < 1 {
|
|
|
- // add log here
|
|
|
- log.Fatal("unhandled for now")
|
|
|
- }
|
|
|
- host := splitPort[0]
|
|
|
- port, err := strconv.Atoi(splitPort[1])
|
|
|
- if err != nil {
|
|
|
- log.Fatal("couldnt parse port. unhandled for now.")
|
|
|
- }
|
|
|
- return host, port
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-// strips everything but the port number and hostname from a string
|
|
|
-func parseServer(s string) (server, error) {
|
|
|
- var proto, host string
|
|
|
- var port int
|
|
|
- if strings.Contains(s, "https") {
|
|
|
- proto = "https"
|
|
|
- } else {
|
|
|
- proto = "http"
|
|
|
- }
|
|
|
- protoStripped := strings.ReplaceAll(
|
|
|
- strings.ReplaceAll(
|
|
|
- strings.ReplaceAll(
|
|
|
- s, "https", "",
|
|
|
- ), "http", "",
|
|
|
- ), "://", "",
|
|
|
- )
|
|
|
- splitHost := strings.Split(protoStripped, "/")
|
|
|
- host, port = splitServerPort(splitHost[0])
|
|
|
- return server{proto: proto, host: host, port: port}, nil
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
/*
|
|
|
create a new qbit client
|
|
|
:param username: username for your qbittorrent Web UI
|
|
|
@@ -118,11 +66,11 @@ func parseServer(s string) (server, error) {
|
|
|
func NewQbittorrentClient(username, password, host string) (QbittorrentClient, error) {
|
|
|
ckJar, err := cookiejar.New(nil)
|
|
|
client := http.Client{Jar: ckJar}
|
|
|
- srv, err := parseServer(host)
|
|
|
+ srv, err := shared.ParseServer(host)
|
|
|
if err != nil {
|
|
|
return QbittorrentClient{}, err
|
|
|
}
|
|
|
- formattedUrl := srv.formatWith(LOGIN_PATH)
|
|
|
+ formattedUrl := srv.FormatWith(LOGIN_PATH)
|
|
|
data := url.Values{}
|
|
|
data.Set("username", username)
|
|
|
data.Set("password", password)
|
|
|
@@ -131,9 +79,9 @@ func NewQbittorrentClient(username, password, host string) (QbittorrentClient, e
|
|
|
if err != nil {
|
|
|
return QbittorrentClient{}, err
|
|
|
}
|
|
|
- req.Header.Add("Referer", srv.format())
|
|
|
- req.Header.Add("Origin", srv.format())
|
|
|
- req.Header.Add("Host", srv.host)
|
|
|
+ req.Header.Add("Referer", srv.Format())
|
|
|
+ req.Header.Add("Origin", srv.Format())
|
|
|
+ // req.Header.Add("Host", srv.Host)
|
|
|
// req.Header.Add("Content-Type", "text/plain")
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
resp, err := client.Do(req)
|
|
|
@@ -141,7 +89,7 @@ func NewQbittorrentClient(username, password, host string) (QbittorrentClient, e
|
|
|
return QbittorrentClient{}, err
|
|
|
}
|
|
|
if resp.StatusCode != 200 {
|
|
|
- return QbittorrentClient{}, errors.New(fmt.Sprintf("Authentication failed.\n Host: %s\n Status Code: %s\n", srv.format(), resp.Status))
|
|
|
+ return QbittorrentClient{}, errors.New(fmt.Sprintf("Authentication failed.\n Host: %s\n Status Code: %s\n", srv.Format(), resp.Status))
|
|
|
}
|
|
|
|
|
|
client.Jar.SetCookies(resp.Request.URL, resp.Cookies())
|
|
|
@@ -158,16 +106,16 @@ func (q QbittorrentClient) UpdateIncomingPort(port int) error {
|
|
|
}
|
|
|
data := url.Values{}
|
|
|
data.Set("json", body)
|
|
|
- req, err := http.NewRequest(http.MethodPost, q.server.formatWith(PREFERENCES_PATH), strings.NewReader(data.Encode()))
|
|
|
- req.Header.Add("Origin", q.server.format())
|
|
|
- req.Header.Add("Referer", q.server.format())
|
|
|
+ req, err := http.NewRequest(http.MethodPost, q.server.FormatWith(PREFERENCES_PATH), strings.NewReader(data.Encode()))
|
|
|
+ req.Header.Add("Origin", q.server.Format())
|
|
|
+ req.Header.Add("Referer", q.server.Format())
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
resp, err := q.client.Do(req)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if resp.StatusCode != 200 {
|
|
|
- return errors.New(fmt.Sprintf("Update request failed.\n Host: %s\n Status Code: %s\n", q.server.format(), resp.Status))
|
|
|
+ return errors.New(fmt.Sprintf("Update request failed.\n Host: %s\n Status Code: %s\n", q.server.Format(), resp.Status))
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
@@ -175,12 +123,12 @@ func (q QbittorrentClient) UpdateIncomingPort(port int) error {
|
|
|
// gets the running configuration
|
|
|
func (q QbittorrentClient) GetRunningConfiguration() (Configuration, error) {
|
|
|
var cfg Configuration
|
|
|
- req, err := http.NewRequest(http.MethodGet, q.server.formatWith(CONFIG_PATH), nil)
|
|
|
+ req, err := http.NewRequest(http.MethodGet, q.server.FormatWith(CONFIG_PATH), nil)
|
|
|
if err != nil {
|
|
|
return cfg, err
|
|
|
}
|
|
|
- req.Header.Add("Origin", q.server.format())
|
|
|
- req.Header.Add("Referer", q.server.format())
|
|
|
+ req.Header.Add("Origin", q.server.Format())
|
|
|
+ req.Header.Add("Referer", q.server.Format())
|
|
|
resp, err := q.client.Do(req)
|
|
|
if err != nil {
|
|
|
return cfg, err
|