2016-12-28 22:52:21 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2017-01-06 21:58:15 -05:00
|
|
|
Copyright (C) 2017 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/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import platform, os, json
|
|
|
|
|
2017-05-16 14:05:48 -04:00
|
|
|
from . import strings, common
|
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.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2017-05-16 14:23:18 -04:00
|
|
|
common.log('Settings', '__init__')
|
|
|
|
|
2016-12-28 22:52:21 -05:00
|
|
|
self.filename = self.build_filename()
|
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 = {
|
2017-05-16 14:05:48 -04:00
|
|
|
'version': common.get_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,
|
2017-05-22 02:47:12 -04:00
|
|
|
'systray_notifications': True,
|
2017-04-15 18:24:08 -04:00
|
|
|
'use_stealth': False,
|
|
|
|
'use_autoupdate': True,
|
|
|
|
'autoupdate_timestamp': None
|
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
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
"""
|
|
|
|
Load the settings from file.
|
|
|
|
"""
|
2017-05-16 14:23:18 -04:00
|
|
|
common.log('Settings', 'load')
|
|
|
|
|
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:
|
|
|
|
self._settings = json.loads(open(self.filename, 'r').read())
|
2017-04-08 16:42:07 -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.
|
|
|
|
"""
|
2017-05-16 14:23:18 -04:00
|
|
|
common.log('Settings', 'save')
|
|
|
|
|
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
|