-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeluge.go
284 lines (227 loc) · 7.65 KB
/
deluge.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package deluge
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"strconv"
"strings"
"golang.org/x/net/publicsuffix"
)
// Custom errors.
var (
ErrInvalidVersion = fmt.Errorf("invalid data returned while checking version")
ErrDelugeError = fmt.Errorf("deluge error")
ErrAuthFailed = fmt.Errorf("authentication failed")
)
// Deluge is what you get for providing a password.
// Version and Backends are only filled if you call New().
type Deluge struct {
password string
url string
auth string
id int
client *http.Client
Version string // Currently unused, for display purposes only.
Backends map[string]Backend // Currently unused, for display purposes only.
}
// NewNoAuth returns a Deluge object without authenticating or trying to connect.
func NewNoAuth(config *Config) (*Deluge, error) {
return newConfig(context.TODO(), config, false)
}
// New creates a http.Client with authenticated cookies.
// Used to make additional, authenticated requests to the APIs.
func New(ctx context.Context, config *Config) (*Deluge, error) {
return newConfig(ctx, config, true)
}
func newConfig(ctx context.Context, config *Config, login bool) (*Deluge, error) {
// The cookie jar is used to auth Deluge.
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, fmt.Errorf("cookiejar.New(publicsuffix): %w", err)
}
delugeURL := strings.TrimSuffix(strings.TrimSuffix(config.URL, "/json"), "/") + "/json"
// This app allows http auth, in addition to deluge web password.
auth := config.HTTPUser + ":" + config.HTTPPass
if auth != ":" {
auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
} else {
auth = ""
}
httpClient := config.Client
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Jar = jar
deluge := &Deluge{
auth: auth,
Backends: make(map[string]Backend),
password: config.Password,
url: delugeURL,
client: httpClient,
}
if !login {
return deluge, nil
}
if err := deluge.LoginContext(ctx); err != nil {
return deluge, err
}
if deluge.Version = config.Version; deluge.Version == "" {
if err = deluge.setVersion(ctx); err != nil {
return deluge, err
}
}
return deluge, nil
}
// Login sets the cookie jar with authentication information.
func (d *Deluge) Login() error {
return d.LoginContext(context.Background())
}
// LoginContext sets the cookie jar with authentication information.
func (d *Deluge) LoginContext(ctx context.Context) error {
// This line is how you send auth creds.
req, err := d.DelReq(ctx, AuthLogin, []string{d.password})
if err != nil {
return fmt.Errorf("DelReq(AuthLogin, json): %w", err)
}
resp, err := d.client.Do(req)
if err != nil {
return fmt.Errorf("d.Do(req): %w", err)
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body) // must read body to avoid memory leak.
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%w: %v[%v] (status: %v/%v)",
ErrAuthFailed, req.URL.String(), AuthLogin, resp.StatusCode, resp.Status)
}
return nil
}
// setVersion digs into the first server in the web UI to find the version.
func (d *Deluge) setVersion(ctx context.Context) error {
response, err := d.Get(ctx, GeHosts, []string{})
if err != nil {
return err
}
// This method returns a "mixed list" which requires an interface.
// Deluge devs apparently hate Go. :(
servers := make([][]interface{}, 0)
if err := json.Unmarshal(response.Result, &servers); err != nil {
return fmt.Errorf("json.Unmarshal(rawResult1): %w", err)
}
serverID := ""
// Store each server info (so consumers can access them easily).
for _, server := range servers {
serverID, _ = server[0].(string)
backend := Backend{ID: serverID}
backend.Addr, _ = server[1].(string)
val, _ := server[2].(float64)
backend.Addr += ":" + strconv.FormatFloat(val, 'f', 0, 64) //nolint:gomnd,nolintlint
backend.Prot, _ = server[3].(string)
d.Backends[serverID] = backend
}
// Store the last server's version as "the version"
response, err = d.Get(ctx, HostStatus, []string{serverID})
if err != nil {
return err
}
server := make([]interface{}, 0)
if err = json.Unmarshal(response.Result, &server); err != nil {
return fmt.Errorf("json.Unmarshal(rawResult2): %w", err)
}
const payloadSegments = 3
if len(server) < payloadSegments {
return ErrInvalidVersion
}
// Version comes last in the mixed list.
var ok bool
if d.Version, ok = server[len(server)-1].(string); !ok {
return ErrInvalidVersion
}
return nil
}
// DelReq is a small helper function that adds headers and marshals the json.
func (d Deluge) DelReq(ctx context.Context, method string, params interface{}) (*http.Request, error) {
d.id++
paramMap := map[string]interface{}{"method": method, "id": d.id, "params": params}
data, err := json.Marshal(paramMap)
if err != nil {
return nil, fmt.Errorf("json.Marshal(params): %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.url, bytes.NewBuffer(data))
if err != nil {
return req, fmt.Errorf("creating request: %w", err)
}
if d.auth != "" {
// In case Deluge is also behind HTTP auth.
req.Header.Add("Authorization", d.auth)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
return req, nil
}
// GetXfers gets all the Transfers from Deluge.
func (d *Deluge) GetXfers() (map[string]*XferStatus, error) {
return d.GetXfersContext(context.Background())
}
func (d *Deluge) GetXfersContext(ctx context.Context) (map[string]*XferStatus, error) {
xfers := make(map[string]*XferStatus)
response, err := d.Get(ctx, GetAllTorrents, []string{"", ""})
if err != nil {
return nil, fmt.Errorf("get(GetAllTorrents): %w", err)
}
if err := json.Unmarshal(response.Result, &xfers); err != nil {
return nil, fmt.Errorf("json.Unmarshal(xfers): %w", err)
}
return xfers, nil
}
// GetXfersCompat gets all the Transfers from Deluge 1.x or 2.x.
// Depend on what you're actually trying to do, this is likely the best method to use.
// This will return a combined struct hat has data for Deluge 1 and Deluge 2.
// All of the data for either version will be made available with this method.
func (d *Deluge) GetXfersCompat() (map[string]*XferStatusCompat, error) {
return d.GetXfersCompatContext(context.Background())
}
func (d *Deluge) GetXfersCompatContext(ctx context.Context) (map[string]*XferStatusCompat, error) {
xfers := make(map[string]*XferStatusCompat)
response, err := d.Get(ctx, GetAllTorrents, []string{"", ""})
if err != nil {
return nil, fmt.Errorf("get(GetAllTorrents): %w", err)
}
if err := json.Unmarshal(response.Result, &xfers); err != nil {
return nil, fmt.Errorf("json.Unmarshal(xfers): %w", err)
}
return xfers, nil
}
// Get a response from Deluge.
func (d *Deluge) Get(ctx context.Context, method string, params interface{}) (*Response, error) {
return d.req(ctx, method, params, true)
}
func (d *Deluge) req(ctx context.Context, method string, params interface{}, loop bool) (*Response, error) {
req, err := d.DelReq(ctx, method, params)
if err != nil {
return nil, fmt.Errorf("d.DelReq: %w", err)
}
resp, err := d.client.Do(req)
if err != nil {
return nil, fmt.Errorf("d.Do: %w", err)
}
defer resp.Body.Close()
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("json.Unmarshal(response): %w", err)
}
if response.Error.Code != 0 {
if err := d.LoginContext(ctx); err != nil {
return nil, err
}
if loop {
return d.req(ctx, method, params, false)
}
return &response, fmt.Errorf("%w: %s", ErrDelugeError, response.Error.Message)
}
return &response, nil
}