From 55d6ac4e3d02e02d718569ee0c718ad8dcbe3eab Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 20 Oct 2021 18:56:37 -0700 Subject: [PATCH 01/29] Refactor SettingsDialog into SettingsTab --- desktop/src/onionshare/main_window.py | 19 ++-- .../{settings_dialog.py => settings_tab.py} | 78 +++------------- desktop/src/onionshare/tab_widget.py | 91 ++++++++++++++----- ...settings_dialog.py => tor_settings_tab.py} | 48 +++++----- 4 files changed, 115 insertions(+), 121 deletions(-) rename desktop/src/onionshare/{settings_dialog.py => settings_tab.py} (84%) rename desktop/src/onionshare/{tor_settings_dialog.py => tor_settings_tab.py} (95%) diff --git a/desktop/src/onionshare/main_window.py b/desktop/src/onionshare/main_window.py index c125741c..0f11cf8e 100644 --- a/desktop/src/onionshare/main_window.py +++ b/desktop/src/onionshare/main_window.py @@ -24,8 +24,6 @@ from PySide2 import QtCore, QtWidgets, QtGui from . import strings from .tor_connection_dialog import TorConnectionDialog -from .tor_settings_dialog import TorSettingsDialog -from .settings_dialog import SettingsDialog from .widgets import Alert from .update_checker import UpdateThread from .tab_widget import TabWidget @@ -245,21 +243,22 @@ class MainWindow(QtWidgets.QMainWindow): def open_tor_settings(self): """ - Open the TorSettingsDialog. + Open the TorSettingsTab """ self.common.log("MainWindow", "open_tor_settings") - d = TorSettingsDialog(self.common) - d.settings_saved.connect(self.settings_have_changed) - d.exec_() + # d = TorSettingsDialog(self.common) + # d.settings_saved.connect(self.settings_have_changed) + # d.exec_() def open_settings(self): """ - Open the SettingsDialog. + Open the SettingsTab """ self.common.log("MainWindow", "open_settings") - d = SettingsDialog(self.common) - d.settings_saved.connect(self.settings_have_changed) - d.exec_() + self.tabs.open_settings_tab() + # d = SettingsDialog(self.common) + # d.settings_saved.connect(self.settings_have_changed) + # d.exec_() def settings_have_changed(self): self.common.log("OnionShareGui", "settings_have_changed") diff --git a/desktop/src/onionshare/settings_dialog.py b/desktop/src/onionshare/settings_tab.py similarity index 84% rename from desktop/src/onionshare/settings_dialog.py rename to desktop/src/onionshare/settings_tab.py index b1003386..251783aa 100644 --- a/desktop/src/onionshare/settings_dialog.py +++ b/desktop/src/onionshare/settings_tab.py @@ -19,57 +19,34 @@ along with this program. If not, see . """ from PySide2 import QtCore, QtWidgets, QtGui -from PySide2.QtCore import Slot, Qt -from PySide2.QtGui import QPalette, QColor import sys import platform import datetime import re import os from onionshare_cli.settings import Settings -from onionshare_cli.onion import ( - Onion, - TorErrorInvalidSetting, - TorErrorAutomatic, - TorErrorSocketPort, - TorErrorSocketFile, - TorErrorMissingPassword, - TorErrorUnreadableCookieFile, - TorErrorAuthError, - TorErrorProtocolError, - BundledTorTimeout, - BundledTorBroken, - TorTooOldEphemeral, - TorTooOldStealth, - PortNotAvailable, -) from . import strings from .widgets import Alert from .update_checker import UpdateThread -from .tor_connection_dialog import TorConnectionDialog from .gui_common import GuiCommon -class SettingsDialog(QtWidgets.QDialog): +class SettingsTab(QtWidgets.QWidget): """ Settings dialog. """ settings_saved = QtCore.Signal() - def __init__(self, common): - super(SettingsDialog, self).__init__() + def __init__(self, common, tab_id): + super(SettingsTab, self).__init__() self.common = common - - self.common.log("SettingsDialog", "__init__") - - self.setModal(True) - self.setWindowTitle(strings._("gui_settings_window_title")) - self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png"))) + self.common.log("SettingsTab", "__init__") self.system = platform.system() + self.tab_id = tab_id # Automatic updates options @@ -146,31 +123,26 @@ class SettingsDialog(QtWidgets.QDialog): # Buttons self.save_button = QtWidgets.QPushButton(strings._("gui_settings_button_save")) self.save_button.clicked.connect(self.save_clicked) - self.cancel_button = QtWidgets.QPushButton( - strings._("gui_settings_button_cancel") - ) - self.cancel_button.clicked.connect(self.cancel_clicked) buttons_layout = QtWidgets.QHBoxLayout() buttons_layout.addStretch() buttons_layout.addWidget(self.save_button) - buttons_layout.addWidget(self.cancel_button) # Layout layout = QtWidgets.QVBoxLayout() + layout.addStretch() layout.addWidget(autoupdate_group) if autoupdate_group.isVisible(): layout.addSpacing(20) layout.addLayout(language_layout) layout.addLayout(theme_layout) layout.addSpacing(20) - layout.addStretch() layout.addWidget(version_label) layout.addWidget(help_label) layout.addSpacing(20) layout.addLayout(buttons_layout) + layout.addStretch() self.setLayout(layout) - self.cancel_button.setFocus() self.reload_settings() @@ -199,7 +171,7 @@ class SettingsDialog(QtWidgets.QDialog): """ Check for Updates button clicked. Manually force an update check. """ - self.common.log("SettingsDialog", "check_for_updates") + self.common.log("SettingsTab", "check_for_updates") # Disable buttons self._disable_buttons() self.common.gui.qtapp.processEvents() @@ -261,7 +233,7 @@ class SettingsDialog(QtWidgets.QDialog): """ Save button clicked. Save current settings to disk. """ - self.common.log("SettingsDialog", "save_clicked") + self.common.log("SettingsTab", "save_clicked") def changed(s1, s2, keys): """ @@ -301,30 +273,12 @@ class SettingsDialog(QtWidgets.QDialog): self.settings_saved.emit() self.close() - def cancel_clicked(self): - """ - Cancel button clicked. - """ - self.common.log("SettingsDialog", "cancel_clicked") - if ( - not self.common.gui.local_only - and not self.common.gui.onion.is_authenticated() - ): - Alert( - self.common, - strings._("gui_tor_connection_canceled"), - QtWidgets.QMessageBox.Warning, - ) - sys.exit() - else: - self.close() - def help_clicked(self): """ Help button clicked. """ - self.common.log("SettingsDialog", "help_clicked") - SettingsDialog.open_help() + self.common.log("SettingsTab", "help_clicked") + SettingsTab.open_help() @staticmethod def open_help(): @@ -335,7 +289,7 @@ class SettingsDialog(QtWidgets.QDialog): """ Return a Settings object that's full of values from the settings dialog. """ - self.common.log("SettingsDialog", "settings_from_fields") + self.common.log("SettingsTab", "settings_from_fields") settings = Settings(self.common) settings.load() # To get the last update timestamp @@ -351,7 +305,7 @@ class SettingsDialog(QtWidgets.QDialog): return settings def _update_autoupdate_timestamp(self, autoupdate_timestamp): - self.common.log("SettingsDialog", "_update_autoupdate_timestamp") + self.common.log("SettingsTab", "_update_autoupdate_timestamp") if autoupdate_timestamp: dt = datetime.datetime.fromtimestamp(autoupdate_timestamp) @@ -363,18 +317,16 @@ class SettingsDialog(QtWidgets.QDialog): ) def _disable_buttons(self): - self.common.log("SettingsDialog", "_disable_buttons") + self.common.log("SettingsTab", "_disable_buttons") self.check_for_updates_button.setEnabled(False) self.save_button.setEnabled(False) - self.cancel_button.setEnabled(False) def _enable_buttons(self): - self.common.log("SettingsDialog", "_enable_buttons") + self.common.log("SettingsTab", "_enable_buttons") # We can't check for updates if we're still not connected to Tor if not self.common.gui.onion.connected_to_tor: self.check_for_updates_button.setEnabled(False) else: self.check_for_updates_button.setEnabled(True) self.save_button.setEnabled(True) - self.cancel_button.setEnabled(True) diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index a955ea53..daf878d7 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -26,6 +26,8 @@ from . import strings from .tab import Tab from .threads import EventHandlerThread from .gui_common import GuiCommon +from .tor_settings_tab import TorSettingsTab +from .settings_tab import SettingsTab class TabWidget(QtWidgets.QTabWidget): @@ -116,6 +118,11 @@ class TabWidget(QtWidgets.QTabWidget): # Active tab was changed tab_id = self.currentIndex() self.common.log("TabWidget", "tab_changed", f"Tab was changed to {tab_id}") + + # If it's Settings or Tor Settings, ignore + if self.is_settings_tab(tab_id): + return + try: mode = self.tabs[tab_id].get_mode() if mode: @@ -160,20 +167,7 @@ class TabWidget(QtWidgets.QTabWidget): # In macOS, manually create a close button because tabs don't seem to have them otherwise if self.common.platform == "Darwin": - - def close_tab(): - self.tabBar().tabCloseRequested.emit(self.indexOf(tab)) - - tab.close_button = QtWidgets.QPushButton() - tab.close_button.setFlat(True) - tab.close_button.setFixedWidth(40) - tab.close_button.setIcon( - QtGui.QIcon(GuiCommon.get_resource_path("images/close_tab.png")) - ) - tab.close_button.clicked.connect(close_tab) - self.tabBar().setTabButton( - index, QtWidgets.QTabBar.RightSide, tab.close_button - ) + self.macos_create_close_button(tab, index) tab.init(mode_settings) @@ -187,6 +181,25 @@ class TabWidget(QtWidgets.QTabWidget): # Bring the window to front, in case this is being added by an event self.bring_to_front.emit() + def open_settings_tab(self): + self.common.log("TabWidget", "open_settings_tab") + + # See if a settings tab is already open, and if so switch to it + for index in range(self.count()): + if self.is_settings_tab(index): + self.setCurrentIndex(index) + return + + settings_tab = SettingsTab(self.common, self.current_tab_id) + self.tabs[self.current_tab_id] = settings_tab + self.current_tab_id += 1 + index = self.addTab(settings_tab, strings._("gui_settings_window_title")) + self.setCurrentIndex(index) + + # In macOS, manually create a close button because tabs don't seem to have them otherwise + if self.common.platform == "Darwin": + self.macos_create_close_button(settings_tab, index) + def change_title(self, tab_id, title): shortened_title = title if len(shortened_title) > 11: @@ -224,9 +237,10 @@ class TabWidget(QtWidgets.QTabWidget): # Figure out the order of persistent tabs to save in settings persistent_tabs = [] for index in range(self.count()): - tab = self.widget(index) - if tab.settings.get("persistent", "enabled"): - persistent_tabs.append(tab.settings.id) + if not self.is_settings_tab(index): + tab = self.widget(index) + if tab.settings.get("persistent", "enabled"): + persistent_tabs.append(tab.settings.id) # Only save if tabs have actually moved if persistent_tabs != self.common.settings.get("persistent_tabs"): self.common.settings.set("persistent_tabs", persistent_tabs) @@ -235,11 +249,8 @@ class TabWidget(QtWidgets.QTabWidget): def close_tab(self, index): self.common.log("TabWidget", "close_tab", f"{index}") tab = self.widget(index) - if tab.close_tab(): - # If the tab is persistent, delete the settings file from disk - if tab.settings.get("persistent", "enabled"): - tab.settings.delete() + if self.is_settings_tab(index): # Remove the tab self.removeTab(index) del self.tabs[tab.tab_id] @@ -248,7 +259,21 @@ class TabWidget(QtWidgets.QTabWidget): if self.count() == 0: self.new_tab_clicked() - self.save_persistent_tabs() + else: + if tab.close_tab(): + # If the tab is persistent, delete the settings file from disk + if tab.settings.get("persistent", "enabled"): + tab.settings.delete() + + self.save_persistent_tabs() + + # Remove the tab + self.removeTab(index) + del self.tabs[tab.tab_id] + + # If the last tab is closed, open a new one + if self.count() == 0: + self.new_tab_clicked() def are_tabs_active(self): """ @@ -273,6 +298,28 @@ class TabWidget(QtWidgets.QTabWidget): super(TabWidget, self).resizeEvent(event) self.move_new_tab_button() + def macos_create_close_button(self, tab, index): + def close_tab(): + self.tabBar().tabCloseRequested.emit(self.indexOf(tab)) + + close_button = QtWidgets.QPushButton() + close_button.setFlat(True) + close_button.setFixedWidth(40) + close_button.setIcon( + QtGui.QIcon(GuiCommon.get_resource_path("images/close_tab.png")) + ) + close_button.clicked.connect(close_tab) + self.tabBar().setTabButton(index, QtWidgets.QTabBar.RightSide, tab.close_button) + + def is_settings_tab(self, tab_id): + if tab_id not in self.tabs: + return True + + return ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ) + class TabBar(QtWidgets.QTabBar): """ diff --git a/desktop/src/onionshare/tor_settings_dialog.py b/desktop/src/onionshare/tor_settings_tab.py similarity index 95% rename from desktop/src/onionshare/tor_settings_dialog.py rename to desktop/src/onionshare/tor_settings_tab.py index 38ff512a..279469df 100644 --- a/desktop/src/onionshare/tor_settings_dialog.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -34,25 +34,21 @@ from .moat_dialog import MoatDialog from .gui_common import GuiCommon -class TorSettingsDialog(QtWidgets.QDialog): +class TorSettingsTab(QtWidgets.QWidget): """ Settings dialog. """ settings_saved = QtCore.Signal() - def __init__(self, common): - super(TorSettingsDialog, self).__init__() + def __init__(self, common, tab_id): + super(TorSettingsTab, self).__init__() self.common = common - - self.common.log("TorSettingsDialog", "__init__") - - self.setModal(True) - self.setWindowTitle(strings._("gui_tor_settings_window_title")) - self.setWindowIcon(QtGui.QIcon(GuiCommon.get_resource_path("images/logo.png"))) + self.common.log("TorSettingsTab", "__init__") self.system = platform.system() + self.tab_id = tab_id # Connection type: either automatic, control port, or socket file @@ -443,7 +439,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Connection type bundled was toggled """ - self.common.log("TorSettingsDialog", "connection_type_bundled_toggled") + self.common.log("TorSettingsTab", "connection_type_bundled_toggled") if checked: self.tor_settings_group.hide() self.connection_type_socks.hide() @@ -495,7 +491,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Request new bridge button clicked """ - self.common.log("TorSettingsDialog", "bridge_moat_button_clicked") + self.common.log("TorSettingsTab", "bridge_moat_button_clicked") moat_dialog = MoatDialog(self.common) moat_dialog.got_bridges.connect(self.bridge_moat_got_bridges) @@ -505,7 +501,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Got new bridges from moat """ - self.common.log("TorSettingsDialog", "bridge_moat_got_bridges") + self.common.log("TorSettingsTab", "bridge_moat_got_bridges") self.bridge_moat_textbox.document().setPlainText(bridges) self.bridge_moat_textbox.show() @@ -522,7 +518,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Connection type automatic was toggled. If checked, hide authentication fields. """ - self.common.log("TorSettingsDialog", "connection_type_automatic_toggled") + self.common.log("TorSettingsTab", "connection_type_automatic_toggled") if checked: self.tor_settings_group.hide() self.connection_type_socks.hide() @@ -533,7 +529,7 @@ class TorSettingsDialog(QtWidgets.QDialog): Connection type control port was toggled. If checked, show extra fields for Tor control address and port. If unchecked, hide those extra fields. """ - self.common.log("TorSettingsDialog", "connection_type_control_port_toggled") + self.common.log("TorSettingsTab", "connection_type_control_port_toggled") if checked: self.tor_settings_group.show() self.connection_type_control_port_extras.show() @@ -547,7 +543,7 @@ class TorSettingsDialog(QtWidgets.QDialog): Connection type socket file was toggled. If checked, show extra fields for socket file. If unchecked, hide those extra fields. """ - self.common.log("TorSettingsDialog", "connection_type_socket_file_toggled") + self.common.log("TorSettingsTab", "connection_type_socket_file_toggled") if checked: self.tor_settings_group.show() self.connection_type_socket_file_extras.show() @@ -560,7 +556,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Authentication option no authentication was toggled. """ - self.common.log("TorSettingsDialog", "authenticate_no_auth_toggled") + self.common.log("TorSettingsTab", "authenticate_no_auth_toggled") if checked: self.authenticate_password_extras.hide() else: @@ -571,7 +567,7 @@ class TorSettingsDialog(QtWidgets.QDialog): Test Tor Settings button clicked. With the given settings, see if we can successfully connect and authenticate to Tor. """ - self.common.log("TorSettingsDialog", "test_tor_clicked") + self.common.log("TorSettingsTab", "test_tor_clicked") settings = self.settings_from_fields() if not settings: return @@ -605,7 +601,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Save button clicked. Save current settings to disk. """ - self.common.log("TorSettingsDialog", "save_clicked") + self.common.log("TorSettingsTab", "save_clicked") def changed(s1, s2, keys): """ @@ -628,7 +624,7 @@ class TorSettingsDialog(QtWidgets.QDialog): if not self.common.gui.local_only: if self.common.gui.onion.is_authenticated(): self.common.log( - "TorSettingsDialog", "save_clicked", "Connected to Tor" + "TorSettingsTab", "save_clicked", "Connected to Tor" ) if changed( @@ -654,7 +650,7 @@ class TorSettingsDialog(QtWidgets.QDialog): else: self.common.log( - "TorSettingsDialog", "save_clicked", "Not connected to Tor" + "TorSettingsTab", "save_clicked", "Not connected to Tor" ) # Tor isn't connected, so try connecting reboot_onion = True @@ -663,7 +659,7 @@ class TorSettingsDialog(QtWidgets.QDialog): if reboot_onion: # Reinitialize the Onion object self.common.log( - "TorSettingsDialog", "save_clicked", "rebooting the Onion" + "TorSettingsTab", "save_clicked", "rebooting the Onion" ) self.common.gui.onion.cleanup() @@ -671,7 +667,7 @@ class TorSettingsDialog(QtWidgets.QDialog): tor_con.start() self.common.log( - "TorSettingsDialog", + "TorSettingsTab", "save_clicked", f"Onion done rebooting, connected to Tor: {self.common.gui.onion.connected_to_tor}", ) @@ -694,7 +690,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Cancel button clicked. """ - self.common.log("TorSettingsDialog", "cancel_clicked") + self.common.log("TorSettingsTab", "cancel_clicked") if ( not self.common.gui.local_only and not self.common.gui.onion.is_authenticated() @@ -712,7 +708,7 @@ class TorSettingsDialog(QtWidgets.QDialog): """ Return a Settings object that's full of values from the settings dialog. """ - self.common.log("TorSettingsDialog", "settings_from_fields") + self.common.log("TorSettingsTab", "settings_from_fields") settings = Settings(self.common) settings.load() # To get the last update timestamp @@ -833,13 +829,13 @@ class TorSettingsDialog(QtWidgets.QDialog): return settings def closeEvent(self, e): - self.common.log("TorSettingsDialog", "closeEvent") + self.common.log("TorSettingsTab", "closeEvent") # On close, if Tor isn't connected, then quit OnionShare altogether if not self.common.gui.local_only: if not self.common.gui.onion.is_authenticated(): self.common.log( - "TorSettingsDialog", + "TorSettingsTab", "closeEvent", "Closing while not connected to Tor", ) From 0fb7d7d761397d6240b76faaaafaa8bf6661280c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 20 Oct 2021 19:03:24 -0700 Subject: [PATCH 02/29] Refactor TorSettingsDialog into TorSettingsTab --- desktop/src/onionshare/main_window.py | 7 +--- desktop/src/onionshare/tab_widget.py | 37 +++++++++++++++++---- desktop/src/onionshare/tor_settings_tab.py | 38 ++++++++++------------ 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/desktop/src/onionshare/main_window.py b/desktop/src/onionshare/main_window.py index 0f11cf8e..4a9d0c7e 100644 --- a/desktop/src/onionshare/main_window.py +++ b/desktop/src/onionshare/main_window.py @@ -246,9 +246,7 @@ class MainWindow(QtWidgets.QMainWindow): Open the TorSettingsTab """ self.common.log("MainWindow", "open_tor_settings") - # d = TorSettingsDialog(self.common) - # d.settings_saved.connect(self.settings_have_changed) - # d.exec_() + self.tabs.open_tor_settings_tab() def open_settings(self): """ @@ -256,9 +254,6 @@ class MainWindow(QtWidgets.QMainWindow): """ self.common.log("MainWindow", "open_settings") self.tabs.open_settings_tab() - # d = SettingsDialog(self.common) - # d.settings_saved.connect(self.settings_have_changed) - # d.exec_() def settings_have_changed(self): self.common.log("OnionShareGui", "settings_have_changed") diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index daf878d7..fe6d08dc 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -92,8 +92,9 @@ class TabWidget(QtWidgets.QTabWidget): # Clean up each tab for index in range(self.count()): - tab = self.widget(index) - tab.cleanup() + if not self.is_settings_tab(index): + tab = self.widget(index) + tab.cleanup() def move_new_tab_button(self): # Find the width of all tabs @@ -186,7 +187,7 @@ class TabWidget(QtWidgets.QTabWidget): # See if a settings tab is already open, and if so switch to it for index in range(self.count()): - if self.is_settings_tab(index): + if type(self.tabs[index]) is SettingsTab: self.setCurrentIndex(index) return @@ -200,6 +201,27 @@ class TabWidget(QtWidgets.QTabWidget): if self.common.platform == "Darwin": self.macos_create_close_button(settings_tab, index) + def open_tor_settings_tab(self): + self.common.log("TabWidget", "open_tor_settings_tab") + + # See if a settings tab is already open, and if so switch to it + for index in range(self.count()): + if type(self.tabs[index]) is TorSettingsTab: + self.setCurrentIndex(index) + return + + tor_settings_tab = TorSettingsTab(self.common, self.current_tab_id) + self.tabs[self.current_tab_id] = tor_settings_tab + self.current_tab_id += 1 + index = self.addTab( + tor_settings_tab, strings._("gui_tor_settings_window_title") + ) + self.setCurrentIndex(index) + + # In macOS, manually create a close button because tabs don't seem to have them otherwise + if self.common.platform == "Darwin": + self.macos_create_close_button(tor_settings_tab, index) + def change_title(self, tab_id, title): shortened_title = title if len(shortened_title) > 11: @@ -280,10 +302,11 @@ class TabWidget(QtWidgets.QTabWidget): See if there are active servers in any open tabs """ for tab_id in self.tabs: - mode = self.tabs[tab_id].get_mode() - if mode: - if mode.server_status.status != mode.server_status.STATUS_STOPPED: - return True + if not self.is_settings_tab(tab_id): + mode = self.tabs[tab_id].get_mode() + if mode: + if mode.server_status.status != mode.server_status.STATUS_STOPPED: + return True return False def paintEvent(self, event): diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 279469df..e46fa729 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -319,15 +319,10 @@ class TorSettingsTab(QtWidgets.QWidget): self.test_tor_button.clicked.connect(self.test_tor_clicked) self.save_button = QtWidgets.QPushButton(strings._("gui_settings_button_save")) self.save_button.clicked.connect(self.save_clicked) - self.cancel_button = QtWidgets.QPushButton( - strings._("gui_settings_button_cancel") - ) - self.cancel_button.clicked.connect(self.cancel_clicked) buttons_layout = QtWidgets.QHBoxLayout() buttons_layout.addWidget(self.test_tor_button) buttons_layout.addStretch() buttons_layout.addWidget(self.save_button) - buttons_layout.addWidget(self.cancel_button) # Layout layout = QtWidgets.QVBoxLayout() @@ -337,7 +332,6 @@ class TorSettingsTab(QtWidgets.QWidget): layout.addLayout(buttons_layout) self.setLayout(layout) - self.cancel_button.setFocus() self.reload_settings() @@ -686,23 +680,27 @@ class TorSettingsTab(QtWidgets.QWidget): self.settings_saved.emit() self.close() - def cancel_clicked(self): + def close_tab(self): """ - Cancel button clicked. + Tab is closed """ self.common.log("TorSettingsTab", "cancel_clicked") - if ( - not self.common.gui.local_only - and not self.common.gui.onion.is_authenticated() - ): - Alert( - self.common, - strings._("gui_tor_connection_canceled"), - QtWidgets.QMessageBox.Warning, - ) - sys.exit() - else: - self.close() + return True + + # TODO: Figure out flow for first connecting, when closing settings when not connected + + # if ( + # not self.common.gui.local_only + # and not self.common.gui.onion.is_authenticated() + # ): + # Alert( + # self.common, + # strings._("gui_tor_connection_canceled"), + # QtWidgets.QMessageBox.Warning, + # ) + # sys.exit() + # else: + # self.close() def settings_from_fields(self): """ From 3b9cc80160bf0658e6b7f908cac325f7c99b9041 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 20 Oct 2021 20:33:16 -0700 Subject: [PATCH 03/29] Create a TorConnectionWidget, and use that when testing settings --- .../src/onionshare/tor_connection_dialog.py | 128 ++++++++++++++++++ desktop/src/onionshare/tor_settings_tab.py | 38 +++++- 2 files changed, 160 insertions(+), 6 deletions(-) diff --git a/desktop/src/onionshare/tor_connection_dialog.py b/desktop/src/onionshare/tor_connection_dialog.py index daf49a32..7ba7c800 100644 --- a/desktop/src/onionshare/tor_connection_dialog.py +++ b/desktop/src/onionshare/tor_connection_dialog.py @@ -157,6 +157,134 @@ class TorConnectionDialog(QtWidgets.QProgressDialog): QtCore.QTimer.singleShot(1, self.cancel) +class TorConnectionWidget(QtWidgets.QWidget): + """ + Connecting to Tor widget, with a progress bar + """ + + open_tor_settings = QtCore.Signal() + success = QtCore.Signal() + fail = QtCore.Signal() + + def __init__(self, common): + super(TorConnectionWidget, self).__init__(None) + self.common = common + self.common.log("TorConnectionWidget", "__init__") + + self.label = QtWidgets.QLabel(strings._("connecting_to_tor")) + self.label.setAlignment(QtCore.Qt.AlignHCenter) + + self.progress = QtWidgets.QProgressBar() + self.progress.setRange(0, 100) + self.cancel_button = QtWidgets.QPushButton( + strings._("gui_settings_button_cancel") + ) + self.cancel_button.clicked.connect(self.cancel_clicked) + + progress_layout = QtWidgets.QHBoxLayout() + progress_layout.addWidget(self.progress) + progress_layout.addWidget(self.cancel_button) + + inner_layout = QtWidgets.QVBoxLayout() + inner_layout.addWidget(self.label) + inner_layout.addLayout(progress_layout) + + layout = QtWidgets.QHBoxLayout() + layout.addStretch() + layout.addLayout(inner_layout) + layout.addStretch() + self.setLayout(layout) + + # Start displaying the status at 0 + self._tor_status_update(0, "") + + def start(self, custom_settings=False, testing_settings=False, onion=None): + self.common.log("TorConnectionWidget", "start") + self.was_canceled = False + + self.testing_settings = testing_settings + + if custom_settings: + self.settings = custom_settings + else: + self.settings = self.common.settings + + if self.testing_settings: + self.onion = onion + else: + self.onion = self.common.gui.onion + + t = TorConnectionThread(self.common, self.settings, self) + 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() + + # The main thread needs to remain active, and checking for Qt events, + # until the thread is finished. Otherwise it won't be able to handle + # accepting signals. + self.active = True + while self.active: + time.sleep(0.1) + self.common.gui.qtapp.processEvents() + + def cancel_clicked(self): + self.was_canceled = True + self.fail.emit() + + def wasCanceled(self): + return self.was_canceled + + def _tor_status_update(self, progress, summary): + self.progress.setValue(int(progress)) + self.label.setText( + f"{strings._('connecting_to_tor')}
{summary}" + ) + + def _connected_to_tor(self): + self.common.log("TorConnectionWidget", "_connected_to_tor") + self.active = False + + # Close the dialog after connecting + self.progress.setValue(self.progress.maximum()) + + self.success.emit() + + def _canceled_connecting_to_tor(self): + self.common.log("TorConnectionWidget", "_canceled_connecting_to_tor") + self.active = False + self.onion.cleanup() + + # Cancel connecting to Tor + QtCore.QTimer.singleShot(1, self.cancel_clicked) + + def _error_connecting_to_tor(self, msg): + self.common.log("TorConnectionWidget", "_error_connecting_to_tor") + self.active = False + + if self.testing_settings: + # If testing, just display the error but don't open settings + def alert(): + Alert(self.common, msg, QtWidgets.QMessageBox.Warning, title=self.title) + + else: + # 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, + ) + + # Open settings + self.open_tor_settings.emit() + + QtCore.QTimer.singleShot(1, alert) + self.fail.emit() + + class TorConnectionThread(QtCore.QThread): tor_status_update = QtCore.Signal(str, str) connected_to_tor = QtCore.Signal() diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index e46fa729..4b84e923 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -29,7 +29,7 @@ from onionshare_cli.onion import Onion from . import strings from .widgets import Alert -from .tor_connection_dialog import TorConnectionDialog +from .tor_connection_dialog import TorConnectionDialog, TorConnectionWidget from .moat_dialog import MoatDialog from .gui_common import GuiCommon @@ -291,6 +291,7 @@ class TorSettingsTab(QtWidgets.QWidget): connection_type_radio_group_layout.addWidget( self.connection_type_socket_file_radio ) + connection_type_radio_group_layout.addStretch() connection_type_radio_group = QtWidgets.QGroupBox( strings._("gui_settings_connection_type_label") ) @@ -311,6 +312,17 @@ class TorSettingsTab(QtWidgets.QWidget): connection_type_layout = QtWidgets.QVBoxLayout() connection_type_layout.addWidget(self.tor_settings_group) connection_type_layout.addWidget(self.connection_type_bridges_radio_group) + connection_type_layout.addStretch() + + # Settings are in columns + columns_layout = QtWidgets.QHBoxLayout() + columns_layout.addWidget(connection_type_radio_group) + columns_layout.addSpacing(20) + columns_layout.addLayout(connection_type_layout, stretch=1) + + # Tor connection widget + self.tor_con = TorConnectionWidget(self.common) + self.tor_con.hide() # Buttons self.test_tor_button = QtWidgets.QPushButton( @@ -320,16 +332,19 @@ class TorSettingsTab(QtWidgets.QWidget): self.save_button = QtWidgets.QPushButton(strings._("gui_settings_button_save")) self.save_button.clicked.connect(self.save_clicked) buttons_layout = QtWidgets.QHBoxLayout() - buttons_layout.addWidget(self.test_tor_button) buttons_layout.addStretch() + buttons_layout.addWidget(self.test_tor_button) buttons_layout.addWidget(self.save_button) + buttons_layout.addStretch() # Layout layout = QtWidgets.QVBoxLayout() - layout.addWidget(connection_type_radio_group) - layout.addLayout(connection_type_layout) + layout.addStretch() + layout.addLayout(columns_layout) + layout.addWidget(self.tor_con) layout.addStretch() layout.addLayout(buttons_layout) + layout.addStretch() self.setLayout(layout) @@ -566,14 +581,18 @@ class TorSettingsTab(QtWidgets.QWidget): if not settings: return + self.test_tor_button.hide() + onion = Onion( self.common, use_tmp_dir=True, get_tor_paths=self.common.gui.get_tor_paths, ) - tor_con = TorConnectionDialog(self.common, settings, True, onion) - tor_con.start() + self.tor_con.show() + self.tor_con.success.connect(self.test_tor_button_finished) + self.tor_con.fail.connect(self.test_tor_button_finished) + self.tor_con.start(settings, True, onion) # If Tor settings worked, show results if onion.connected_to_tor: @@ -591,6 +610,13 @@ class TorSettingsTab(QtWidgets.QWidget): # Clean up onion.cleanup() + def test_tor_button_finished(self): + """ + Finished testing tor connection. + """ + self.tor_con.hide() + self.test_tor_button.show() + def save_clicked(self): """ Save button clicked. Save current settings to disk. From 556aedf08d296d7fcb83c4f1933689f250561bd2 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 20 Oct 2021 21:06:38 -0700 Subject: [PATCH 04/29] Fix mixup with tab_ids and their indicies, so tabs open and close smoothly --- desktop/src/onionshare/settings_tab.py | 10 +++++--- desktop/src/onionshare/tab_widget.py | 30 +++++++++++++++++----- desktop/src/onionshare/tor_settings_tab.py | 14 +++++----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/desktop/src/onionshare/settings_tab.py b/desktop/src/onionshare/settings_tab.py index 251783aa..c01d2662 100644 --- a/desktop/src/onionshare/settings_tab.py +++ b/desktop/src/onionshare/settings_tab.py @@ -37,7 +37,7 @@ class SettingsTab(QtWidgets.QWidget): Settings dialog. """ - settings_saved = QtCore.Signal() + close_this_tab = QtCore.Signal() def __init__(self, common, tab_id): super(SettingsTab, self).__init__() @@ -94,6 +94,7 @@ class SettingsTab(QtWidgets.QWidget): locale = language_names_to_locales[language_name] self.language_combobox.addItem(language_name, locale) language_layout = QtWidgets.QHBoxLayout() + language_layout.addStretch() language_layout.addWidget(language_label) language_layout.addWidget(self.language_combobox) language_layout.addStretch() @@ -108,6 +109,7 @@ class SettingsTab(QtWidgets.QWidget): ] self.theme_combobox.addItems(theme_choices) theme_layout = QtWidgets.QHBoxLayout() + theme_layout.addStretch() theme_layout.addWidget(theme_label) theme_layout.addWidget(self.theme_combobox) theme_layout.addStretch() @@ -116,7 +118,9 @@ class SettingsTab(QtWidgets.QWidget): version_label = QtWidgets.QLabel( strings._("gui_settings_version_label").format(self.common.version) ) + version_label.setAlignment(QtCore.Qt.AlignHCenter) help_label = QtWidgets.QLabel(strings._("gui_settings_help_label")) + help_label.setAlignment(QtCore.Qt.AlignHCenter) help_label.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction) help_label.setOpenExternalLinks(True) @@ -126,6 +130,7 @@ class SettingsTab(QtWidgets.QWidget): buttons_layout = QtWidgets.QHBoxLayout() buttons_layout.addStretch() buttons_layout.addWidget(self.save_button) + buttons_layout.addStretch() # Layout layout = QtWidgets.QVBoxLayout() @@ -270,8 +275,7 @@ class SettingsTab(QtWidgets.QWidget): # Save the new settings settings.save() - self.settings_saved.emit() - self.close() + self.close_this_tab.emit() def help_clicked(self): """ diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index fe6d08dc..36a6c22f 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -186,12 +186,13 @@ class TabWidget(QtWidgets.QTabWidget): self.common.log("TabWidget", "open_settings_tab") # See if a settings tab is already open, and if so switch to it - for index in range(self.count()): - if type(self.tabs[index]) is SettingsTab: - self.setCurrentIndex(index) + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is SettingsTab: + self.setCurrentIndex(self.indexOf(self.tabs[tab_id])) return settings_tab = SettingsTab(self.common, self.current_tab_id) + settings_tab.close_this_tab.connect(self.close_settings_tab) self.tabs[self.current_tab_id] = settings_tab self.current_tab_id += 1 index = self.addTab(settings_tab, strings._("gui_settings_window_title")) @@ -205,12 +206,13 @@ class TabWidget(QtWidgets.QTabWidget): self.common.log("TabWidget", "open_tor_settings_tab") # See if a settings tab is already open, and if so switch to it - for index in range(self.count()): - if type(self.tabs[index]) is TorSettingsTab: - self.setCurrentIndex(index) + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is TorSettingsTab: + self.setCurrentIndex(self.indexOf(self.tabs[tab_id])) return tor_settings_tab = TorSettingsTab(self.common, self.current_tab_id) + tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) self.tabs[self.current_tab_id] = tor_settings_tab self.current_tab_id += 1 index = self.addTab( @@ -297,6 +299,22 @@ class TabWidget(QtWidgets.QTabWidget): if self.count() == 0: self.new_tab_clicked() + def close_settings_tab(self): + self.common.log("TabWidget", "close_settings_tab") + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is SettingsTab: + index = self.indexOf(self.tabs[tab_id]) + self.close_tab(index) + return + + def close_tor_settings_tab(self): + self.common.log("TabWidget", "close_tor_settings_tab") + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is TorSettingsTab: + index = self.indexOf(self.tabs[tab_id]) + self.close_tab(index) + return + def are_tabs_active(self): """ See if there are active servers in any open tabs diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 4b84e923..e2fcead4 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -39,7 +39,7 @@ class TorSettingsTab(QtWidgets.QWidget): Settings dialog. """ - settings_saved = QtCore.Signal() + close_this_tab = QtCore.Signal() def __init__(self, common, tab_id): super(TorSettingsTab, self).__init__() @@ -339,7 +339,6 @@ class TorSettingsTab(QtWidgets.QWidget): # Layout layout = QtWidgets.QVBoxLayout() - layout.addStretch() layout.addLayout(columns_layout) layout.addWidget(self.tor_con) layout.addStretch() @@ -582,6 +581,7 @@ class TorSettingsTab(QtWidgets.QWidget): return self.test_tor_button.hide() + self.save_button.hide() onion = Onion( self.common, @@ -616,6 +616,7 @@ class TorSettingsTab(QtWidgets.QWidget): """ self.tor_con.hide() self.test_tor_button.show() + self.save_button.show() def save_clicked(self): """ @@ -696,15 +697,12 @@ class TorSettingsTab(QtWidgets.QWidget): self.common.gui.onion.is_authenticated() and not tor_con.wasCanceled() ): - self.settings_saved.emit() - self.close() + self.close_this_tab.emit() else: - self.settings_saved.emit() - self.close() + self.close_this_tab.emit() else: - self.settings_saved.emit() - self.close() + self.close_this_tab.emit() def close_tab(self): """ From c3eeaefb9f1f67afc81c75068e6e7216d9fe8bfb Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 17:35:24 -0700 Subject: [PATCH 05/29] In CLI get_tor_path, stop trying to look in resources first --- cli/onionshare_cli/common.py | 58 ++++++++++-------------------------- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/cli/onionshare_cli/common.py b/cli/onionshare_cli/common.py index 945a75bb..07e0aa0a 100644 --- a/cli/onionshare_cli/common.py +++ b/cli/onionshare_cli/common.py @@ -309,30 +309,14 @@ class Common: def get_tor_paths(self): if self.platform == "Linux": - # Look in resources first - base_path = self.get_resource_path("tor") - if os.path.exists(base_path): - self.log( - "Common", "get_tor_paths", f"using tor binaries in {base_path}" - ) - tor_path = os.path.join(base_path, "tor") - tor_geo_ip_file_path = os.path.join(base_path, "geoip") - tor_geo_ipv6_file_path = os.path.join(base_path, "geoip6") - obfs4proxy_file_path = os.path.join(base_path, "obfs4proxy") - snowflake_file_path = os.path.join(base_path, "snowflake-client") - else: - # Fallback to looking in the path - self.log( - "Common", "get_tor_paths", f"using tor binaries in system path" - ) - tor_path = shutil.which("tor") - if not tor_path: - raise CannotFindTor() - obfs4proxy_file_path = shutil.which("obfs4proxy") - snowflake_file_path = shutil.which("snowflake-client") - prefix = os.path.dirname(os.path.dirname(tor_path)) - tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") - tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6") + tor_path = shutil.which("tor") + if not tor_path: + raise CannotFindTor() + obfs4proxy_file_path = shutil.which("obfs4proxy") + snowflake_file_path = shutil.which("snowflake-client") + prefix = os.path.dirname(os.path.dirname(tor_path)) + tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") + tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6") elif self.platform == "Windows": base_path = self.get_resource_path("tor") tor_path = os.path.join(base_path, "Tor", "tor.exe") @@ -341,24 +325,14 @@ class Common: tor_geo_ip_file_path = os.path.join(base_path, "Data", "Tor", "geoip") tor_geo_ipv6_file_path = os.path.join(base_path, "Data", "Tor", "geoip6") elif self.platform == "Darwin": - # Look in resources first - base_path = self.get_resource_path("tor") - if os.path.exists(base_path): - tor_path = os.path.join(base_path, "tor") - tor_geo_ip_file_path = os.path.join(base_path, "geoip") - tor_geo_ipv6_file_path = os.path.join(base_path, "geoip6") - obfs4proxy_file_path = os.path.join(base_path, "obfs4proxy") - snowflake_file_path = os.path.join(base_path, "snowflake-client") - else: - # Fallback to looking in the path - tor_path = shutil.which("tor") - if not tor_path: - raise CannotFindTor() - obfs4proxy_file_path = shutil.which("obfs4proxy") - snowflake_file_path = shutil.which("snowflake-client") - prefix = os.path.dirname(os.path.dirname(tor_path)) - tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") - tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6") + tor_path = shutil.which("tor") + if not tor_path: + raise CannotFindTor() + obfs4proxy_file_path = shutil.which("obfs4proxy") + snowflake_file_path = shutil.which("snowflake-client") + prefix = os.path.dirname(os.path.dirname(tor_path)) + tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") + tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6") elif self.platform == "BSD": tor_path = "/usr/local/bin/tor" tor_geo_ip_file_path = "/usr/local/share/tor/geoip" From 1420b28d2332cb1f16a7f0ca5ae11c167e4a1eb0 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 18:55:25 -0700 Subject: [PATCH 06/29] Saving tor settings connects to tor in the widget, not the dialog. And erros are displayed in a label, not an alert --- desktop/src/onionshare/gui_common.py | 6 +- desktop/src/onionshare/moat_dialog.py | 2 +- .../src/onionshare/resources/locale/en.json | 2 +- desktop/src/onionshare/settings_tab.py | 4 - .../src/onionshare/tor_connection_dialog.py | 26 +--- desktop/src/onionshare/tor_settings_tab.py | 130 +++++++++++------- 6 files changed, 88 insertions(+), 82 deletions(-) diff --git a/desktop/src/onionshare/gui_common.py b/desktop/src/onionshare/gui_common.py index 0f1dd46e..f2fd6ef0 100644 --- a/desktop/src/onionshare/gui_common.py +++ b/desktop/src/onionshare/gui_common.py @@ -392,10 +392,10 @@ class GuiCommon: QPushButton { padding: 5px 10px; }""", - # Moat dialog - "moat_error": """ + # Tor Settings dialogs + "tor_settings_error": """ QLabel { - color: #990000; + color: #FF0000; } """, } diff --git a/desktop/src/onionshare/moat_dialog.py b/desktop/src/onionshare/moat_dialog.py index 28193c25..56e872b5 100644 --- a/desktop/src/onionshare/moat_dialog.py +++ b/desktop/src/onionshare/moat_dialog.py @@ -67,7 +67,7 @@ class MoatDialog(QtWidgets.QDialog): # Error label self.error_label = QtWidgets.QLabel() - self.error_label.setStyleSheet(self.common.gui.css["moat_error"]) + self.error_label.setStyleSheet(self.common.gui.css["tor_settings_error"]) self.error_label.hide() # Buttons diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index a9fb562a..3f380466 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -71,7 +71,7 @@ "gui_settings_bridge_custom_radio_option": "Provide a bridge you learned about from a trusted source", "gui_settings_bridge_custom_placeholder": "type address:port (one per line)", "gui_settings_moat_bridges_invalid": "You have not requested a bridge from torproject.org yet.", - "gui_settings_tor_bridges_invalid": "None of the bridges you added work.\nDouble-check them or add others.", + "gui_settings_tor_bridges_invalid": "None of the bridges you added work. Double-check them or add others.", "gui_settings_button_save": "Save", "gui_settings_button_cancel": "Cancel", "gui_settings_button_help": "Help", diff --git a/desktop/src/onionshare/settings_tab.py b/desktop/src/onionshare/settings_tab.py index c01d2662..c792d94e 100644 --- a/desktop/src/onionshare/settings_tab.py +++ b/desktop/src/onionshare/settings_tab.py @@ -19,17 +19,13 @@ along with this program. If not, see . """ from PySide2 import QtCore, QtWidgets, QtGui -import sys import platform import datetime -import re -import os from onionshare_cli.settings import Settings from . import strings from .widgets import Alert from .update_checker import UpdateThread -from .gui_common import GuiCommon class SettingsTab(QtWidgets.QWidget): diff --git a/desktop/src/onionshare/tor_connection_dialog.py b/desktop/src/onionshare/tor_connection_dialog.py index 7ba7c800..3eb38876 100644 --- a/desktop/src/onionshare/tor_connection_dialog.py +++ b/desktop/src/onionshare/tor_connection_dialog.py @@ -164,7 +164,7 @@ class TorConnectionWidget(QtWidgets.QWidget): open_tor_settings = QtCore.Signal() success = QtCore.Signal() - fail = QtCore.Signal() + fail = QtCore.Signal(str) def __init__(self, common): super(TorConnectionWidget, self).__init__(None) @@ -231,7 +231,7 @@ class TorConnectionWidget(QtWidgets.QWidget): def cancel_clicked(self): self.was_canceled = True - self.fail.emit() + self.fail.emit("") def wasCanceled(self): return self.was_canceled @@ -262,27 +262,7 @@ class TorConnectionWidget(QtWidgets.QWidget): def _error_connecting_to_tor(self, msg): self.common.log("TorConnectionWidget", "_error_connecting_to_tor") self.active = False - - if self.testing_settings: - # If testing, just display the error but don't open settings - def alert(): - Alert(self.common, msg, QtWidgets.QMessageBox.Warning, title=self.title) - - else: - # 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, - ) - - # Open settings - self.open_tor_settings.emit() - - QtCore.QTimer.singleShot(1, alert) - self.fail.emit() + self.fail.emit(msg) class TorConnectionThread(QtCore.QThread): diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index e2fcead4..be9dac37 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -29,9 +29,8 @@ from onionshare_cli.onion import Onion from . import strings from .widgets import Alert -from .tor_connection_dialog import TorConnectionDialog, TorConnectionWidget +from .tor_connection_dialog import TorConnectionWidget from .moat_dialog import MoatDialog -from .gui_common import GuiCommon class TorSettingsTab(QtWidgets.QWidget): @@ -319,10 +318,21 @@ class TorSettingsTab(QtWidgets.QWidget): columns_layout.addWidget(connection_type_radio_group) columns_layout.addSpacing(20) columns_layout.addLayout(connection_type_layout, stretch=1) + columns_wrapper = QtWidgets.QWidget() + columns_wrapper.setFixedHeight(400) + columns_wrapper.setLayout(columns_layout) # Tor connection widget self.tor_con = TorConnectionWidget(self.common) + self.tor_con.success.connect(self.tor_con_success) + self.tor_con.fail.connect(self.tor_con_fail) self.tor_con.hide() + self.tor_con_type = None + + # Error label + self.error_label = QtWidgets.QLabel() + self.error_label.setStyleSheet(self.common.gui.css["tor_settings_error"]) + self.error_label.setWordWrap(True) # Buttons self.test_tor_button = QtWidgets.QPushButton( @@ -332,18 +342,18 @@ class TorSettingsTab(QtWidgets.QWidget): self.save_button = QtWidgets.QPushButton(strings._("gui_settings_button_save")) self.save_button.clicked.connect(self.save_clicked) buttons_layout = QtWidgets.QHBoxLayout() - buttons_layout.addStretch() + buttons_layout.addWidget(self.error_label, stretch=1) + buttons_layout.addSpacing(20) buttons_layout.addWidget(self.test_tor_button) buttons_layout.addWidget(self.save_button) - buttons_layout.addStretch() # Layout layout = QtWidgets.QVBoxLayout() - layout.addLayout(columns_layout) + layout.addWidget(columns_wrapper) + layout.addStretch() layout.addWidget(self.tor_con) layout.addStretch() layout.addLayout(buttons_layout) - layout.addStretch() self.setLayout(layout) @@ -576,6 +586,9 @@ class TorSettingsTab(QtWidgets.QWidget): successfully connect and authenticate to Tor. """ self.common.log("TorSettingsTab", "test_tor_clicked") + + self.error_label.setText("") + settings = self.settings_from_fields() if not settings: return @@ -583,40 +596,15 @@ class TorSettingsTab(QtWidgets.QWidget): self.test_tor_button.hide() self.save_button.hide() - onion = Onion( + self.test_onion = Onion( self.common, use_tmp_dir=True, get_tor_paths=self.common.gui.get_tor_paths, ) + self.tor_con_type = "test" self.tor_con.show() - self.tor_con.success.connect(self.test_tor_button_finished) - self.tor_con.fail.connect(self.test_tor_button_finished) - self.tor_con.start(settings, True, onion) - - # If Tor settings worked, show results - if onion.connected_to_tor: - Alert( - self.common, - strings._("settings_test_success").format( - onion.tor_version, - onion.supports_ephemeral, - onion.supports_stealth, - onion.supports_v3_onions, - ), - title=strings._("gui_settings_connection_type_test_button"), - ) - - # Clean up - onion.cleanup() - - def test_tor_button_finished(self): - """ - Finished testing tor connection. - """ - self.tor_con.hide() - self.test_tor_button.show() - self.save_button.show() + self.tor_con.start(settings, True, self.test_onion) def save_clicked(self): """ @@ -624,6 +612,8 @@ class TorSettingsTab(QtWidgets.QWidget): """ self.common.log("TorSettingsTab", "save_clicked") + self.error_label.setText("") + def changed(s1, s2, keys): """ Compare the Settings objects s1 and s2 and return true if any values @@ -684,26 +674,62 @@ class TorSettingsTab(QtWidgets.QWidget): ) self.common.gui.onion.cleanup() - tor_con = TorConnectionDialog(self.common, settings) - tor_con.start() - - self.common.log( - "TorSettingsTab", - "save_clicked", - f"Onion done rebooting, connected to Tor: {self.common.gui.onion.connected_to_tor}", - ) - - if ( - self.common.gui.onion.is_authenticated() - and not tor_con.wasCanceled() - ): - self.close_this_tab.emit() + self.test_tor_button.hide() + self.save_button.hide() + self.tor_con_type = "save" + self.tor_con.show() + self.tor_con.start(settings) else: self.close_this_tab.emit() else: self.close_this_tab.emit() + def tor_con_success(self): + """ + Finished testing tor connection. + """ + self.tor_con.hide() + self.test_tor_button.show() + self.save_button.show() + + if self.tor_con_type == "test": + Alert( + self.common, + strings._("settings_test_success").format( + self.test_onion.tor_version, + self.test_onion.supports_ephemeral, + self.test_onion.supports_stealth, + self.test_onion.supports_v3_onions, + ), + title=strings._("gui_settings_connection_type_test_button"), + ) + self.test_onion.cleanup() + + elif self.tor_con_type == "save": + if ( + self.common.gui.onion.is_authenticated() + and not self.tor_con.wasCanceled() + ): + self.close_this_tab.emit() + + self.tor_con_type = None + + def tor_con_fail(self, msg): + """ + Finished testing tor connection. + """ + self.tor_con.hide() + self.test_tor_button.show() + self.save_button.show() + + self.error_label.setText(msg) + + if self.tor_con_type == "test": + self.test_onion.cleanup() + + self.tor_con_type = None + def close_tab(self): """ Tab is closed @@ -796,7 +822,9 @@ class TorSettingsTab(QtWidgets.QWidget): moat_bridges = self.bridge_moat_textbox.toPlainText() if moat_bridges.strip() == "": - Alert(self.common, strings._("gui_settings_moat_bridges_invalid")) + self.error_label.setText( + strings._("gui_settings_moat_bridges_invalid") + ) return False settings.set( @@ -843,7 +871,9 @@ class TorSettingsTab(QtWidgets.QWidget): new_bridges = "\n".join(new_bridges) + "\n" settings.set("tor_bridges_use_custom_bridges", new_bridges) else: - Alert(self.common, strings._("gui_settings_tor_bridges_invalid")) + self.error_label.setText( + strings._("gui_settings_tor_bridges_invalid") + ) return False else: settings.set("no_bridges", True) From 4897015ad7f6b98bcf27a50d20ebe5de339b6924 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 18:57:14 -0700 Subject: [PATCH 07/29] Rename tor_connection_dialog.py to tor_connection.py --- desktop/src/onionshare/main_window.py | 2 +- .../onionshare/{tor_connection_dialog.py => tor_connection.py} | 0 desktop/src/onionshare/tor_settings_tab.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename desktop/src/onionshare/{tor_connection_dialog.py => tor_connection.py} (100%) diff --git a/desktop/src/onionshare/main_window.py b/desktop/src/onionshare/main_window.py index 4a9d0c7e..546592a1 100644 --- a/desktop/src/onionshare/main_window.py +++ b/desktop/src/onionshare/main_window.py @@ -23,7 +23,7 @@ import time from PySide2 import QtCore, QtWidgets, QtGui from . import strings -from .tor_connection_dialog import TorConnectionDialog +from .tor_connection import TorConnectionDialog from .widgets import Alert from .update_checker import UpdateThread from .tab_widget import TabWidget diff --git a/desktop/src/onionshare/tor_connection_dialog.py b/desktop/src/onionshare/tor_connection.py similarity index 100% rename from desktop/src/onionshare/tor_connection_dialog.py rename to desktop/src/onionshare/tor_connection.py diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index be9dac37..5905b44d 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -29,7 +29,7 @@ from onionshare_cli.onion import Onion from . import strings from .widgets import Alert -from .tor_connection_dialog import TorConnectionWidget +from .tor_connection import TorConnectionWidget from .moat_dialog import MoatDialog From 20a0d7f25bb066a7a67f47b11de69bb98c50ab63 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 19:31:53 -0700 Subject: [PATCH 08/29] Fix TabWidget to stop confusing tab_id and index --- desktop/src/onionshare/tab_widget.py | 69 +++++++++++++++------- desktop/src/onionshare/tor_settings_tab.py | 22 ------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 36a6c22f..0ab19279 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -45,7 +45,9 @@ class TabWidget(QtWidgets.QTabWidget): self.system_tray = system_tray self.status_bar = status_bar - # Keep track of tabs in a dictionary + # Keep track of tabs in a dictionary that maps tab_id to tab. + # Each tab has a unique, auto-incremented id (tab_id). This is different than the + # tab's index, which changes as tabs are re-arranged. self.tabs = {} self.current_tab_id = 0 # Each tab has a unique id @@ -91,10 +93,12 @@ class TabWidget(QtWidgets.QTabWidget): self.event_handler_t.wait(50) # Clean up each tab - for index in range(self.count()): - if not self.is_settings_tab(index): - tab = self.widget(index) - tab.cleanup() + for tab_id in self.tabs: + if not ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + self.tabs[tab_id].cleanup() def move_new_tab_button(self): # Find the width of all tabs @@ -117,11 +121,26 @@ class TabWidget(QtWidgets.QTabWidget): def tab_changed(self): # Active tab was changed - tab_id = self.currentIndex() + tab = self.widget(self.currentIndex()) + if not tab: + self.common.log( + "TabWidget", + "tab_changed", + f"tab at index {self.currentIndex()} does not exist", + ) + return + + tab_id = tab.tab_id self.common.log("TabWidget", "tab_changed", f"Tab was changed to {tab_id}") # If it's Settings or Tor Settings, ignore - if self.is_settings_tab(tab_id): + if ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + # Blank the server status indicator + self.status_bar.server_status_image_label.clear() + self.status_bar.server_status_label.clear() return try: @@ -260,9 +279,12 @@ class TabWidget(QtWidgets.QTabWidget): def save_persistent_tabs(self): # Figure out the order of persistent tabs to save in settings persistent_tabs = [] - for index in range(self.count()): - if not self.is_settings_tab(index): - tab = self.widget(index) + for tab_id in self.tabs: + if not ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + tab = self.widget(self.indexOf(self.tabs[tab_id])) if tab.settings.get("persistent", "enabled"): persistent_tabs.append(tab.settings.id) # Only save if tabs have actually moved @@ -273,8 +295,14 @@ class TabWidget(QtWidgets.QTabWidget): def close_tab(self, index): self.common.log("TabWidget", "close_tab", f"{index}") tab = self.widget(index) + tab_id = tab.tab_id + + if ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + self.common.log("TabWidget", "closing a settings tab") - if self.is_settings_tab(index): # Remove the tab self.removeTab(index) del self.tabs[tab.tab_id] @@ -284,7 +312,10 @@ class TabWidget(QtWidgets.QTabWidget): self.new_tab_clicked() else: + self.common.log("TabWidget", "closing a service tab") if tab.close_tab(): + self.common.log("TabWidget", "user is okay with closing the tab") + # If the tab is persistent, delete the settings file from disk if tab.settings.get("persistent", "enabled"): tab.settings.delete() @@ -298,6 +329,8 @@ class TabWidget(QtWidgets.QTabWidget): # If the last tab is closed, open a new one if self.count() == 0: self.new_tab_clicked() + else: + self.common.log("TabWidget", "user does not want to close the tab") def close_settings_tab(self): self.common.log("TabWidget", "close_settings_tab") @@ -320,7 +353,10 @@ class TabWidget(QtWidgets.QTabWidget): See if there are active servers in any open tabs """ for tab_id in self.tabs: - if not self.is_settings_tab(tab_id): + if not ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): mode = self.tabs[tab_id].get_mode() if mode: if mode.server_status.status != mode.server_status.STATUS_STOPPED: @@ -352,15 +388,6 @@ class TabWidget(QtWidgets.QTabWidget): close_button.clicked.connect(close_tab) self.tabBar().setTabButton(index, QtWidgets.QTabBar.RightSide, tab.close_button) - def is_settings_tab(self, tab_id): - if tab_id not in self.tabs: - return True - - return ( - type(self.tabs[tab_id]) is SettingsTab - or type(self.tabs[tab_id]) is TorSettingsTab - ) - class TabBar(QtWidgets.QTabBar): """ diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 5905b44d..21941268 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -730,28 +730,6 @@ class TorSettingsTab(QtWidgets.QWidget): self.tor_con_type = None - def close_tab(self): - """ - Tab is closed - """ - self.common.log("TorSettingsTab", "cancel_clicked") - return True - - # TODO: Figure out flow for first connecting, when closing settings when not connected - - # if ( - # not self.common.gui.local_only - # and not self.common.gui.onion.is_authenticated() - # ): - # Alert( - # self.common, - # strings._("gui_tor_connection_canceled"), - # QtWidgets.QMessageBox.Warning, - # ) - # sys.exit() - # else: - # self.close() - def settings_from_fields(self): """ Return a Settings object that's full of values from the settings dialog. From f784870c76bb89a41529c5e5282cc54e47148d9e Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 19:53:37 -0700 Subject: [PATCH 09/29] Implement blank settings_have_changed in SettingsTab and TorSettingsTab --- desktop/src/onionshare/settings_tab.py | 4 ++++ desktop/src/onionshare/tor_connection.py | 10 +++++----- desktop/src/onionshare/tor_settings_tab.py | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/desktop/src/onionshare/settings_tab.py b/desktop/src/onionshare/settings_tab.py index c792d94e..8f41b2aa 100644 --- a/desktop/src/onionshare/settings_tab.py +++ b/desktop/src/onionshare/settings_tab.py @@ -304,6 +304,10 @@ class SettingsTab(QtWidgets.QWidget): return settings + def settings_have_changed(self): + # Global settings have changed + self.common.log("SettingsTab", "settings_have_changed") + def _update_autoupdate_timestamp(self, autoupdate_timestamp): self.common.log("SettingsTab", "_update_autoupdate_timestamp") diff --git a/desktop/src/onionshare/tor_connection.py b/desktop/src/onionshare/tor_connection.py index 3eb38876..1cfed2a8 100644 --- a/desktop/src/onionshare/tor_connection.py +++ b/desktop/src/onionshare/tor_connection.py @@ -271,20 +271,20 @@ class TorConnectionThread(QtCore.QThread): canceled_connecting_to_tor = QtCore.Signal() error_connecting_to_tor = QtCore.Signal(str) - def __init__(self, common, settings, dialog): + def __init__(self, common, settings, parent): super(TorConnectionThread, self).__init__() self.common = common self.common.log("TorConnectionThread", "__init__") self.settings = settings - self.dialog = dialog + self.parent = parent def run(self): self.common.log("TorConnectionThread", "run") # Connect to the Onion try: - self.dialog.onion.connect(self.settings, False, self._tor_status_update) - if self.dialog.onion.connected_to_tor: + self.parent.onion.connect(self.settings, False, self._tor_status_update) + if self.parent.onion.connected_to_tor: self.connected_to_tor.emit() else: self.canceled_connecting_to_tor.emit() @@ -320,4 +320,4 @@ class TorConnectionThread(QtCore.QThread): self.tor_status_update.emit(progress, summary) # Return False if the dialog was canceled - return not self.dialog.wasCanceled() + return not self.parent.wasCanceled() diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 21941268..df7cf3be 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -872,3 +872,7 @@ class TorSettingsTab(QtWidgets.QWidget): # Wait 1ms for the event loop to finish, then quit QtCore.QTimer.singleShot(1, self.common.gui.qtapp.quit) + + def settings_have_changed(self): + # Global settings have changed + self.common.log("TorSettingsTab", "settings_have_changed") From e6c7cc989f78a8de531a3cc7420eb9193abd9a06 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 20:03:19 -0700 Subject: [PATCH 10/29] Only show bridge error if connection type is bundled --- cli/onionshare_cli/onion.py | 8 ++++++-- desktop/src/onionshare/tor_settings_tab.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cli/onionshare_cli/onion.py b/cli/onionshare_cli/onion.py index 0d205b97..aa2344db 100644 --- a/cli/onionshare_cli/onion.py +++ b/cli/onionshare_cli/onion.py @@ -199,8 +199,6 @@ class Onion(object): ) return - self.common.log("Onion", "connect") - # Either use settings that are passed in, or use them from common if custom_settings: self.settings = custom_settings @@ -211,6 +209,12 @@ class Onion(object): self.common.load_settings() self.settings = self.common.settings + self.common.log( + "Onion", + "connect", + f"connection_type={self.settings.get('connection_type')}", + ) + # The Tor controller self.c = None diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index df7cf3be..a56d360b 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -799,7 +799,10 @@ class TorSettingsTab(QtWidgets.QWidget): settings.set("tor_bridges_use_moat", True) moat_bridges = self.bridge_moat_textbox.toPlainText() - if moat_bridges.strip() == "": + if ( + self.connection_type_bundled_radio.isChecked() + and moat_bridges.strip() == "" + ): self.error_label.setText( strings._("gui_settings_moat_bridges_invalid") ) From 44f4053603eef5ea7dedd000416256cbdf6350c9 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 20:23:55 -0700 Subject: [PATCH 11/29] Make meek debug log show host:port on one line --- cli/onionshare_cli/meek.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cli/onionshare_cli/meek.py b/cli/onionshare_cli/meek.py index 6b31a584..b2e70dc1 100644 --- a/cli/onionshare_cli/meek.py +++ b/cli/onionshare_cli/meek.py @@ -136,8 +136,11 @@ class Meek(object): if "CMETHOD meek socks5" in line: self.meek_host = line.split(" ")[3].split(":")[0] self.meek_port = line.split(" ")[3].split(":")[1] - self.common.log("Meek", "start", f"Meek host is {self.meek_host}") - self.common.log("Meek", "start", f"Meek port is {self.meek_port}") + self.common.log( + "Meek", + "start", + f"Meek running on {self.meek_host}:{self.meek_port}", + ) break if self.meek_port: From 54f4f2a53fe541f2a17dd453051d32dc0e43a75b Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 24 Oct 2021 20:26:36 -0700 Subject: [PATCH 12/29] Oops, fix meek-client path --- cli/onionshare_cli/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/onionshare_cli/common.py b/cli/onionshare_cli/common.py index b76e72b2..bab3fd86 100644 --- a/cli/onionshare_cli/common.py +++ b/cli/onionshare_cli/common.py @@ -315,7 +315,7 @@ class Common: raise CannotFindTor() obfs4proxy_file_path = shutil.which("obfs4proxy") snowflake_file_path = shutil.which("snowflake-client") - meek_client_file_path = os.path.join(base_path, "meek-client") + meek_client_file_path = shutil.which("meek-client") prefix = os.path.dirname(os.path.dirname(tor_path)) tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6") From 2b3b6d7635b935741646281c7a206a71993c0f9e Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 21:06:05 -0700 Subject: [PATCH 13/29] Update bridge related settings in Settings, and use those new settings in Onion --- cli/onionshare_cli/onion.py | 63 +++++++++++++++++----------------- cli/onionshare_cli/settings.py | 12 +++---- cli/tests/test_cli_settings.py | 21 ++++++------ 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/cli/onionshare_cli/onion.py b/cli/onionshare_cli/onion.py index e8fcc12a..5ce83261 100644 --- a/cli/onionshare_cli/onion.py +++ b/cli/onionshare_cli/onion.py @@ -319,40 +319,39 @@ class Onion(object): f.write(torrc_template) # Bridge support - if self.settings.get("tor_bridges_use_obfs4"): - with open( - self.common.get_resource_path("torrc_template-obfs4") - ) as o: - for line in o: - f.write(line) - elif self.settings.get("tor_bridges_use_meek_lite_azure"): - with open( - self.common.get_resource_path("torrc_template-meek_lite_azure") - ) as o: - for line in o: - f.write(line) - elif self.settings.get("tor_bridges_use_snowflake"): - with open( - self.common.get_resource_path("torrc_template-snowflake") - ) as o: - for line in o: - f.write(line) + if self.settings.get("bridges_enabled"): + if self.settings.get("bridges_type") == "built-in": + if self.settings.get("bridges_builtin_pt") == "obfs4": + with open( + self.common.get_resource_path("torrc_template-obfs4") + ) as o: + f.write(o.read()) + elif self.settings.get("bridges_builtin_pt") == "meek-azure": + with open( + self.common.get_resource_path( + "torrc_template-meek_lite_azure" + ) + ) as o: + f.write(o.read()) + elif self.settings.get("bridges_builtin_pt") == "snowflake": + with open( + self.common.get_resource_path( + "torrc_template-snowflake" + ) + ) as o: + f.write(o.read()) - elif self.settings.get("tor_bridges_use_moat"): - for line in self.settings.get("tor_bridges_use_moat_bridges").split( - "\n" - ): - if line.strip() != "": - f.write(f"Bridge {line}\n") - f.write("\nUseBridges 1\n") + elif self.settings.get("bridges_type") == "moat": + for line in self.settings.get("bridges_moat").split("\n"): + if line.strip() != "": + f.write(f"Bridge {line}\n") + f.write("\nUseBridges 1\n") - elif self.settings.get("tor_bridges_use_custom_bridges"): - for line in self.settings.get( - "tor_bridges_use_custom_bridges" - ).split("\n"): - if line.strip() != "": - f.write(f"Bridge {line}\n") - f.write("\nUseBridges 1\n") + elif self.settings.get("bridges_type") == "custom": + for line in self.settings.get("bridges_custom").split("\n"): + if line.strip() != "": + f.write(f"Bridge {line}\n") + f.write("\nUseBridges 1\n") # Execute a tor subprocess start_ts = time.time() diff --git a/cli/onionshare_cli/settings.py b/cli/onionshare_cli/settings.py index 29b59c80..c7d74a70 100644 --- a/cli/onionshare_cli/settings.py +++ b/cli/onionshare_cli/settings.py @@ -105,13 +105,11 @@ class Settings(object): "auth_password": "", "use_autoupdate": True, "autoupdate_timestamp": None, - "no_bridges": True, - "tor_bridges_use_obfs4": False, - "tor_bridges_use_meek_lite_azure": False, - "tor_bridges_use_snowflake": False, - "tor_bridges_use_moat": False, - "tor_bridges_use_moat_bridges": "", - "tor_bridges_use_custom_bridges": "", + "bridges_enabled": False, + "bridges_type": "built-in", # "built-in", "moat", or "custom" + "bridges_builtin_pt": "obfs4", # "obfs4", "meek-azure", or "snowflake" + "bridges_moat": "", + "bridges_custom": "", "persistent_tabs": [], "locale": None, # this gets defined in fill_in_defaults() "theme": 0, diff --git a/cli/tests/test_cli_settings.py b/cli/tests/test_cli_settings.py index b44ddbec..c7140e70 100644 --- a/cli/tests/test_cli_settings.py +++ b/cli/tests/test_cli_settings.py @@ -29,13 +29,11 @@ class TestSettings: "auth_password": "", "use_autoupdate": True, "autoupdate_timestamp": None, - "no_bridges": True, - "tor_bridges_use_obfs4": False, - "tor_bridges_use_meek_lite_azure": False, - "tor_bridges_use_snowflake": False, - "tor_bridges_use_moat": False, - "tor_bridges_use_moat_bridges": "", - "tor_bridges_use_custom_bridges": "", + "bridges_enabled": False, + "bridges_type": "built-in", + "bridges_builtin_pt": "obfs4", + "bridges_moat": "", + "bridges_custom": "", "persistent_tabs": [], "theme": 0, } @@ -96,10 +94,11 @@ class TestSettings: assert settings_obj.get("use_autoupdate") is True assert settings_obj.get("autoupdate_timestamp") is None assert settings_obj.get("autoupdate_timestamp") is None - assert settings_obj.get("no_bridges") is True - assert settings_obj.get("tor_bridges_use_obfs4") is False - assert settings_obj.get("tor_bridges_use_meek_lite_azure") is False - assert settings_obj.get("tor_bridges_use_custom_bridges") == "" + assert settings_obj.get("bridges_enabled") is False + assert settings_obj.get("bridges_type") == "built-in" + assert settings_obj.get("bridges_builtin_pt") == "obfs4" + assert settings_obj.get("bridges_moat") == "" + assert settings_obj.get("bridges_custom") == "" def test_set_version(self, settings_obj): settings_obj.set("version", "CUSTOM_VERSION") From 9515fe6aaf387868c1406c972435591edb70f785 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 21:07:38 -0700 Subject: [PATCH 14/29] Remove all references to old settings --- cli/onionshare_cli/onion.py | 6 +----- cli/tests/test_cli_settings.py | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/cli/onionshare_cli/onion.py b/cli/onionshare_cli/onion.py index 5ce83261..536b9ba0 100644 --- a/cli/onionshare_cli/onion.py +++ b/cli/onionshare_cli/onion.py @@ -424,11 +424,7 @@ class Onion(object): time.sleep(0.2) # If using bridges, it might take a bit longer to connect to Tor - if ( - self.settings.get("tor_bridges_use_custom_bridges") - or self.settings.get("tor_bridges_use_obfs4") - or self.settings.get("tor_bridges_use_meek_lite_azure") - ): + if self.settings.get("bridges_enabled"): # Only override timeout if a custom timeout has not been passed in if connect_timeout == 120: connect_timeout = 150 diff --git a/cli/tests/test_cli_settings.py b/cli/tests/test_cli_settings.py index c7140e70..9513b013 100644 --- a/cli/tests/test_cli_settings.py +++ b/cli/tests/test_cli_settings.py @@ -141,10 +141,10 @@ class TestSettings: def test_set_custom_bridge(self, settings_obj): settings_obj.set( - "tor_bridges_use_custom_bridges", + "bridges_custom", "Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E", ) assert ( - settings_obj._settings["tor_bridges_use_custom_bridges"] + settings_obj._settings["bridges_custom"] == "Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E" ) From 53c192665bfcc4942d42cfdd3b356524943950a9 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 21:33:58 -0700 Subject: [PATCH 15/29] Refactor Tor Settings tab to use the new settings --- desktop/src/onionshare/tor_settings_tab.py | 119 +++++++++------------ 1 file changed, 48 insertions(+), 71 deletions(-) diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 4f73ca66..e3dc9bee 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -405,57 +405,54 @@ class TorSettingsTab(QtWidgets.QWidget): self.old_settings.get("auth_password") ) - if self.old_settings.get("no_bridges"): - self.bridge_use_checkbox.setCheckState(QtCore.Qt.Unchecked) - self.bridge_settings.hide() - - else: + if self.old_settings.get("bridges_enabled"): self.bridge_use_checkbox.setCheckState(QtCore.Qt.Checked) self.bridge_settings.show() - builtin_obfs4 = self.old_settings.get("tor_bridges_use_obfs4") - builtin_meek_azure = self.old_settings.get( - "tor_bridges_use_meek_lite_azure" - ) - builtin_snowflake = self.old_settings.get("tor_bridges_use_snowflake") - - if builtin_obfs4 or builtin_meek_azure or builtin_snowflake: + bridges_type = self.old_settings.get("bridges_type") + if bridges_type == "built-in": self.bridge_builtin_radio.setChecked(True) self.bridge_builtin_dropdown.show() - if builtin_obfs4: + self.bridge_moat_radio.setChecked(False) + self.bridge_moat_textbox_options.hide() + self.bridge_custom_radio.setChecked(False) + self.bridge_custom_textbox_options.hide() + + bridges_builtin_pt = self.old_settings.get("bridges_builtin_pt") + if bridges_builtin_pt == "obfs4": self.bridge_builtin_dropdown.setCurrentText("obfs4") - elif builtin_meek_azure: + elif bridges_builtin_pt == "meek-azure": self.bridge_builtin_dropdown.setCurrentText("meek-azure") - elif builtin_snowflake: + else: self.bridge_builtin_dropdown.setCurrentText("snowflake") self.bridge_moat_textbox_options.hide() self.bridge_custom_textbox_options.hide() + + elif bridges_type == "moat": + self.bridge_builtin_radio.setChecked(False) + self.bridge_builtin_dropdown.hide() + self.bridge_moat_radio.setChecked(True) + self.bridge_moat_textbox_options.show() + self.bridge_custom_radio.setChecked(False) + self.bridge_custom_textbox_options.hide() + else: self.bridge_builtin_radio.setChecked(False) self.bridge_builtin_dropdown.hide() + self.bridge_moat_radio.setChecked(False) + self.bridge_moat_textbox_options.hide() + self.bridge_custom_radio.setChecked(True) + self.bridge_custom_textbox_options.show() - use_moat = self.old_settings.get("tor_bridges_use_moat") - self.bridge_moat_radio.setChecked(use_moat) - if use_moat: - self.bridge_builtin_dropdown.hide() - self.bridge_custom_textbox_options.hide() + bridges_moat = self.old_settings.get("bridges_moat") + self.bridge_moat_textbox.document().setPlainText(bridges_moat) + bridges_custom = self.old_settings.get("bridges_custom") + self.bridge_custom_textbox.document().setPlainText(bridges_custom) - moat_bridges = self.old_settings.get("tor_bridges_use_moat_bridges") - self.bridge_moat_textbox.document().setPlainText(moat_bridges) - if len(moat_bridges.strip()) > 0: - self.bridge_moat_textbox_options.show() - else: - self.bridge_moat_textbox_options.hide() - - custom_bridges = self.old_settings.get("tor_bridges_use_custom_bridges") - if len(custom_bridges.strip()) != 0: - self.bridge_custom_radio.setChecked(True) - self.bridge_custom_textbox.setPlainText(custom_bridges) - - self.bridge_builtin_dropdown.hide() - self.bridge_moat_textbox_options.hide() - self.bridge_custom_textbox_options.show() + else: + self.bridge_use_checkbox.setCheckState(QtCore.Qt.Unchecked) + self.bridge_settings.hide() def connection_type_bundled_toggled(self, checked): """ @@ -493,7 +490,7 @@ class TorSettingsTab(QtWidgets.QWidget): """ if selection == "meek-azure": # Alert the user about meek's costliness if it looks like they're turning it on - if not self.old_settings.get("tor_bridges_use_meek_lite_azure"): + if not self.old_settings.get("bridges_builtin_pt") == "meek-azure": Alert( self.common, strings._("gui_settings_meek_lite_expensive_warning"), @@ -654,10 +651,11 @@ class TorSettingsTab(QtWidgets.QWidget): "socket_file_path", "auth_type", "auth_password", - "no_bridges", - "tor_bridges_use_obfs4", - "tor_bridges_use_meek_lite_azure", - "tor_bridges_use_custom_bridges", + "bridges_enabled", + "bridges_type", + "bridges_builtin_pt", + "bridges_moat", + "bridges_custom", ], ): @@ -775,33 +773,16 @@ class TorSettingsTab(QtWidgets.QWidget): # Whether we use bridges if self.bridge_use_checkbox.checkState() == QtCore.Qt.Checked: - settings.set("no_bridges", False) + settings.set("bridges_enabled", True) if self.bridge_builtin_radio.isChecked(): - selection = self.bridge_builtin_dropdown.currentText() - if selection == "obfs4": - settings.set("tor_bridges_use_obfs4", True) - settings.set("tor_bridges_use_meek_lite_azure", False) - settings.set("tor_bridges_use_snowflake", False) - elif selection == "meek-azure": - settings.set("tor_bridges_use_obfs4", False) - settings.set("tor_bridges_use_meek_lite_azure", True) - settings.set("tor_bridges_use_snowflake", False) - elif selection == "snowflake": - settings.set("tor_bridges_use_obfs4", False) - settings.set("tor_bridges_use_meek_lite_azure", False) - settings.set("tor_bridges_use_snowflake", True) + settings.set("bridges_type", "built-in") - settings.set("tor_bridges_use_moat", False) - settings.set("tor_bridges_use_custom_bridges", "") + selection = self.bridge_builtin_dropdown.currentText() + settings.set("bridges_builtin_pt", selection) if self.bridge_moat_radio.isChecked(): - settings.set("tor_bridges_use_obfs4", False) - settings.set("tor_bridges_use_meek_lite_azure", False) - settings.set("tor_bridges_use_snowflake", False) - - settings.set("tor_bridges_use_moat", True) - + settings.set("bridges_type", "moat") moat_bridges = self.bridge_moat_textbox.toPlainText() if ( self.connection_type_bundled_radio.isChecked() @@ -812,15 +793,11 @@ class TorSettingsTab(QtWidgets.QWidget): ) return False - settings.set("tor_bridges_use_moat_bridges", moat_bridges) - - settings.set("tor_bridges_use_custom_bridges", "") + settings.set("bridges_moat", moat_bridges) + settings.set("bridges_custom", "") if self.bridge_custom_radio.isChecked(): - settings.set("tor_bridges_use_obfs4", False) - settings.set("tor_bridges_use_meek_lite_azure", False) - settings.set("tor_bridges_use_snowflake", False) - settings.set("tor_bridges_use_moat", False) + settings.set("bridges_type", "custom") new_bridges = [] bridges = self.bridge_custom_textbox.toPlainText().split("\n") @@ -851,14 +828,14 @@ class TorSettingsTab(QtWidgets.QWidget): if bridges_valid: new_bridges = "\n".join(new_bridges) + "\n" - settings.set("tor_bridges_use_custom_bridges", new_bridges) + settings.set("bridges_custom", new_bridges) else: self.error_label.setText( strings._("gui_settings_tor_bridges_invalid") ) return False else: - settings.set("no_bridges", True) + settings.set("bridges_enabled", False) return settings From 706a04242ff7a70b2fc50b2dbda78f435db9d9e6 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 22:00:39 -0700 Subject: [PATCH 16/29] Show message in Tor Settings tab if any tabs have active services, to prevent the user from changing settings without stopping them --- .../src/onionshare/resources/locale/en.json | 1 + desktop/src/onionshare/tab_widget.py | 21 ++++++--- desktop/src/onionshare/tor_settings_tab.py | 44 +++++++++++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index 3f380466..398782af 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -72,6 +72,7 @@ "gui_settings_bridge_custom_placeholder": "type address:port (one per line)", "gui_settings_moat_bridges_invalid": "You have not requested a bridge from torproject.org yet.", "gui_settings_tor_bridges_invalid": "None of the bridges you added work. Double-check them or add others.", + "gui_settings_stop_active_tabs_label": "There are services running in some of your tabs.\nYou must stop all services to change your Tor settings.", "gui_settings_button_save": "Save", "gui_settings_button_cancel": "Cancel", "gui_settings_button_help": "Help", diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 0ab19279..ead4d960 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -50,6 +50,7 @@ class TabWidget(QtWidgets.QTabWidget): # tab's index, which changes as tabs are re-arranged. self.tabs = {} self.current_tab_id = 0 # Each tab has a unique id + self.tor_settings_tab = None # Define the new tab button self.new_tab_button = QtWidgets.QPushButton("+", parent=self) @@ -230,18 +231,20 @@ class TabWidget(QtWidgets.QTabWidget): self.setCurrentIndex(self.indexOf(self.tabs[tab_id])) return - tor_settings_tab = TorSettingsTab(self.common, self.current_tab_id) - tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) - self.tabs[self.current_tab_id] = tor_settings_tab + self.tor_settings_tab = TorSettingsTab( + self.common, self.current_tab_id, self.are_tabs_active() + ) + self.tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) + self.tabs[self.current_tab_id] = self.tor_settings_tab self.current_tab_id += 1 index = self.addTab( - tor_settings_tab, strings._("gui_tor_settings_window_title") + self.tor_settings_tab, strings._("gui_tor_settings_window_title") ) self.setCurrentIndex(index) # In macOS, manually create a close button because tabs don't seem to have them otherwise if self.common.platform == "Darwin": - self.macos_create_close_button(tor_settings_tab, index) + self.macos_create_close_button(self.tor_settings_tab, index) def change_title(self, tab_id, title): shortened_title = title @@ -256,6 +259,11 @@ class TabWidget(QtWidgets.QTabWidget): index = self.indexOf(self.tabs[tab_id]) self.setTabIcon(index, QtGui.QIcon(GuiCommon.get_resource_path(icon_path))) + # The icon changes when the server status changes, so if we have an open + # Tor Settings tab, tell it to update + if self.tor_settings_tab: + self.tor_settings_tab.active_tabs_changed(self.are_tabs_active()) + def change_persistent(self, tab_id, is_persistent): self.common.log( "TabWidget", @@ -307,6 +315,9 @@ class TabWidget(QtWidgets.QTabWidget): self.removeTab(index) del self.tabs[tab.tab_id] + if type(self.tabs[tab_id]) is TorSettingsTab: + self.tor_settings_tab = None + # If the last tab is closed, open a new one if self.count() == 0: self.new_tab_clicked() diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index e3dc9bee..c8990901 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -41,7 +41,7 @@ class TorSettingsTab(QtWidgets.QWidget): close_this_tab = QtCore.Signal() - def __init__(self, common, tab_id): + def __init__(self, common, tab_id, are_tabs_active): super(TorSettingsTab, self).__init__() self.common = common @@ -351,16 +351,36 @@ class TorSettingsTab(QtWidgets.QWidget): buttons_layout.addWidget(self.test_tor_button) buttons_layout.addWidget(self.save_button) - # Layout - layout = QtWidgets.QVBoxLayout() - layout.addWidget(columns_wrapper) - layout.addStretch() - layout.addWidget(self.tor_con) - layout.addStretch() - layout.addLayout(buttons_layout) + # Main layout + main_layout = QtWidgets.QVBoxLayout() + main_layout.addWidget(columns_wrapper) + main_layout.addStretch() + main_layout.addWidget(self.tor_con) + main_layout.addStretch() + main_layout.addLayout(buttons_layout) + self.main_widget = QtWidgets.QWidget() + self.main_widget.setLayout(main_layout) + # Tabs are active label + active_tabs_label = QtWidgets.QLabel( + strings._("gui_settings_stop_active_tabs_label") + ) + active_tabs_label.setAlignment(QtCore.Qt.AlignHCenter) + + # Active tabs layout + active_tabs_layout = QtWidgets.QVBoxLayout() + active_tabs_layout.addStretch() + active_tabs_layout.addWidget(active_tabs_label) + active_tabs_layout.addStretch() + self.active_tabs_widget = QtWidgets.QWidget() + self.active_tabs_widget.setLayout(active_tabs_layout) + + layout = QtWidgets.QVBoxLayout() + layout.addWidget(self.main_widget) + layout.addWidget(self.active_tabs_widget) self.setLayout(layout) + self.active_tabs_changed(are_tabs_active) self.reload_settings() def reload_settings(self): @@ -454,6 +474,14 @@ class TorSettingsTab(QtWidgets.QWidget): self.bridge_use_checkbox.setCheckState(QtCore.Qt.Unchecked) self.bridge_settings.hide() + def active_tabs_changed(self, are_tabs_active): + if are_tabs_active: + self.main_widget.hide() + self.active_tabs_widget.show() + else: + self.main_widget.show() + self.active_tabs_widget.hide() + def connection_type_bundled_toggled(self, checked): """ Connection type bundled was toggled From bde94d37fc1cdf16767437a624bbdfe2150bf598 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 22:09:24 -0700 Subject: [PATCH 17/29] Don't delete any custom bridges that are set --- desktop/src/onionshare/tor_settings_tab.py | 1 - 1 file changed, 1 deletion(-) diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index c8990901..7ef8d850 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -822,7 +822,6 @@ class TorSettingsTab(QtWidgets.QWidget): return False settings.set("bridges_moat", moat_bridges) - settings.set("bridges_custom", "") if self.bridge_custom_radio.isChecked(): settings.set("bridges_type", "custom") From de3c95cc507968b406871f46620b84991df2f056 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 22:12:22 -0700 Subject: [PATCH 18/29] Set self.torr_settings_tab to None _before_ deleting the tab --- desktop/src/onionshare/tab_widget.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index ead4d960..da7d50bf 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -311,13 +311,13 @@ class TabWidget(QtWidgets.QTabWidget): ): self.common.log("TabWidget", "closing a settings tab") + if type(self.tabs[tab_id]) is TorSettingsTab: + self.tor_settings_tab = None + # Remove the tab self.removeTab(index) del self.tabs[tab.tab_id] - if type(self.tabs[tab_id]) is TorSettingsTab: - self.tor_settings_tab = None - # If the last tab is closed, open a new one if self.count() == 0: self.new_tab_clicked() From 7da1ac187b7991a4bdd0f8b0e2828bc31f0adcb7 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 19:20:36 -0700 Subject: [PATCH 19/29] Remove sticky "Disconnected from Tor" message (patch thanks to @mig5) --- desktop/src/onionshare/tab_widget.py | 2 +- desktop/src/onionshare/tor_connection.py | 5 +++-- desktop/src/onionshare/tor_settings_tab.py | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index da7d50bf..3579d21b 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -232,7 +232,7 @@ class TabWidget(QtWidgets.QTabWidget): return self.tor_settings_tab = TorSettingsTab( - self.common, self.current_tab_id, self.are_tabs_active() + self.common, self.current_tab_id, self.are_tabs_active(), self.status_bar ) self.tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) self.tabs[self.current_tab_id] = self.tor_settings_tab diff --git a/desktop/src/onionshare/tor_connection.py b/desktop/src/onionshare/tor_connection.py index 1cfed2a8..2cc599c4 100644 --- a/desktop/src/onionshare/tor_connection.py +++ b/desktop/src/onionshare/tor_connection.py @@ -117,7 +117,6 @@ class TorConnectionDialog(QtWidgets.QProgressDialog): def _connected_to_tor(self): self.common.log("TorConnectionDialog", "_connected_to_tor") self.active = False - # Close the dialog after connecting self.setValue(self.maximum()) @@ -166,11 +165,12 @@ class TorConnectionWidget(QtWidgets.QWidget): success = QtCore.Signal() fail = QtCore.Signal(str) - def __init__(self, common): + def __init__(self, common, status_bar): super(TorConnectionWidget, self).__init__(None) self.common = common self.common.log("TorConnectionWidget", "__init__") + self.status_bar = status_bar self.label = QtWidgets.QLabel(strings._("connecting_to_tor")) self.label.setAlignment(QtCore.Qt.AlignHCenter) @@ -245,6 +245,7 @@ class TorConnectionWidget(QtWidgets.QWidget): def _connected_to_tor(self): self.common.log("TorConnectionWidget", "_connected_to_tor") self.active = False + self.status_bar.clearMessage() # Close the dialog after connecting self.progress.setValue(self.progress.maximum()) diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 7ef8d850..85645ca0 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -41,12 +41,13 @@ class TorSettingsTab(QtWidgets.QWidget): close_this_tab = QtCore.Signal() - def __init__(self, common, tab_id, are_tabs_active): + def __init__(self, common, tab_id, are_tabs_active, status_bar): super(TorSettingsTab, self).__init__() self.common = common self.common.log("TorSettingsTab", "__init__") + self.status_bar = status_bar self.meek = Meek(common, get_tor_paths=self.common.gui.get_tor_paths) self.system = platform.system() @@ -327,7 +328,7 @@ class TorSettingsTab(QtWidgets.QWidget): columns_wrapper.setLayout(columns_layout) # Tor connection widget - self.tor_con = TorConnectionWidget(self.common) + self.tor_con = TorConnectionWidget(self.common, self.status_bar) self.tor_con.success.connect(self.tor_con_success) self.tor_con.fail.connect(self.tor_con_fail) self.tor_con.hide() From 7b162b4bc7475ab124a91f95dc8e58986f3ab476 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:05:20 -0700 Subject: [PATCH 20/29] In all modes, if Tor isn't connected display a message instead of showing the mode content --- desktop/src/onionshare/gui_common.py | 5 ++ .../src/onionshare/resources/locale/en.json | 3 +- desktop/src/onionshare/tab/mode/__init__.py | 54 ++++++++++++++++++- .../onionshare/tab/mode/chat_mode/__init__.py | 6 +-- .../tab/mode/receive_mode/__init__.py | 6 +-- .../tab/mode/share_mode/__init__.py | 6 +-- .../tab/mode/website_mode/__init__.py | 6 +-- desktop/src/onionshare/tab/tab.py | 1 - desktop/src/onionshare/tab_widget.py | 22 ++++++++ desktop/src/onionshare/tor_settings_tab.py | 8 +++ 10 files changed, 98 insertions(+), 19 deletions(-) diff --git a/desktop/src/onionshare/gui_common.py b/desktop/src/onionshare/gui_common.py index b081774e..39f6d46f 100644 --- a/desktop/src/onionshare/gui_common.py +++ b/desktop/src/onionshare/gui_common.py @@ -281,6 +281,11 @@ class GuiCommon: QLabel { color: #cc0000; }""", + "tor_not_connected_label": """ + QLabel { + font-size: 16px; + font-style: italic; + }""", # New tab "new_tab_button_image": """ QLabel { diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index 398782af..868a6fa9 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -229,5 +229,6 @@ "moat_captcha_reload": "Reload", "moat_bridgedb_error": "Error contacting BridgeDB.", "moat_captcha_error": "The solution is not correct. Please try again.", - "moat_solution_empty_error": "You must enter the characters from the image" + "moat_solution_empty_error": "You must enter the characters from the image", + "mode_tor_not_connected_label": "OnionShare is not connected to the Tor network" } \ No newline at end of file diff --git a/desktop/src/onionshare/tab/mode/__init__.py b/desktop/src/onionshare/tab/mode/__init__.py index d4f2c23a..936c91cb 100644 --- a/desktop/src/onionshare/tab/mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/__init__.py @@ -28,7 +28,7 @@ from .mode_settings_widget import ModeSettingsWidget from ..server_status import ServerStatus from ... import strings from ...threads import OnionThread, AutoStartTimer -from ...widgets import Alert +from ...widgets import Alert, MinimumSizeWidget class Mode(QtWidgets.QWidget): @@ -101,6 +101,35 @@ class Mode(QtWidgets.QWidget): self.primary_action = QtWidgets.QWidget() self.primary_action.setLayout(self.primary_action_layout) + # It's up to the downstream Mode to add stuff to self.content_layout + # self.content_layout shows the actual content of the mode + # self.tor_not_connected_layout is displayed when Tor isn't connected + self.content_layout = QtWidgets.QVBoxLayout() + self.content_widget = QtWidgets.QWidget() + self.content_widget.setLayout(self.content_layout) + + tor_not_connected_label = QtWidgets.QLabel( + strings._("mode_tor_not_connected_label") + ) + tor_not_connected_label.setAlignment(QtCore.Qt.AlignHCenter) + tor_not_connected_label.setStyleSheet( + self.common.gui.css["tor_not_connected_label"] + ) + self.tor_not_connected_layout = QtWidgets.QVBoxLayout() + self.tor_not_connected_layout.addStretch() + self.tor_not_connected_layout.addWidget(tor_not_connected_label) + self.tor_not_connected_layout.addWidget(MinimumSizeWidget(700, 0)) + self.tor_not_connected_layout.addStretch() + self.tor_not_connected_widget = QtWidgets.QWidget() + self.tor_not_connected_widget.setLayout(self.tor_not_connected_layout) + + self.wrapper_layout = QtWidgets.QVBoxLayout() + self.wrapper_layout.addWidget(self.content_widget) + self.wrapper_layout.addWidget(self.tor_not_connected_widget) + self.setLayout(self.wrapper_layout) + + self.tor_connection_init() + def init(self): """ Add custom initialization here. @@ -524,3 +553,26 @@ class Mode(QtWidgets.QWidget): Used in both Share and Website modes, so implemented here. """ self.history.cancel(event["data"]["id"]) + + def tor_connection_init(self): + """ + Figure out if Tor is connected and display the right widget + """ + if self.common.gui.onion.is_authenticated(): + self.tor_connection_started() + else: + self.tor_connection_stopped() + + def tor_connection_started(self): + """ + This is called on every Mode when Tor is connected + """ + self.content_widget.show() + self.tor_not_connected_widget.hide() + + def tor_connection_stopped(self): + """ + This is called on every Mode when Tor is disconnected + """ + self.content_widget.hide() + self.tor_not_connected_widget.show() diff --git a/desktop/src/onionshare/tab/mode/chat_mode/__init__.py b/desktop/src/onionshare/tab/mode/chat_mode/__init__.py index e7a17ce7..1081fe9d 100644 --- a/desktop/src/onionshare/tab/mode/chat_mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/chat_mode/__init__.py @@ -98,10 +98,8 @@ class ChatMode(Mode): self.column_layout.addWidget(self.image) self.column_layout.addLayout(self.main_layout) - # Wrapper layout - self.wrapper_layout = QtWidgets.QVBoxLayout() - self.wrapper_layout.addLayout(self.column_layout) - self.setLayout(self.wrapper_layout) + # Content layout + self.content_layout.addLayout(self.column_layout) def get_type(self): """ diff --git a/desktop/src/onionshare/tab/mode/receive_mode/__init__.py b/desktop/src/onionshare/tab/mode/receive_mode/__init__.py index d5036d1d..b2b2fc5a 100644 --- a/desktop/src/onionshare/tab/mode/receive_mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/receive_mode/__init__.py @@ -198,10 +198,8 @@ class ReceiveMode(Mode): self.column_layout.addLayout(row_layout) self.column_layout.addWidget(self.history, stretch=1) - # Wrapper layout - self.wrapper_layout = QtWidgets.QVBoxLayout() - self.wrapper_layout.addLayout(self.column_layout) - self.setLayout(self.wrapper_layout) + # Content layout + self.content_layout.addLayout(self.column_layout) def get_type(self): """ diff --git a/desktop/src/onionshare/tab/mode/share_mode/__init__.py b/desktop/src/onionshare/tab/mode/share_mode/__init__.py index 5d3e3c35..7be93f1d 100644 --- a/desktop/src/onionshare/tab/mode/share_mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/share_mode/__init__.py @@ -169,10 +169,8 @@ class ShareMode(Mode): self.column_layout.addLayout(self.main_layout) self.column_layout.addWidget(self.history, stretch=1) - # Wrapper layout - self.wrapper_layout = QtWidgets.QVBoxLayout() - self.wrapper_layout.addLayout(self.column_layout) - self.setLayout(self.wrapper_layout) + # Content layout + self.content_layout.addLayout(self.column_layout) # Always start with focus on file selection self.file_selection.setFocus() diff --git a/desktop/src/onionshare/tab/mode/website_mode/__init__.py b/desktop/src/onionshare/tab/mode/website_mode/__init__.py index a50d15b9..73c4bad2 100644 --- a/desktop/src/onionshare/tab/mode/website_mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/website_mode/__init__.py @@ -167,10 +167,8 @@ class WebsiteMode(Mode): self.column_layout.addLayout(self.main_layout) self.column_layout.addWidget(self.history, stretch=1) - # Wrapper layout - self.wrapper_layout = QtWidgets.QVBoxLayout() - self.wrapper_layout.addLayout(self.column_layout) - self.setLayout(self.wrapper_layout) + # Content layout + self.content_layout.addLayout(self.column_layout) # Always start with focus on file selection self.file_selection.setFocus() diff --git a/desktop/src/onionshare/tab/tab.py b/desktop/src/onionshare/tab/tab.py index 5d9bb077..fb7f1836 100644 --- a/desktop/src/onionshare/tab/tab.py +++ b/desktop/src/onionshare/tab/tab.py @@ -96,7 +96,6 @@ class Tab(QtWidgets.QWidget): tab_id, system_tray, status_bar, - mode_settings=None, filenames=None, ): super(Tab, self).__init__() diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 3579d21b..c7a3552a 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -235,6 +235,8 @@ class TabWidget(QtWidgets.QTabWidget): self.common, self.current_tab_id, self.are_tabs_active(), self.status_bar ) self.tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) + self.tor_settings_tab.tor_is_connected.connect(self.tor_is_connected) + self.tor_settings_tab.tor_is_disconnected.connect(self.tor_is_disconnected) self.tabs[self.current_tab_id] = self.tor_settings_tab self.current_tab_id += 1 index = self.addTab( @@ -399,6 +401,26 @@ class TabWidget(QtWidgets.QTabWidget): close_button.clicked.connect(close_tab) self.tabBar().setTabButton(index, QtWidgets.QTabBar.RightSide, tab.close_button) + def tor_is_connected(self): + for tab_id in self.tabs: + if not ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + mode = self.tabs[tab_id].get_mode() + if mode: + mode.tor_connection_started() + + def tor_is_disconnected(self): + for tab_id in self.tabs: + if not ( + type(self.tabs[tab_id]) is SettingsTab + or type(self.tabs[tab_id]) is TorSettingsTab + ): + mode = self.tabs[tab_id].get_mode() + if mode: + mode.tor_connection_stopped() + class TabBar(QtWidgets.QTabBar): """ diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index 85645ca0..e28e5260 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -40,6 +40,8 @@ class TorSettingsTab(QtWidgets.QWidget): """ close_this_tab = QtCore.Signal() + tor_is_connected = QtCore.Signal() + tor_is_disconnected = QtCore.Signal() def __init__(self, common, tab_id, are_tabs_active, status_bar): super(TorSettingsTab, self).__init__() @@ -699,6 +701,9 @@ class TorSettingsTab(QtWidgets.QWidget): # Do we need to reinitialize Tor? if reboot_onion: + # Tell the tabs that Tor is disconnected + self.tor_is_disconnected.emit() + # Reinitialize the Onion object self.common.log( "TorSettingsTab", "save_clicked", "rebooting the Onion" @@ -742,6 +747,9 @@ class TorSettingsTab(QtWidgets.QWidget): self.common.gui.onion.is_authenticated() and not self.tor_con.wasCanceled() ): + # Tell the tabs that Tor is connected + self.tor_is_connected.emit() + # Close the tab self.close_this_tab.emit() self.tor_con_type = None From 7448f76f2e54899e932ecc2a116c04cf25ed061d Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:17:02 -0700 Subject: [PATCH 21/29] Respect --local-only --- desktop/src/onionshare/tab/mode/__init__.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/desktop/src/onionshare/tab/mode/__init__.py b/desktop/src/onionshare/tab/mode/__init__.py index 936c91cb..6be97995 100644 --- a/desktop/src/onionshare/tab/mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/__init__.py @@ -128,7 +128,10 @@ class Mode(QtWidgets.QWidget): self.wrapper_layout.addWidget(self.tor_not_connected_widget) self.setLayout(self.wrapper_layout) - self.tor_connection_init() + if self.common.gui.onion.is_authenticated(): + self.tor_connection_started() + else: + self.tor_connection_stopped() def init(self): """ @@ -554,15 +557,6 @@ class Mode(QtWidgets.QWidget): """ self.history.cancel(event["data"]["id"]) - def tor_connection_init(self): - """ - Figure out if Tor is connected and display the right widget - """ - if self.common.gui.onion.is_authenticated(): - self.tor_connection_started() - else: - self.tor_connection_stopped() - def tor_connection_started(self): """ This is called on every Mode when Tor is connected @@ -574,5 +568,9 @@ class Mode(QtWidgets.QWidget): """ This is called on every Mode when Tor is disconnected """ + if self.common.gui.local_only: + self.tor_connection_started() + return + self.content_widget.hide() self.tor_not_connected_widget.show() From 4e62b8831a867c47cfa880c7d0841ba1483da5c2 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:42:51 -0700 Subject: [PATCH 22/29] Get tor from Tor Browser 11.0a10 on all platforms --- desktop/scripts/get-tor-linux.py | 6 +++--- desktop/scripts/get-tor-osx.py | 6 +++--- desktop/scripts/get-tor-windows.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/desktop/scripts/get-tor-linux.py b/desktop/scripts/get-tor-linux.py index b8f83c92..51beb475 100755 --- a/desktop/scripts/get-tor-linux.py +++ b/desktop/scripts/get-tor-linux.py @@ -34,10 +34,10 @@ import requests def main(): - tarball_url = "https://dist.torproject.org/torbrowser/11.0a9/tor-browser-linux64-11.0a9_en-US.tar.xz" - tarball_filename = "tor-browser-linux64-11.0a9_en-US.tar.xz" + tarball_url = "https://dist.torproject.org/torbrowser/11.0a10/tor-browser-linux64-11.0a10_en-US.tar.xz" + tarball_filename = "tor-browser-linux64-11.0a10_en-US.tar.xz" expected_tarball_sha256 = ( - "cba4a2120b4f847d1ade637e41e69bd01b2e70b4a13e41fe8e69d0424fcf7ca7" + "5d3e2ebc4fb6a10f44624359bc2a5a151a57e8402cbd8563d15f9b2524374f1f" ) # Build paths diff --git a/desktop/scripts/get-tor-osx.py b/desktop/scripts/get-tor-osx.py index be5f7a56..410a8157 100755 --- a/desktop/scripts/get-tor-osx.py +++ b/desktop/scripts/get-tor-osx.py @@ -34,10 +34,10 @@ import requests def main(): - dmg_url = "https://dist.torproject.org/torbrowser/11.0a7/TorBrowser-11.0a7-osx64_en-US.dmg" - dmg_filename = "TorBrowser-11.0a7-osx64_en-US.dmg" + dmg_url = "https://dist.torproject.org/torbrowser/11.0a10/TorBrowser-11.0a10-osx64_en-US.dmg" + dmg_filename = "TorBrowser-11.0a10-osx64_en-US.dmg" expected_dmg_sha256 = ( - "46594cefa29493150d1c0e1933dd656aafcb6b51ef310d44ac059eed2fd1388e" + "c6823a28fd28205437564815f93011ff93b7972da2a8ce16919adfc65909e7b9" ) # Build paths diff --git a/desktop/scripts/get-tor-windows.py b/desktop/scripts/get-tor-windows.py index 751faecc..8ca2e79f 100644 --- a/desktop/scripts/get-tor-windows.py +++ b/desktop/scripts/get-tor-windows.py @@ -33,10 +33,10 @@ import requests def main(): - exe_url = "https://dist.torproject.org/torbrowser/11.0a7/torbrowser-install-11.0a7_en-US.exe" - exe_filename = "torbrowser-install-11.0a7_en-US.exe" + exe_url = "https://dist.torproject.org/torbrowser/11.0a10/torbrowser-install-11.0a10_en-US.exe" + exe_filename = "torbrowser-install-11.0a10_en-US.exe" expected_exe_sha256 = ( - "8b2013669d88e3ae8fa9bc17a3495eaac9475f79a849354e826e5132811a860b" + "f567dd8368dea0a8d7bbf7c19ece7840f93d493e70662939b92f5058c8dc8d2d" ) # Build paths root_path = os.path.dirname( From fa70368a8d4891461ae503ba40594e456e1b4dae Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:46:52 -0700 Subject: [PATCH 23/29] Copy snowflake-client from macOS Tor Browser --- desktop/scripts/get-tor-osx.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/desktop/scripts/get-tor-osx.py b/desktop/scripts/get-tor-osx.py index 410a8157..80d7aee8 100755 --- a/desktop/scripts/get-tor-osx.py +++ b/desktop/scripts/get-tor-osx.py @@ -101,6 +101,14 @@ def main(): os.path.join(dist_path, "obfs4proxy"), ) os.chmod(os.path.join(dist_path, "obfs4proxy"), 0o755) + # snowflake-client binary + shutil.copyfile( + os.path.join( + dmg_tor_path, "MacOS", "Tor", "PluggableTransports", "snowflake-client" + ), + os.path.join(dist_path, "snowflake-client"), + ) + os.chmod(os.path.join(dist_path, "snowflake-client"), 0o755) # Eject dmg subprocess.call(["diskutil", "eject", "/Volumes/Tor Browser"]) From 0e2d2a1e46f87df6fa76bf180f6bda2df1efa5e9 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:52:05 -0700 Subject: [PATCH 24/29] macOS seems to have close buttons that work on their own now --- desktop/src/onionshare/tab_widget.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index c7a3552a..7f42632d 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -186,10 +186,6 @@ class TabWidget(QtWidgets.QTabWidget): index = self.addTab(tab, strings._("gui_new_tab")) self.setCurrentIndex(index) - # In macOS, manually create a close button because tabs don't seem to have them otherwise - if self.common.platform == "Darwin": - self.macos_create_close_button(tab, index) - tab.init(mode_settings) # Make sure the title is set @@ -218,10 +214,6 @@ class TabWidget(QtWidgets.QTabWidget): index = self.addTab(settings_tab, strings._("gui_settings_window_title")) self.setCurrentIndex(index) - # In macOS, manually create a close button because tabs don't seem to have them otherwise - if self.common.platform == "Darwin": - self.macos_create_close_button(settings_tab, index) - def open_tor_settings_tab(self): self.common.log("TabWidget", "open_tor_settings_tab") @@ -244,10 +236,6 @@ class TabWidget(QtWidgets.QTabWidget): ) self.setCurrentIndex(index) - # In macOS, manually create a close button because tabs don't seem to have them otherwise - if self.common.platform == "Darwin": - self.macos_create_close_button(self.tor_settings_tab, index) - def change_title(self, tab_id, title): shortened_title = title if len(shortened_title) > 11: @@ -388,19 +376,6 @@ class TabWidget(QtWidgets.QTabWidget): super(TabWidget, self).resizeEvent(event) self.move_new_tab_button() - def macos_create_close_button(self, tab, index): - def close_tab(): - self.tabBar().tabCloseRequested.emit(self.indexOf(tab)) - - close_button = QtWidgets.QPushButton() - close_button.setFlat(True) - close_button.setFixedWidth(40) - close_button.setIcon( - QtGui.QIcon(GuiCommon.get_resource_path("images/close_tab.png")) - ) - close_button.clicked.connect(close_tab) - self.tabBar().setTabButton(index, QtWidgets.QTabBar.RightSide, tab.close_button) - def tor_is_connected(self): for tab_id in self.tabs: if not ( From f915f911c6fc7dd4dea500538a993b7382fee85b Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 20:55:50 -0700 Subject: [PATCH 25/29] Make autoupdate group in Settings Tab centered --- desktop/src/onionshare/settings_tab.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/desktop/src/onionshare/settings_tab.py b/desktop/src/onionshare/settings_tab.py index 8f41b2aa..75b7a326 100644 --- a/desktop/src/onionshare/settings_tab.py +++ b/desktop/src/onionshare/settings_tab.py @@ -73,9 +73,16 @@ class SettingsTab(QtWidgets.QWidget): ) autoupdate_group.setLayout(autoupdate_group_layout) + autoupdate_layout = QtWidgets.QHBoxLayout() + autoupdate_layout.addStretch() + autoupdate_layout.addWidget(autoupdate_group) + autoupdate_layout.addStretch() + autoupdate_widget = QtWidgets.QWidget() + autoupdate_widget.setLayout(autoupdate_layout) + # Autoupdate is only available for Windows and Mac (Linux updates using package manager) if self.system != "Windows" and self.system != "Darwin": - autoupdate_group.hide() + autoupdate_widget.hide() # Language settings language_label = QtWidgets.QLabel(strings._("gui_settings_language_label")) @@ -131,8 +138,8 @@ class SettingsTab(QtWidgets.QWidget): # Layout layout = QtWidgets.QVBoxLayout() layout.addStretch() - layout.addWidget(autoupdate_group) - if autoupdate_group.isVisible(): + layout.addWidget(autoupdate_widget) + if autoupdate_widget.isVisible(): layout.addSpacing(20) layout.addLayout(language_layout) layout.addLayout(theme_layout) From 472e383b7d0f4040e4963ffff95da1e86fe85cca Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 6 Nov 2021 21:02:24 -0700 Subject: [PATCH 26/29] Fix settings error color in dark mode --- desktop/src/onionshare/gui_common.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/desktop/src/onionshare/gui_common.py b/desktop/src/onionshare/gui_common.py index 39f6d46f..0db0f051 100644 --- a/desktop/src/onionshare/gui_common.py +++ b/desktop/src/onionshare/gui_common.py @@ -93,6 +93,7 @@ class GuiCommon: share_zip_progess_bar_chunk_color = "#4E064F" history_background_color = "#ffffff" history_label_color = "#000000" + settings_error_color = "#FF0000" if color_mode == "dark": header_color = "#F2F2F2" title_color = "#F2F2F2" @@ -103,6 +104,7 @@ class GuiCommon: share_zip_progess_bar_border_color = "#F2F2F2" history_background_color = "#191919" history_label_color = "#ffffff" + settings_error_color = "#FF9999" return { # OnionShareGui styles @@ -400,7 +402,9 @@ class GuiCommon: # Tor Settings dialogs "tor_settings_error": """ QLabel { - color: #FF0000; + color: """ + + settings_error_color + + """; } """, } From 9430439b5f02829e3677ac187e1db7a52076ddad Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 7 Nov 2021 12:12:12 -0800 Subject: [PATCH 27/29] Fix meek-client in Windows --- cli/onionshare_cli/meek.py | 12 +++++++++++- desktop/README.md | 2 +- desktop/scripts/build-meek-client.py | 16 +++++++++++----- desktop/src/onionshare/moat_dialog.py | 10 ++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/cli/onionshare_cli/meek.py b/cli/onionshare_cli/meek.py index b2e70dc1..c5df7b7f 100644 --- a/cli/onionshare_cli/meek.py +++ b/cli/onionshare_cli/meek.py @@ -85,6 +85,10 @@ class Meek(object): self.common.log("Meek", "start", "Starting meek client") if self.common.platform == "Windows": + env = os.environ.copy() + for key in self.meek_env: + env[key] = self.meek_env[key] + # In Windows, hide console window when opening meek-client.exe subprocess startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW @@ -100,7 +104,7 @@ class Meek(object): stderr=subprocess.PIPE, startupinfo=startupinfo, bufsize=1, - env=self.meek_env, + env=env, text=True, ) else: @@ -129,6 +133,7 @@ class Meek(object): # read stdout without blocking try: line = q.get_nowait() + self.common.log("Meek", "start", line.strip()) except Empty: # no stdout yet? pass @@ -143,6 +148,10 @@ class Meek(object): ) break + if "CMETHOD-ERROR" in line: + self.cleanup() + raise MeekNotRunning() + if self.meek_port: self.meek_proxies = { "http": f"socks5h://{self.meek_host}:{self.meek_port}", @@ -150,6 +159,7 @@ class Meek(object): } else: self.common.log("Meek", "start", "Could not obtain the meek port") + self.cleanup() raise MeekNotRunning() def cleanup(self): diff --git a/desktop/README.md b/desktop/README.md index 408b6852..7f13ad70 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -65,7 +65,7 @@ python scripts\get-tor-windows.py ### Compile dependencies -Install Go. The simplest way to make sure everything works is to install Go by following [these instructions](https://golang.org/doc/install). +Install Go. The simplest way to make sure everything works is to install Go by following [these instructions](https://golang.org/doc/install). (In Windows, make sure to install the 32-bit version of Go, such as `go1.17.3.windows-386.msi`.) Download and compile `meek-client`: diff --git a/desktop/scripts/build-meek-client.py b/desktop/scripts/build-meek-client.py index d043754c..af58173a 100755 --- a/desktop/scripts/build-meek-client.py +++ b/desktop/scripts/build-meek-client.py @@ -28,6 +28,7 @@ import shutil import os import subprocess import inspect +import platform def main(): @@ -46,16 +47,21 @@ def main(): root_path = os.path.dirname( os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) ) - dist_path = os.path.join(root_path, "src", "onionshare", "resources", "tor") + if platform.system() == "Windows": + dist_path = os.path.join(root_path, "src", "onionshare", "resources", "tor", "Tor") + bin_filename = "meek-client.exe" + else: + dist_path = os.path.join(root_path, "src", "onionshare", "resources", "tor") + bin_filename = "meek-client" - bin_path = os.path.expanduser("~/go/bin/meek-client") + bin_path = os.path.join(os.path.expanduser("~"), "go", "bin", bin_filename) shutil.copyfile( os.path.join(bin_path), - os.path.join(dist_path, "meek-client"), + os.path.join(dist_path, bin_filename), ) - os.chmod(os.path.join(dist_path, "meek-client"), 0o755) + os.chmod(os.path.join(dist_path, bin_filename), 0o755) - print(f"Installed meek-client in {dist_path}") + print(f"Installed {bin_filename} in {dist_path}") if __name__ == "__main__": diff --git a/desktop/src/onionshare/moat_dialog.py b/desktop/src/onionshare/moat_dialog.py index 85b5e888..84a52390 100644 --- a/desktop/src/onionshare/moat_dialog.py +++ b/desktop/src/onionshare/moat_dialog.py @@ -26,7 +26,7 @@ import json from . import strings from .gui_common import GuiCommon -from onionshare_cli.meek import MeekNotFound +from onionshare_cli.meek import MeekNotFound, MeekNotRunning class MoatDialog(QtWidgets.QDialog): @@ -237,7 +237,13 @@ class MoatThread(QtCore.QThread): try: self.meek.start() except MeekNotFound: - self.common.log("MoatThread", "run", f"Could not find the Meek Client") + self.common.log("MoatThread", "run", f"Could not find meek-client") + self.bridgedb_error.emit() + return + except MeekNotRunning: + self.common.log( + "MoatThread", "run", f"Ran meek-client, but there was an error" + ) self.bridgedb_error.emit() return From 73b570f4b44da8790d7e7d22b6c160b047fc86c7 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 9 Nov 2021 18:49:23 -0800 Subject: [PATCH 28/29] Check if Tor is connected instead of if the Tor controller is authenticated --- desktop/src/onionshare/tab/mode/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/desktop/src/onionshare/tab/mode/__init__.py b/desktop/src/onionshare/tab/mode/__init__.py index 6be97995..c9b5cad1 100644 --- a/desktop/src/onionshare/tab/mode/__init__.py +++ b/desktop/src/onionshare/tab/mode/__init__.py @@ -128,7 +128,7 @@ class Mode(QtWidgets.QWidget): self.wrapper_layout.addWidget(self.tor_not_connected_widget) self.setLayout(self.wrapper_layout) - if self.common.gui.onion.is_authenticated(): + if self.common.gui.onion.connected_to_tor: self.tor_connection_started() else: self.tor_connection_stopped() @@ -571,6 +571,6 @@ class Mode(QtWidgets.QWidget): if self.common.gui.local_only: self.tor_connection_started() return - + self.content_widget.hide() self.tor_not_connected_widget.show() From d88005d550ab65024e298f05e0c269eccf98f9e3 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 9 Nov 2021 18:57:07 -0800 Subject: [PATCH 29/29] When Tor is disconnected, hide the Check for Updates button in the Settings tab --- desktop/src/onionshare/settings_tab.py | 11 ++++++++++ desktop/src/onionshare/tab_widget.py | 28 +++++++++++++------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/desktop/src/onionshare/settings_tab.py b/desktop/src/onionshare/settings_tab.py index 75b7a326..cfa3261e 100644 --- a/desktop/src/onionshare/settings_tab.py +++ b/desktop/src/onionshare/settings_tab.py @@ -154,6 +154,11 @@ class SettingsTab(QtWidgets.QWidget): self.reload_settings() + if self.common.gui.onion.connected_to_tor: + self.tor_is_connected() + else: + self.tor_is_disconnected() + def reload_settings(self): # Load settings, and fill them in self.old_settings = Settings(self.common) @@ -341,3 +346,9 @@ class SettingsTab(QtWidgets.QWidget): else: self.check_for_updates_button.setEnabled(True) self.save_button.setEnabled(True) + + def tor_is_connected(self): + self.check_for_updates_button.show() + + def tor_is_disconnected(self): + self.check_for_updates_button.hide() diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 7f42632d..7162fcc4 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -378,23 +378,23 @@ class TabWidget(QtWidgets.QTabWidget): def tor_is_connected(self): for tab_id in self.tabs: - if not ( - type(self.tabs[tab_id]) is SettingsTab - or type(self.tabs[tab_id]) is TorSettingsTab - ): - mode = self.tabs[tab_id].get_mode() - if mode: - mode.tor_connection_started() + if type(self.tabs[tab_id]) is SettingsTab: + self.tabs[tab_id].tor_is_connected() + else: + if not type(self.tabs[tab_id]) is TorSettingsTab: + mode = self.tabs[tab_id].get_mode() + if mode: + mode.tor_connection_started() def tor_is_disconnected(self): for tab_id in self.tabs: - if not ( - type(self.tabs[tab_id]) is SettingsTab - or type(self.tabs[tab_id]) is TorSettingsTab - ): - mode = self.tabs[tab_id].get_mode() - if mode: - mode.tor_connection_stopped() + if type(self.tabs[tab_id]) is SettingsTab: + self.tabs[tab_id].tor_is_disconnected() + else: + if not type(self.tabs[tab_id]) is TorSettingsTab: + mode = self.tabs[tab_id].get_mode() + if mode: + mode.tor_connection_stopped() class TabBar(QtWidgets.QTabBar):