mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-04 08:13:38 +00:00
I remember the default being Moderate earlier. Maybe the Debian package or upstream changed it to None. To be safe, we should set it in our installation script too. One problem I observed with default setting being None is that any queries performed with this setting will be cached and a stricter setting will not apply on the same search query. For example, if you searched for "computer" with the default setting of None, there will be some NSFW images returned by SearX. Setting safe_search to Moderate or Strict later will have no effect on the search query "computer". Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
186 lines
5.4 KiB
Python
Executable File
186 lines
5.4 KiB
Python
Executable File
#!/usr/bin/python3
|
|
#
|
|
# This file is part of FreedomBox.
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
"""
|
|
Configuration helper for searx.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import secrets
|
|
import shutil
|
|
|
|
import augeas
|
|
import yaml
|
|
|
|
from plinth import action_utils
|
|
from plinth.modules.searx.manifest import PUBLIC_ACCESS_SETTING_FILE
|
|
from plinth.utils import gunzip
|
|
|
|
SETTINGS_FILE = '/etc/searx/settings.yml'
|
|
|
|
UWSGI_FILE = '/etc/uwsgi/apps-available/searx.ini'
|
|
|
|
|
|
def parse_arguments():
|
|
"""Return parsed command line arguments as dictionary."""
|
|
parser = argparse.ArgumentParser()
|
|
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
|
|
|
subparsers.add_parser(
|
|
'setup', help='Perform post-installation operations for Searx')
|
|
|
|
subparsers.add_parser('enable-public-access',
|
|
help='Enable public access to the Searx application')
|
|
subparsers.add_parser(
|
|
'disable-public-access',
|
|
help='Disable public access to the Searx application')
|
|
|
|
safe_search = subparsers.add_parser(
|
|
'set-safe-search',
|
|
help='Set the default filter for safe search on Searx')
|
|
safe_search.add_argument(
|
|
'filter', type=int,
|
|
help='Filter results. 0: None, 1: Moderate, 2: Strict')
|
|
|
|
subparsers.add_parser('get-safe-search',
|
|
help='Print the value of the safe search setting.')
|
|
|
|
subparsers.required = True
|
|
return parser.parse_args()
|
|
|
|
|
|
def _copy_uwsgi_configuration():
|
|
"""Copy example uwsgi configuration
|
|
|
|
Copy the example uwsgi configuration shipped with Searx documentation to
|
|
the appropriate uwsgi directory.
|
|
"""
|
|
example_config = ('/usr/share/doc/searx/examples/'
|
|
'uwsgi/apps-available/searx.ini')
|
|
if not os.path.exists(UWSGI_FILE):
|
|
shutil.copy(example_config, os.path.dirname(UWSGI_FILE))
|
|
|
|
|
|
def _update_uwsgi_configuration():
|
|
"""Fix uwsgi configuration.
|
|
|
|
uwsgi 2.0.15-debian crashes when trying to autoload.
|
|
"""
|
|
aug = augeas.Augeas(
|
|
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
|
|
aug.set('/augeas/load/inifile/lens', 'Puppet.lns')
|
|
aug.set('/augeas/load/inifile/incl[last() + 1]', UWSGI_FILE)
|
|
aug.load()
|
|
aug.set('/files/etc/uwsgi/apps-available/searx.ini/uwsgi/autoload',
|
|
'false')
|
|
aug.save()
|
|
|
|
|
|
def _generate_secret_key(settings):
|
|
"""Generate a secret key for the Searx installation."""
|
|
secret_key = secrets.token_hex(64)
|
|
settings['server']['secret_key'] = secret_key
|
|
|
|
|
|
def _set_title(settings):
|
|
"""Set the page title to '{box_name} Web Search'."""
|
|
title = 'FreedomBox Web Search'
|
|
settings['general']['instance_name'] = title
|
|
|
|
|
|
def _set_timeout(settings):
|
|
"""Set timeout to 20 seconds."""
|
|
settings['outgoing']['request_timeout'] = 20.0
|
|
|
|
|
|
def _set_safe_search(settings):
|
|
"""Set safe search to Moderate."""
|
|
settings['search']['safe_search'] = 1
|
|
|
|
|
|
def subcommand_set_safe_search(arguments):
|
|
"""Set safe search filter for search results."""
|
|
value = arguments.filter
|
|
settings = read_settings()
|
|
settings['search']['safe_search'] = value
|
|
write_settings(settings)
|
|
|
|
|
|
def subcommand_get_safe_search(_):
|
|
"""Print the value of the safe search setting."""
|
|
if os.path.exists(SETTINGS_FILE):
|
|
settings = read_settings()
|
|
print(settings['search']['safe_search'])
|
|
else:
|
|
print(0)
|
|
|
|
|
|
def read_settings():
|
|
"""Load settings as dictionary from YAML config file."""
|
|
with open(SETTINGS_FILE, 'rb') as settings_file:
|
|
return yaml.load(settings_file)
|
|
|
|
|
|
def write_settings(settings):
|
|
"""Write settings from dictionary to YAML config file."""
|
|
with open(SETTINGS_FILE, 'w') as settings_file:
|
|
yaml.dump(settings, settings_file)
|
|
|
|
|
|
def subcommand_setup(_):
|
|
"""Post installation actions for Searx"""
|
|
_copy_uwsgi_configuration()
|
|
_update_uwsgi_configuration()
|
|
|
|
if not os.path.exists(SETTINGS_FILE):
|
|
example_settings_file = '/usr/share/doc/searx/examples/settings.yml.gz'
|
|
gunzip(example_settings_file, SETTINGS_FILE)
|
|
|
|
settings = read_settings()
|
|
_generate_secret_key(settings)
|
|
_set_title(settings)
|
|
_set_timeout(settings)
|
|
_set_safe_search(settings)
|
|
write_settings(settings)
|
|
|
|
action_utils.service_restart('uwsgi')
|
|
|
|
|
|
def subcommand_enable_public_access(_):
|
|
"""Enable public access to the SearX application."""
|
|
open(PUBLIC_ACCESS_SETTING_FILE, 'w').close()
|
|
|
|
|
|
def subcommand_disable_public_access(_):
|
|
"""Disable public access to the SearX application."""
|
|
if os.path.exists(PUBLIC_ACCESS_SETTING_FILE):
|
|
os.remove(PUBLIC_ACCESS_SETTING_FILE)
|
|
|
|
|
|
def main():
|
|
"""Parse arguments and perform all duties."""
|
|
arguments = parse_arguments()
|
|
|
|
subcommand = arguments.subcommand.replace('-', '_')
|
|
subcommand_method = globals()['subcommand_' + subcommand]
|
|
subcommand_method(arguments)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|