.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
tor_check.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""A plugin to check if the ip address of the request is a Tor exit-node if the
3user searches for ``tor-check``. It fetches the tor exit node list from
4:py:obj:`url_exit_list` and parses all the IPs into a list, then checks if the
5user's IP address is in it.
6"""
7from ipaddress import ip_address
8import typing
9
10import re
11from flask_babel import gettext
12from httpx import HTTPError
13
14from searx.network import get
15from searx.plugins import Plugin, PluginInfo
16from searx.result_types import EngineResults
17
18if typing.TYPE_CHECKING:
19 from searx.search import SearchWithPlugins
20 from searx.extended_types import SXNG_Request
21 from searx.plugins import PluginCfg
22
23
24# Regex for exit node addresses in the list.
25reg = re.compile(r"(?<=ExitAddress )\S+")
26
27url_exit_list = "https://check.torproject.org/exit-addresses"
28"""URL to load Tor exit list from."""
29
30
32 """Rewrite hostnames, remove results or prioritize them."""
33
34 id = "tor_check"
35 keywords = ["tor-check", "tor_check", "torcheck", "tor", "tor check"]
36
37 def __init__(self, plg_cfg: "PluginCfg") -> None:
38 super().__init__(plg_cfg)
40 id=self.id,
41 name=gettext("Tor check plugin"),
42 description=gettext(
43 "This plugin checks if the address of the request is a Tor exit-node, and"
44 " informs the user if it is; like check.torproject.org, but from SearXNG."
45 ),
46 preference_section="query",
47 )
48
49 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
50 results = EngineResults()
51
52 if search.search_query.pageno > 1:
53 return results
54
55 if search.search_query.query.lower() in self.keywords:
56
57 # Request the list of tor exit nodes.
58 try:
59 resp = get(url_exit_list)
60 node_list = re.findall(reg, resp.text) # type: ignore
61
62 except HTTPError:
63 # No answer, return error
64 msg = gettext("Could not download the list of Tor exit-nodes from")
65 results.add(results.types.Answer(answer=f"{msg} {url_exit_list}"))
66 return results
67
68 real_ip = ip_address(address=str(request.remote_addr)).compressed
69
70 if real_ip in node_list:
71 msg = gettext("You are using Tor and it looks like you have the external IP address")
72 results.add(results.types.Answer(answer=f"{msg} {real_ip}"))
73
74 else:
75 msg = gettext("You are not using Tor and you have the external IP address")
76 results.add(results.types.Answer(answer=f"{msg} {real_ip}"))
77
78 return results
EngineResults post_search(self, "SXNG_Request" request, "SearchWithPlugins" search)
Definition tor_check.py:49
None __init__(self, "PluginCfg" plg_cfg)
Definition tor_check.py:37