mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00

In PR-2894[1] we isolated botdetection from the limiter, this PR isolates the botdetection from the SearXNG core code. This PR also fixes the issue [2] that the ``server.public_instance`` option needs to activate the limiter. - [1] https://github.com/searxng/searxng/pull/2894 - [2] https://github.com/searxng/searxng/issues/2975 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
# lint: pylint
|
|
""".. _botdetection src:
|
|
|
|
Implementations used for bot detection.
|
|
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import pathlib
|
|
|
|
import redis
|
|
from .config import Config
|
|
|
|
from ._helpers import logger
|
|
from ._helpers import dump_request
|
|
from ._helpers import get_real_ip
|
|
from ._helpers import get_network
|
|
from ._helpers import too_many_requests
|
|
|
|
logger = logger.getChild('init')
|
|
|
|
__all__ = ['dump_request', 'get_network', 'get_real_ip', 'too_many_requests']
|
|
|
|
CFG_SCHEMA = pathlib.Path(__file__).parent / "schema.toml"
|
|
"""Base configuration (schema) of the botdetection."""
|
|
|
|
CFG_DEPRECATED = {
|
|
# "dummy.old.foo": "config 'dummy.old.foo' exists only for tests. Don't use it in your real project config."
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class Context:
|
|
"""A global context of the botdetection"""
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
redis_client: redis.Redis | None = None
|
|
cfg: Config = Config.from_toml(schema_file=CFG_SCHEMA, cfg_file=None, deprecated=CFG_DEPRECATED)
|
|
|
|
def init(self, toml_cfg: pathlib.Path, redis_client: redis.Redis | None):
|
|
self.redis_client = redis_client
|
|
self.cfg.load_toml(toml_cfg)
|
|
|
|
|
|
ctx = Context()
|