Refactor Settings object so it does not load from file by default. Make it so you can pass a Settings into Onion, to test settings

This commit is contained in:
Micah Lee 2016-12-29 08:02:32 -08:00
parent 9462b7d05f
commit 5bfa4da648
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 66 additions and 52 deletions

View File

@ -57,11 +57,16 @@ class Onion(object):
onion services are supported. If not, it falls back to modifying the onion services are supported. If not, it falls back to modifying the
Tor configuration. 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.transparent_torification = transparent_torification
self.stealth = stealth 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 # files and dirs to delete on shutdown
self.cleanup_filenames = [] self.cleanup_filenames = []

View File

@ -31,7 +31,18 @@ class Settings(object):
""" """
def __init__(self): def __init__(self):
self.filename = self.build_filename() 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): def build_filename(self):
""" """
@ -50,28 +61,12 @@ class Settings(object):
""" """
Load the settings from file. Load the settings from file.
""" """
default_settings = { # If the settings file exists, load it
'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 os.path.exists(self.filename): if os.path.exists(self.filename):
# If the settings file exists, load it
try: try:
self._settings = json.loads(open(self.filename, 'r').read()) self._settings = json.loads(open(self.filename, 'r').read())
except: except:
# If the settings don't work, use default ones instead pass
self._settings = default_settings
else:
# Otherwise, use default settings
self._settings = default_settings
def save(self): def save(self):
""" """

View File

@ -21,6 +21,7 @@ from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings from onionshare import strings
from onionshare.settings import Settings from onionshare.settings import Settings
from onionshare.onion import Onion
class SettingsDialog(QtWidgets.QDialog): class SettingsDialog(QtWidgets.QDialog):
""" """
@ -146,26 +147,28 @@ class SettingsDialog(QtWidgets.QDialog):
# Load settings, and fill them in # Load settings, and fill them in
self.settings = Settings() settings = Settings()
connection_type = self.settings.get('connection_type') settings.load()
connection_type = settings.get('connection_type')
if connection_type == 'automatic': if connection_type == 'automatic':
self.connection_type_automatic_radio.setChecked(True) self.connection_type_automatic_radio.setChecked(True)
elif connection_type == 'control_port': elif connection_type == 'control_port':
self.connection_type_control_port_radio.setChecked(True) self.connection_type_control_port_radio.setChecked(True)
elif connection_type == 'socket_file': elif connection_type == 'socket_file':
self.connection_type_socket_file_radio.setChecked(True) 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_address.setText(settings.get('control_port_address'))
self.connection_type_control_port_extras_port.setText(self.settings.get('control_port_port')) self.connection_type_control_port_extras_port.setText(settings.get('control_port_port'))
self.connection_type_socket_file_extras_path.setText(self.settings.get('socket_file_path')) self.connection_type_socket_file_extras_path.setText(settings.get('socket_file_path'))
auth_type = self.settings.get('auth_type') auth_type = settings.get('auth_type')
if auth_type == 'no_auth': if auth_type == 'no_auth':
self.authenticate_no_auth_radio.setChecked(True) self.authenticate_no_auth_radio.setChecked(True)
elif auth_type == 'password': elif auth_type == 'password':
self.authenticate_password_radio.setChecked(True) self.authenticate_password_radio.setChecked(True)
elif auth_type == 'cookie': elif auth_type == 'cookie':
self.authenticate_cookie_radio.setChecked(True) self.authenticate_cookie_radio.setChecked(True)
self.authenticate_password_extras_password.setText(self.settings.get('auth_password')) self.authenticate_password_extras_password.setText(settings.get('auth_password'))
self.authenticate_cookie_extras_cookie_path.setText(self.settings.get('auth_cookie_path')) self.authenticate_cookie_extras_cookie_path.setText(settings.get('auth_cookie_path'))
# Show the dialog # Show the dialog
self.exec_() self.exec_()
@ -232,34 +235,16 @@ class SettingsDialog(QtWidgets.QDialog):
Test Settings button clicked. With the given settings, see if we can Test Settings button clicked. With the given settings, see if we can
successfully connect and authenticate to Tor. successfully connect and authenticate to Tor.
""" """
pass print("Testing settings")
settings = self.settings_from_fields()
onion = Onion(settings=settings)
def save_clicked(self): def save_clicked(self):
""" """
Save button clicked. Save current settings to disk. Save button clicked. Save current settings to disk.
""" """
if self.connection_type_automatic_radio.isChecked(): settings = self.settings_from_fields()
self.settings.set('connection_type', 'automatic') settings.save()
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()
self.close() self.close()
def cancel_clicked(self): def cancel_clicked(self):
@ -267,3 +252,32 @@ class SettingsDialog(QtWidgets.QDialog):
Cancel button clicked. Cancel button clicked.
""" """
self.close() 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