From 55431c71c0ed4d348c53a02c05dac811f1a0b3a4 Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Sun, 12 Apr 2020 19:13:20 +0200 Subject: [PATCH] Load metainfo directly when unchecking 'anonymous' checkbox --- .../modules/libtorrent/download_manager.py | 1 + .../tests/test_torrentinfo_endpoint.py | 11 ++++++- .../restapi/torrentinfo_endpoint.py | 13 ++++++-- .../dialogs/startdownloaddialog.py | 30 ++++++++++--------- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/tribler-core/tribler_core/modules/libtorrent/download_manager.py b/src/tribler-core/tribler_core/modules/libtorrent/download_manager.py index f1daa381b0a..7897e435164 100644 --- a/src/tribler-core/tribler_core/modules/libtorrent/download_manager.py +++ b/src/tribler-core/tribler_core/modules/libtorrent/download_manager.py @@ -371,6 +371,7 @@ async def get_metainfo(self, infohash, timeout=30, hops=None): to a few peers, and downloading the metadata for the torrent. :param infohash: The (binary) infohash to lookup metainfo for. :param timeout: A timeout in seconds. + :param hops: the number of tunnel hops to use for this lookup. If None, use config default. :return: The metainfo """ infohash_hex = hexlify(infohash) diff --git a/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_torrentinfo_endpoint.py b/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_torrentinfo_endpoint.py index 98f4699cd8f..59eb4afc60b 100644 --- a/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_torrentinfo_endpoint.py +++ b/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_torrentinfo_endpoint.py @@ -66,7 +66,11 @@ def verify_valid_dict(json_data): path = "http://localhost:%d/ubuntu.torrent" % file_server_port verify_valid_dict(await self.do_request('torrentinfo?uri=%s' % path, expected_code=200)) - def get_metainfo(infohash, timeout=20): + hops_list = [] + + def get_metainfo(infohash, timeout=20, hops=None): + if hops is not None: + hops_list.append(hops) with open(TESTS_DATA_DIR / "bak_single.torrent", mode='rb') as torrent_file: torrent_data = torrent_file.read() tdef = TorrentDef.load_from_memory(torrent_data) @@ -92,6 +96,11 @@ def get_metainfo_timeout(*args, **kwargs): self.session.dlmgr.get_metainfo = get_metainfo verify_valid_dict(await self.do_request('torrentinfo?uri=%s' % path, expected_code=200)) + await self.do_request('torrentinfo?uri=%s&hops=0' % path, expected_code=200) + self.assertListEqual([0], hops_list) + + await self.do_request('torrentinfo?uri=%s&hops=foo' % path, expected_code=400) + path = 'http://fdsafksdlafdslkdksdlfjs9fsafasdf7lkdzz32.n38/324.torrent' await self.do_request('torrentinfo?uri=%s' % path, expected_code=500) diff --git a/src/tribler-core/tribler_core/modules/libtorrent/restapi/torrentinfo_endpoint.py b/src/tribler-core/tribler_core/modules/libtorrent/restapi/torrentinfo_endpoint.py index 831558bbebf..0835f60a2bf 100644 --- a/src/tribler-core/tribler_core/modules/libtorrent/restapi/torrentinfo_endpoint.py +++ b/src/tribler-core/tribler_core/modules/libtorrent/restapi/torrentinfo_endpoint.py @@ -45,6 +45,15 @@ async def get_torrent_info(self, request): """ args = request.query + + hops = None + if 'hops' in args: + try: + hops = int(args['hops']) + except ValueError: + return RESTResponse({"error": f"wrong value of 'hops' parameter: {repr(args['hops'])}"}, + status=HTTP_BAD_REQUEST) + if 'uri' not in args or not args['uri']: return RESTResponse({"error": "uri parameter missing"}, status=HTTP_BAD_REQUEST) @@ -67,14 +76,14 @@ async def get_torrent_info(self, request): if response.startswith(b'magnet'): _, infohash, _ = parse_magnetlink(response) if infohash: - metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60) + metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60, hops=hops) else: metainfo = bdecode_compat(response) elif uri.startswith('magnet'): infohash = parse_magnetlink(uri)[1] if infohash is None: return RESTResponse({"error": "missing infohash"}, status=HTTP_BAD_REQUEST) - metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60) + metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60, hops=hops) else: return RESTResponse({"error": "invalid uri"}, status=HTTP_BAD_REQUEST) diff --git a/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py b/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py index c94169df13f..78443bafe5c 100644 --- a/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py +++ b/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py @@ -57,6 +57,7 @@ def __init__(self, parent, download_uri): self.dialog_widget.select_all_files_button.clicked.connect(self.on_all_files_selected_clicked) self.dialog_widget.deselect_all_files_button.clicked.connect(self.on_all_files_deselected_clicked) self.dialog_widget.loading_files_label.clicked.connect(self.on_reload_torrent_info) + self.dialog_widget.anon_download_checkbox.clicked.connect(self.on_reload_torrent_info) self.dialog_widget.destination_input.setStyleSheet( """ @@ -153,20 +154,22 @@ def get_selected_files(self): return included_files def perform_files_request(self): - self.rest_request = TriblerNetworkRequest( - "torrentinfo?uri=%s" % quote_plus_unicode(self.download_uri), - self.on_received_metainfo, - capture_errors=False, - ) + direct = not self.dialog_widget.anon_download_checkbox.isChecked() + request = f"torrentinfo?uri={quote_plus_unicode(self.download_uri)}" + if direct is True: + request = request + f"&hops=0" + self.rest_request = TriblerNetworkRequest(request, self.on_received_metainfo, capture_errors=False) if self.metainfo_retries <= METAINFO_MAX_RETRIES: - loading_message = ( - "Loading torrent files..." - if not self.metainfo_retries - else "Timeout in fetching files. Retrying (%s/%s)" % (self.metainfo_retries, METAINFO_MAX_RETRIES) + fetch_mode = 'directly' if direct else 'anonymously' + loading_message = f"Loading torrent files {fetch_mode}..." + timeout_message = ( + f"Timeout in fetching files {fetch_mode}. Retrying ({self.metainfo_retries}/{METAINFO_MAX_RETRIES})" ) - self.dialog_widget.loading_files_label.setText(loading_message) + self.dialog_widget.loading_files_label.setText( + loading_message if not self.metainfo_retries else timeout_message + ) self.metainfo_fetch_timer = QTimer() self.metainfo_fetch_timer.timeout.connect(self.perform_files_request) self.metainfo_fetch_timer.setSingleShot(True) @@ -230,10 +233,9 @@ def on_reload_torrent_info(self): This method is called when user clicks the QLabel text showing loading or error message. Here, we reset the number of retries to fetch the metainfo. Note color of QLabel is also reset to white. """ - if self.metainfo_retries > METAINFO_MAX_RETRIES: - self.dialog_widget.loading_files_label.setStyleSheet("color:#ffffff;") - self.metainfo_retries = 0 - self.perform_files_request() + self.dialog_widget.loading_files_label.setStyleSheet("color:#ffffff;") + self.metainfo_retries = 0 + self.perform_files_request() def on_browse_dir_clicked(self): chosen_dir = QFileDialog.getExistingDirectory(