From 48070409f7a61575bdb2b9f4ab397ce2347e8061 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Dec 2019 10:13:56 -0800 Subject: [PATCH] Move private_key, hidservauth_string, and password from "persistent" mode settings to "onion" mode settings; and make it so the onion settings are always saved in each tab, even if the tab is not persistent, so if you stop and start a service in the same tab it has the same onion address and password --- onionshare/__init__.py | 6 ++-- onionshare/mode_settings.py | 5 ++- onionshare/onion.py | 53 +++++++++++++++++------------ onionshare/web/web.py | 12 +++---- onionshare_gui/tab/server_status.py | 7 ++-- onionshare_gui/threads.py | 2 +- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 704f0a51..8d2077c2 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -284,7 +284,7 @@ def main(cwd=None): try: common.settings.load() if not mode_settings.get("general", "public"): - web.generate_password(mode_settings.get("persistent", "password")) + web.generate_password(mode_settings.get("onion", "password")) else: web.password = None app = OnionShare(common, onion, local_only, autostop_timer) @@ -385,8 +385,8 @@ def main(cwd=None): # Save the web password if we are using a persistent private key if mode_settings.get("persistent", "enabled"): - if not mode_settings.get("persistent", "password"): - mode_settings.set("persistent", "password", web.password) + if not mode_settings.get("onion", "password"): + mode_settings.set("onion", "password", web.password) # mode_settings.save() # Build the URL diff --git a/onionshare/mode_settings.py b/onionshare/mode_settings.py index 6e875f0d..9201721e 100644 --- a/onionshare/mode_settings.py +++ b/onionshare/mode_settings.py @@ -32,13 +32,12 @@ class ModeSettings: self.common = common self.default_settings = { - "persistent": { - "enabled": False, - "mode": None, + "onion": { "private_key": None, "hidservauth_string": None, "password": None, }, + "persistent": {"mode": None, "enabled": False}, "general": { "public": False, "autostart_timer": False, diff --git a/onionshare/onion.py b/onionshare/onion.py index e368819c..9520f381 100644 --- a/onionshare/onion.py +++ b/onionshare/onion.py @@ -229,7 +229,9 @@ class Onion(object): else: self.tor_data_directory_name = self.common.build_tor_dir() self.common.log( - "Onion", "connect", f"tor_data_directory_name={self.tor_data_directory_name}" + "Onion", + "connect", + f"tor_data_directory_name={self.tor_data_directory_name}", ) # Create the torrc @@ -595,12 +597,10 @@ class Onion(object): else: # If we don't have a scheduled share, but are using persistence, then # we should be able to find a hidservauth_string in saved settings - if mode_settings.get( - "persistent", "hidservauth_string" - ): - auth_cookie = mode_settings.get("persistent", "hidservauth_string").split()[ - 2 - ] + if mode_settings.get("onion", "hidservauth_string"): + auth_cookie = mode_settings.get( + "onion", "hidservauth_string" + ).split()[2] if auth_cookie: basic_auth = {"onionshare": auth_cookie} # If we had neither a scheduled auth cookie or a persistent hidservauth string, @@ -611,8 +611,8 @@ class Onion(object): # Not using client auth at all basic_auth = None - if mode_settings.get("persistent", "private_key"): - key_content = mode_settings.get("persistent", "private_key") + if mode_settings.get("onion", "private_key"): + key_content = mode_settings.get("onion", "private_key") if self.is_v2_key(key_content): key_type = "RSA1024" else: @@ -668,16 +668,15 @@ class Onion(object): # Save the service_id mode_settings.set("general", "service_id", res.service_id) - # Save the private key and hidservauth string if persistence is enabled - if mode_settings.get("persistent", "enabled"): - if not mode_settings.get("persistent", "private_key"): - mode_settings.set("persistent", "private_key", res.private_key) - if mode_settings.get("general", "client_auth") and not mode_settings.get( - "persistent", "hidservauth_string" - ): - auth_cookie = list(res.client_auth.values())[0] - auth_string = f"HidServAuth {onion_host} {auth_cookie}" - mode_settings.set("persistent", "hidservauth_string", auth_string) + # Save the private key and hidservauth string + if not mode_settings.get("onion", "private_key"): + mode_settings.set("onion", "private_key", res.private_key) + if mode_settings.get("general", "client_auth") and not mode_settings.get( + "onion", "hidservauth_string" + ): + auth_cookie = list(res.client_auth.values())[0] + auth_string = f"HidServAuth {onion_host} {auth_cookie}" + mode_settings.set("onion", "hidservauth_string", auth_string) # If we were scheduling a future share, register the private key for later re-use if save_scheduled_key: @@ -750,14 +749,24 @@ class Onion(object): self.tor_proc.terminate() time.sleep(0.2) if self.tor_proc.poll() == None: - self.common.log("Onion", "cleanup", "Tried to terminate tor process but it's still running") + self.common.log( + "Onion", + "cleanup", + "Tried to terminate tor process but it's still running", + ) try: self.tor_proc.kill() time.sleep(0.2) if self.tor_proc.poll() == None: - self.common.log("Onion", "cleanup", "Tried to kill tor process but it's still running") + self.common.log( + "Onion", + "cleanup", + "Tried to kill tor process but it's still running", + ) except: - self.common.log("Onion", "cleanup", "Exception while killing tor process") + self.common.log( + "Onion", "cleanup", "Exception while killing tor process" + ) self.tor_proc = None # Reset other Onion settings diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 8c4373fb..8582e694 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -304,16 +304,14 @@ class Web: """ self.q.put({"type": request_type, "path": path, "data": data}) - def generate_password(self, persistent_password=None): - self.common.log( - "Web", "generate_password", f"persistent_password={persistent_password}" - ) - if persistent_password != None and persistent_password != "": - self.password = persistent_password + def generate_password(self, saved_password=None): + self.common.log("Web", "generate_password", f"saved_password={saved_password}") + if saved_password != None and saved_password != "": + self.password = saved_password self.common.log( "Web", "generate_password", - f'persistent_password sent, so password is: "{self.password}"', + f'saved_password sent, so password is: "{self.password}"', ) else: self.password = self.common.build_password() diff --git a/onionshare_gui/tab/server_status.py b/onionshare_gui/tab/server_status.py index 33e1f37c..5f58877c 100644 --- a/onionshare_gui/tab/server_status.py +++ b/onionshare_gui/tab/server_status.py @@ -207,10 +207,9 @@ class ServerStatus(QtWidgets.QWidget): self.common.settings.load() self.show_url() - if self.settings.get("persistent", "enabled"): - if not self.settings.get("persistent", "password"): - self.settings.set("persistent", "password", self.web.password) - self.settings.save() + if not self.settings.get("onion", "password"): + self.settings.set("onion", "password", self.web.password) + self.settings.save() if self.settings.get("general", "autostop_timer"): self.server_button.setToolTip( diff --git a/onionshare_gui/threads.py b/onionshare_gui/threads.py index bfa4f72d..81d5ac5c 100644 --- a/onionshare_gui/threads.py +++ b/onionshare_gui/threads.py @@ -64,7 +64,7 @@ class OnionThread(QtCore.QThread): if not self.mode.settings.get("general", "public"): if not self.mode.web.password: self.mode.web.generate_password( - self.mode.settings.get("persistent", "password") + self.mode.settings.get("onion", "password") ) try: