diff --git a/onionshare/onion.py b/onionshare/onion.py index 90881513..22a95c19 100644 --- a/onionshare/onion.py +++ b/onionshare/onion.py @@ -57,11 +57,16 @@ class Onion(object): onion services are supported. If not, it falls back to modifying the Tor configuration. """ - def __init__(self, transparent_torification=False, stealth=False): + def __init__(self, transparent_torification=False, stealth=False, settings=False): self.transparent_torification = transparent_torification self.stealth = stealth - self.settings = Settings() + # Either use settings that are passed in, or load them from disk + if settings: + self.settings = settings + else: + self.settings = Settings() + self.settings.load() # files and dirs to delete on shutdown self.cleanup_filenames = [] diff --git a/onionshare/settings.py b/onionshare/settings.py index 6ba2779d..1fa743b3 100644 --- a/onionshare/settings.py +++ b/onionshare/settings.py @@ -31,7 +31,18 @@ class Settings(object): """ def __init__(self): self.filename = self.build_filename() - self.load() + + # These are the default settings. They will get overwritten when loading from disk + self._settings = { + 'version': helpers.get_version(), + 'connection_type': 'automatic', + 'control_port_address': '127.0.0.1', + 'control_port_port': '9051', + 'socket_file_path': '/var/run/tor/control', + 'auth_type': 'no_auth', + 'auth_password': '', + 'auth_cookie_path': '/var/run/tor/control.authcookie' + } def build_filename(self): """ @@ -50,28 +61,12 @@ class Settings(object): """ Load the settings from file. """ - default_settings = { - 'version': helpers.get_version(), - 'connection_type': 'automatic', - 'control_port_address': '127.0.0.1', - 'control_port_port': '9051', - 'socket_file_path': '/var/run/tor/control', - 'auth_type': 'no_auth', - 'auth_password': '', - 'auth_cookie_path': '/var/run/tor/control.authcookie' - } - + # If the settings file exists, load it if os.path.exists(self.filename): - # If the settings file exists, load it try: self._settings = json.loads(open(self.filename, 'r').read()) except: - # If the settings don't work, use default ones instead - self._settings = default_settings - - else: - # Otherwise, use default settings - self._settings = default_settings + pass def save(self): """ diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index 1f1120e3..13db7270 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -21,6 +21,7 @@ from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings from onionshare.settings import Settings +from onionshare.onion import Onion class SettingsDialog(QtWidgets.QDialog): """ @@ -146,26 +147,28 @@ class SettingsDialog(QtWidgets.QDialog): # Load settings, and fill them in - self.settings = Settings() - connection_type = self.settings.get('connection_type') + settings = Settings() + settings.load() + + connection_type = settings.get('connection_type') if connection_type == 'automatic': self.connection_type_automatic_radio.setChecked(True) elif connection_type == 'control_port': self.connection_type_control_port_radio.setChecked(True) elif connection_type == 'socket_file': self.connection_type_socket_file_radio.setChecked(True) - self.connection_type_control_port_extras_address.setText(self.settings.get('control_port_address')) - self.connection_type_control_port_extras_port.setText(self.settings.get('control_port_port')) - self.connection_type_socket_file_extras_path.setText(self.settings.get('socket_file_path')) - auth_type = self.settings.get('auth_type') + self.connection_type_control_port_extras_address.setText(settings.get('control_port_address')) + self.connection_type_control_port_extras_port.setText(settings.get('control_port_port')) + self.connection_type_socket_file_extras_path.setText(settings.get('socket_file_path')) + auth_type = settings.get('auth_type') if auth_type == 'no_auth': self.authenticate_no_auth_radio.setChecked(True) elif auth_type == 'password': self.authenticate_password_radio.setChecked(True) elif auth_type == 'cookie': self.authenticate_cookie_radio.setChecked(True) - self.authenticate_password_extras_password.setText(self.settings.get('auth_password')) - self.authenticate_cookie_extras_cookie_path.setText(self.settings.get('auth_cookie_path')) + self.authenticate_password_extras_password.setText(settings.get('auth_password')) + self.authenticate_cookie_extras_cookie_path.setText(settings.get('auth_cookie_path')) # Show the dialog self.exec_() @@ -232,34 +235,16 @@ class SettingsDialog(QtWidgets.QDialog): Test Settings button clicked. With the given settings, see if we can successfully connect and authenticate to Tor. """ - pass + print("Testing settings") + settings = self.settings_from_fields() + onion = Onion(settings=settings) def save_clicked(self): """ Save button clicked. Save current settings to disk. """ - if self.connection_type_automatic_radio.isChecked(): - self.settings.set('connection_type', 'automatic') - if self.connection_type_control_port_radio.isChecked(): - self.settings.set('connection_type', 'control_port') - if self.connection_type_socket_file_radio.isChecked(): - self.settings.set('connection_type', 'socket_file') - - self.settings.set('control_port_address', self.connection_type_control_port_extras_address.text()) - self.settings.set('control_port_port', self.connection_type_control_port_extras_port.text()) - self.settings.set('socket_file_path', self.connection_type_socket_file_extras_path.text()) - - if self.authenticate_no_auth_radio.isChecked(): - self.settings.set('auth_type', 'no_auth') - if self.authenticate_password_radio.isChecked(): - self.settings.set('auth_type', 'password') - if self.authenticate_cookie_radio.isChecked(): - self.settings.set('auth_type', 'cookie') - - self.settings.set('auth_password', self.authenticate_password_extras_password.text()) - self.settings.set('auth_cookie_path', self.authenticate_cookie_extras_cookie_path.text()) - - self.settings.save() + settings = self.settings_from_fields() + settings.save() self.close() def cancel_clicked(self): @@ -267,3 +252,32 @@ class SettingsDialog(QtWidgets.QDialog): Cancel button clicked. """ self.close() + + def settings_from_fields(self): + """ + Return a Settings object that's full of values from the settings dialog. + """ + settings = Settings() + + if self.connection_type_automatic_radio.isChecked(): + settings.set('connection_type', 'automatic') + if self.connection_type_control_port_radio.isChecked(): + settings.set('connection_type', 'control_port') + if self.connection_type_socket_file_radio.isChecked(): + settings.set('connection_type', 'socket_file') + + settings.set('control_port_address', self.connection_type_control_port_extras_address.text()) + settings.set('control_port_port', self.connection_type_control_port_extras_port.text()) + settings.set('socket_file_path', self.connection_type_socket_file_extras_path.text()) + + if self.authenticate_no_auth_radio.isChecked(): + settings.set('auth_type', 'no_auth') + if self.authenticate_password_radio.isChecked(): + settings.set('auth_type', 'password') + if self.authenticate_cookie_radio.isChecked(): + settings.set('auth_type', 'cookie') + + settings.set('auth_password', self.authenticate_password_extras_password.text()) + settings.set('auth_cookie_path', self.authenticate_cookie_extras_cookie_path.text()) + + return settings