-
Notifications
You must be signed in to change notification settings - Fork 445
/
downloads_endpoint.py
655 lines (585 loc) · 28.7 KB
/
downloads_endpoint.py
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
from asyncio import CancelledError, TimeoutError as AsyncTimeoutError, wait_for
from binascii import unhexlify
from contextlib import suppress
from pathlib import PurePosixPath
from aiohttp import web
from aiohttp_apispec import docs, json_schema
from ipv8.REST.schema import schema
from ipv8.messaging.anonymization.tunnel import CIRCUIT_ID_PORT, PEER_FLAG_EXIT_BT
from marshmallow.fields import Boolean, Float, Integer, List, String
from tribler_core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler_core.components.libtorrent.download_manager.download_manager import DownloadManager
from tribler_core.components.libtorrent.download_manager.stream import STREAM_PAUSE_TIME, StreamChunk
from tribler_core.components.libtorrent.utils.libtorrent_helper import libtorrent as lt
from tribler_core.components.restapi.rest.rest_endpoint import (
RESTEndpoint,
RESTResponse,
RESTStreamResponse,
)
from tribler_core.components.restapi.rest.util import return_handled_exception
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.simpledefs import (
DLSTATUS_CIRCUITS,
DLSTATUS_EXIT_NODES,
DLSTATUS_STOPPED,
DOWNLOAD,
UPLOAD,
dlstatus_strings,
)
from tribler_core.utilities.unicode import ensure_unicode, hexlify
from tribler_core.utilities.utilities import froze_it
def _safe_extended_peer_info(ext_peer_info):
"""
Given a string describing peer info, return a json.dumps() safe representation.
:param ext_peer_info: the string to convert to a dumpable format
:return: the safe string
"""
# First see if we can use this as-is
if not ext_peer_info:
return ''
try:
return ensure_unicode(ext_peer_info, "utf8")
except UnicodeDecodeError:
# We might have some special unicode characters in here
return ''.join([chr(c) for c in ext_peer_info])
def get_extended_status(tunnel_community, download):
"""
This function filters the original download status to possibly add tunnel-related status.
Extracted from DownloadState to remove coupling between DownloadState and Tunnels.
"""
state = download.get_state()
dlstatus = state.get_status()
# Nothing to do with tunnels. If stopped - it happened by the user or libtorrent-only reason
stopped_by_user = state.lt_status and state.lt_status.paused
if dlstatus is DLSTATUS_STOPPED and not stopped_by_user:
if download.config.get_hops() > 0:
if tunnel_community.get_candidates(PEER_FLAG_EXIT_BT):
return DLSTATUS_CIRCUITS
else:
return DLSTATUS_EXIT_NODES
return DLSTATUS_STOPPED
return dlstatus
@froze_it
class DownloadsEndpoint(RESTEndpoint):
"""
This endpoint is responsible for all requests regarding downloads. Examples include getting all downloads,
starting, pausing and stopping downloads.
"""
def __init__(self, download_manager, metadata_store=None, tunnel_community=None):
super().__init__()
self.download_manager = download_manager
self.mds = metadata_store
self.tunnel_community = tunnel_community
self.app.on_shutdown.append(self.on_shutdown)
async def on_shutdown(self, _):
pass
def setup_routes(self):
self.app.add_routes([web.get('', self.get_downloads),
web.put('', self.add_download),
web.delete('/{infohash}', self.delete_download),
web.patch('/{infohash}', self.update_download),
web.get('/{infohash}/torrent', self.get_torrent),
web.get('/{infohash}/files', self.get_files),
web.get('/{infohash}/stream/{fileindex}', self.stream, allow_head=False)])
@staticmethod
def create_dconfig_from_params(parameters):
"""
Create a download configuration based on some given parameters. Possible parameters are:
- anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
- safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
- destination: the destination path of the torrent (where it is saved on disk)
"""
download_config = DownloadConfig()
anon_hops = parameters.get('anon_hops', 0)
safe_seeding = bool(parameters.get('safe_seeding', 0))
if anon_hops > 0 and not safe_seeding:
return None, "Cannot set anonymous download without safe seeding enabled"
if anon_hops > 0:
download_config.set_hops(anon_hops)
if safe_seeding:
download_config.set_safe_seeding(True)
if 'destination' in parameters:
download_config.set_dest_dir(parameters['destination'])
if 'selected_files' in parameters:
download_config.set_selected_files(parameters['selected_files'])
return download_config, None
@staticmethod
def get_files_info_json(download):
"""
Return file information as JSON from a specified download.
"""
files_json = []
files_completion = {name: progress for name, progress in download.get_state().get_files_completion()}
selected_files = download.config.get_selected_files()
file_index = 0
for fn, size in download.get_def().get_files_with_length():
files_json.append({
"index": file_index,
# We always return files in Posix format to make GUI independent of Core and simplify testing
"name": str(PurePosixPath(fn)),
"size": size,
"included": (file_index in selected_files or not selected_files),
"progress": files_completion.get(fn, 0.0)
})
file_index += 1
return files_json
@docs(
tags=["Libtorrent"],
summary="Return all downloads, both active and inactive",
parameters=[{
'in': 'query',
'name': 'get_peers',
'description': 'Flag indicating whether or not to include peers',
'type': 'boolean',
'required': False
},
{
'in': 'query',
'name': 'get_pieces',
'description': 'Flag indicating whether or not to include pieces',
'type': 'boolean',
'required': False
},
{
'in': 'query',
'name': 'get_files',
'description': 'Flag indicating whether or not to include files',
'type': 'boolean',
'required': False
}],
responses={
200: {
"schema": schema(DownloadsResponse={
'downloads': schema(Download={
'name': String,
'progress': Float,
'infohash': String,
'speed_down': Float,
'speed_up': Float,
'status': String,
'size': Integer,
'eta': Integer,
'num_peers': Integer,
'num_seeds': Integer,
'total_up': Integer,
'total_down': Integer,
'ratio': Float,
'files': String,
'trackers': String,
'hops': Integer,
'anon_download': Boolean,
'safe_seeding': Boolean,
'max_upload_speed': Integer,
'max_download_speed': Integer,
'destination': String,
'availability': Float,
'peers': String,
'total_pieces': Integer,
'vod_mode': Boolean,
'vod_prebuffering_progress': Float,
'vod_prebuffering_progress_consec': Float,
'error': String,
'time_added': Integer
})
}),
}
},
description="This endpoint returns all downloads in Tribler, both active and inactive. The progress "
"is a number ranging from 0 to 1, indicating the progress of the specific state (downloading, "
"checking etc). The download speeds have the unit bytes/sec. The size of the torrent is given "
"in bytes. The estimated time assumed is given in seconds.\n\n"
"Detailed information about peers and pieces is only requested when the get_peers and/or "
"get_pieces flag is set. Note that setting this flag has a negative impact on performance "
"and should only be used in situations where this data is required. "
)
async def get_downloads(self, request):
params = request.query
get_peers = params.get('get_peers', '0') == '1'
get_pieces = params.get('get_pieces', '0') == '1'
get_files = params.get('get_files', '0') == '1'
downloads_json = []
downloads = self.download_manager.get_downloads()
for download in downloads:
if download.hidden and not download.config.get_channel_download():
# We still want to send channel downloads since they are displayed in the GUI
continue
state = download.get_state()
tdef = download.get_def()
# Create tracker information of the download
tracker_info = []
for url, url_info in download.get_tracker_status().items():
tracker_info.append({"url": url, "peers": url_info[0], "status": url_info[1]})
num_seeds, num_peers = state.get_num_seeds_peers()
num_connected_seeds, num_connected_peers = download.get_num_connected_seeds_peers()
if download.config.get_channel_download():
download_name = self.mds.ChannelMetadata.get_channel_name_cached(
tdef.get_name_utf8(), tdef.get_infohash())
elif self.mds is None:
download_name = tdef.get_name_utf8()
else:
download_name = self.mds.TorrentMetadata.get_torrent_title(tdef.get_infohash()) or \
tdef.get_name_utf8()
download_status = get_extended_status(
self.tunnel_community, download) if self.tunnel_community else download.get_state().get_status()
download_json = {
"name": download_name,
"progress": state.get_progress(),
"infohash": hexlify(tdef.get_infohash()),
"speed_down": state.get_current_payload_speed(DOWNLOAD),
"speed_up": state.get_current_payload_speed(UPLOAD),
"status": dlstatus_strings[download_status],
"size": tdef.get_length(),
"eta": state.get_eta(),
"num_peers": num_peers,
"num_seeds": num_seeds,
"num_connected_peers": num_connected_peers,
"num_connected_seeds": num_connected_seeds,
"total_up": state.get_total_transferred(UPLOAD),
"total_down": state.get_total_transferred(DOWNLOAD),
"ratio": state.get_seeding_ratio(),
"trackers": tracker_info,
"hops": download.config.get_hops(),
"anon_download": download.get_anon_mode(),
"safe_seeding": download.config.get_safe_seeding(),
# Maximum upload/download rates are set for entire sessions
"max_upload_speed": DownloadManager.get_libtorrent_max_upload_rate(self.download_manager.config),
"max_download_speed": DownloadManager.get_libtorrent_max_download_rate(self.download_manager.config),
"destination": str(download.config.get_dest_dir()),
"availability": state.get_availability(),
"total_pieces": tdef.get_nr_pieces(),
"vod_mode": download.stream and download.stream.enabled,
"error": repr(state.get_error()) if state.get_error() else "",
"time_added": download.config.get_time_added(),
"channel_download": download.config.get_channel_download()
}
if download.stream:
download_json.update({
"vod_prebuffering_progress": download.stream.prebuffprogress,
"vod_prebuffering_progress_consec": download.stream.prebuffprogress_consec,
"vod_header_progress": download.stream.headerprogress,
"vod_footer_progress": download.stream.footerprogress,
})
# Add peers information if requested
if get_peers:
peer_list = state.get_peerlist()
for peer_info in peer_list: # Remove have field since it is very large to transmit.
del peer_info['have']
if 'extended_version' in peer_info:
peer_info['extended_version'] = _safe_extended_peer_info(peer_info['extended_version'])
# Does this peer represent a hidden services circuit?
if peer_info.get('port') == CIRCUIT_ID_PORT and self.tunnel_community:
tc = self.tunnel_community
circuit_id = tc.ip_to_circuit_id(peer_info['ip'])
circuit = tc.circuits.get(circuit_id, None)
if circuit:
peer_info['circuit'] = circuit_id
download_json["peers"] = peer_list
# Add piece information if requested
if get_pieces:
download_json["pieces"] = download.get_pieces_base64().decode('utf-8')
# Add files if requested
if get_files:
download_json["files"] = self.get_files_info_json(download)
downloads_json.append(download_json)
return RESTResponse({"downloads": downloads_json})
@docs(
tags=["Libtorrent"],
summary="Start a download from a provided URI.",
parameters=[{
'in': 'query',
'name': 'get_peers',
'description': 'Flag indicating whether or not to include peers',
'type': 'boolean',
'required': False
},
{
'in': 'query',
'name': 'get_pieces',
'description': 'Flag indicating whether or not to include pieces',
'type': 'boolean',
'required': False
},
{
'in': 'query',
'name': 'get_files',
'description': 'Flag indicating whether or not to include files',
'type': 'boolean',
'required': False
}],
responses={
200: {
"schema": schema(AddDownloadResponse={"started": Boolean, "infohash": String}),
'examples': {"started": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
}
},
)
@json_schema(schema(AddDownloadRequest={
'anon_hops': (Integer, 'Number of hops for the anonymous download. No hops is equivalent to a plain download'),
'safe_seeding': (Boolean, 'Whether the seeding of the download should be anonymous or not'),
'destination': (String, 'the download destination path of the torrent'),
'uri*': (String, 'The URI of the torrent file that should be downloaded. This URI can either represent a file '
'location, a magnet link or a HTTP(S) url.'),
}))
async def add_download(self, request):
params = await request.json()
uri = params.get('uri')
if not uri:
return self.bad_request("uri parameter missing")
download_config, error = DownloadsEndpoint.create_dconfig_from_params(params)
if error:
return self.bad_request(error)
try:
download = await self.download_manager.start_download_from_uri(uri, config=download_config)
except Exception as e:
return self.internal_error(e)
return RESTResponse({"started": True, "infohash": hexlify(download.get_def().get_infohash())})
@docs(
tags=["Libtorrent"],
summary="Remove a specific download.",
parameters=[{
'in': 'path',
'name': 'infohash',
'description': 'Infohash of the download to remove',
'type': 'string',
'required': True
}],
responses={
200: {
"schema": schema(DeleteDownloadResponse={"removed": Boolean, "infohash": String}),
'examples': {"removed": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
}
},
)
@json_schema(schema(RemoveDownloadRequest={
'remove_data': (Boolean, 'Whether or not to remove the associated data'),
}))
async def delete_download(self, request):
parameters = await request.json()
if 'remove_data' not in parameters:
return self.bad_request(f"remove_data parameter missing", parameters=parameters)
infohash = unhexlify(request.match_info['infohash'])
download = self.download_manager.get_download(infohash)
if not download:
return self.not_found("download does not exist")
try:
await self.download_manager.remove_download(download, remove_content=parameters['remove_data'])
except Exception as e:
self._logger.exception(e)
return return_handled_exception(request, e)
return RESTResponse({"removed": True, "infohash": hexlify(download.get_def().get_infohash())})
async def vod_response(self, download, parameters, request, vod_mode):
modified = False
if vod_mode:
file_index = parameters.get("fileindex")
if file_index is None:
return self.bad_request(f"fileindex is necessary to enable vod_mode", parameters=parameters)
if download.stream is None:
download.add_stream()
if not download.stream.enabled or download.stream.fileindex != file_index:
await wait_for(download.stream.enable(file_index, request.http_range.start or 0), 10)
await download.stream.updateprios()
modified = True
elif not vod_mode and download.stream is not None and download.stream.enabled:
download.stream.disable()
modified = True
return RESTResponse({"vod_prebuffering_progress": download.stream.prebuffprogress,
"vod_prebuffering_progress_consec": download.stream.prebuffprogress_consec,
"vod_header_progress": download.stream.headerprogress,
"vod_footer_progress": download.stream.footerprogress,
"vod_mode": download.stream.enabled,
"infohash": hexlify(download.get_def().get_infohash()),
"modified": modified,
})
@docs(
tags=["Libtorrent"],
summary="Update a specific download.",
parameters=[{
'in': 'path',
'name': 'infohash',
'description': 'Infohash of the download to update',
'type': 'string',
'required': True
}],
responses={
200: {
"schema": schema(UpdateDownloadResponse={"modified": Boolean, "infohash": String}),
'examples': {"modified": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
}
},
)
@json_schema(schema(UpdateDownloadRequest={
'state': (String, 'State parameter to be passed to modify the state of the download (resume/stop/recheck)'),
'selected_files': (List(Integer), 'File indexes to be included in the download'),
'anon_hops': (Integer, 'The anonymity of a download can be changed at runtime by passing the anon_hops '
'parameter, however, this must be the only parameter in this request.')
}))
async def update_download(self, request):
infohash = unhexlify(request.match_info['infohash'])
download = self.download_manager.get_download(infohash)
if not download:
return self.not_found("download does not exist")
parameters = await request.json()
vod_mode = parameters.get("vod_mode")
if vod_mode is not None:
if not isinstance(vod_mode, bool):
return self.bad_request(f"vod_mode must be bool flag. Got: {vod_mode!r}")
return await self.vod_response(download, parameters, request, vod_mode)
if len(parameters) > 1 and 'anon_hops' in parameters:
return self.bad_request(f"anon_hops must be the only parameter in request", parameters=parameters)
elif 'anon_hops' in parameters:
anon_hops = int(parameters['anon_hops'])
try:
await self.download_manager.update_hops(download, anon_hops)
except Exception as e:
self._logger.exception(e)
return return_handled_exception(request, e)
return RESTResponse({"modified": True, "infohash": hexlify(download.get_def().get_infohash())})
if 'selected_files' in parameters:
selected_files = parameters['selected_files']
num_files = len(download.tdef.get_files())
for index in selected_files:
if not 0 <= index < num_files:
return self.bad_request(f"index of file out of range: {index}", num_files=num_files,
selected_files=selected_files)
download.set_selected_files(selected_files)
if parameters.get('state'):
state = parameters['state']
if state == "resume":
download.resume()
elif state == "stop":
await download.stop(user_stopped=True)
elif state == "recheck":
download.force_recheck()
elif state == "move_storage":
dest_dir = Path(parameters['dest_dir'])
if not dest_dir.exists():
return RESTResponse({"error": f"Target directory ({dest_dir}) does not exist"})
download.move_storage(dest_dir)
download.checkpoint()
else:
return self.bad_request(f"unknown state parameter: {state!r}")
return RESTResponse({"modified": True, "infohash": hexlify(download.get_def().get_infohash())})
@docs(
tags=["Libtorrent"],
summary="Return the .torrent file associated with the specified download.",
parameters=[{
'in': 'path',
'name': 'infohash',
'description': 'Infohash of the download from which to get the .torrent file',
'type': 'string',
'required': True
}],
responses={
200: {'description': 'The torrent'}
}
)
async def get_torrent(self, request):
infohash = unhexlify(request.match_info['infohash'])
download = self.download_manager.get_download(infohash)
if not download:
return self.not_found("download does not exist")
torrent = download.get_torrent_data()
if not torrent:
return self.not_found("download does not exist")
return RESTResponse(lt.bencode(torrent), headers={'content-type': 'application/x-bittorrent',
'Content-Disposition': 'attachment; filename=%s.torrent'
% hexlify(infohash).encode('utf-8')})
@docs(
tags=["Libtorrent"],
summary="Return file information of a specific download.",
parameters=[{
'in': 'path',
'name': 'infohash',
'description': 'Infohash of the download to from which to get file information',
'type': 'string',
'required': True
}],
responses={
200: {
"schema": schema(GetFilesResponse={"files": [schema(File={'index': Integer,
'name': String,
'size': Integer,
'included': Boolean,
'progress': Float})]})
}
}
)
async def get_files(self, request):
infohash = unhexlify(request.match_info['infohash'])
download = self.download_manager.get_download(infohash)
if not download:
return self.not_found("download does not exist")
return RESTResponse({"files": self.get_files_info_json(download)})
@docs(
tags=["Libtorrent"],
summary="Stream the contents of a file that is being downloaded.",
parameters=[{
'in': 'path',
'name': 'infohash',
'description': 'Infohash of the download to stream',
'type': 'string',
'required': True
},
{
'in': 'path',
'name': 'fileindex',
'description': 'The fileindex to stream',
'type': 'string',
'required': True
}],
responses={
206: {'description': 'Contents of the stream'}
}
)
async def stream(self, request):
infohash = unhexlify(request.match_info['infohash'])
download = self.download_manager.get_download(infohash)
if not download:
return self.not_found("download does not exist")
file_index = int(request.match_info['fileindex'])
http_range = request.http_range
start = http_range.start or 0
if download.stream is None:
download.add_stream()
await wait_for(download.stream.enable(file_index, None if start > 0 else 0), 10)
stop = download.stream.filesize if http_range.stop is None else min(http_range.stop, download.stream.filesize)
if not start < stop or not 0 <= start < download.stream.filesize or not 0 < stop <= download.stream.filesize:
return RESTResponse('Requested Range Not Satisfiable', status=416)
response = RESTStreamResponse(status=206,
reason='OK',
headers={'Accept-Ranges': 'bytes',
'Content-Type': 'application/octet-stream',
'Content-Length': f'{stop - start}',
'Content-Range': f'{start}-{stop}/{download.stream.filesize}'})
response.force_close()
with suppress(CancelledError, ConnectionResetError):
async with StreamChunk(download.stream, start) as chunk:
await response.prepare(request)
bytes_todo = stop - start
bytes_done = 0
self._logger.info('Got range request for %s-%s (%s bytes)', start, stop, bytes_todo)
while not request.transport.is_closing():
if chunk.seekpos >= download.stream.filesize:
break
data = await chunk.read()
try:
if len(data) == 0:
break
if bytes_done + len(data) > bytes_todo:
# if we have more data than we need
endlen = bytes_todo - bytes_done
if endlen != 0:
await wait_for(response.write(data[:endlen]), STREAM_PAUSE_TIME)
bytes_done += endlen
break
await wait_for(response.write(data), STREAM_PAUSE_TIME)
bytes_done += len(data)
if chunk.resume():
self._logger.debug("Stream %s-%s is resumed, starting sequential buffer", start, stop)
except AsyncTimeoutError:
# This means that stream writer has a full buffer, in practice means that
# the client keeps the conenction but sets the window size to 0. In this case
# there is no need to keep sequenial buffer if there are other chunks waiting for prios
if chunk.pause():
self._logger.debug("Stream %s-%s is paused, stopping sequential buffer", start, stop)
return response