If running from onionshare CLI, use a new temporary tor data dir, and if running onionshare-gui, always use the same tor data dir

This commit is contained in:
Micah Lee 2019-11-28 14:02:00 -08:00
parent e316af0e11
commit 3fe6d43d9d
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 33 additions and 12 deletions

View File

@ -266,7 +266,7 @@ 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) onion = Onion(common, use_tmp_dir=True)
try: try:
onion.connect( onion.connect(
custom_settings=False, custom_settings=False,

View File

@ -181,15 +181,30 @@ class Common:
os.makedirs(onionshare_data_dir, 0o700, True) os.makedirs(onionshare_data_dir, 0o700, True)
return onionshare_data_dir return onionshare_data_dir
def build_tmp_dir(self):
"""
Returns path to a folder that can hold temporary files
"""
tmp_dir = os.path.join(self.build_data_dir(), "tmp")
os.makedirs(tmp_dir, 0o700, True)
return tmp_dir
def build_persistent_dir(self): def build_persistent_dir(self):
""" """
Returns the path to the folder that holds persistent files Returns the path to the folder that holds persistent files
""" """
onionshare_data_dir = self.build_data_dir() persistent_dir = os.path.join(self.build_data_dir(), "persistent")
persistent_dir = os.path.join(onionshare_data_dir, "persistent")
os.makedirs(persistent_dir, 0o700, True) os.makedirs(persistent_dir, 0o700, True)
return persistent_dir return persistent_dir
def build_tor_dir(self):
"""
Returns path to the tor data directory
"""
tor_dir = os.path.join(self.build_data_dir(), "tor_data")
os.makedirs(tor_dir, 0o700, True)
return tor_dir
def build_password(self, word_count=2): def build_password(self, word_count=2):
""" """
Returns a random string made of words from the wordlist, such as "deter-trig". Returns a random string made of words from the wordlist, such as "deter-trig".

View File

@ -150,10 +150,12 @@ class Onion(object):
is necessary for status updates to reach the GUI. is necessary for status updates to reach the GUI.
""" """
def __init__(self, common): def __init__(self, common, use_tmp_dir=False):
self.common = common self.common = common
self.common.log("Onion", "__init__") self.common.log("Onion", "__init__")
self.use_tmp_dir = use_tmp_dir
# Is bundled tor supported? # Is bundled tor supported?
if ( if (
self.common.platform == "Windows" or self.common.platform == "Darwin" self.common.platform == "Windows" or self.common.platform == "Darwin"
@ -217,24 +219,28 @@ class Onion(object):
) )
# Create a torrc for this session # Create a torrc for this session
if self.use_tmp_dir:
self.tor_data_directory = tempfile.TemporaryDirectory( self.tor_data_directory = tempfile.TemporaryDirectory(
dir=self.common.build_data_dir() dir=self.common.build_tmp_dir()
) )
self.tor_data_directory_name = self.tor_data_directory.name
else:
self.tor_data_directory_name = self.common.build_tor_dir()
self.common.log( self.common.log(
"Onion", "connect", f"tor_data_directory={self.tor_data_directory.name}" "Onion", "connect", f"tor_data_directory_name={self.tor_data_directory_name}"
) )
# Create the torrc # Create the torrc
with open(self.common.get_resource_path("torrc_template")) as f: with open(self.common.get_resource_path("torrc_template")) as f:
torrc_template = f.read() torrc_template = f.read()
self.tor_cookie_auth_file = os.path.join( self.tor_cookie_auth_file = os.path.join(
self.tor_data_directory.name, "cookie" self.tor_data_directory_name, "cookie"
) )
try: try:
self.tor_socks_port = self.common.get_available_port(1000, 65535) self.tor_socks_port = self.common.get_available_port(1000, 65535)
except: except:
raise OSError(strings._("no_available_port")) raise OSError(strings._("no_available_port"))
self.tor_torrc = os.path.join(self.tor_data_directory.name, "torrc") self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
if self.common.platform == "Windows" or self.common.platform == "Darwin": if self.common.platform == "Windows" or self.common.platform == "Darwin":
# Windows doesn't support unix sockets, so it must use a network port. # Windows doesn't support unix sockets, so it must use a network port.
@ -252,11 +258,11 @@ class Onion(object):
torrc_template += "ControlSocket {{control_socket}}\n" torrc_template += "ControlSocket {{control_socket}}\n"
self.tor_control_port = None self.tor_control_port = None
self.tor_control_socket = os.path.join( self.tor_control_socket = os.path.join(
self.tor_data_directory.name, "control_socket" self.tor_data_directory_name, "control_socket"
) )
torrc_template = torrc_template.replace( torrc_template = torrc_template.replace(
"{{data_directory}}", self.tor_data_directory.name "{{data_directory}}", self.tor_data_directory_name
) )
torrc_template = torrc_template.replace( torrc_template = torrc_template.replace(
"{{control_port}}", str(self.tor_control_port) "{{control_port}}", str(self.tor_control_port)