From 99c85f55e27e777e64e4de12c74e3dedc88feec1 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 14 Nov 2021 20:53:22 -0800 Subject: [PATCH] Fix bug that was preventing tor from connecting in snapcraft --- cli/onionshare_cli/common.py | 6 ++++++ cli/onionshare_cli/onion.py | 32 +++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/cli/onionshare_cli/common.py b/cli/onionshare_cli/common.py index ca88deef..4333d2d5 100644 --- a/cli/onionshare_cli/common.py +++ b/cli/onionshare_cli/common.py @@ -437,6 +437,12 @@ class Common: Returns True if OnionShare is running in a Flatpak sandbox """ return os.environ.get("FLATPAK_ID") == "org.onionshare.OnionShare" + + def is_snapcraft(self): + """ + Returns True if OnionShare is running in a Flatpak sandbox + """ + return os.environ.get("SNAP_INSTANCE_NAME") == "onionshare" @staticmethod def random_string(num_bytes, output_len=None): diff --git a/cli/onionshare_cli/onion.py b/cli/onionshare_cli/onion.py index 39b1dc9a..5ac669b8 100644 --- a/cli/onionshare_cli/onion.py +++ b/cli/onionshare_cli/onion.py @@ -29,6 +29,7 @@ import subprocess import time import shlex import psutil +import traceback from distutils.version import LooseVersion as Version @@ -371,11 +372,16 @@ class Onion(object): startupinfo=startupinfo, ) else: + if self.common.is_snapcraft(): + env = None + else: + env = {"LD_LIBRARY_PATH": os.path.dirname(self.tor_path)} + self.tor_proc = subprocess.Popen( [self.tor_path, "-f", self.tor_torrc], stdout=subprocess.PIPE, stderr=subprocess.PIPE, - env={"LD_LIBRARY_PATH": os.path.dirname(self.tor_path)}, + env=env, ) # Wait for the tor controller to start @@ -392,16 +398,20 @@ class Onion(object): "connect", "authenticating to tor controller", ) - # try: - if self.common.platform == "Windows" or self.common.platform == "Darwin": - self.c = Controller.from_port(port=self.tor_control_port) - self.c.authenticate() - else: - self.c = Controller.from_socket_file(path=self.tor_control_socket) - self.c.authenticate() - # except Exception as e: - # print("OnionShare could not connect to Tor:\n{}".format(e)) - # raise BundledTorBroken(e.args[0]) + try: + if ( + self.common.platform == "Windows" + or self.common.platform == "Darwin" + ): + self.c = Controller.from_port(port=self.tor_control_port) + self.c.authenticate() + else: + self.c = Controller.from_socket_file(path=self.tor_control_socket) + self.c.authenticate() + except Exception as e: + print("OnionShare could not connect to Tor:\n{}".format(e.args[0])) + print(traceback.format_exc()) + raise BundledTorBroken(e.args[0]) while True: try: