From a0abab3653515766f0830cd280ac33eebc614bae Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 29 Dec 2016 12:57:58 -0800 Subject: [PATCH] Make automatic settings work with with Tor Browser 6.0.8 --- onionshare/onion.py | 64 ++++++++++++++++--------------- onionshare/onionshare.py | 2 +- onionshare_gui/onionshare_gui.py | 2 +- onionshare_gui/settings_dialog.py | 4 +- resources/locale/en.json | 5 +-- 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/onionshare/onion.py b/onionshare/onion.py index ca3a6e67..0f20a77f 100644 --- a/onionshare/onion.py +++ b/onionshare/onion.py @@ -27,6 +27,13 @@ from . import socks from . import helpers, strings from .settings import Settings +class TorErrorAutomatic(Exception): + """ + OnionShare is failing to connect and authenticate to the Tor controller, + using automatic settings that should work with Tor Browser. + """ + pass + class TorErrorInvalidSetting(Exception): """ This exception is raised if the settings just don't make sense. @@ -58,13 +65,6 @@ class TorErrorUnreadableCookieFile(Exception): """ pass -class NoTor(Exception): - """ - This exception is raised if onionshare can't find a Tor control port - to connect to, or if it can't find a Tor socks5 proxy to proxy though. - """ - pass - class TorTooOld(Exception): """ This exception is raised if onionshare needs to use a feature of Tor or stem @@ -109,30 +109,34 @@ class Onion(object): if self.settings.get('connection_type') == 'automatic': # Automatically try to guess the right way to connect to Tor Browser - # if the TOR_CONTROL_PORT environment variable is set, use that - # otherwise, default to Tor Browser, Tor Messenger, and system tor ports - env_port = os.environ.get('TOR_CONTROL_PORT') - if env_port: - ports = [int(env_port)] - else: - ports = [9151, 9153, 9051] + # Try connecting + try: + # If the TOR_CONTROL_PORT environment variable is set, use that + env_port = os.environ.get('TOR_CONTROL_PORT') + if env_port: + self.c = Controller.from_port(port=int(env_port)) - # connect to the tor controlport - found_tor = False - for port in ports: - try: - self.c = Controller.from_port(port=port) - self.c.authenticate() - found_tor = True - break - except SocketError: - pass - except MissingPassword: - raise NoTor(strings._("ctrlport_missing_password").format(str(ports))) - except UnreadableCookieFile: - raise NoTor(strings._("ctrlport_unreadable_cookie").format(str(ports))) - if not found_tor: - raise NoTor(strings._("cant_connect_ctrlport").format(str(ports))) + else: + # Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor + found_tor = False + try: + ports = [9151, 9153, 9051] + for port in ports: + self.c = Controller.from_port(port=port) + found_tor = True + except: + pass + if not found_tor: + raise TorErrorAutomatic(strings._('settings_error_automatic')) + + except TorErrorAutomatic: + raise TorErrorAutomatic(strings._('settings_error_automatic')) + + # Try authenticating + try: + self.c.authenticate() + except: + raise TorErrorAutomatic(strings._('settings_error_automatic')) else: # Use specific settings to connect to tor diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index 6537f105..9ce9b310 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -155,7 +155,7 @@ def main(cwd=None): app = OnionShare(debug, local_only, stay_open, transparent_torification, stealth) app.choose_port() app.start_onion_service() - except (onion.NoTor, onion.TorTooOld, onion.TorErrorInvalidSetting, onion.TorErrorSocketPort, onion.TorErrorSocketFile, onion.TorErrorMissingPassword, onion.TorErrorUnreadableCookieFile) as e: + except (onion.TorTooOld, onion.TorErrorInvalidSetting, onion.TorErrorAutomatic, onion.TorErrorSocketPort, onion.TorErrorSocketFile, onion.TorErrorMissingPassword, onion.TorErrorUnreadableCookieFile) as e: sys.exit(e.args[0]) except KeyboardInterrupt: print("") diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 8a5858db..16b5615c 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -177,7 +177,7 @@ class OnionShareGui(QtWidgets.QMainWindow): self.app.start_onion_service() self.starting_server_step2.emit() - except (onionshare.onion.NoTor, onionshare.onion.TorTooOld, onionshare.onion.TorErrorInvalidSetting, onionshare.onion.TorErrorSocketPort, onionshare.onion.TorErrorSocketFile, onionshare.onion.TorErrorMissingPassword, onionshare.onion.TorErrorUnreadableCookieFile) as e: + except (onionshare.onion.TorTooOld, onionshare.onion.TorErrorInvalidSetting, onionshare.onion.TorErrorAutomatic, onionshare.onion.TorErrorSocketPort, onionshare.onion.TorErrorSocketFile, onionshare.onion.TorErrorMissingPassword, onionshare.onion.TorErrorUnreadableCookieFile) as e: self.starting_server_error.emit(e.args[0]) return diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index 24cecbb3..e18980de 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -21,7 +21,7 @@ from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings from onionshare.settings import Settings -from onionshare.onion import Onion, TorErrorInvalidSetting, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile +from onionshare.onion import Onion, TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile from .alert import Alert @@ -216,7 +216,7 @@ class SettingsDialog(QtWidgets.QDialog): # If an exception hasn't been raised yet, the Tor settings work Alert(strings._('settings_test_success', True).format(onion.tor_version, onion.supports_ephemeral, onion.supports_stealth)) - except (TorErrorInvalidSetting, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile) as e: + except (TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile) as e: Alert(e.args[0], QtWidgets.QMessageBox.Warning) def save_clicked(self): diff --git a/resources/locale/en.json b/resources/locale/en.json index c69fbab8..1d222a20 100644 --- a/resources/locale/en.json +++ b/resources/locale/en.json @@ -1,9 +1,5 @@ { "config_onion_service": "Configuring onion service on port {0:d}.", - "cant_connect_ctrlport": "Can't connect to Tor control port on port {0:s}. OnionShare requires Tor Browser to be running in the background to work. If you don't have it you can get it from https://www.torproject.org/.", - "cant_connect_socksport": "Can't connect to Tor SOCKS5 server on port {0:s}. OnionShare requires Tor Browser to be running in the background to work. If you don't have it you can get it from https://www.torproject.org/.", - "ctrlport_missing_password": "Connected to Tor control port on port {0:s}, but you require a password. You must have the TOR_AUTHENTICATION_PASSWORD environment variable set. Or just open Tor Browser in the background.", - "ctrlport_unreadable_cookie": "Connected to Tor control port on port {0:s}, but your user does not have permission to authenticate. You might want to add a HashedControlPassword to your torrc, and set the TOR_AUTHENTICATION_PASSWORD environment variable. Or just open Tor Browser in the background.", "preparing_files": "Preparing files to share.", "wait_for_hs": "Waiting for HS to be ready:", "wait_for_hs_trying": "Trying...", @@ -80,6 +76,7 @@ "gui_settings_button_cancel": "Cancel", "settings_saved": "Settings saved to {}", "settings_error_unknown": "Can't connect to Tor controller because the settings don't make sense.", + "settings_error_automatic": "Can't connect to Tor controller. Is Tor Browser running in the background? If you don't have it you can get it from:\nhttps://www.torproject.org/.", "settings_error_socket_port": "Can't connect to Tor controller on address {} with port {}.", "settings_error_socket_file": "Can't connect to Tor controller using socket file {}.", "settings_error_missing_password": "Connected to Tor controller, but it requires a password to authenticate.",