Make CLI throw an error if it cannot find tor binary

This commit is contained in:
Micah Lee 2020-11-29 11:08:46 -08:00
parent 7bcfe6cad1
commit aecaae896d
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
3 changed files with 20 additions and 21 deletions

View file

@ -22,7 +22,7 @@ import os, sys, time, argparse, threading
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
from .common import Common from .common import Common, CannotFindTor
from .web import Web from .web import Web
from .onion import * from .onion import *
from .onionshare import OnionShare from .onionshare import OnionShare
@ -320,7 +320,15 @@ def main(cwd=None):
web = Web(common, False, mode_settings, mode) web = Web(common, False, mode_settings, mode)
# Start the Onion object # Start the Onion object
onion = Onion(common, use_tmp_dir=True) try:
onion = Onion(common, use_tmp_dir=True)
except CannotFindTor:
print("You must install tor to use OnionShare from the command line")
if common.platform == "Darwin":
print("In macOS, you can do this with Homebrew (https://brew.sh):")
print(" brew install tor")
sys.exit()
try: try:
onion.connect( onion.connect(
custom_settings=False, custom_settings=False,

View file

@ -34,6 +34,12 @@ from pkg_resources import resource_filename
from .settings import Settings from .settings import Settings
class CannotFindTor(Exception):
"""
OnionShare can't find a tor binary
"""
class Common: class Common:
""" """
The Common object is shared amongst all parts of OnionShare. The Common object is shared amongst all parts of OnionShare.
@ -82,6 +88,8 @@ class Common:
def get_tor_paths(self): def get_tor_paths(self):
if self.platform == "Linux": if self.platform == "Linux":
tor_path = shutil.which("tor") tor_path = shutil.which("tor")
if not tor_path:
raise CannotFindTor()
obfs4proxy_file_path = shutil.which("obfs4proxy") obfs4proxy_file_path = shutil.which("obfs4proxy")
prefix = os.path.dirname(os.path.dirname(tor_path)) prefix = os.path.dirname(os.path.dirname(tor_path))
tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip")
@ -94,6 +102,8 @@ class Common:
tor_geo_ipv6_file_path = os.path.join(base_path, "Data", "Tor", "geoip6") tor_geo_ipv6_file_path = os.path.join(base_path, "Data", "Tor", "geoip6")
elif self.platform == "Darwin": elif self.platform == "Darwin":
tor_path = shutil.which("tor") tor_path = shutil.which("tor")
if not tor_path:
raise CannotFindTor()
obfs4proxy_file_path = shutil.which("obfs4proxy") obfs4proxy_file_path = shutil.which("obfs4proxy")
prefix = os.path.dirname(os.path.dirname(tor_path)) prefix = os.path.dirname(os.path.dirname(tor_path))
tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip") tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip")

View file

@ -32,7 +32,6 @@ import getpass
import psutil import psutil
from distutils.version import LooseVersion as Version from distutils.version import LooseVersion as Version
from . import common
from .settings import Settings from .settings import Settings
# TODO: Figure out how to localize this for the GUI # TODO: Figure out how to localize this for the GUI
@ -44,40 +43,30 @@ class TorErrorAutomatic(Exception):
using automatic settings that should work with Tor Browser. using automatic settings that should work with Tor Browser.
""" """
pass
class TorErrorInvalidSetting(Exception): class TorErrorInvalidSetting(Exception):
""" """
This exception is raised if the settings just don't make sense. This exception is raised if the settings just don't make sense.
""" """
pass
class TorErrorSocketPort(Exception): class TorErrorSocketPort(Exception):
""" """
OnionShare can't connect to the Tor controller using the supplied address and port. OnionShare can't connect to the Tor controller using the supplied address and port.
""" """
pass
class TorErrorSocketFile(Exception): class TorErrorSocketFile(Exception):
""" """
OnionShare can't connect to the Tor controller using the supplied socket file. OnionShare can't connect to the Tor controller using the supplied socket file.
""" """
pass
class TorErrorMissingPassword(Exception): class TorErrorMissingPassword(Exception):
""" """
OnionShare connected to the Tor controller, but it requires a password. OnionShare connected to the Tor controller, but it requires a password.
""" """
pass
class TorErrorUnreadableCookieFile(Exception): class TorErrorUnreadableCookieFile(Exception):
""" """
@ -85,8 +74,6 @@ class TorErrorUnreadableCookieFile(Exception):
to access the cookie file. to access the cookie file.
""" """
pass
class TorErrorAuthError(Exception): class TorErrorAuthError(Exception):
""" """
@ -94,8 +81,6 @@ class TorErrorAuthError(Exception):
that a Tor controller isn't listening on this port. that a Tor controller isn't listening on this port.
""" """
pass
class TorErrorProtocolError(Exception): class TorErrorProtocolError(Exception):
""" """
@ -103,8 +88,6 @@ class TorErrorProtocolError(Exception):
isn't acting like a Tor controller (such as in Whonix). isn't acting like a Tor controller (such as in Whonix).
""" """
pass
class TorTooOld(Exception): class TorTooOld(Exception):
""" """
@ -113,8 +96,6 @@ class TorTooOld(Exception):
is too old. is too old.
""" """
pass
class BundledTorTimeout(Exception): class BundledTorTimeout(Exception):
""" """