From e93507f148e24b3db5e7a20b163bf2732479d204 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Fri, 12 Nov 2021 17:19:45 -0700 Subject: [PATCH] Catch connection error during Tor validation step Validation of the Tor connection occasionally fails with a ConnectionError from requests, which was previously uncaught. This is now handled appropriately (error message shown and connection dropped). Fixes #532 --- app/request.py | 17 +++++++++++------ app/utils/search.py | 4 +--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/request.py b/app/request.py index 71f4e9b..6d25224 100644 --- a/app/request.py +++ b/app/request.py @@ -274,14 +274,19 @@ class Request: # Make sure that the tor connection is valid, if enabled if self.tor: - tor_check = requests.get('https://check.torproject.org/', - proxies=self.proxies, headers=headers) - self.tor_valid = 'Congratulations' in tor_check.text + try: + tor_check = requests.get('https://check.torproject.org/', + proxies=self.proxies, headers=headers) + self.tor_valid = 'Congratulations' in tor_check.text - if not self.tor_valid: + if not self.tor_valid: + raise TorError( + "Tor connection succeeded, but the connection could " + "not be validated by torproject.org", + disable=True) + except ConnectionError: raise TorError( - "Tor connection succeeded, but the connection could not " - "be validated by torproject.org", + "Error raised during Tor connection validation", disable=True) response = requests.get( diff --git a/app/utils/search.py b/app/utils/search.py index ee092fe..7ee191c 100644 --- a/app/utils/search.py +++ b/app/utils/search.py @@ -140,10 +140,8 @@ class Search: html_soup = content_filter.view_image(html_soup) # Indicate whether or not a Tor connection is active - tor_banner = bsoup('', 'html.parser') if g.user_request.tor_valid: - tor_banner = bsoup(TOR_BANNER, 'html.parser') - html_soup.insert(0, tor_banner) + html_soup.insert(0, bsoup(TOR_BANNER, 'html.parser')) if self.feeling_lucky: return get_first_link(html_soup)