-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclient.go
382 lines (334 loc) · 9.45 KB
/
client.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package transmission
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
)
var (
// ErrDuplicateTorrent returned when the torrent is already added
ErrDuplicateTorrent = errors.New("Torrent already added")
)
// Config used to configure transmission client
type Config struct {
Address string
User string
Password string
// HTTPClient specify your own if you need default: http.Client
HTTPClient *http.Client
}
// Client transmission client
type Client struct {
Session *Session
sessionID string
*Config
}
// AddTorrentArg params for Client.AddTorrent
type AddTorrentArg struct {
//The format of the "cookies" should be NAME=CONTENTS, where NAME is the
//cookie name and CONTENTS is what the cookie should contain. Set multiple
//cookies like this: "name1=content1; name2=content2;" etc.
//<http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCOOKIE>
Cookies string `json:"cookies,omitempty"`
// DownloadDir path to download the torrent to
DownloadDir string `json:"download-dir,omitempty"`
// Filename filename or URL of the .torrent file
Filename string `json:"filename,omitempty"`
// Metainfo base64-encoded .torrent content
Metainfo string `json:"metainfo,omitempty"`
// Paused if true add torrent paused default false
Paused bool `json:"paused,omitempty"`
// PeerLimit maximum number of peers
PeerLimit int `json:"peer-limit,omitempty"`
// BandwidthPriority torrent's bandwidth
BandwidthPriority int `json:",omitempty"`
FilesWanted []int `json:"files-wanted,omitempty"`
FilesUnwanted []int `json:"files-unwanted,omitempty"`
PriorityHigh []int `json:"priority-high,omitempty"`
PriorityLow []int `json:"priority-low,omitempty"`
PriorityNormal []int `json:"priority-normal,omitempty"`
}
// Request object for API call
type Request struct {
Method string `json:"method"`
Arguments interface{} `json:"arguments"`
}
// Response object for API call response
type Response struct {
Arguments interface{} `json:"arguments"`
Result string `json:"result"`
}
// Do low level function for interact with transmission only take care of
// authentification and session id
func (c *Client) Do(req *http.Request, retry bool) (*http.Response, error) {
if c.User != "" || c.Password != "" {
req.SetBasicAuth(c.User, c.Password)
}
if c.sessionID != "" {
// Delete the previous session in header in case there was one
// We need to do this because the previous header won't be overridden
// by the "Add", we need to manually delete the previous header or the
// request will fail
req.Header.Del("X-Transmission-Session-Id")
req.Header.Add("X-Transmission-Session-Id", c.sessionID)
}
//Body copy for replay it if needed
b, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
// Most Transmission RPC servers require a X-Transmission-Session-Id header
// to be sent with requests, to prevent CSRF attacks. When your request
// has the wrong id -- such as when you send your first request, or when
// the server expires the CSRF token -- the Transmission RPC server will
// return an HTTP 409 error with the right X-Transmission-Session-Id in its
// own headers. So, the correct way to handle a 409 response is to update
// your X-Transmission-Session-Id and to resend the previous request.
if resp.StatusCode == http.StatusConflict && retry {
c.sessionID = resp.Header.Get("X-Transmission-Session-Id")
// Copy the previous request body in order to do it again
req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
// We also need to read the body before closing it, or it will trigger
// a "net/http: request cancelled" error
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
return c.Do(req, false)
}
return resp, nil
}
func (c *Client) request(tReq *Request, tResp *Response) error {
data, err := json.Marshal(tReq)
if err != nil {
return err
}
req, err := http.NewRequest("POST", c.Address, bytes.NewReader(data))
if err != nil {
return err
}
resp, err := c.Do(req, true)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("got http error %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, tResp)
if err != nil {
return err
}
if tResp.Result != "success" {
return fmt.Errorf("transmission: request response %q", tResp.Result)
}
return nil
}
// GetTorrents return list of torrent
func (c *Client) GetTorrents() ([]*Torrent, error) {
type arg struct {
Fields []string `json:"fields,omitempty"`
Ids []int `json:"ids,omitempty"`
}
tReq := &Request{
Arguments: arg{
Fields: torrentGetFields,
},
Method: "torrent-get",
}
r := &Response{Arguments: &Torrents{}}
err := c.request(tReq, r)
if err != nil {
return nil, err
}
t := r.Arguments.(*Torrents).Torrents
for i := range t {
t[i].Client = c
}
return t, nil
}
// GetTorrentMap returns a map of torrents indexed by torrent hash.
func (c *Client) GetTorrentMap() (TorrentMap, error) {
torrents, err := c.GetTorrents()
if err != nil {
return nil, err
}
tm := make(TorrentMap)
for _, t := range torrents {
tm[t.HashString] = t
}
return tm, nil
}
// Add shortcut for Client.AddTorrent
func (c *Client) Add(filename string) (*Torrent, error) {
args := AddTorrentArg{
Filename: filename,
}
return c.AddTorrent(args)
}
// AddTorrent add torrent from filename or metadata see AddTorrentArg for
// arguments
func (c *Client) AddTorrent(args AddTorrentArg) (*Torrent, error) {
tReq := &Request{
Arguments: args,
Method: "torrent-add",
}
// TODO: When there is an error like that, the name of the error is in the
// name of the key of the JSON
// Here we only get the torrent-added response, but in other cases the
// response could be for example torrent-duplicate
// Need to unmarshal with json.RawMessage
type added struct {
Torrent *Torrent `json:"torrent-added"`
}
r := &Response{Arguments: &added{}}
err := c.request(tReq, r)
if err != nil {
return nil, err
}
t := r.Arguments.(*added)
// If it's a success but we didn't add any torrent, it's because the
// torrent is already added
if t.Torrent == nil {
return nil, ErrDuplicateTorrent
}
t.Torrent.Client = c
return t.Torrent, nil
}
// RemoveTorrents remove torrents
func (c *Client) RemoveTorrents(torrents []*Torrent, removeData bool) error {
ids := make([]int, len(torrents))
for i := range torrents {
ids[i] = torrents[i].ID
}
type arg struct {
Ids []int `json:"ids,string"`
DeleteLocalData bool `json:"delete-local-data,omitempty"`
}
tReq := &Request{
Arguments: arg{
Ids: ids,
DeleteLocalData: removeData,
},
Method: "torrent-remove",
}
r := &Response{}
err := c.request(tReq, r)
if err != nil {
return err
}
return nil
}
// BlocklistUpdate update blocklist and return blocklist rules size
func (c *Client) BlocklistUpdate() (int, error) {
tReq := &Request{
Method: "blocklist-update",
}
type update struct {
BlocklistSize int `json:"blocklist-size"`
}
r := &Response{Arguments: &update{}}
err := c.request(tReq, r)
if err != nil {
return 0, err
}
s := r.Arguments.(*update)
return s.BlocklistSize, nil
}
// PortTest tests to see if your incoming peer port is accessible from the
// outside world.
func (c *Client) PortTest() (bool, error) {
tReq := &Request{
Method: "port-test",
}
type rep struct {
IsOpen bool `json:"port-is-open"`
}
r := &Response{Arguments: &rep{}}
err := c.request(tReq, r)
if err != nil {
return false, err
}
s := r.Arguments.(*rep)
return s.IsOpen, nil
}
// FreeSpace tests how much free space is available in a client-specified
// folder.
func (c *Client) FreeSpace(path string) (int, error) {
type arg struct {
Path string `json:"path"`
}
tReq := &Request{
Arguments: arg{path},
Method: "free-space",
}
type rep struct {
Path string `json:"path"`
SizeBytes int `json:"size-bytes"`
}
r := &Response{Arguments: &rep{}}
err := c.request(tReq, r)
if err != nil {
return 0, err
}
s := r.Arguments.(*rep)
return s.SizeBytes, nil
}
// QueueMoveTop moves torrents to top of the queue
func (c *Client) QueueMoveTop(torrents []*Torrent) error {
return c.queueAction("queue-move-top", torrents)
}
// QueueMoveUp moves torrents up in the queue
func (c *Client) QueueMoveUp(torrents []*Torrent) error {
return c.queueAction("queue-move-up", torrents)
}
// QueueMoveDown moves torrents down in the queue
func (c *Client) QueueMoveDown(torrents []*Torrent) error {
return c.queueAction("queue-move-down", torrents)
}
// QueueMoveBottom moves torrents to botton of the queue
func (c *Client) QueueMoveBottom(torrents []*Torrent) error {
return c.queueAction("queue-move-bottom", torrents)
}
func (c *Client) queueAction(method string, torrents []*Torrent) error {
ids := make([]int, len(torrents))
for i := range torrents {
ids[i] = torrents[i].ID
}
type arg struct {
Ids []int `json:"ids"`
}
tReq := &Request{
Arguments: arg{ids},
Method: method,
}
r := &Response{}
err := c.request(tReq, r)
if err != nil {
return err
}
return nil
}
// New create a new transmission client
func New(conf Config) (*Client, error) {
if conf.HTTPClient == nil {
conf.HTTPClient = &http.Client{}
}
client := Client{
Session: &Session{},
Config: &conf,
}
client.Session.Client = &client
return &client, nil
}