-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
transmission.go
60 lines (48 loc) · 1.14 KB
/
transmission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 int
}
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)
}