ip_limit.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """.. _botdetection.ip_limit:
  2. Method ``ip_limit``
  3. -------------------
  4. The ``ip_limit`` method counts request from an IP in *sliding windows*. If
  5. there are to many requests in a sliding window, the request is evaluated as a
  6. bot request. This method requires a redis DB and needs a HTTP X-Forwarded-For_
  7. header. To take privacy only the hash value of an IP is stored in the redis DB
  8. and at least for a maximum of 10 minutes.
  9. The :py:obj:`.link_token` method can be used to investigate whether a request is
  10. *suspicious*. To activate the :py:obj:`.link_token` method in the
  11. :py:obj:`.ip_limit` method add the following to your
  12. ``/etc/searxng/limiter.toml``:
  13. .. code:: toml
  14. [botdetection.ip_limit]
  15. link_token = true
  16. If the :py:obj:`.link_token` method is activated and a request is *suspicious*
  17. the request rates are reduced:
  18. - :py:obj:`BURST_MAX` -> :py:obj:`BURST_MAX_SUSPICIOUS`
  19. - :py:obj:`LONG_MAX` -> :py:obj:`LONG_MAX_SUSPICIOUS`
  20. .. _X-Forwarded-For:
  21. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
  22. """
  23. from typing import Optional, Tuple
  24. import flask
  25. from searx.tools import config
  26. from searx import redisdb
  27. from searx import logger
  28. from searx.redislib import incr_sliding_window
  29. from . import link_token
  30. logger = logger.getChild('botdetection.ip_limit')
  31. BURST_WINDOW = 20
  32. """Time (sec) before sliding window for *burst* requests expires."""
  33. BURST_MAX = 15
  34. """Maximum requests from one IP in the :py:obj:`BURST_WINDOW`"""
  35. BURST_MAX_SUSPICIOUS = 2
  36. """Maximum of suspicious requests from one IP in the :py:obj:`BURST_WINDOW`"""
  37. LONG_WINDOW = 600
  38. """Time (sec) before the longer sliding window expires."""
  39. LONG_MAX = 150
  40. """Maximum requests from one IP in the :py:obj:`LONG_WINDOW`"""
  41. LONG_MAX_SUSPICIOUS = 10
  42. """Maximum suspicious requests from one IP in the :py:obj:`LONG_WINDOW`"""
  43. API_WONDOW = 3600
  44. """Time (sec) before sliding window for API requests (format != html) expires."""
  45. API_MAX = 4
  46. """Maximum requests from one IP in the :py:obj:`API_WONDOW`"""
  47. def filter_request(request: flask.Request, cfg: config.Config) -> Optional[Tuple[int, str]]:
  48. redis_client = redisdb.client()
  49. x_forwarded_for = request.headers.get('X-Forwarded-For', '')
  50. if not x_forwarded_for:
  51. logger.error("missing HTTP header X-Forwarded-For")
  52. if request.args.get('format', 'html') != 'html':
  53. c = incr_sliding_window(redis_client, 'IP limit - API_WONDOW:' + x_forwarded_for, API_WONDOW)
  54. if c > API_MAX:
  55. return 429, "BLOCK %s: API limit exceeded"
  56. suspicious = False
  57. if cfg['botdetection.ip_limit.link_token']:
  58. suspicious = link_token.is_suspicious(request)
  59. if suspicious:
  60. c = incr_sliding_window(redis_client, 'IP limit - BURST_WINDOW:' + x_forwarded_for, BURST_WINDOW)
  61. if c > BURST_MAX_SUSPICIOUS:
  62. return 429, f"bot detected, too many request from {x_forwarded_for} in BURST_MAX_SUSPICIOUS"
  63. c = incr_sliding_window(redis_client, 'IP limit - LONG_WINDOW:' + x_forwarded_for, LONG_WINDOW)
  64. if c > LONG_MAX_SUSPICIOUS:
  65. return 429, f"bot detected, too many request from {x_forwarded_for} in LONG_MAX_SUSPICIOUS"
  66. else:
  67. c = incr_sliding_window(redis_client, 'IP limit - BURST_WINDOW:' + x_forwarded_for, BURST_WINDOW)
  68. if c > BURST_MAX:
  69. return 429, f"bot detected, too many request from {x_forwarded_for} in BURST_MAX"
  70. c = incr_sliding_window(redis_client, 'IP limit - LONG_WINDOW:' + x_forwarded_for, LONG_WINDOW)
  71. if c > LONG_MAX:
  72. return 429, f"bot detected, too many request from {x_forwarded_for} in LONG_MAX"
  73. return None