diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 99beb0e0..a3474da9 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -23,7 +23,7 @@ import os, sys, time, argparse, threading from . import strings, common, web from .onion import * from .onionshare import OnionShare - +from .settings import Settings def main(cwd=None): """ @@ -77,6 +77,10 @@ def main(cwd=None): if not valid: sys.exit() + + settings = Settings(config) + settings.load() + # Start the Onion object onion = Onion() try: @@ -108,7 +112,7 @@ def main(cwd=None): print('') # Start OnionShare http service in new thread - t = threading.Thread(target=web.start, args=(app.port, app.stay_open)) + t = threading.Thread(target=web.start, args=(app.port, app.stay_open, settings.get('slug'))) t.daemon = True t.start() @@ -120,6 +124,12 @@ def main(cwd=None): if app.shutdown_timeout > 0: app.shutdown_timer.start() + # Save the web slug if we are using a persistent private key + if settings.get('save_private_key'): + if not settings.get('slug'): + settings.set('slug', web.slug) + settings.save() + if(stealth): print(strings._("give_this_url_stealth")) print('http://{0:s}/{1:s}'.format(app.onion_host, web.slug)) diff --git a/onionshare/settings.py b/onionshare/settings.py index e87638f1..e8103757 100644 --- a/onionshare/settings.py +++ b/onionshare/settings.py @@ -63,6 +63,7 @@ class Settings(object): 'autoupdate_timestamp': None, 'save_private_key': False, 'private_key': '', + 'slug': '', 'hidservauth_string': '' } self._settings = {} diff --git a/onionshare/web.py b/onionshare/web.py index 03da8fd7..3eef67c7 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -128,9 +128,12 @@ def add_request(request_type, path, data=None): slug = None -def generate_slug(): +def generate_slug(persistent_slug=''): global slug - slug = common.build_slug() + if persistent_slug: + slug = persistent_slug + else: + slug = common.build_slug() download_count = 0 error404_count = 0 @@ -383,11 +386,11 @@ def force_shutdown(): func() -def start(port, stay_open=False): +def start(port, stay_open=False, persistent_slug=''): """ Start the flask web server. """ - generate_slug() + generate_slug(persistent_slug) set_stay_open(stay_open) diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 452e58db..582ebdb3 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -69,7 +69,7 @@ class OnionShareGui(QtWidgets.QMainWindow): self.file_selection.file_list.add_file(filename) # Server status - self.server_status = ServerStatus(self.qtapp, self.app, web, self.file_selection) + self.server_status = ServerStatus(self.qtapp, self.app, web, self.file_selection, self.settings) self.server_status.server_started.connect(self.file_selection.server_started) self.server_status.server_started.connect(self.start_server) self.server_status.server_stopped.connect(self.file_selection.server_stopped) @@ -278,7 +278,7 @@ class OnionShareGui(QtWidgets.QMainWindow): self.app.stay_open = not self.settings.get('close_after_first_download') # start onionshare http service in new thread - t = threading.Thread(target=web.start, args=(self.app.port, self.app.stay_open)) + t = threading.Thread(target=web.start, args=(self.app.port, self.app.stay_open, self.settings.get('slug'))) t.daemon = True t.start() # wait for modules in thread to load, preventing a thread-related cx_Freeze crash diff --git a/onionshare_gui/server_status.py b/onionshare_gui/server_status.py index 48bffdca..442ae440 100644 --- a/onionshare_gui/server_status.py +++ b/onionshare_gui/server_status.py @@ -21,7 +21,7 @@ import platform from .alert import Alert from PyQt5 import QtCore, QtWidgets, QtGui -from onionshare import strings, common +from onionshare import strings, common, settings class ServerStatus(QtWidgets.QVBoxLayout): """ @@ -36,7 +36,7 @@ class ServerStatus(QtWidgets.QVBoxLayout): STATUS_WORKING = 1 STATUS_STARTED = 2 - def __init__(self, qtapp, app, web, file_selection): + def __init__(self, qtapp, app, web, file_selection, settings): super(ServerStatus, self).__init__() self.status = self.STATUS_STOPPED @@ -45,6 +45,8 @@ class ServerStatus(QtWidgets.QVBoxLayout): self.web = web self.file_selection = file_selection + self.settings = settings + # Helper boolean as this is used in a few places self.timer_enabled = False # Shutdown timeout layout @@ -141,6 +143,11 @@ class ServerStatus(QtWidgets.QVBoxLayout): self.url_label.show() self.copy_url_button.show() + if self.settings.get('save_private_key'): + if not self.settings.get('slug'): + self.settings.set('slug', self.web.slug) + self.settings.save() + if self.app.stealth: self.copy_hidservauth_button.show() else: diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index a978c403..ca2cc4c7 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -570,10 +570,12 @@ class SettingsDialog(QtWidgets.QDialog): if self.save_private_key_checkbox.isChecked(): settings.set('save_private_key', True) settings.set('private_key', self.old_settings.get('private_key')) + settings.set('slug', self.old_settings.get('slug')) settings.set('hidservauth_string', self.old_settings.get('hidservauth_string')) else: settings.set('save_private_key', False) settings.set('private_key', '') + settings.set('slug', '') # Also unset the HidServAuth if we are removing our reusable private key settings.set('hidservauth_string', '') settings.set('use_stealth', self.stealth_checkbox.isChecked()) diff --git a/test/test_onionshare_settings.py b/test/test_onionshare_settings.py index 0e03080f..ba45ef5e 100644 --- a/test/test_onionshare_settings.py +++ b/test/test_onionshare_settings.py @@ -60,6 +60,7 @@ class TestSettings: 'autoupdate_timestamp': None, 'save_private_key': False, 'private_key': '', + 'slug': '', 'hidservauth_string': '' }