limiter.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """.. _limiter src:
  4. Limiter
  5. =======
  6. .. sidebar:: info
  7. The limiter requires a :ref:`Redis <settings redis>` database.
  8. Bot protection / IP rate limitation. The intention of rate limitation is to
  9. limit suspicious requests from an IP. The motivation behind this is the fact
  10. that SearXNG passes through requests from bots and is thus classified as a bot
  11. itself. As a result, the SearXNG engine then receives a CAPTCHA or is blocked
  12. by the search engine (the origin) in some other way.
  13. To avoid blocking, the requests from bots to SearXNG must also be blocked, this
  14. is the task of the limiter. To perform this task, the limiter uses the methods
  15. from the :py:obj:`searx.botdetection`.
  16. To enable the limiter activate:
  17. .. code:: yaml
  18. server:
  19. ...
  20. limiter: true # rate limit the number of request on the instance, block some bots
  21. and set the redis-url connection. Check the value, it depends on your redis DB
  22. (see :ref:`settings redis`), by example:
  23. .. code:: yaml
  24. redis:
  25. url: unix:///usr/local/searxng-redis/run/redis.sock?db=0
  26. """
  27. from __future__ import annotations
  28. from pathlib import Path
  29. import flask
  30. import werkzeug
  31. from searx.tools import config
  32. from searx import logger
  33. from . import (
  34. http_accept,
  35. http_accept_encoding,
  36. http_accept_language,
  37. http_connection,
  38. http_user_agent,
  39. ip_limit,
  40. )
  41. from ._helpers import (
  42. get_network,
  43. get_real_ip,
  44. dump_request,
  45. )
  46. logger = logger.getChild('botdetection.limiter')
  47. CFG: config.Config = None # type: ignore
  48. LIMITER_CFG_SCHEMA = Path(__file__).parent / "limiter.toml"
  49. """Base configuration (schema) of the botdetection."""
  50. LIMITER_CFG = Path('/etc/searxng/limiter.toml')
  51. """Lokal Limiter configuration."""
  52. CFG_DEPRECATED = {
  53. # "dummy.old.foo": "config 'dummy.old.foo' exists only for tests. Don't use it in your real project config."
  54. }
  55. def get_cfg() -> config.Config:
  56. global CFG # pylint: disable=global-statement
  57. if CFG is None:
  58. CFG = config.Config.from_toml(LIMITER_CFG_SCHEMA, LIMITER_CFG, CFG_DEPRECATED)
  59. return CFG
  60. def filter_request(request: flask.Request) -> werkzeug.Response | None:
  61. cfg = get_cfg()
  62. real_ip = get_real_ip(request)
  63. network = get_network(real_ip, cfg)
  64. if network.is_link_local:
  65. return None
  66. if request.path == '/healthz':
  67. return None
  68. for func in [
  69. http_user_agent,
  70. ]:
  71. val = func.filter_request(network, request, cfg)
  72. if val is not None:
  73. return val
  74. if request.path == '/search':
  75. for func in [
  76. http_accept,
  77. http_accept_encoding,
  78. http_accept_language,
  79. http_connection,
  80. http_user_agent,
  81. ip_limit,
  82. ]:
  83. val = func.filter_request(network, request, cfg)
  84. if val is not None:
  85. return val
  86. logger.debug(f"OK {network}: %s", dump_request(flask.request))
  87. return None