Merge branch 'mig5-custom_config'

This commit is contained in:
Micah Lee 2017-07-09 14:12:49 -07:00
commit 882ade94c7
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
8 changed files with 39 additions and 20 deletions

View File

@ -44,6 +44,7 @@ def main(cwd=None):
parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open")) parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open"))
parser.add_argument('--stealth', action='store_true', dest='stealth', help=strings._("help_stealth")) parser.add_argument('--stealth', action='store_true', dest='stealth', help=strings._("help_stealth"))
parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug")) parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug"))
parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config'))
parser.add_argument('filename', metavar='filename', nargs='+', help=strings._('help_filename')) parser.add_argument('filename', metavar='filename', nargs='+', help=strings._('help_filename'))
args = parser.parse_args() args = parser.parse_args()
@ -55,6 +56,7 @@ def main(cwd=None):
debug = bool(args.debug) debug = bool(args.debug)
stay_open = bool(args.stay_open) stay_open = bool(args.stay_open)
stealth = bool(args.stealth) stealth = bool(args.stealth)
config = args.config
# Debug mode? # Debug mode?
if debug: if debug:
@ -76,7 +78,7 @@ def main(cwd=None):
# Start the Onion object # Start the Onion object
onion = Onion() onion = Onion()
try: try:
onion.connect() onion.connect(settings=False, config=config)
except (TorTooOld, TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile, TorErrorAuthError, TorErrorProtocolError, BundledTorNotSupported, BundledTorTimeout) as e: except (TorTooOld, TorErrorInvalidSetting, TorErrorAutomatic, TorErrorSocketPort, TorErrorSocketFile, TorErrorMissingPassword, TorErrorUnreadableCookieFile, TorErrorAuthError, TorErrorProtocolError, BundledTorNotSupported, BundledTorTimeout) as e:
sys.exit(e.args[0]) sys.exit(e.args[0])
except KeyboardInterrupt: except KeyboardInterrupt:

View File

@ -148,14 +148,14 @@ class Onion(object):
# Start out not connected to Tor # Start out not connected to Tor
self.connected_to_tor = False self.connected_to_tor = False
def connect(self, settings=False, tor_status_update_func=None): def connect(self, settings=False, config=False, tor_status_update_func=None):
common.log('Onion', 'connect') common.log('Onion', 'connect')
# Either use settings that are passed in, or load them from disk # Either use settings that are passed in, or load them from disk
if settings: if settings:
self.settings = settings self.settings = settings
else: else:
self.settings = Settings() self.settings = Settings(config)
self.settings.load() self.settings.load()
# The Tor controller # The Tor controller

View File

@ -29,11 +29,19 @@ class Settings(object):
which is to attempt to connect automatically using default Tor Browser which is to attempt to connect automatically using default Tor Browser
settings. settings.
""" """
def __init__(self): def __init__(self, config=False):
common.log('Settings', '__init__') common.log('Settings', '__init__')
# Default config
self.filename = self.build_filename() self.filename = self.build_filename()
# If a readable config file was provided, use that instead
if config:
if os.path.isfile(config):
self.filename = config
else:
common.log('Settings', '__init__', 'Supplied config does not exist or is unreadable. Falling back to default location')
# These are the default settings. They will get overwritten when loading from disk # These are the default settings. They will get overwritten when loading from disk
self.default_settings = { self.default_settings = {
'version': common.get_version(), 'version': common.get_version(),
@ -85,6 +93,7 @@ class Settings(object):
# If the settings file exists, load it # If the settings file exists, load it
if os.path.exists(self.filename): if os.path.exists(self.filename):
try: try:
common.log('Settings', 'load', 'Trying to load {}'.format(self.filename))
with open(self.filename, 'r') as f: with open(self.filename, 'r') as f:
self._settings = json.loads(f.read()) self._settings = json.loads(f.read())
self.fill_in_defaults() self.fill_in_defaults()

View File

@ -66,6 +66,7 @@ def main():
parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open")) parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open"))
parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug")) parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug"))
parser.add_argument('--filenames', metavar='filenames', nargs='+', help=strings._('help_filename')) parser.add_argument('--filenames', metavar='filenames', nargs='+', help=strings._('help_filename'))
parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config'))
args = parser.parse_args() args = parser.parse_args()
filenames = args.filenames filenames = args.filenames
@ -73,6 +74,8 @@ def main():
for i in range(len(filenames)): for i in range(len(filenames)):
filenames[i] = os.path.abspath(filenames[i]) filenames[i] = os.path.abspath(filenames[i])
config = args.config
local_only = bool(args.local_only) local_only = bool(args.local_only)
stay_open = bool(args.stay_open) stay_open = bool(args.stay_open)
debug = bool(args.debug) debug = bool(args.debug)
@ -103,7 +106,7 @@ def main():
app = OnionShare(onion, local_only, stay_open) app = OnionShare(onion, local_only, stay_open)
# Launch the gui # Launch the gui
gui = OnionShareGui(onion, qtapp, app, filenames) gui = OnionShareGui(onion, qtapp, app, filenames, config)
# Clean up when app quits # Clean up when app quits
def shutdown(): def shutdown():

View File

@ -43,7 +43,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
starting_server_step3 = QtCore.pyqtSignal() starting_server_step3 = QtCore.pyqtSignal()
starting_server_error = QtCore.pyqtSignal(str) starting_server_error = QtCore.pyqtSignal(str)
def __init__(self, onion, qtapp, app, filenames): def __init__(self, onion, qtapp, app, filenames, config=False):
super(OnionShareGui, self).__init__() super(OnionShareGui, self).__init__()
self._initSystemTray() self._initSystemTray()
@ -58,7 +58,8 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.setWindowIcon(QtGui.QIcon(common.get_resource_path('images/logo.png'))) self.setWindowIcon(QtGui.QIcon(common.get_resource_path('images/logo.png')))
# Load settings # Load settings
self.settings = Settings() self.config = config
self.settings = Settings(self.config)
self.settings.load() self.settings.load()
# File selection # File selection
@ -219,7 +220,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
common.log('OnionShareGui', 'open_settings', 'settings have changed, reloading') common.log('OnionShareGui', 'open_settings', 'settings have changed, reloading')
self.settings.load() self.settings.load()
d = SettingsDialog(self.onion, self.qtapp) d = SettingsDialog(self.onion, self.qtapp, self.config)
d.settings_saved.connect(reload_settings) d.settings_saved.connect(reload_settings)
d.exec_() d.exec_()
@ -351,7 +352,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
def update_available(update_url, installed_version, latest_version): def update_available(update_url, installed_version, latest_version):
Alert(strings._("update_available", True).format(update_url, installed_version, latest_version)) Alert(strings._("update_available", True).format(update_url, installed_version, latest_version))
self.update_thread = UpdateThread(self.onion) self.update_thread = UpdateThread(self.onion, self.config)
self.update_thread.update_available.connect(update_available) self.update_thread.update_available.connect(update_available)
self.update_thread.start() self.update_thread.start()

View File

@ -34,12 +34,13 @@ class SettingsDialog(QtWidgets.QDialog):
""" """
settings_saved = QtCore.pyqtSignal() settings_saved = QtCore.pyqtSignal()
def __init__(self, onion, qtapp): def __init__(self, onion, qtapp, config=False):
super(SettingsDialog, self).__init__() super(SettingsDialog, self).__init__()
common.log('SettingsDialog', '__init__') common.log('SettingsDialog', '__init__')
self.onion = onion self.onion = onion
self.qtapp = qtapp self.qtapp = qtapp
self.config = config
self.setModal(True) self.setModal(True)
self.setWindowTitle(strings._('gui_settings_window_title', True)) self.setWindowTitle(strings._('gui_settings_window_title', True))
@ -259,7 +260,7 @@ class SettingsDialog(QtWidgets.QDialog):
self.cancel_button.setFocus() self.cancel_button.setFocus()
# Load settings, and fill them in # Load settings, and fill them in
self.old_settings = Settings() self.old_settings = Settings(self.config)
self.old_settings.load() self.old_settings.load()
close_after_first_download = self.old_settings.get('close_after_first_download') close_after_first_download = self.old_settings.get('close_after_first_download')
@ -397,7 +398,7 @@ class SettingsDialog(QtWidgets.QDialog):
tor_status_update_func = None tor_status_update_func = None
onion = Onion() onion = Onion()
onion.connect(settings=settings, tor_status_update_func=tor_status_update_func) onion.connect(settings=settings, config=self.config, tor_status_update_func=tor_status_update_func)
# If an exception hasn't been raised yet, the Tor settings work # If an exception hasn't been raised yet, the Tor settings work
Alert(strings._('settings_test_success', True).format(onion.tor_version, onion.supports_ephemeral, onion.supports_stealth)) Alert(strings._('settings_test_success', True).format(onion.tor_version, onion.supports_ephemeral, onion.supports_stealth))
@ -441,7 +442,7 @@ class SettingsDialog(QtWidgets.QDialog):
self._enable_buttons() self._enable_buttons()
# Update the last checked label # Update the last checked label
settings = Settings() settings = Settings(self.config)
settings.load() settings.load()
autoupdate_timestamp = settings.get('autoupdate_timestamp') autoupdate_timestamp = settings.get('autoupdate_timestamp')
self._update_autoupdate_timestamp(autoupdate_timestamp) self._update_autoupdate_timestamp(autoupdate_timestamp)
@ -519,7 +520,7 @@ class SettingsDialog(QtWidgets.QDialog):
Return a Settings object that's full of values from the settings dialog. Return a Settings object that's full of values from the settings dialog.
""" """
common.log('SettingsDialog', 'settings_from_fields') common.log('SettingsDialog', 'settings_from_fields')
settings = Settings() settings = Settings(self.config)
settings.load() # To get the last update timestamp settings.load() # To get the last update timestamp
settings.set('close_after_first_download', self.close_after_first_download_checkbox.isChecked()) settings.set('close_after_first_download', self.close_after_first_download_checkbox.isChecked())

View File

@ -52,15 +52,16 @@ class UpdateChecker(QtCore.QObject):
update_available = QtCore.pyqtSignal(str, str, str) update_available = QtCore.pyqtSignal(str, str, str)
update_not_available = QtCore.pyqtSignal() update_not_available = QtCore.pyqtSignal()
def __init__(self, onion): def __init__(self, onion, config=False):
super(UpdateChecker, self).__init__() super(UpdateChecker, self).__init__()
common.log('UpdateChecker', '__init__') common.log('UpdateChecker', '__init__')
self.onion = onion self.onion = onion
self.config = config
def check(self, force=False): def check(self, force=False, config=False):
common.log('UpdateChecker', 'check', 'force={}'.format(force)) common.log('UpdateChecker', 'check', 'force={}'.format(force))
# Load the settings # Load the settings
settings = Settings() settings = Settings(config)
settings.load() settings.load()
# If force=True, then definitely check # If force=True, then definitely check
@ -146,20 +147,21 @@ class UpdateThread(QtCore.QThread):
update_available = QtCore.pyqtSignal(str, str, str) update_available = QtCore.pyqtSignal(str, str, str)
update_not_available = QtCore.pyqtSignal() update_not_available = QtCore.pyqtSignal()
def __init__(self, onion): def __init__(self, onion, config=False):
super(UpdateThread, self).__init__() super(UpdateThread, self).__init__()
common.log('UpdateThread', '__init__') common.log('UpdateThread', '__init__')
self.onion = onion self.onion = onion
self.config = config
def run(self): def run(self):
common.log('UpdateThread', 'run') common.log('UpdateThread', 'run')
u = UpdateChecker(self.onion) u = UpdateChecker(self.onion, self.config)
u.update_available.connect(self._update_available) u.update_available.connect(self._update_available)
u.update_not_available.connect(self._update_not_available) u.update_not_available.connect(self._update_not_available)
try: try:
u.check() u.check(config=self.config)
except Exception as e: except Exception as e:
# If update check fails, silently ignore # If update check fails, silently ignore
common.log('UpdateThread', 'run', '{}'.format(e)) common.log('UpdateThread', 'run', '{}'.format(e))

View File

@ -29,6 +29,7 @@
"help_stealth": "Create stealth onion service (advanced)", "help_stealth": "Create stealth onion service (advanced)",
"help_debug": "Log application errors to stdout, and log web errors to disk", "help_debug": "Log application errors to stdout, and log web errors to disk",
"help_filename": "List of files or folders to share", "help_filename": "List of files or folders to share",
"help_config": "Path to a custom JSON config file (optional)",
"gui_drag_and_drop": "Drag and drop\nfiles here", "gui_drag_and_drop": "Drag and drop\nfiles here",
"gui_add": "Add", "gui_add": "Add",
"gui_delete": "Delete", "gui_delete": "Delete",