When you click "Test Connecting to Tor" in Tor settings, it now uses the TorConnectionDialog

This commit is contained in:
Micah Lee 2021-10-13 20:14:38 -07:00
parent c4a038720b
commit 496adf01d2
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 50 additions and 99 deletions

View File

@ -49,11 +49,15 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
""" """
open_settings = QtCore.Signal() open_settings = QtCore.Signal()
success = QtCore.Signal()
def __init__(self, common, custom_settings=False): def __init__(
self, common, custom_settings=False, testing_settings=False, onion=None
):
super(TorConnectionDialog, self).__init__(None) super(TorConnectionDialog, self).__init__(None)
self.common = common self.common = common
self.testing_settings = testing_settings
if custom_settings: if custom_settings:
self.settings = custom_settings self.settings = custom_settings
@ -62,7 +66,15 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
self.common.log("TorConnectionDialog", "__init__") self.common.log("TorConnectionDialog", "__init__")
self.setWindowTitle("OnionShare") if self.testing_settings:
self.title = strings._("gui_settings_connection_type_test_button")
self.onion = onion
else:
self.title = "OnionShare"
self.onion = self.common.gui.onion
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png"))) self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png")))
self.setModal(True) self.setModal(True)
self.setFixedSize(400, 150) self.setFixedSize(400, 150)
@ -112,7 +124,7 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
def _canceled_connecting_to_tor(self): def _canceled_connecting_to_tor(self):
self.common.log("TorConnectionDialog", "_canceled_connecting_to_tor") self.common.log("TorConnectionDialog", "_canceled_connecting_to_tor")
self.active = False self.active = False
self.common.gui.onion.cleanup() self.onion.cleanup()
# Cancel connecting to Tor # Cancel connecting to Tor
QtCore.QTimer.singleShot(1, self.cancel) QtCore.QTimer.singleShot(1, self.cancel)
@ -121,18 +133,25 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
self.common.log("TorConnectionDialog", "_error_connecting_to_tor") self.common.log("TorConnectionDialog", "_error_connecting_to_tor")
self.active = False self.active = False
def alert_and_open_settings(): if self.testing_settings:
# Display the exception in an alert box # If testing, just display the error but don't open settings
Alert( def alert():
self.common, Alert(self.common, msg, QtWidgets.QMessageBox.Warning, title=self.title)
f"{msg}\n\n{strings._('gui_tor_connection_error_settings')}",
QtWidgets.QMessageBox.Warning,
)
# Open settings else:
self.open_settings.emit() # If not testing, open settings after displaying the error
def alert():
Alert(
self.common,
f"{msg}\n\n{strings._('gui_tor_connection_error_settings')}",
QtWidgets.QMessageBox.Warning,
title=self.title,
)
QtCore.QTimer.singleShot(1, alert_and_open_settings) # Open settings
self.open_settings.emit()
QtCore.QTimer.singleShot(1, alert)
# Cancel connecting to Tor # Cancel connecting to Tor
QtCore.QTimer.singleShot(1, self.cancel) QtCore.QTimer.singleShot(1, self.cancel)
@ -146,13 +165,9 @@ class TorConnectionThread(QtCore.QThread):
def __init__(self, common, settings, dialog): def __init__(self, common, settings, dialog):
super(TorConnectionThread, self).__init__() super(TorConnectionThread, self).__init__()
self.common = common self.common = common
self.common.log("TorConnectionThread", "__init__") self.common.log("TorConnectionThread", "__init__")
self.settings = settings self.settings = settings
self.dialog = dialog self.dialog = dialog
def run(self): def run(self):
@ -160,8 +175,8 @@ class TorConnectionThread(QtCore.QThread):
# Connect to the Onion # Connect to the Onion
try: try:
self.common.gui.onion.connect(self.settings, False, self._tor_status_update) self.dialog.onion.connect(self.settings, False, self._tor_status_update)
if self.common.gui.onion.connected_to_tor: if self.dialog.onion.connected_to_tor:
self.connected_to_tor.emit() self.connected_to_tor.emit()
else: else:
self.canceled_connecting_to_tor.emit() self.canceled_connecting_to_tor.emit()

View File

@ -358,16 +358,10 @@ class TorSettingsDialog(QtWidgets.QDialog):
buttons_layout.addWidget(self.save_button) buttons_layout.addWidget(self.save_button)
buttons_layout.addWidget(self.cancel_button) buttons_layout.addWidget(self.cancel_button)
# Tor network connection status
self.tor_status = QtWidgets.QLabel()
self.tor_status.setStyleSheet(self.common.gui.css["settings_tor_status"])
self.tor_status.hide()
# Layout # Layout
layout = QtWidgets.QVBoxLayout() layout = QtWidgets.QVBoxLayout()
layout.addWidget(connection_type_radio_group) layout.addWidget(connection_type_radio_group)
layout.addLayout(connection_type_layout) layout.addLayout(connection_type_layout)
layout.addWidget(self.tor_status)
layout.addStretch() layout.addStretch()
layout.addLayout(buttons_layout) layout.addLayout(buttons_layout)
@ -547,30 +541,17 @@ class TorSettingsDialog(QtWidgets.QDialog):
self.common.log("TorSettingsDialog", "test_tor_clicked") self.common.log("TorSettingsDialog", "test_tor_clicked")
settings = self.settings_from_fields() settings = self.settings_from_fields()
try: onion = Onion(
# Show Tor connection status if connection type is bundled tor self.common,
if settings.get("connection_type") == "bundled": use_tmp_dir=True,
self.tor_status.show() get_tor_paths=self.common.gui.get_tor_paths,
self._disable_buttons() )
def tor_status_update_func(progress, summary): tor_con = TorConnectionDialog(self.common, settings, True, onion)
self._tor_status_update(progress, summary) tor_con.start()
return True
else: # If Tor settings worked, show results
tor_status_update_func = None if onion.connected_to_tor:
onion = Onion(
self.common,
use_tmp_dir=True,
get_tor_paths=self.common.gui.get_tor_paths,
)
onion.connect(
custom_settings=settings,
tor_status_update_func=tor_status_update_func,
)
# If an exception hasn't been raised yet, the Tor settings work
Alert( Alert(
self.common, self.common,
strings._("settings_test_success").format( strings._("settings_test_success").format(
@ -579,35 +560,11 @@ class TorSettingsDialog(QtWidgets.QDialog):
onion.supports_stealth, onion.supports_stealth,
onion.supports_v3_onions, onion.supports_v3_onions,
), ),
title=strings._("gui_settings_connection_type_test_button"),
) )
# Clean up # Clean up
onion.cleanup() onion.cleanup()
except (
TorErrorInvalidSetting,
TorErrorAutomatic,
TorErrorSocketPort,
TorErrorSocketFile,
TorErrorMissingPassword,
TorErrorUnreadableCookieFile,
TorErrorAuthError,
TorErrorProtocolError,
BundledTorTimeout,
BundledTorBroken,
TorTooOldEphemeral,
TorTooOldStealth,
PortNotAvailable,
) as e:
message = self.common.gui.get_translated_tor_error(e)
Alert(
self.common,
message,
QtWidgets.QMessageBox.Warning,
)
if settings.get("connection_type") == "bundled":
self.tor_status.hide()
self._enable_buttons()
def save_clicked(self): def save_clicked(self):
""" """
@ -748,9 +705,9 @@ class TorSettingsDialog(QtWidgets.QDialog):
settings.set("socks_address", self.connection_type_socks_address.text()) settings.set("socks_address", self.connection_type_socks_address.text())
settings.set("socks_port", self.connection_type_socks_port.text()) settings.set("socks_port", self.connection_type_socks_port.text())
if self.authenticate_no_auth_radio.isChecked(): if self.authenticate_no_auth_checkbox.checkState() == QtCore.Qt.Checked:
settings.set("auth_type", "no_auth") settings.set("auth_type", "no_auth")
if self.authenticate_password_radio.isChecked(): else:
settings.set("auth_type", "password") settings.set("auth_type", "password")
settings.set("auth_password", self.authenticate_password_extras_password.text()) settings.set("auth_password", self.authenticate_password_extras_password.text())
@ -826,25 +783,3 @@ class TorSettingsDialog(QtWidgets.QDialog):
# Wait 1ms for the event loop to finish, then quit # Wait 1ms for the event loop to finish, then quit
QtCore.QTimer.singleShot(1, self.common.gui.qtapp.quit) QtCore.QTimer.singleShot(1, self.common.gui.qtapp.quit)
def _tor_status_update(self, progress, summary):
self.tor_status.setText(
f"<strong>{strings._('connecting_to_tor')}</strong><br>{progress}% {summary}"
)
self.common.gui.qtapp.processEvents()
if "Done" in summary:
self.tor_status.hide()
self._enable_buttons()
def _disable_buttons(self):
self.common.log("TorSettingsDialog", "_disable_buttons")
self.connection_type_test_button.setEnabled(False)
self.save_button.setEnabled(False)
self.cancel_button.setEnabled(False)
def _enable_buttons(self):
self.common.log("TorSettingsDialog", "_enable_buttons")
self.connection_type_test_button.setEnabled(True)
self.save_button.setEnabled(True)
self.cancel_button.setEnabled(True)

View File

@ -37,6 +37,7 @@ class Alert(QtWidgets.QMessageBox):
icon=QtWidgets.QMessageBox.NoIcon, icon=QtWidgets.QMessageBox.NoIcon,
buttons=QtWidgets.QMessageBox.Ok, buttons=QtWidgets.QMessageBox.Ok,
autostart=True, autostart=True,
title="OnionShare",
): ):
super(Alert, self).__init__(None) super(Alert, self).__init__(None)
@ -44,7 +45,7 @@ class Alert(QtWidgets.QMessageBox):
self.common.log("Alert", "__init__") self.common.log("Alert", "__init__")
self.setWindowTitle("OnionShare") self.setWindowTitle(title)
self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png"))) self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png")))
self.setText(message) self.setText(message)
self.setIcon(icon) self.setIcon(icon)