From 99505a4c7df690ccd0f6262595b4ea4d61125207 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 12 Mar 2019 15:29:07 +1100 Subject: [PATCH] Allow the bundled Tor connection timeout to be configurable from the CLI --- onionshare/__init__.py | 4 +++- onionshare/onion.py | 12 ++++++------ share/locale/en.json | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 2f44c846..cbfe222c 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -54,6 +54,7 @@ def main(cwd=None): parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only")) parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open")) parser.add_argument('--shutdown-timeout', metavar='', dest='shutdown_timeout', default=0, help=strings._("help_shutdown_timeout")) + parser.add_argument('--connect-timeout', metavar='', dest='connect_timeout', default=120, help=strings._("help_connect_timeout")) parser.add_argument('--stealth', action='store_true', dest='stealth', help=strings._("help_stealth")) parser.add_argument('--receive', action='store_true', dest='receive', help=strings._("help_receive")) parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config')) @@ -69,6 +70,7 @@ def main(cwd=None): debug = bool(args.debug) stay_open = bool(args.stay_open) shutdown_timeout = int(args.shutdown_timeout) + connect_timeout = int(args.connect_timeout) stealth = bool(args.stealth) receive = bool(args.receive) config = args.config @@ -111,7 +113,7 @@ def main(cwd=None): # Start the Onion object onion = Onion(common) try: - onion.connect(custom_settings=False, config=config) + onion.connect(custom_settings=False, config=config, connect_timeout=connect_timeout) except KeyboardInterrupt: print("") sys.exit() diff --git a/onionshare/onion.py b/onionshare/onion.py index ed4fde7b..03d1a1b0 100644 --- a/onionshare/onion.py +++ b/onionshare/onion.py @@ -152,7 +152,7 @@ class Onion(object): # Start out not connected to Tor self.connected_to_tor = False - def connect(self, custom_settings=False, config=False, tor_status_update_func=None): + def connect(self, custom_settings=False, config=False, tor_status_update_func=None, connect_timeout=120): self.common.log('Onion', 'connect') # Either use settings that are passed in, or use them from common @@ -284,13 +284,13 @@ class Onion(object): self.settings.get('tor_bridges_use_obfs4') or \ self.settings.get('tor_bridges_use_meek_lite_azure'): connect_timeout = 150 - else: - # Timeout after 120 seconds - connect_timeout = 120 if time.time() - start_ts > connect_timeout: print("") - self.tor_proc.terminate() - raise BundledTorTimeout(strings._('settings_error_bundled_tor_timeout')) + try: + self.tor_proc.terminate() + raise BundledTorTimeout(strings._('settings_error_bundled_tor_timeout')) + except FileNotFoundError: + pass elif self.settings.get('connection_type') == 'automatic': # Automatically try to guess the right way to connect to Tor Browser diff --git a/share/locale/en.json b/share/locale/en.json index f7af9a80..9dd0648e 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -16,6 +16,7 @@ "help_local_only": "Don't use Tor (only for development)", "help_stay_open": "Continue sharing after files have been sent", "help_shutdown_timeout": "Stop sharing after a given amount of seconds", + "help_connect_timeout": "Give up connecting to Tor after a given amount of seconds (default: 120)", "help_stealth": "Use client authorization (advanced)", "help_receive": "Receive shares instead of sending them", "help_debug": "Log OnionShare errors to stdout, and web errors to disk",