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
main
Ben Busby 2021-11-12 17:19:45 -07:00
parent 3f40a6c485
commit e93507f148
No known key found for this signature in database
GPG Key ID: 339B7B7EB5333D14
2 changed files with 12 additions and 9 deletions

View File

@ -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(

View File

@ -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)