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
This commit is contained in:
Alexandre Flament 2023-09-29 10:30:16 +00:00
parent 26fed56d51
commit bee313c61c
3 changed files with 26 additions and 13 deletions

View file

@ -42,3 +42,6 @@ X-Forwarded-For
from ._helpers import dump_request from ._helpers import dump_request
from ._helpers import get_real_ip from ._helpers import get_real_ip
from ._helpers import too_many_requests from ._helpers import too_many_requests
from .install import initialize, is_installed
__all__ = ['dump_request', 'get_real_ip', 'too_many_requests', 'initialize', 'is_installed']

View file

@ -6,26 +6,31 @@
import sys import sys
import flask import flask
from searx import redisdb from searx import redisdb, logger
from searx.plugins import logger
from searx.botdetection import limiter from searx.botdetection import limiter
name = "Request limiter" # the configuration are limiter.toml and "limiter" in settings.yml
description = "Limit the number of request" # so, for coherency, the logger is "limiter" even if the module name "searx.botdetection"
default_on = False
preference_section = 'service'
logger = logger.getChild('limiter') logger = logger.getChild('limiter')
_INSTALLED = False
def pre_request(): def pre_request():
"""See :ref:`flask.Flask.before_request`""" """See :ref:`flask.Flask.before_request`"""
return limiter.filter_request(flask.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']: if not settings['server']['limiter'] and not settings['server']['public_instance']:
return False return
if not redisdb.client(): if not redisdb.client():
logger.error( logger.error(
"The limiter requires Redis, please consult the documentation: " "The limiter requires Redis, please consult the documentation: "
@ -33,6 +38,6 @@ def init(app: flask.Flask, settings) -> bool:
) )
if settings['server']['public_instance']: if settings['server']['public_instance']:
sys.exit(1) sys.exit(1)
return False return
app.before_request(pre_request) app.before_request(pre_request)
return True _INSTALLED = True

View file

@ -94,7 +94,11 @@ from searx.utils import (
from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH
from searx.query import RawTextQuery from searx.query import RawTextQuery
from searx.plugins import Plugin, plugins, initialize as plugin_initialize 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.plugins.oa_doi_rewrite import get_doi_resolver
from searx.preferences import ( from searx.preferences import (
Preferences, Preferences,
@ -1288,7 +1292,7 @@ def config():
'DOCS_URL': get_setting('brand.docs_url'), 'DOCS_URL': get_setting('brand.docs_url'),
}, },
'limiter': { 'limiter': {
'enabled': settings['server']['limiter'], 'enabled': botdetection_is_installed(),
'botdetection.ip_limit.link_token': _limiter_cfg.get('botdetection.ip_limit.link_token'), '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'), '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() redis_initialize()
plugin_initialize(app) plugin_initialize(app)
search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics']) search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics'])
botdetection_initialize(app, settings)
def run(): def run():