onionshare/cli/onionshare_cli/settings.py

210 lines
7.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
2022-01-16 19:15:49 -05:00
Copyright (C) 2014-2022 Micah Lee, et al. <micah@micahflee.com>
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 locale
2018-12-19 14:45:31 -05:00
2017-07-10 22:13:30 -04: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.
"""
2019-10-13 00:01:25 -04:00
def __init__(self, common, config=False):
self.common = common
2019-10-13 00:01:25 -04:00
self.common.log("Settings", "__init__")
# If a readable config file was provided, use that instead
if config:
if os.path.isfile(config):
self.filename = config
else:
2019-10-13 00:01:25 -04:00
self.common.log(
"Settings",
"__init__",
"Supplied config does not exist or is unreadable. Falling back to default location",
)
2019-04-20 00:43:04 -04:00
self.filename = self.build_filename()
else:
# Default config
self.filename = self.build_filename()
# Dictionary of available languages in this version of OnionShare,
# mapped to the language name, in that language
self.available_locales = {
2023-09-05 11:52:46 -04:00
"af": "Afrikaans", # Afrikaans
"sq": "Shqip", # Albanian
"ar": "العربية", # Arabic
"be": "Беларуская", # Belarusian
"bn": "বাংলা", # Bengali
"bg": "Български", #Bulgarian
2023-09-05 11:52:46 -04:00
"ca": "Català", # Catalan
"zh_Hant": "正體中文 (繁體)", # Traditional Chinese
2019-10-13 00:01:25 -04:00
"zh_Hans": "中文 (简体)", # Simplified Chinese
2022-10-09 18:01:55 -04:00
"hr": "Hrvatski", # Croatian
"cs": "čeština", # Czech
# "da": "Dansk", # Danish
# "nl": "Nederlands", # Dutch
2019-10-13 00:01:25 -04:00
"en": "English", # English
2023-09-05 11:52:46 -04:00
"fi": "Suomi", # Finnish
"fr": "Français", # French
"gl": "Galego", # Galician
2019-10-13 00:01:25 -04:00
"de": "Deutsch", # German
2022-01-17 13:33:13 -05:00
"el": "Ελληνικά", # Greek
2019-10-13 00:01:25 -04:00
"is": "Íslenska", # Icelandic
# "id": "Bahasa Indonesia", # Indonesian
2021-02-15 18:49:54 -05:00
# "ga": "Gaeilge", # Irish
2023-09-05 11:52:46 -04:00
"it": "Italiano", # Italian
2022-01-17 13:33:13 -05:00
"ja": "日本語", # Japanese
"km": "ខ្មែរ", # Khmer(Central)
# "ckb": "Soranî", # Kurdish (Central)
2023-09-05 11:52:46 -04:00
"lt": "Lietuvių Kalba", # Lithuanian
"nb_NO": "Norsk Bokmål", # Norwegian Bokmål
"fa": "فارسی", # Persian
"pl": "Polski", # Polish
"pt_BR": "Português (Brasil)", # Portuguese Brazil
"pt_PT": "Português (Portugal)", # Portuguese Portugal
2021-02-15 18:49:54 -05:00
# "ro": "Română", # Romanian
2023-09-05 11:52:46 -04:00
"ru": "Русский", # Russian
"sn": "chiShona", # Shona
# "sr_Latn": "Srpska (latinica)", # Serbian (latin)
# "sk": "Slovenčina", # Slovak
"es": "Español", # Spanish
2023-09-05 11:52:46 -04:00
"sw": "Kiswahili", # Swahili
"sv": "Svenska", # Swedish
2021-02-15 18:49:54 -05:00
# "te": "తెలుగు", # Telugu
2019-10-13 00:01:25 -04:00
"tr": "Türkçe", # Turkish
"uk": "Українська", # Ukrainian
2023-09-05 11:52:46 -04:00
"vi": "Tiếng Việt", # Vietnamese
}
# These are the default settings. They will get overwritten when loading from disk
self.default_settings = {
2019-10-13 00:01:25 -04:00
"version": self.common.version,
"connection_type": "bundled",
"control_port_address": "127.0.0.1",
"control_port_port": 9051,
"socks_address": "127.0.0.1",
"socks_port": 9050,
"socket_file_path": "/var/run/tor/control",
"auth_type": "no_auth",
"auth_password": "",
"auto_connect": False,
2019-10-13 00:01:25 -04:00
"use_autoupdate": True,
"autoupdate_timestamp": None,
"bridges_enabled": False,
"bridges_type": "built-in", # "built-in", "moat", or "custom"
"bridges_builtin_pt": "obfs4", # "obfs4", "meek-azure", or "snowflake"
"bridges_moat": "",
"bridges_custom": "",
"bridges_builtin": {},
"persistent_tabs": [],
2019-10-13 00:01:25 -04:00
"locale": None, # this gets defined in fill_in_defaults()
2021-08-20 16:24:15 -04:00
"theme": 0,
}
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]
# Choose the default locale based on the OS preference, and fall-back to English
2019-10-13 00:01:25 -04:00
if self._settings["locale"] is None:
language_code, encoding = locale.getdefaultlocale()
# Default to English
if not language_code:
2019-10-13 00:01:25 -04:00
language_code = "en_US"
2019-10-13 00:01:25 -04:00
if language_code == "pt_PT" and language_code == "pt_BR":
# Portuguese locales include country code
default_locale = language_code
else:
# All other locales cut off the country code
default_locale = language_code[:2]
if default_locale not in self.available_locales:
2019-10-13 00:01:25 -04:00
default_locale = "en"
self._settings["locale"] = default_locale
def build_filename(self):
"""
Returns the path of the settings file.
"""
2019-10-13 00:01:25 -04:00
return os.path.join(self.common.build_data_dir(), "onionshare.json")
def load(self):
"""
Load the settings from file.
"""
2019-10-13 00:01:25 -04:00
self.common.log("Settings", "load")
# If the settings file exists, load it
if os.path.exists(self.filename):
try:
self.common.log("Settings", "load", f"Trying to load {self.filename}")
2019-10-13 00:01:25 -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()
2021-04-29 20:13:05 -04:00
except Exception:
pass
# Make sure data_dir exists
try:
2019-10-13 00:01:25 -04:00
os.makedirs(self.get("data_dir"), exist_ok=True)
2021-04-29 20:13:05 -04:00
except Exception:
pass
def save(self):
"""
Save settings to file.
"""
2019-10-13 00:01:25 -04:00
self.common.log("Settings", "save")
open(self.filename, "w").write(json.dumps(self._settings, indent=2))
self.common.log("Settings", "save", f"Settings saved in {self.filename}")
def get(self, key):
return self._settings[key]
def set(self, key, val):
# If typecasting int values fails, fallback to default values
2019-10-13 00:01:25 -04:00
if key == "control_port_port" or key == "socks_port":
try:
val = int(val)
2021-04-29 20:13:05 -04:00
except Exception:
2019-10-13 00:01:25 -04:00
if key == "control_port_port":
val = self.default_settings["control_port_port"]
elif key == "socks_port":
val = self.default_settings["socks_port"]
self._settings[key] = val