mirror of https://github.com/searxng/searxng.git
Compare commits
4 Commits
13ef9cc125
...
242db53118
Author | SHA1 | Date |
---|---|---|
Alexandre Flament | 242db53118 | |
Alexandre Flament | a7bd2b47c2 | |
Alexandre FLAMENT | 4adc9920e9 | |
Alexandre FLAMENT | 341ad46303 |
|
@ -121,6 +121,7 @@ ui:
|
||||||
#
|
#
|
||||||
# result_proxy:
|
# result_proxy:
|
||||||
# url: http://127.0.0.1:3000/
|
# url: http://127.0.0.1:3000/
|
||||||
|
# # the key is a base64 encoded string, the YAML !!binary prefix is optional
|
||||||
# key: !!binary "your_morty_proxy_key"
|
# key: !!binary "your_morty_proxy_key"
|
||||||
# # [true|false] enable the "proxy" button next to each result
|
# # [true|false] enable the "proxy" button next to each result
|
||||||
# proxify_results: true
|
# proxify_results: true
|
||||||
|
|
|
@ -9,6 +9,7 @@ import numbers
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
from base64 import b64decode
|
||||||
from os.path import dirname, abspath
|
from os.path import dirname, abspath
|
||||||
|
|
||||||
from searx.languages import language_codes as languages
|
from searx.languages import language_codes as languages
|
||||||
|
@ -41,16 +42,6 @@ STR_TO_BOOL = {
|
||||||
}
|
}
|
||||||
_UNDEFINED = object()
|
_UNDEFINED = object()
|
||||||
|
|
||||||
# compatibility
|
|
||||||
SEARX_ENVIRON_VARIABLES = {
|
|
||||||
'SEARX_DISABLE_ETC_SETTINGS': 'SEARXNG_DISABLE_ETC_SETTINGS',
|
|
||||||
'SEARX_SETTINGS_PATH': 'SEARXNG_SETTINGS_PATH',
|
|
||||||
'SEARX_DEBUG': 'SEARXNG_DEBUG',
|
|
||||||
'SEARX_PORT': 'SEARXNG_PORT',
|
|
||||||
'SEARX_BIND_ADDRESS': 'SEARXNG_BIND_ADDRESS',
|
|
||||||
'SEARX_SECRET': 'SEARXNG_SECRET',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class SettingsValue:
|
class SettingsValue:
|
||||||
"""Check and update a setting value"""
|
"""Check and update a setting value"""
|
||||||
|
@ -117,6 +108,15 @@ class SettingsDirectoryValue(SettingsValue):
|
||||||
return super().__call__(value)
|
return super().__call__(value)
|
||||||
|
|
||||||
|
|
||||||
|
class SettingsBytesValue(SettingsValue):
|
||||||
|
"""str are base64 decoded"""
|
||||||
|
|
||||||
|
def __call__(self, value: typing.Any) -> typing.Any:
|
||||||
|
if isinstance(value, str):
|
||||||
|
value = b64decode(value)
|
||||||
|
return super().__call__(value)
|
||||||
|
|
||||||
|
|
||||||
def apply_schema(settings, schema, path_list):
|
def apply_schema(settings, schema, path_list):
|
||||||
error = False
|
error = False
|
||||||
for key, value in schema.items():
|
for key, value in schema.items():
|
||||||
|
@ -215,6 +215,11 @@ SCHEMA = {
|
||||||
'extra_proxy_timeout': SettingsValue(int, 0),
|
'extra_proxy_timeout': SettingsValue(int, 0),
|
||||||
'networks': {},
|
'networks': {},
|
||||||
},
|
},
|
||||||
|
'result_proxy': {
|
||||||
|
'url': SettingsValue((None, str), None),
|
||||||
|
'key': SettingsBytesValue((None, bytes), None),
|
||||||
|
'proxify_results': SettingsValue(bool, False),
|
||||||
|
},
|
||||||
'plugins': SettingsValue(list, []),
|
'plugins': SettingsValue(list, []),
|
||||||
'enabled_plugins': SettingsValue((None, list), None),
|
'enabled_plugins': SettingsValue((None, list), None),
|
||||||
'checker': {
|
'checker': {
|
||||||
|
@ -227,11 +232,5 @@ SCHEMA = {
|
||||||
|
|
||||||
|
|
||||||
def settings_set_defaults(settings):
|
def settings_set_defaults(settings):
|
||||||
# compatibility with searx variables
|
|
||||||
for searx, searxng in SEARX_ENVIRON_VARIABLES.items():
|
|
||||||
if searx in os.environ and searxng not in os.environ:
|
|
||||||
os.environ[searxng] = os.environ[searx]
|
|
||||||
logger.warning('%s uses value from %s', searxng, searx)
|
|
||||||
|
|
||||||
apply_schema(settings, SCHEMA, [])
|
apply_schema(settings, SCHEMA, [])
|
||||||
return settings
|
return settings
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
from os import environ
|
from os import environ
|
||||||
from os.path import dirname, join, abspath, isfile
|
from os.path import dirname, join, abspath, isfile
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
@ -13,7 +14,7 @@ from searx.exceptions import SearxSettingsException
|
||||||
searx_dir = abspath(dirname(__file__))
|
searx_dir = abspath(dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
def check_settings_yml(file_name):
|
def existing_filename_or_none(file_name: str) -> Optional[str]:
|
||||||
if isfile(file_name):
|
if isfile(file_name):
|
||||||
return file_name
|
return file_name
|
||||||
return None
|
return None
|
||||||
|
@ -30,29 +31,29 @@ def load_yaml(file_name):
|
||||||
|
|
||||||
|
|
||||||
def get_default_settings_path():
|
def get_default_settings_path():
|
||||||
return check_settings_yml(join(searx_dir, 'settings.yml'))
|
return existing_filename_or_none(join(searx_dir, 'settings.yml'))
|
||||||
|
|
||||||
|
|
||||||
def get_user_settings_path():
|
def get_user_settings_path() -> Optional[str]:
|
||||||
# find location of settings.yml
|
"""Get an user settings file.
|
||||||
|
By descending priority:
|
||||||
|
1. ``environ['SEARXNG_SETTINGS_PATH']``
|
||||||
|
2. ``/etc/searxng/settings.yml`` except if ``SEARXNG_DISABLE_ETC_SETTINGS`` is ``true`` or ``1``
|
||||||
|
3. ``None``
|
||||||
|
"""
|
||||||
|
|
||||||
|
# check the environment variable SEARXNG_SETTINGS_PATH
|
||||||
|
# if the environment variable is defined, this is the last check
|
||||||
if 'SEARXNG_SETTINGS_PATH' in environ:
|
if 'SEARXNG_SETTINGS_PATH' in environ:
|
||||||
# if possible set path to settings using the
|
return existing_filename_or_none(environ['SEARXNG_SETTINGS_PATH'])
|
||||||
# enviroment variable SEARXNG_SETTINGS_PATH
|
|
||||||
return check_settings_yml(environ['SEARXNG_SETTINGS_PATH'])
|
|
||||||
|
|
||||||
|
# if SEARXNG_DISABLE_ETC_SETTINGS don't look any futher
|
||||||
if environ.get('SEARXNG_DISABLE_ETC_SETTINGS', '').lower() in ('1', 'true'):
|
if environ.get('SEARXNG_DISABLE_ETC_SETTINGS', '').lower() in ('1', 'true'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# if not, get it from searx code base or last solution from /etc/searxng
|
# check /etc/searxng/settings.yml
|
||||||
try:
|
# (continue with other locations if the file is not found)
|
||||||
return check_settings_yml('/etc/searxng/settings.yml')
|
return existing_filename_or_none('/etc/searxng/settings.yml')
|
||||||
except SearxSettingsException as e:
|
|
||||||
# fall back to searx settings
|
|
||||||
try:
|
|
||||||
return check_settings_yml('/etc/searx/settings.yml')
|
|
||||||
except SearxSettingsException:
|
|
||||||
# if none are found, raise the exception about SearXNG
|
|
||||||
raise e # pylint: disable=raise-missing-from
|
|
||||||
|
|
||||||
|
|
||||||
def update_dict(default_dict, user_dict):
|
def update_dict(default_dict, user_dict):
|
||||||
|
|
|
@ -315,16 +315,16 @@ def custom_url_for(endpoint: str, **values):
|
||||||
return url_for(endpoint, **values) + suffix
|
return url_for(endpoint, **values) + suffix
|
||||||
|
|
||||||
|
|
||||||
def proxify(url: str):
|
def morty_proxify(url: str):
|
||||||
if url.startswith('//'):
|
if url.startswith('//'):
|
||||||
url = 'https:' + url
|
url = 'https:' + url
|
||||||
|
|
||||||
if not settings.get('result_proxy'):
|
if not settings['result_proxy']['url']:
|
||||||
return url
|
return url
|
||||||
|
|
||||||
url_params = dict(mortyurl=url)
|
url_params = dict(mortyurl=url)
|
||||||
|
|
||||||
if settings['result_proxy'].get('key'):
|
if settings['result_proxy']['key']:
|
||||||
url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'], url.encode(), hashlib.sha256).hexdigest()
|
url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'], url.encode(), hashlib.sha256).hexdigest()
|
||||||
|
|
||||||
return '{0}?{1}'.format(settings['result_proxy']['url'], urlencode(url_params))
|
return '{0}?{1}'.format(settings['result_proxy']['url'], urlencode(url_params))
|
||||||
|
@ -349,8 +349,8 @@ def image_proxify(url: str):
|
||||||
return url
|
return url
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if settings.get('result_proxy'):
|
if settings['result_proxy']['url']:
|
||||||
return proxify(url)
|
return morty_proxify(url)
|
||||||
|
|
||||||
h = new_hmac(settings['server']['secret_key'], url.encode())
|
h = new_hmac(settings['server']['secret_key'], url.encode())
|
||||||
|
|
||||||
|
@ -462,8 +462,8 @@ def render(template_name: str, **kwargs):
|
||||||
# helpers to create links to other pages
|
# helpers to create links to other pages
|
||||||
kwargs['url_for'] = custom_url_for # override url_for function in templates
|
kwargs['url_for'] = custom_url_for # override url_for function in templates
|
||||||
kwargs['image_proxify'] = image_proxify
|
kwargs['image_proxify'] = image_proxify
|
||||||
kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None
|
kwargs['proxify'] = morty_proxify if settings['result_proxy']['url'] is not None else None
|
||||||
kwargs['proxify_results'] = settings.get('result_proxy', {}).get('proxify_results', True)
|
kwargs['proxify_results'] = settings['result_proxy']['proxify_results']
|
||||||
kwargs['get_result_template'] = get_result_template
|
kwargs['get_result_template'] = get_result_template
|
||||||
kwargs['opensearch_url'] = (
|
kwargs['opensearch_url'] = (
|
||||||
url_for('opensearch')
|
url_for('opensearch')
|
||||||
|
|
|
@ -22,11 +22,11 @@ class TestLoad(SearxTestCase):
|
||||||
with self.assertRaises(SearxSettingsException):
|
with self.assertRaises(SearxSettingsException):
|
||||||
settings_loader.load_yaml(join(test_dir, '/settings/empty_settings.yml'))
|
settings_loader.load_yaml(join(test_dir, '/settings/empty_settings.yml'))
|
||||||
|
|
||||||
def test_check_settings_yml(self):
|
def test_existing_filename_or_none(self):
|
||||||
self.assertIsNone(settings_loader.check_settings_yml('/dev/zero'))
|
self.assertIsNone(settings_loader.existing_filename_or_none('/dev/zero'))
|
||||||
|
|
||||||
bad_settings_path = join(test_dir, 'settings/syntaxerror_settings.yml')
|
bad_settings_path = join(test_dir, 'settings/syntaxerror_settings.yml')
|
||||||
self.assertEqual(settings_loader.check_settings_yml(bad_settings_path), bad_settings_path)
|
self.assertEqual(settings_loader.existing_filename_or_none(bad_settings_path), bad_settings_path)
|
||||||
|
|
||||||
|
|
||||||
class TestDefaultSettings(SearxTestCase):
|
class TestDefaultSettings(SearxTestCase):
|
||||||
|
|
Loading…
Reference in New Issue