Merge branch '493_500_detect_broken_tor_connection' of https://github.com/mig5/onionshare into mig5-493_500_detect_broken_tor_connection

This commit is contained in:
Micah Lee 2018-01-13 16:32:56 -08:00
commit ebbe490a65
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
5 changed files with 51 additions and 14 deletions

View file

@ -375,6 +375,17 @@ class Onion(object):
# ephemeral stealth onion services are not supported
self.supports_stealth = False
def is_authenticated(self):
"""
Returns True if the Tor connection is still working, or False otherwise.
"""
if self.c is not None:
return self.c.is_authenticated()
else:
return False
def start_onion_service(self, port):
"""
Start a onion service on port 80, pointing to the given port, and

View file

@ -142,10 +142,10 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.set_server_active(False)
# Start the "Connecting to Tor" dialog, which calls onion.connect()
tor_con = TorConnectionDialog(self.qtapp, self.settings, self.onion)
tor_con.canceled.connect(self._tor_connection_canceled)
tor_con.open_settings.connect(self._tor_connection_open_settings)
tor_con.start()
self.tor_con = TorConnectionDialog(self.qtapp, self.settings, self.onion)
self.tor_con.canceled.connect(self._tor_connection_canceled)
self.tor_con.open_settings.connect(self._tor_connection_open_settings)
self.tor_con.start()
# After connecting to Tor, check for updates
self.check_for_updates()
@ -219,6 +219,17 @@ class OnionShareGui(QtWidgets.QMainWindow):
def reload_settings():
common.log('OnionShareGui', 'open_settings', 'settings have changed, reloading')
self.settings.load()
# We might've stopped the main requests timer if a Tor connection failed.
# If we've reloaded settings, we probably succeeded in obtaining a new
# connection. If so, restart the timer.
if self.onion.is_authenticated():
if not self.timer.isActive():
self.timer.start()
# If there were some files listed for sharing, we should be ok to
# re-enable the 'Start Sharing' button now.
if self.server_status.file_selection.get_num_files() > 0:
self.server_status.server_button.setEnabled(True)
self.status_bar.clearMessage()
d = SettingsDialog(self.onion, self.qtapp, self.config)
d.settings_saved.connect(reload_settings)
@ -398,6 +409,17 @@ class OnionShareGui(QtWidgets.QMainWindow):
"""
self.update()
# Have we lost connection to Tor somehow?
if self.tor_con.t.isFinished():
if not self.onion.is_authenticated():
self.timer.stop()
if self.server_status.status != self.server_status.STATUS_STOPPED:
self.server_status.stop_server()
self.server_status.server_button.setEnabled(False)
self.status_bar.showMessage(strings._('gui_tor_connection_lost', True))
if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'):
self.systemTray.showMessage(strings._('gui_tor_connection_lost', True), strings._('gui_tor_connection_error_settings', True))
# scroll to the bottom of the dl progress bar log pane
# if a new download has been added
if self.new_download:

View file

@ -459,7 +459,8 @@ class SettingsDialog(QtWidgets.QDialog):
# If Tor isn't connected, or if Tor settings have changed, Reinitialize
# the Onion object
reboot_onion = False
if self.onion.connected_to_tor:
if self.onion.is_authenticated():
common.log('SettingsDialog', 'save_clicked', 'Connected to Tor')
def changed(s1, s2, keys):
"""
Compare the Settings objects s1 and s2 and return true if any values
@ -478,6 +479,7 @@ class SettingsDialog(QtWidgets.QDialog):
reboot_onion = True
else:
common.log('SettingsDialog', 'save_clicked', 'Not connected to Tor')
# Tor isn't connected, so try connecting
reboot_onion = True
@ -492,7 +494,7 @@ class SettingsDialog(QtWidgets.QDialog):
common.log('SettingsDialog', 'save_clicked', 'Onion done rebooting, connected to Tor: {}'.format(self.onion.connected_to_tor))
if self.onion.connected_to_tor and not tor_con.wasCanceled():
if self.onion.is_authenticated() and not tor_con.wasCanceled():
self.settings_saved.emit()
self.close()
@ -505,7 +507,7 @@ class SettingsDialog(QtWidgets.QDialog):
Cancel button clicked.
"""
common.log('SettingsDialog', 'cancel_clicked')
if not self.onion.connected_to_tor:
if not self.onion.is_authenticated():
Alert(strings._('gui_tor_connection_canceled', True), QtWidgets.QMessageBox.Warning)
sys.exit()
else:
@ -560,7 +562,7 @@ class SettingsDialog(QtWidgets.QDialog):
common.log('SettingsDialog', 'closeEvent')
# On close, if Tor isn't connected, then quit OnionShare altogether
if not self.onion.connected_to_tor:
if not self.onion.is_authenticated():
common.log('SettingsDialog', 'closeEvent', 'Closing while not connected to Tor')
# Wait 1ms for the event loop to finish, then quit

View file

@ -57,12 +57,12 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
def start(self):
common.log('TorConnectionDialog', 'start')
t = TorConnectionThread(self, self.settings, self.onion)
t.tor_status_update.connect(self._tor_status_update)
t.connected_to_tor.connect(self._connected_to_tor)
t.canceled_connecting_to_tor.connect(self._canceled_connecting_to_tor)
t.error_connecting_to_tor.connect(self._error_connecting_to_tor)
t.start()
self.t = TorConnectionThread(self, self.settings, self.onion)
self.t.tor_status_update.connect(self._tor_status_update)
self.t.connected_to_tor.connect(self._connected_to_tor)
self.t.canceled_connecting_to_tor.connect(self._canceled_connecting_to_tor)
self.t.error_connecting_to_tor.connect(self._error_connecting_to_tor)
self.t.start()
# The main thread needs to remain active, and checkign for Qt events,
# until the thread is finished. Otherwise it won't be able to handle
@ -86,6 +86,7 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
def _canceled_connecting_to_tor(self):
common.log('TorConnectionDialog', '_canceled_connecting_to_tor')
self.active = False
self.onion.cleanup()
# Cancel connecting to Tor
QtCore.QTimer.singleShot(1, self.cancel)

View file

@ -119,6 +119,7 @@
"gui_tor_connection_ask_quit": "Quit",
"gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings.",
"gui_tor_connection_canceled": "OnionShare cannot connect to Tor.\n\nMake sure you're connected to the internet, then re-open OnionShare to configure the Tor connection.",
"gui_tor_connection_lost": "Disconnected from Tor.",
"gui_server_started_after_timeout": "The server started after your chosen auto-timeout.\nPlease start a new share.",
"gui_server_timeout_expired": "The chosen timeout has already expired.\nPlease update the timeout and then you may start sharing.",
"share_via_onionshare": "Share via OnionShare"