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

This commit is contained in:
Micah Lee 2019-12-08 10:13:56 -08:00
parent 19e378dff4
commit 76d109747e
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
6 changed files with 45 additions and 40 deletions

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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()

View File

@ -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(

View File

@ -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: