autobrr/pkg/transmission/transmission.go
ze0s 861f30c144
fix(actions): reject if client is disabled (#1626)
* fix(actions): error on disabled client

* fix(actions): sql scan args

* refactor: download client cache for actions

* fix: tests client store

* fix: tests client store and int conversion

* fix: tests revert findbyid ctx timeout

* fix: tests row.err

* feat: add logging to download client cache
2024-08-27 19:45:06 +02:00

60 lines
1.1 KiB
Go

package transmission
import (
"crypto/tls"
"net/http"
"net/url"
"time"
"github.com/autobrr/autobrr/pkg/sharedhttp"
"github.com/hekmon/transmissionrpc/v3"
)
type Config struct {
UserAgent string
CustomClient *http.Client
Username string
Password string
TLSSkipVerify bool
Timeout time.Duration
}
func New(endpoint *url.URL, cfg *Config) (*transmissionrpc.Client, error) {
ct := &customTransport{
Username: cfg.Username,
Password: cfg.Password,
TLSSkipVerify: cfg.TLSSkipVerify,
}
extra := &transmissionrpc.Config{
CustomClient: &http.Client{
Transport: ct,
Timeout: time.Second * 60,
},
UserAgent: cfg.UserAgent,
}
return transmissionrpc.New(endpoint, extra)
}
type customTransport struct {
Username string
Password string
TLSSkipVerify bool
}
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
dt := sharedhttp.Transport
if t.TLSSkipVerify {
dt.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
r := req.Clone(req.Context())
if t.Username != "" && t.Password != "" {
r.SetBasicAuth(t.Username, t.Password)
}
return dt.RoundTrip(r)
}