2016-12-28 22:52:21 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2018-04-24 13:07:59 -04:00
|
|
|
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
2016-12-28 22:52:21 -05:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2017-07-10 22:13:30 -04:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import platform
|
2018-09-30 17:45:21 -04:00
|
|
|
import locale
|
2016-12-28 22:52:21 -05:00
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
from . import strings
|
2016-12-28 22:52:21 -05:00
|
|
|
|
2017-07-10 22:13:30 -04:00
|
|
|
|
2016-12-28 22:52:21 -05:00
|
|
|
class Settings(object):
|
|
|
|
"""
|
|
|
|
This class stores all of the settings for OnionShare, specifically for how
|
|
|
|
to connect to Tor. If it can't find the settings file, it uses the default,
|
|
|
|
which is to attempt to connect automatically using default Tor Browser
|
|
|
|
settings.
|
|
|
|
"""
|
2018-03-08 13:18:31 -05:00
|
|
|
def __init__(self, common, config=False):
|
|
|
|
self.common = common
|
|
|
|
|
|
|
|
self.common.log('Settings', '__init__')
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2017-06-01 03:35:27 -04:00
|
|
|
# Default config
|
2016-12-28 22:52:21 -05:00
|
|
|
self.filename = self.build_filename()
|
2016-12-29 11:02:32 -05:00
|
|
|
|
2017-06-01 03:35:27 -04:00
|
|
|
# If a readable config file was provided, use that instead
|
|
|
|
if config:
|
|
|
|
if os.path.isfile(config):
|
|
|
|
self.filename = config
|
|
|
|
else:
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.log('Settings', '__init__', 'Supplied config does not exist or is unreadable. Falling back to default location')
|
2017-06-01 03:35:27 -04:00
|
|
|
|
2018-09-30 17:45:21 -04:00
|
|
|
# Available languages in this version of OnionShare
|
|
|
|
self.available_locales = [
|
|
|
|
'cs', 'da', 'de', 'en', 'eo', 'es', 'fi',
|
|
|
|
'fr', 'it', 'nl', 'no', 'pt', 'ru', 'tr'
|
|
|
|
]
|
|
|
|
|
2016-12-29 11:02:32 -05:00
|
|
|
# These are the default settings. They will get overwritten when loading from disk
|
2017-04-08 16:42:07 -04:00
|
|
|
self.default_settings = {
|
2018-03-08 13:18:31 -05:00
|
|
|
'version': self.common.version,
|
2017-04-14 02:20:24 -04:00
|
|
|
'connection_type': 'bundled',
|
2016-12-29 11:02:32 -05:00
|
|
|
'control_port_address': '127.0.0.1',
|
2016-12-29 12:58:13 -05:00
|
|
|
'control_port_port': 9051,
|
2017-04-15 19:33:41 -04:00
|
|
|
'socks_address': '127.0.0.1',
|
|
|
|
'socks_port': 9050,
|
2016-12-29 11:02:32 -05:00
|
|
|
'socket_file_path': '/var/run/tor/control',
|
|
|
|
'auth_type': 'no_auth',
|
2017-04-08 16:42:07 -04:00
|
|
|
'auth_password': '',
|
|
|
|
'close_after_first_download': True,
|
2018-02-07 12:55:55 -05:00
|
|
|
'shutdown_timeout': False,
|
2017-04-15 18:24:08 -04:00
|
|
|
'use_stealth': False,
|
|
|
|
'use_autoupdate': True,
|
2017-12-10 22:53:13 -05:00
|
|
|
'autoupdate_timestamp': None,
|
|
|
|
'no_bridges': True,
|
|
|
|
'tor_bridges_use_obfs4': False,
|
2018-02-15 18:19:53 -05:00
|
|
|
'tor_bridges_use_meek_lite_azure': False,
|
2018-01-15 23:30:36 -05:00
|
|
|
'tor_bridges_use_custom_bridges': '',
|
2018-08-21 21:45:08 -04:00
|
|
|
'use_legacy_v2_onions': False,
|
2018-01-13 04:58:24 -05:00
|
|
|
'save_private_key': False,
|
|
|
|
'private_key': '',
|
2018-07-21 03:06:11 -04:00
|
|
|
'public_mode': False,
|
2018-01-14 18:01:34 -05:00
|
|
|
'slug': '',
|
2018-03-05 10:52:51 -05:00
|
|
|
'hidservauth_string': '',
|
2018-04-29 00:08:53 -04:00
|
|
|
'downloads_dir': self.build_default_downloads_dir(),
|
2018-09-30 17:45:21 -04:00
|
|
|
'receive_allow_receiver_shutdown': True,
|
|
|
|
'locale': None # this gets defined in fill_in_defaults()
|
2016-12-29 11:02:32 -05:00
|
|
|
}
|
2017-04-08 16:42:07 -04:00
|
|
|
self._settings = {}
|
|
|
|
self.fill_in_defaults()
|
|
|
|
|
|
|
|
def fill_in_defaults(self):
|
|
|
|
"""
|
|
|
|
If there are any missing settings from self._settings, replace them with
|
|
|
|
their default values.
|
|
|
|
"""
|
|
|
|
for key in self.default_settings:
|
|
|
|
if key not in self._settings:
|
|
|
|
self._settings[key] = self.default_settings[key]
|
2016-12-28 22:52:21 -05:00
|
|
|
|
2018-09-30 17:45:21 -04:00
|
|
|
# Choose the default locale based on the OS preference, and fall-back to English
|
|
|
|
if self._settings['locale'] is None:
|
|
|
|
default_locale = locale.getdefaultlocale()[0][:2]
|
|
|
|
if default_locale not in self.available_locales:
|
|
|
|
default_locale = 'en'
|
|
|
|
self._settings['locale'] = default_locale
|
|
|
|
|
2016-12-28 22:52:21 -05:00
|
|
|
def build_filename(self):
|
|
|
|
"""
|
|
|
|
Returns the path of the settings file.
|
|
|
|
"""
|
|
|
|
p = platform.system()
|
|
|
|
if p == 'Windows':
|
|
|
|
appdata = os.environ['APPDATA']
|
|
|
|
return '{}\\OnionShare\\onionshare.json'.format(appdata)
|
|
|
|
elif p == 'Darwin':
|
|
|
|
return os.path.expanduser('~/Library/Application Support/OnionShare/onionshare.json')
|
|
|
|
else:
|
|
|
|
return os.path.expanduser('~/.config/onionshare/onionshare.json')
|
|
|
|
|
2018-03-05 10:52:51 -05:00
|
|
|
def build_default_downloads_dir(self):
|
|
|
|
"""
|
|
|
|
Returns the path of the default Downloads directory for receive mode.
|
|
|
|
"""
|
|
|
|
# TODO: Test in Windows, though it looks like it should work
|
|
|
|
# https://docs.python.org/3/library/os.path.html#os.path.expanduser
|
2018-03-06 08:25:49 -05:00
|
|
|
return os.path.expanduser('~/OnionShare')
|
2018-03-05 10:52:51 -05:00
|
|
|
|
2016-12-28 22:52:21 -05:00
|
|
|
def load(self):
|
|
|
|
"""
|
|
|
|
Load the settings from file.
|
|
|
|
"""
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.log('Settings', 'load')
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2016-12-29 11:02:32 -05:00
|
|
|
# If the settings file exists, load it
|
2016-12-28 22:52:21 -05:00
|
|
|
if os.path.exists(self.filename):
|
|
|
|
try:
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.log('Settings', 'load', 'Trying to load {}'.format(self.filename))
|
2017-05-23 07:22:14 -04:00
|
|
|
with open(self.filename, 'r') as f:
|
2017-07-10 22:13:30 -04:00
|
|
|
self._settings = json.load(f)
|
2017-05-23 18:16:27 -04:00
|
|
|
self.fill_in_defaults()
|
2016-12-28 22:52:21 -05:00
|
|
|
except:
|
2016-12-29 11:02:32 -05:00
|
|
|
pass
|
2016-12-28 22:52:21 -05:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Save settings to file.
|
|
|
|
"""
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common.log('Settings', 'save')
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2016-12-28 23:03:32 -05:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(self.filename))
|
|
|
|
except:
|
|
|
|
pass
|
2016-12-28 22:52:21 -05:00
|
|
|
open(self.filename, 'w').write(json.dumps(self._settings))
|
2016-12-28 23:03:32 -05:00
|
|
|
print(strings._('settings_saved').format(self.filename))
|
2016-12-28 22:52:21 -05:00
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
return self._settings[key]
|
|
|
|
|
|
|
|
def set(self, key, val):
|
2017-04-15 19:33:41 -04:00
|
|
|
# If typecasting int values fails, fallback to default values
|
|
|
|
if key == 'control_port_port' or key == 'socks_port':
|
|
|
|
try:
|
|
|
|
val = int(val)
|
|
|
|
except:
|
|
|
|
if key == 'control_port_port':
|
|
|
|
val = self.default_settings['control_port_port']
|
|
|
|
elif key == 'socks_port':
|
|
|
|
val = self.default_settings['socks_port']
|
|
|
|
|
2016-12-28 22:52:21 -05:00
|
|
|
self._settings[key] = val
|