
    i
                         d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ej                   rdd	lmZ dd
lmZ ddlmZ  ej,                  d      ZdZ	  G d de      Zy)a  A plugin to check if the ip address of the request is a Tor exit-node if the
user searches for ``tor-check``.  It fetches the tor exit node list from
:py:obj:`url_exit_list` and parses all the IPs into a list, then checks if the
user's IP address is in it.
    )
ip_addressN)gettext)	HTTPError)get)Plugin
PluginInfo)EngineResults)SearchWithPlugins)SXNG_Request)	PluginCfgz(?<=ExitAddress )\S+z+https://check.torproject.org/exit-addressesc                   D     e Zd ZdZdZg dZd fdZdddd	defd
Z xZ	S )
SXNGPluginz5Rewrite hostnames, remove results or prioritize them.	tor_check)z	tor-checkr   torchecktorz	tor checkreturnc                     t         |   |       t        | j                  t	        d      t	        d      d      | _        y )NzTor check pluginzThis plugin checks if the address of the request is a Tor exit-node, and informs the user if it is; like check.torproject.org, but from SearXNG.query)idnamedescriptionpreference_section)super__init__r   r   r   info)selfplg_cfg	__class__s     (/root/searxng/searx/plugins/tor_check.pyr   zSXNGPlugin.__init__%   s?    !ww+,[  '
	    requestr   searchr
   c                    t               }|j                  j                  dkD  r|S |j                  j                  j	                         | j
                  v r	 t        t              }t        j                  t        |j                        }t#        t%        |j&                              j(                  }||v r=t        d      }|j                  |j                  j!                  | d|              |S t        d      }|j                  |j                  j!                  | d|              |S # t        $ rD t        d      }|j                  |j                  j!                  | dt                      |cY S w xY w)N   z2Could not download the list of Tor exit-nodes from )answer)addresszDYou are using Tor and it looks like you have the external IP addressz:You are not using Tor and you have the external IP address)r	   search_querypagenor   lowerkeywordsr   url_exit_listrefindallregtextr   r   addtypesAnswerr   strremote_addr
compressed)r   r!   r"   resultsresp	node_listmsgreal_ips           r   post_searchzSXNGPlugin.post_search1   sK   /%%)N$$**,==)JJsDII6	 !W-@-@)ABMMG)#deGMM003%q	8J0KL  Z[GMM003%q	8J0KL!  RSGMM003%q8P0QR	s   3D1 1A
E>=E>)r   r   r   N)
__name__
__module____qualname____doc__r   r+   r   r	   r<   __classcell__)r   s   @r   r   r      s1    ?	BIH

> ;N S` r    r   )r@   	ipaddressr   typingr-   flask_babelr   httpxr   searx.networkr   searx.pluginsr   r   searx.result_typesr	   TYPE_CHECKINGsearx.searchr
   searx.extended_typesr   r   compiler/   r,   r    r    r   <module>rN      s]   
 !  	    , ,	.1' bjj()= %/ /r    