settings_defaults.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Implementation of the default settings.
  4. """
  5. import typing
  6. import numbers
  7. import errno
  8. import os
  9. import logging
  10. from base64 import b64decode
  11. from os.path import dirname, abspath
  12. from .sxng_locales import sxng_locales
  13. searx_dir = abspath(dirname(__file__))
  14. logger = logging.getLogger('searx')
  15. OUTPUT_FORMATS = ['html', 'csv', 'json', 'rss']
  16. SXNG_LOCALE_TAGS = ['all', 'auto'] + list(l[0] for l in sxng_locales)
  17. SIMPLE_STYLE = ('auto', 'light', 'dark')
  18. CATEGORIES_AS_TABS = {
  19. 'general': {},
  20. 'images': {},
  21. 'videos': {},
  22. 'news': {},
  23. 'map': {},
  24. 'music': {},
  25. 'it': {},
  26. 'science': {},
  27. 'files': {},
  28. 'social media': {},
  29. }
  30. STR_TO_BOOL = {
  31. '0': False,
  32. 'false': False,
  33. 'off': False,
  34. '1': True,
  35. 'true': True,
  36. 'on': True,
  37. }
  38. _UNDEFINED = object()
  39. class SettingsValue:
  40. """Check and update a setting value"""
  41. def __init__(
  42. self,
  43. type_definition: typing.Union[None, typing.Any, typing.Tuple[typing.Any]] = None,
  44. default: typing.Any = None,
  45. environ_name: str = None,
  46. ):
  47. self.type_definition = (
  48. type_definition if type_definition is None or isinstance(type_definition, tuple) else (type_definition,)
  49. )
  50. self.default = default
  51. self.environ_name = environ_name
  52. @property
  53. def type_definition_repr(self):
  54. types_str = [t.__name__ if isinstance(t, type) else repr(t) for t in self.type_definition]
  55. return ', '.join(types_str)
  56. def check_type_definition(self, value: typing.Any) -> None:
  57. if value in self.type_definition:
  58. return
  59. type_list = tuple(t for t in self.type_definition if isinstance(t, type))
  60. if not isinstance(value, type_list):
  61. raise ValueError('The value has to be one of these types/values: {}'.format(self.type_definition_repr))
  62. def __call__(self, value: typing.Any) -> typing.Any:
  63. if value == _UNDEFINED:
  64. value = self.default
  65. # override existing value with environ
  66. if self.environ_name and self.environ_name in os.environ:
  67. value = os.environ[self.environ_name]
  68. if self.type_definition == (bool,):
  69. value = STR_TO_BOOL[value.lower()]
  70. self.check_type_definition(value)
  71. return value
  72. class SettingSublistValue(SettingsValue):
  73. """Check the value is a sublist of type definition."""
  74. def check_type_definition(self, value: typing.Any) -> typing.Any:
  75. if not isinstance(value, list):
  76. raise ValueError('The value has to a list')
  77. for item in value:
  78. if not item in self.type_definition[0]:
  79. raise ValueError('{} not in {}'.format(item, self.type_definition))
  80. class SettingsDirectoryValue(SettingsValue):
  81. """Check and update a setting value that is a directory path"""
  82. def check_type_definition(self, value: typing.Any) -> typing.Any:
  83. super().check_type_definition(value)
  84. if not os.path.isdir(value):
  85. raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), value)
  86. def __call__(self, value: typing.Any) -> typing.Any:
  87. if value == '':
  88. value = self.default
  89. return super().__call__(value)
  90. class SettingsBytesValue(SettingsValue):
  91. """str are base64 decoded"""
  92. def __call__(self, value: typing.Any) -> typing.Any:
  93. if isinstance(value, str):
  94. value = b64decode(value)
  95. return super().__call__(value)
  96. def apply_schema(settings, schema, path_list):
  97. error = False
  98. for key, value in schema.items():
  99. if isinstance(value, SettingsValue):
  100. try:
  101. settings[key] = value(settings.get(key, _UNDEFINED))
  102. except Exception as e: # pylint: disable=broad-except
  103. # don't stop now: check other values
  104. logger.error('%s: %s', '.'.join([*path_list, key]), e)
  105. error = True
  106. elif isinstance(value, dict):
  107. error = error or apply_schema(settings.setdefault(key, {}), schema[key], [*path_list, key])
  108. else:
  109. settings.setdefault(key, value)
  110. if len(path_list) == 0 and error:
  111. raise ValueError('Invalid settings.yml')
  112. return error
  113. SCHEMA = {
  114. 'general': {
  115. 'debug': SettingsValue(bool, False, 'SEARXNG_DEBUG'),
  116. 'instance_name': SettingsValue(str, 'SearXNG'),
  117. 'privacypolicy_url': SettingsValue((None, False, str), None),
  118. 'contact_url': SettingsValue((None, False, str), None),
  119. 'donation_url': SettingsValue((bool, str), "https://docs.searxng.org/donate.html"),
  120. 'enable_metrics': SettingsValue(bool, True),
  121. },
  122. 'brand': {
  123. 'issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues'),
  124. 'new_issue_url': SettingsValue(str, 'https://github.com/searxng/searxng/issues/new'),
  125. 'docs_url': SettingsValue(str, 'https://docs.searxng.org'),
  126. 'public_instances': SettingsValue((False, str), 'https://searx.space'),
  127. 'wiki_url': SettingsValue(str, 'https://github.com/searxng/searxng/wiki'),
  128. },
  129. 'search': {
  130. 'safe_search': SettingsValue((0, 1, 2), 0),
  131. 'autocomplete': SettingsValue(str, ''),
  132. 'autocomplete_min': SettingsValue(int, 4),
  133. 'default_lang': SettingsValue(tuple(SXNG_LOCALE_TAGS + ['']), ''),
  134. 'languages': SettingSublistValue(SXNG_LOCALE_TAGS, SXNG_LOCALE_TAGS),
  135. 'ban_time_on_fail': SettingsValue(numbers.Real, 5),
  136. 'max_ban_time_on_fail': SettingsValue(numbers.Real, 120),
  137. 'suspended_times': {
  138. 'SearxEngineAccessDenied': SettingsValue(numbers.Real, 86400),
  139. 'SearxEngineCaptcha': SettingsValue(numbers.Real, 86400),
  140. 'SearxEngineTooManyRequests': SettingsValue(numbers.Real, 3600),
  141. 'cf_SearxEngineCaptcha': SettingsValue(numbers.Real, 1296000),
  142. 'cf_SearxEngineAccessDenied': SettingsValue(numbers.Real, 86400),
  143. 'recaptcha_SearxEngineCaptcha': SettingsValue(numbers.Real, 604800),
  144. },
  145. 'formats': SettingsValue(list, OUTPUT_FORMATS),
  146. },
  147. 'server': {
  148. 'port': SettingsValue((int, str), 8888, 'SEARXNG_PORT'),
  149. 'bind_address': SettingsValue(str, '127.0.0.1', 'SEARXNG_BIND_ADDRESS'),
  150. 'limiter': SettingsValue(bool, False),
  151. 'secret_key': SettingsValue(str, environ_name='SEARXNG_SECRET'),
  152. 'base_url': SettingsValue((False, str), False, 'SEARXNG_BASE_URL'),
  153. 'image_proxy': SettingsValue(bool, False),
  154. 'http_protocol_version': SettingsValue(('1.0', '1.1'), '1.0'),
  155. 'method': SettingsValue(('POST', 'GET'), 'POST'),
  156. 'default_http_headers': SettingsValue(dict, {}),
  157. },
  158. 'redis': {
  159. 'url': SettingsValue((None, False, str), False, 'SEARXNG_REDIS_URL'),
  160. },
  161. 'ui': {
  162. 'static_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'static')),
  163. 'static_use_hash': SettingsValue(bool, False),
  164. 'templates_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'templates')),
  165. 'default_theme': SettingsValue(str, 'simple'),
  166. 'default_locale': SettingsValue(str, ''),
  167. 'theme_args': {
  168. 'simple_style': SettingsValue(SIMPLE_STYLE, 'auto'),
  169. },
  170. 'center_alignment': SettingsValue(bool, False),
  171. 'results_on_new_tab': SettingsValue(bool, False),
  172. 'advanced_search': SettingsValue(bool, False),
  173. 'query_in_title': SettingsValue(bool, False),
  174. 'infinite_scroll': SettingsValue(bool, False),
  175. 'cache_url': SettingsValue(str, 'https://web.archive.org/web/'),
  176. },
  177. 'preferences': {
  178. 'lock': SettingsValue(list, []),
  179. },
  180. 'outgoing': {
  181. 'useragent_suffix': SettingsValue(str, ''),
  182. 'request_timeout': SettingsValue(numbers.Real, 3.0),
  183. 'enable_http2': SettingsValue(bool, True),
  184. 'verify': SettingsValue((bool, str), True),
  185. 'max_request_timeout': SettingsValue((None, numbers.Real), None),
  186. 'pool_connections': SettingsValue(int, 100),
  187. 'pool_maxsize': SettingsValue(int, 10),
  188. 'keepalive_expiry': SettingsValue(numbers.Real, 5.0),
  189. # default maximum redirect
  190. # from https://github.com/psf/requests/blob/8c211a96cdbe9fe320d63d9e1ae15c5c07e179f8/requests/models.py#L55
  191. 'max_redirects': SettingsValue(int, 30),
  192. 'retries': SettingsValue(int, 0),
  193. 'proxies': SettingsValue((None, str, dict), None),
  194. 'source_ips': SettingsValue((None, str, list), None),
  195. # Tor configuration
  196. 'using_tor_proxy': SettingsValue(bool, False),
  197. 'extra_proxy_timeout': SettingsValue(int, 0),
  198. 'networks': {},
  199. },
  200. 'result_proxy': {
  201. 'url': SettingsValue((None, str), None),
  202. 'key': SettingsBytesValue((None, bytes), None),
  203. 'proxify_results': SettingsValue(bool, False),
  204. },
  205. 'plugins': SettingsValue(list, []),
  206. 'enabled_plugins': SettingsValue((None, list), None),
  207. 'checker': {
  208. 'off_when_debug': SettingsValue(bool, True, None),
  209. 'scheduling': SettingsValue((None, dict), None, None),
  210. },
  211. 'categories_as_tabs': SettingsValue(dict, CATEGORIES_AS_TABS),
  212. 'engines': SettingsValue(list, []),
  213. 'doi_resolvers': {},
  214. }
  215. def settings_set_defaults(settings):
  216. apply_schema(settings, SCHEMA, [])
  217. return settings