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