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 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']

View file

@ -0,0 +1,43 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
# pyright: basic
"""see :ref:`limiter src`"""
import sys
import flask
from searx import redisdb, logger
from searx.botdetection import limiter
# 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 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
if not redisdb.client():
logger.error(
"The limiter requires Redis, please consult the documentation: "
+ "https://docs.searxng.org/admin/searx.botdetection.html#limiter"
)
if settings['server']['public_instance']:
sys.exit(1)
return
app.before_request(pre_request)
_INSTALLED = True