.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
7Enable in ``settings.yml``:
8
9.. code:: yaml
10
11 enabled_plugins:
12 ..
13 - 'Tor check plugin'
14
15"""
16
17from __future__ import annotations
18
19import re
20from flask_babel import gettext
21from httpx import HTTPError
22
23from searx.network import get
24from searx.result_types import Answer
25
26
27default_on = False
28
29name = gettext("Tor check plugin")
30'''Translated name of the plugin'''
31
32description = gettext(
33 "This plugin checks if the address of the request is a Tor exit-node, and"
34 " informs the user if it is; like check.torproject.org, but from SearXNG."
35)
36'''Translated description of the plugin.'''
37
38preference_section = 'query'
39'''The preference section where the plugin is shown.'''
40
41query_keywords = ['tor-check']
42'''Query keywords shown in the preferences.'''
43
44query_examples = ''
45'''Query examples shown in the preferences.'''
46
47# Regex for exit node addresses in the list.
48reg = re.compile(r"(?<=ExitAddress )\S+")
49
50url_exit_list = "https://check.torproject.org/exit-addresses"
51"""URL to load Tor exit list from."""
52
53
54def post_search(request, search) -> list[Answer]:
55 results = []
56
57 if search.search_query.pageno > 1:
58 return results
59
60 if search.search_query.query.lower() == "tor-check":
61
62 # Request the list of tor exit nodes.
63 try:
64 resp = get(url_exit_list)
65 node_list = re.findall(reg, resp.text) # type: ignore
66
67 except HTTPError:
68 # No answer, return error
69 msg = gettext("Could not download the list of Tor exit-nodes from")
70 Answer(results=results, answer=f"{msg} {url_exit_list}")
71 return results
72
73 x_forwarded_for = request.headers.getlist("X-Forwarded-For")
74
75 if x_forwarded_for:
76 ip_address = x_forwarded_for[0]
77 else:
78 ip_address = request.remote_addr
79
80 if ip_address in node_list:
81 msg = gettext("You are using Tor and it looks like you have the external IP address")
82 Answer(results=results, answer=f"{msg} {ip_address}")
83
84 else:
85 msg = gettext("You are not using Tor and you have the external IP address")
86 Answer(results=results, answer=f"{msg} {ip_address}")
87
88 return results
list[Answer] post_search(request, search)
Definition tor_check.py:54