-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendpoints.go
511 lines (432 loc) · 14.1 KB
/
endpoints.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package main
import (
"net/http"
"net/url"
"strings"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/dustin/go-humanize"
"github.com/gorilla/mux"
)
// Endpoint handler for torrent adding to client
func apiAddTorrent(w http.ResponseWriter, r *http.Request) {
var t *torrent.Torrent
var spec *torrent.TorrentSpec = nil
/* Decodes the request body */
body := apiAddTorrentBody{}
if decodeBody(w, r.Body, &body) != nil {
return
}
/* Parses the inputs */
// If magnet link is present
if body.Magnet != "" {
var err error
spec, err = torrent.TorrentSpecFromMagnetUri(body.Magnet)
if err != nil {
errorRes(w, "Magnet decoding error: "+err.Error(), http.StatusInternalServerError)
return
}
}
// If manual metainfo is present
if body.Magnet == "" && body.InfoHash != "" && body.DisplayName != "" {
spec = makeTorrentSpec(body.InfoHash, body.DisplayName, body.Trackers)
}
if spec == nil {
errorRes(w, "No torrent provided", http.StatusNotFound)
return
}
var terr error
t, terr = btEngine.addTorrent(spec, false)
if terr != nil {
errorRes(w, "Torrent add error: "+terr.Error(), http.StatusInternalServerError)
return
}
/* Creates the response body*/
res := createAddTorrentRes(t)
encodeRes(w, &res)
}
// Endpoint for selecting which file/s to download
func apiTorrentSelectFile(w http.ResponseWriter, r *http.Request) {
res := apiTorrentSelectFileRes{}
/* Parse the request body to apiTorrentSelectFileBody */
body := apiTorrentSelectFileBody{}
if decodeBody(w, r.Body, &body) != nil {
return
}
/* Check if no provided files */
if !body.AllFiles && len(body.Files) < 1 {
errorRes(w, "No files provided", http.StatusNotFound)
return
}
/* Gets torrent handler from client */
t, err := btEngine.getTorrHandle(body.InfoHash)
if err != nil {
errorRes(w, err.Error(), http.StatusInternalServerError)
return
}
/* Create the response body */
res.InfoHash = t.InfoHash().String()
res.Name = t.Name()
/* Initiate download for selected files */
// If AllFiles is toggled
if body.AllFiles {
// Empties the Files slice to prevent the execution of the code below when AllFiles if toggled
body.Files = nil
// Starts download for all files in the torrent
/* Go through the selected files to append its info to the response */
for _, f := range t.Files() {
f.SetPriority(torrent.PiecePriorityNormal)
saveSpecFile(t.InfoHash().String(), f.DisplayPath(), f.Priority())
res.Files = append(res.Files, apiTorrentSelectFileResFiles{
FileName: f.DisplayPath(),
Stream: createFileLink(t.InfoHash().String(), f.DisplayPath(), false),
Download: createFileLink(t.InfoHash().String(), f.DisplayPath(), true),
})
}
}
// If specific files are selected
for _, f := range body.Files {
/* Get the handle of the torrent file from its DisplayPath */
tf, tferr := getTorrentFile(t, f)
if tferr != nil {
continue
}
// Starts download of said torrent file
tf.SetPriority(torrent.PiecePriorityNormal)
// Save the filename to the DB for persistence
saveSpecFile(t.InfoHash().String(), tf.DisplayPath(), tf.Priority())
/* Go through the selected files to append its info to the response */
res.Files = append(res.Files, apiTorrentSelectFileResFiles{
FileName: tf.DisplayPath(),
Stream: createFileLink(t.InfoHash().String(), tf.DisplayPath(), false),
Download: createFileLink(t.InfoHash().String(), tf.DisplayPath(), true),
})
}
encodeRes(w, &res)
}
// Endpoint for setting the file/s priority
func apiTorrentPriorityFile(w http.ResponseWriter, r *http.Request) {
res := apiTorrentPriorityFileRes{}
/* Parse the request body to apiTorrentSelectFileBody */
body := apiTorrentPriorityFileBody{}
if decodeBody(w, r.Body, &body) != nil {
return
}
/* Check if no provided files */
if !body.AllFiles && len(body.Files) < 1 {
errorRes(w, "No files provided", http.StatusNotFound)
return
}
/* Gets torrent handler from client */
t, err := btEngine.getTorrHandle(body.InfoHash)
if err != nil {
errorRes(w, err.Error(), http.StatusInternalServerError)
return
}
/* Create the response body */
res.InfoHash = t.InfoHash().String()
res.Name = t.Name()
res.Priority = body.Priority
// Parse the priority from the request body
var selectedPriority torrent.PiecePriority
switch strings.ToLower(body.Priority) {
case "none":
selectedPriority = torrent.PiecePriorityNone
case "normal":
selectedPriority = torrent.PiecePriorityNormal
case "high":
selectedPriority = torrent.PiecePriorityHigh
case "readahead":
selectedPriority = torrent.PiecePriorityReadahead
}
/* Set file priority for selected files */
// If AllFiles is toggled
if body.AllFiles {
// Empties the Files slice to prevent the execution of the code below when AllFiles if toggled
body.Files = nil
/* Go through the selected files to append its info to the response */
for _, f := range t.Files() {
// Set priority of said torrent file to none essentially disabling its download
f.SetPriority(selectedPriority)
// Save the filename to the DB for persistence
saveSpecFile(t.InfoHash().String(), f.DisplayPath(), f.Priority())
/* Go through the all files to append its info to the response */
res.Files = append(res.Files, apiTorrentPriorityFileResFiles{
FileName: f.DisplayPath(),
})
}
}
// If specific files are selected
for _, f := range body.Files {
/* Get the handle of the torrent file from its DisplayPath */
tf, tferr := getTorrentFile(t, f)
if tferr != nil {
continue
}
// Set priority of said torrent file to none essentially disabling its download
tf.SetPriority(selectedPriority)
// Save the filename to the DB for persistence
saveSpecFile(t.InfoHash().String(), tf.DisplayPath(), tf.Priority())
/* Go through the selected files to append its info to the response */
res.Files = append(res.Files, apiTorrentPriorityFileResFiles{
FileName: tf.DisplayPath(),
})
}
encodeRes(w, &res)
}
// Endpoint for streaming a file
func apiStreamTorrentFile(w http.ResponseWriter, r *http.Request) {
// Get infohash and filename variables
vars := mux.Vars(r)
/* Get torrent handle from infohash */
t, err := btEngine.getTorrHandle(vars["infohash"])
if err != nil {
errorRes(w, err.Error(), http.StatusNotFound)
return
}
/* Unescape given filename */
fn, fnerr := url.QueryUnescape(vars["file"])
if fnerr != nil {
errorRes(w, "Filename unescaping error: "+fnerr.Error(), http.StatusInternalServerError)
return
}
/* Get torrent file handle from filename */
f, ferr := getTorrentFile(t, fn)
if ferr != nil {
errorRes(w, ferr.Error(), http.StatusNotFound)
return
}
/* Make torrent file reader for streaming */
reader := f.NewReader()
defer reader.Close()
// Set the buffer to 1% of the file size
reader.SetReadahead(f.Length() / 100)
// Send the reader as HTTP response
http.ServeContent(w, r, f.DisplayPath(), time.Now(), reader)
}
// Endpoint for removing a torrent
func apiRemoveTorrent(w http.ResponseWriter, r *http.Request) {
/* Parses the request body to apiRemoveTorrent */
body := apiRemoveTorrentBody{}
if decodeBody(w, r.Body, &body) != nil {
return
}
/* Getting the torrent handle */
t, terr := btEngine.getTorrHandle(body.InfoHash)
if terr != nil {
errorRes(w, terr.Error(), http.StatusNotFound)
return
}
/* Saving of variables for response body */
tname := t.Name()
ih := t.InfoHash().String()
/* Remover function */
rmerr := btEngine.dropTorrent(ih, body.RemoveFiles)
if rmerr != nil {
errorRes(w, "Torrent removal error: "+rmerr.Error(), http.StatusInternalServerError)
return
}
/* Creating response body */
res := apiRemoveTorrentRes{
Name: tname,
InfoHash: ih,
}
encodeRes(w, &res)
}
// Torrent stats endpoint
func apiTorrentStats(w http.ResponseWriter, r *http.Request) {
/* Get infohash variable from the request */
vars := mux.Vars(r)
res := apiTorrentStasRes{}
/* Variables */
tlist := btEngine.Torrents
ih := vars["infohash"]
/* If provided with infohash */
if ih != "" {
/* Check if infohash is valid */
_, terr := btEngine.getTorrHandle(ih)
if terr != nil {
errorRes(w, terr.Error(), http.StatusNotFound)
return
}
/* Overwrite tlist with only the selected torrent's handle */
templist := make(map[string]*torrentHandle)
templist[ih] = btEngine.Torrents[ih]
tlist = templist
}
/* Go through the tlist */
for _, v := range tlist {
tstats := apiTorrentStasResTorrents{}
/* Setting main stats */
tstats.Name = v.Torrent.Name()
tstats.InfoHash = v.Torrent.InfoHash().String()
tstats.TotalPeers = v.Torrent.Stats().TotalPeers
tstats.ActivePeers = v.Torrent.Stats().ActivePeers
tstats.PendingPeers = v.Torrent.Stats().PendingPeers
tstats.HalfOpenPeers = v.Torrent.Stats().HalfOpenPeers
tstats.DownloadSpeed = v.DlSpeedReadable
tstats.UploadSpeed = v.UlSpeedReadable
tstats.Progress = calcTorrentProgress(v.Torrent)
/* Setting the peers info */
for _, peer := range v.Torrent.PeerConns() {
paddr := peer.Peer.RemoteAddr.String()
pcli, ok := peer.PeerClientName.Load().(string)
if !ok {
pcli = "NOTPROVIDED"
}
tstats.Peers = append(tstats.Peers, apiTorrentStatsPeersInfo{
PeerAddr: paddr,
PeerClient: pcli,
})
}
/* Setting the files available in the torrent */
for _, tf := range v.Torrent.Files() {
tfname := tf.DisplayPath()
tfbc := tf.BytesCompleted()
tflen := tf.Length()
curf := apiTorrentStatsTorrentsFiles{
FileName: tfname,
FileSizeBytes: int(tflen),
FileSizeReadable: humanize.Bytes(uint64(tflen)),
DownloadedBytes: int(tfbc),
DownloadedReadable: humanize.Bytes(uint64(tfbc)),
Priority: torrentPriorityToString(tf.Priority()),
}
if tf.BytesCompleted() > 0 {
curf.Stream = createFileLink(tstats.InfoHash, tfname, false)
curf.Download = createFileLink(tstats.InfoHash, tfname, true)
}
tstats.Files = append(tstats.Files, curf)
}
/* Append it response body */
res.Torrents = append(res.Torrents, tstats)
}
/* Send response */
encodeRes(w, &res)
}
func apiDownloadFile(w http.ResponseWriter, r *http.Request) {
/* Get infohash and filename vars*/
vars := mux.Vars(r)
/* Get torrent handle from infohash */
t, err := btEngine.getTorrHandle(vars["infohash"])
if err != nil {
errorRes(w, err.Error(), http.StatusNotFound)
return
}
/* Unescape given filename */
fn, fnerr := url.QueryUnescape(vars["file"])
if fnerr != nil {
errorRes(w, "Filename unescaping error: "+fnerr.Error(), http.StatusInternalServerError)
return
}
/* Get torrent file handle from filename */
f, ferr := getTorrentFile(t, fn)
if ferr != nil {
errorRes(w, ferr.Error(), http.StatusNotFound)
return
}
/* Check if file is finished downloading */
if f.BytesCompleted() != f.Length() {
errorRes(w, "File is not completed", http.StatusAccepted)
return
}
/* Set Content-Disposition as f.DisplayPath() */
w.Header().Add("Content-Disposition", "attachment; filename=\""+safenDisplayPath(f.DisplayPath())+"\"")
/* Send file as response */
reader := f.NewReader()
defer reader.Close()
http.ServeContent(w, r, f.DisplayPath(), time.Now(), reader)
}
func apiAddTorrentFile(w http.ResponseWriter, r *http.Request) {
/* Gets file from form */
torrfile, _, err := r.FormFile("torrent")
if err != nil {
errorRes(w, err.Error(), http.StatusInternalServerError)
return
}
defer torrfile.Close()
/* Loads torrent file to the BitTorrent client */
/* Loads the torrent file as a MetaInfo */
mi, mierr := metainfo.Load(torrfile)
if mierr != nil {
errorRes(w, mierr.Error(), http.StatusInternalServerError)
return
}
/* Makes torrent spec from given MetaInfo */
spec, specerr := torrent.TorrentSpecFromMetaInfoErr(mi)
if specerr != nil {
errorRes(w, specerr.Error(), http.StatusInternalServerError)
return
}
/* Adds torrent spec to the BitTorrent client */
t, terr := btEngine.addTorrent(spec, false)
if terr != nil {
errorRes(w, terr.Error(), http.StatusInternalServerError)
return
}
/* Create response */
res := createAddTorrentRes(t)
encodeRes(w, &res)
}
// Make playlist for direct streaming of magnet link or torrent files
func apiDirectPlay(w http.ResponseWriter, r *http.Request) {
/* Parse magnet link from query */
magnet := r.URL.Query().Get("magnet")
displayName := r.URL.Query().Get("dn")
trackers, trackersOk := r.URL.Query()["tr"]
files, filesOk := r.URL.Query()["file"]
// Check if magnet link is present
if magnet == "" {
errorRes(w, "Invalid magnet link", http.StatusBadRequest)
return
}
// Parses magnet link to add display name and trackers from query
parsedMagnet, parseMagnetErr := metainfo.ParseMagnetUri(magnet)
if parseMagnetErr != nil {
errorRes(w, "Parsing magnet link error", http.StatusInternalServerError)
return
}
if displayName != "" {
parsedMagnet.DisplayName = displayName
}
if trackersOk {
parsedMagnet.Trackers = trackers
}
// Parse magnet link to torrent spec
spec, specErr := torrent.TorrentSpecFromMagnetUri(parsedMagnet.String())
if specErr != nil {
errorRes(w, "Creating torrent spec error", http.StatusInternalServerError)
return
}
// Add torrent spec to BT engine
t, addTorrentErr := btEngine.addTorrent(spec, false)
if addTorrentErr != nil {
errorRes(w, "Adding torrent error", http.StatusInternalServerError)
return
}
/* Create the playlist file with the selected files */
w.Header().Set("Content-Disposition", "attachment; filename=\""+t.InfoHash().String()+".m3u\"")
playList := "#EXTM3U\n"
httpScheme := "http"
if r.Header.Get("X-Forwarded-Proto") != "" {
httpScheme = r.Header.Get("X-Forwarded-Proto")
}
if !filesOk {
for _, file := range t.Files() {
file.SetPriority(torrent.PiecePriorityNormal)
saveSpecFile(t.InfoHash().String(), file.DisplayPath(), file.Priority())
playList += appendFilePlaylist(httpScheme, r.Host, t.InfoHash().String(), file.DisplayPath())
}
}
for _, file := range files {
torrentFile, getTorretnFileErr := getTorrentFile(t, file)
if getTorretnFileErr != nil {
continue
}
torrentFile.SetPriority(torrent.PiecePriorityNormal)
saveSpecFile(t.InfoHash().String(), torrentFile.DisplayPath(), torrentFile.Priority())
playList += appendFilePlaylist(httpScheme, r.Host, t.InfoHash().String(), torrentFile.DisplayPath())
}
w.Write([]byte(playList))
}