diff options
author | Markus Heiser <markus.heiser@darmarit.de> | 2023-08-25 09:51:54 +0200 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2023-08-28 08:05:17 +0200 |
commit | 3294e7cfe41ac4e6bdc9fb87e12b9a446d5d9038 (patch) | |
tree | 5bf7e2f2399496a9651128c790748b39b9b09b5f /searx | |
parent | bad692e0f023fb349e212468590f411b5c794f83 (diff) | |
download | searxng-3294e7cfe41ac4e6bdc9fb87e12b9a446d5d9038.tar.gz searxng-3294e7cfe41ac4e6bdc9fb87e12b9a446d5d9038.zip |
[fix] reduce error logs related to X-Forwarded-For & X-Real-IP headers
To test start a `make run` instance and query multiple times for `ip` (the
answerer), you should see only once the error message(s):
ERROR searx.botdetection : X-Forwarded-For header is not set!
ERROR searx.botdetection : X-Real-IP header is not set!
[1] https://github.com/searxng/searxng-docker/discussions/174
[2] https://github.com/searxng/searxng-docker/pull/162#issuecomment-1692834457
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx')
-rw-r--r-- | searx/botdetection/_helpers.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/searx/botdetection/_helpers.py b/searx/botdetection/_helpers.py index 19905fd15..f50250e8b 100644 --- a/searx/botdetection/_helpers.py +++ b/searx/botdetection/_helpers.py @@ -58,6 +58,15 @@ def get_network(real_ip: IPv4Address | IPv6Address, cfg: config.Config) -> IPv4N return network +_logged_errors = [] + + +def _log_error_only_once(err_msg): + if err_msg not in _logged_errors: + logger.error(err_msg) + _logged_errors.append(err_msg) + + def get_real_ip(request: flask.Request) -> str: """Returns real IP of the request. Since not all proxies set all the HTTP headers and incoming headers can be faked it may happen that the IP cannot @@ -93,7 +102,7 @@ def get_real_ip(request: flask.Request) -> str: # ) if not forwarded_for: - logger.error("X-Forwarded-For header is not set!") + _log_error_only_once("X-Forwarded-For header is not set!") else: from .limiter import get_cfg # pylint: disable=import-outside-toplevel, cyclic-import @@ -102,7 +111,7 @@ def get_real_ip(request: flask.Request) -> str: forwarded_for = forwarded_for[-min(len(forwarded_for), x_for)] if not real_ip: - logger.error("X-Real-IP header is not set!") + _log_error_only_once("X-Real-IP header is not set!") if forwarded_for and real_ip and forwarded_for != real_ip: logger.warning("IP from X-Real-IP (%s) is not equal to IP from X-Forwarded-For (%s)", real_ip, forwarded_for) |