[fix] plugins: bugfix of tor_check and unit_converter

Closes: https://github.com/searxng/searxng/issues/4461
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-03-07 17:35:26 +01:00 committed by Markus Heiser
parent f49b2c94a9
commit da7b069d6e
3 changed files with 19 additions and 19 deletions

View File

@ -8,6 +8,7 @@ from ipaddress import (
IPv4Address, IPv4Address,
IPv6Address, IPv6Address,
ip_network, ip_network,
ip_address,
) )
import flask import flask
import werkzeug import werkzeug
@ -125,6 +126,9 @@ def get_real_ip(request: SXNG_Request) -> str:
if real_ip and remote_addr and real_ip != remote_addr: if real_ip and remote_addr and real_ip != remote_addr:
logger.warning("IP from WSGI environment (%s) is not equal to IP from X-Real-IP (%s)", remote_addr, real_ip) logger.warning("IP from WSGI environment (%s) is not equal to IP from X-Real-IP (%s)", remote_addr, real_ip)
request_ip = forwarded_for or real_ip or remote_addr or '0.0.0.0' request_ip = ip_address(forwarded_for or real_ip or remote_addr or '0.0.0.0')
if request_ip.version == 6 and request_ip.ipv4_mapped:
request_ip = request_ip.ipv4_mapped
# logger.debug("get_real_ip() -> %s", request_ip) # logger.debug("get_real_ip() -> %s", request_ip)
return request_ip return str(request_ip)

View File

@ -21,7 +21,8 @@ from flask_babel import gettext
from httpx import HTTPError from httpx import HTTPError
from searx.network import get from searx.network import get
from searx.result_types import Answer from searx.result_types import EngineResults
from searx.botdetection import get_real_ip
default_on = False default_on = False
@ -51,8 +52,8 @@ url_exit_list = "https://check.torproject.org/exit-addresses"
"""URL to load Tor exit list from.""" """URL to load Tor exit list from."""
def post_search(request, search) -> list[Answer]: def post_search(request, search) -> EngineResults:
results = [] results = EngineResults()
if search.search_query.pageno > 1: if search.search_query.pageno > 1:
return results return results
@ -67,22 +68,17 @@ def post_search(request, search) -> list[Answer]:
except HTTPError: except HTTPError:
# No answer, return error # No answer, return error
msg = gettext("Could not download the list of Tor exit-nodes from") msg = gettext("Could not download the list of Tor exit-nodes from")
Answer(results=results, answer=f"{msg} {url_exit_list}") results.add(results.types.Answer(answer=f"{msg} {url_exit_list}"))
return results return results
x_forwarded_for = request.headers.getlist("X-Forwarded-For") real_ip = get_real_ip(request)
if x_forwarded_for: if real_ip in node_list:
ip_address = x_forwarded_for[0]
else:
ip_address = request.remote_addr
if ip_address in node_list:
msg = gettext("You are using Tor and it looks like you have the external IP address") msg = gettext("You are using Tor and it looks like you have the external IP address")
Answer(results=results, answer=f"{msg} {ip_address}") results.add(results.types.Answer(answer=f"{msg} {real_ip}"))
else: else:
msg = gettext("You are not using Tor and you have the external IP address") msg = gettext("You are not using Tor and you have the external IP address")
Answer(results=results, answer=f"{msg} {ip_address}") results.add(results.types.Answer(answer=f"{msg} {real_ip}"))
return results return results

View File

@ -25,7 +25,7 @@ import babel.numbers
from flask_babel import gettext, get_locale from flask_babel import gettext, get_locale
from searx import data from searx import data
from searx.result_types import Answer from searx.result_types import EngineResults
name = "Unit converter plugin" name = "Unit converter plugin"
@ -245,8 +245,8 @@ def _parse_text_and_convert(from_query, to_query) -> str | None:
return f'{result} {target_symbol}' return f'{result} {target_symbol}'
def post_search(_request, search) -> list[Answer]: def post_search(_request, search) -> EngineResults:
results = [] results = EngineResults()
# only convert between units on the first page # only convert between units on the first page
if search.search_query.pageno > 1: if search.search_query.pageno > 1:
@ -264,6 +264,6 @@ def post_search(_request, search) -> list[Answer]:
from_query, to_query = query.split(keyword, 1) from_query, to_query = query.split(keyword, 1)
target_val = _parse_text_and_convert(from_query.strip(), to_query.strip()) target_val = _parse_text_and_convert(from_query.strip(), to_query.strip())
if target_val: if target_val:
Answer(results=results, answer=target_val) results.add(results.types.Answer(answer=target_val))
return results return results