From bee313c61c63aaa42ae859d3fa53041a2d1e67b4 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Fri, 29 Sep 2023 10:30:16 +0000 Subject: [PATCH] Convert plugin searx.plugin.limiter to normal code The limiter plugin is not a plugin: * the user can't enable or disable the plugin and there is no point to allow that * the limiter does use any of the features provides by the plugin framework This commit convert the limiter plugin into normal code --- searx/botdetection/__init__.py | 3 +++ .../limiter.py => botdetection/install.py} | 27 +++++++++++-------- searx/webapp.py | 9 +++++-- 3 files changed, 26 insertions(+), 13 deletions(-) rename searx/{plugins/limiter.py => botdetection/install.py} (62%) diff --git a/searx/botdetection/__init__.py b/searx/botdetection/__init__.py index 74f6c4263..b054ad768 100644 --- a/searx/botdetection/__init__.py +++ b/searx/botdetection/__init__.py @@ -42,3 +42,6 @@ X-Forwarded-For from ._helpers import dump_request from ._helpers import get_real_ip from ._helpers import too_many_requests +from .install import initialize, is_installed + +__all__ = ['dump_request', 'get_real_ip', 'too_many_requests', 'initialize', 'is_installed'] diff --git a/searx/plugins/limiter.py b/searx/botdetection/install.py similarity index 62% rename from searx/plugins/limiter.py rename to searx/botdetection/install.py index 24b14f3b4..34ff5921c 100644 --- a/searx/plugins/limiter.py +++ b/searx/botdetection/install.py @@ -6,26 +6,31 @@ import sys import flask -from searx import redisdb -from searx.plugins import logger +from searx import redisdb, logger from searx.botdetection import limiter -name = "Request limiter" -description = "Limit the number of request" -default_on = False -preference_section = 'service' - +# the configuration are limiter.toml and "limiter" in settings.yml +# so, for coherency, the logger is "limiter" even if the module name "searx.botdetection" logger = logger.getChild('limiter') +_INSTALLED = False + + def pre_request(): """See :ref:`flask.Flask.before_request`""" return limiter.filter_request(flask.request) -def init(app: flask.Flask, settings) -> bool: +def is_installed(): + return _INSTALLED + + +def initialize(app: flask.Flask, settings): + """Instal the botlimiter aka limiter""" + global _INSTALLED # pylint: disable=global-statement if not settings['server']['limiter'] and not settings['server']['public_instance']: - return False + return if not redisdb.client(): logger.error( "The limiter requires Redis, please consult the documentation: " @@ -33,6 +38,6 @@ def init(app: flask.Flask, settings) -> bool: ) if settings['server']['public_instance']: sys.exit(1) - return False + return app.before_request(pre_request) - return True + _INSTALLED = True diff --git a/searx/webapp.py b/searx/webapp.py index 13a31d3e5..8ea8f27ee 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -94,7 +94,11 @@ from searx.utils import ( from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.query import RawTextQuery from searx.plugins import Plugin, plugins, initialize as plugin_initialize -from searx.botdetection import link_token +from searx.botdetection import ( + link_token, + initialize as botdetection_initialize, + is_installed as botdetection_is_installed, +) from searx.plugins.oa_doi_rewrite import get_doi_resolver from searx.preferences import ( Preferences, @@ -1288,7 +1292,7 @@ def config(): 'DOCS_URL': get_setting('brand.docs_url'), }, 'limiter': { - 'enabled': settings['server']['limiter'], + 'enabled': botdetection_is_installed(), 'botdetection.ip_limit.link_token': _limiter_cfg.get('botdetection.ip_limit.link_token'), 'botdetection.ip_lists.pass_searxng_org': _limiter_cfg.get('botdetection.ip_lists.pass_searxng_org'), }, @@ -1322,6 +1326,7 @@ if not werkzeug_reloader or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_ redis_initialize() plugin_initialize(app) search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics']) + botdetection_initialize(app, settings) def run():