
    h|[                        d Z ddlZddlZddlZddlZddlZddlZddlmZmZm	Z	 	 ddl
mZ 	 ddlmZ dZdZej(                  j+                  ej(                  j-                  e      d      Z ej2                  d      Z ej2                  d	      Z ej2                  d
      Z ej2                  d      ZdZ ej2                  d      Z ej2                  d      Z  ej2                  d      Z! ej2                  d      Z" ej2                  d      Z#dQdZ$dQdZ% G d de&      Z' G d de'      Z( G d de'      Z)d Z* e(dddddd !       e(d"d#d$d%d&d'd()       e(d*d+d$d%d,d-!       e(d.d/d$d%d0d1d2)       e(d3d4d$d%d5d6d78       e(d9d:d%d$d;d<d=)       e(d>d?d$d%d@dA!       e(dBdCd$d%dDdE!       e(dFdGd$d%dHdIdJ)       e(dKdLdMdNdOd!      dP
Z+y# e$ r
 ddlmZ Y w xY w# e$ r ddlZY w xY w)Ra  
Directories that provide `relay descriptor information
<../tutorials/mirror_mirror_on_the_wall.html>`_. At a very high level tor works
as follows...

1. Volunteer starts a new tor relay, during which it sends a `server
   descriptor <descriptor/server_descriptor.html>`_ to each of the directory
   authorities.

2. Each hour the directory authorities make a `vote
   <descriptor/networkstatus.html>`_  that says who they think the active
   relays are in the network and some attributes about them.

3. The directory authorities send each other their votes, and compile that
   into the `consensus <descriptor/networkstatus.html>`_. This document is very
   similar to the votes, the only difference being that the majority of the
   authorities agree upon and sign this document. The idividual relay entries
   in the vote or consensus is called `router status entries
   <descriptor/router_status_entry.html>`_.

4. Tor clients (people using the service) download the consensus from an
   authority, fallback, or other mirror to determine who the active relays in
   the network are. They then use this to construct circuits and use the
   network.

::

  Directory - Relay we can retrieve descriptor information from
    | |- from_cache - Provides cached information bundled with Stem.
    | +- from_remote - Downloads the latest directory information from tor.
    |
    |- Authority - Tor directory authority
    +- Fallback - Mirrors that can be used instead of the authorities

.. versionadded:: 1.7.0
    N)
connection	str_tools	tor_tools)OrderedDictzHhttps://gitweb.torproject.org/tor.git/plain/src/app/config/auth_dirs.inczLhttps://gitweb.torproject.org/tor.git/plain/src/app/config/fallback_dirs.inczcached_fallbacks.cfgz"(\S+) orport=(\d+) .*"z"v3ident=([\dA-F]{40}) "z"ipv6=\[([\da-f:]+)\]:(\d+) "z!"([\d\.]+):(\d+) ([\dA-F ]{49})",z/* ===== */z/\*\s+(\S+)=(\S*)\s+\*/z0"([\d\.]+):(\d+) orport=(\d+) id=([\dA-F]{40}).*z/\* nickname=(\S+) \*/z/\* extrainfo=([0-1]) \*/z" ipv6=\[([\da-f:]+)\]:(\d+)"c                    i }| D ]X  }|D ]Q  }|j                  t        j                  |            }|s*|j                         }t	        |      dkD  r|n|d   ||<   S Z |r(|D ]#  }||vst        ddj                  |       z         |S )a  
  Scans the given content against a series of regex matchers, providing back a
  mapping of regexes to their capture groups. This maping is with the value if
  the regex has just a single capture group, and a tuple otherwise.

  :param list lines: text to parse
  :param list regexes: regexes to match against
  :param list required: matches that must be in the content

  :returns: **dict** mapping matchers against their capture groups

  :raises: **ValueError** if a required match is not present
     r   z(Failed to parse mandatory data from:

%s
)searchr   _to_unicodegroupslen
ValueErrorjoin)	linesregexesrequiredmatcheslinematchermmatch_groupsrequired_matchers	            X/var/www/betterdocs.net/sherlock_api/venv/lib/python3.12/site-packages/stem/directory.py_match_withr   Q   s     ' Vd V
....t4
5a	
xxz+.|+<q+@<lSToVV $ Z		(E		RWHXXYYZ 
.    c              #   V   K    ||       }|rt        |||        ||       }|ry y wN)r   )r   pop_section_funcr   r   next_sections        r   _directory_entriesr    r   s1     !%(,
lGX
66#E*L 	s   $))c                   J    e Zd ZdZd Zed        Zed	d       Zd Zd Z	d Z
y)
	Directorya  
  Relay we can contact for descriptor information.

  Our :func:`~stem.directory.Directory.from_cache` and
  :func:`~stem.directory.Directory.from_remote` functions key off a
  different identifier based on our subclass...

    * :class:`~stem.directory.Authority` keys off the nickname.
    * :class:`~stem.directory.Fallback` keys off fingerprints.

  This is because authorities are highly static and canonically known by their
  names, whereas fallbacks vary more and don't necessarily have a nickname to
  key off of.

  :var str address: IPv4 address of the directory
  :var int or_port: port on which the relay services relay traffic
  :var int dir_port: port on which directory information is available
  :var str fingerprint: relay fingerprint
  :var str nickname: relay nickname
  :var str orport_v6: **(address, port)** tuple for the directory's IPv6
    ORPort, or **None** if it doesn't have one
  c                 ^   |r|d|dn|}t        j                  |      st        |d|      t        j                  |      st        |d|      t        j                  |      st        |d|      t	        j
                  |      st        |d|      |r%t	        j                  |      st        |d|      |rt        |t              rt        |      dk7  rt        |d	t        |            t        j                  |d
         st        |d|d
         t        j                  |d         st        |d|d         || _        t        |      | _        t        |      | _        || _        || _        |r|d
   t        |d         f| _        y d | _        y )N ()z has an invalid IPv4 address: z has an invalid ORPort: z has an invalid DirPort: z has an invalid fingerprint: z has an invalid nickname:    z( orport_v6 should be a two value tuple: r   z has an invalid IPv6 address: r   z has an invalid IPv6 port: )r   is_valid_ipv4_addressr   is_valid_portr   is_valid_fingerprintis_valid_nickname
isinstancetupler   stris_valid_ipv6_addressaddressintor_portdir_portfingerprintnickname	orport_v6)selfr/   r1   r2   r3   r4   r5   
identifiers           r   __init__zDirectory.__init__   sr   8@k84kJ++G4z7STT%%g.WMNN%%h/*hOPP++K8j+VWW	)55h?;QRR	5)S^q-@:WZ[dWefgg//	!=YWX\Z[[''	!5j)TU,WXXDLw<DLMDM"DDM:CilC	!$56DNDNr   c                      t        d      )a  
    Provides cached Tor directory information. This information is hardcoded
    into Tor and occasionally changes, so the information provided by this
    method may not necessarily match the latest version of tor.

    .. versionadded:: 1.5.0

    .. versionchanged:: 1.7.0
       Support added to the :class:`~stem.directory.Authority` class.

    :returns: **dict** of **str** identifiers to
      :class:`~stem.directory.Directory` instances
    KUnsupported Operation: this should be implemented by the Directory subclassNotImplementedError r   r   
from_cachezDirectory.from_cache   s      k
llr   c                     t        d      )a  
    Reads and parses tor's directory data `from gitweb.torproject.org <https://gitweb.torproject.org/>`_.
    Note that while convenient, this reliance on GitWeb means you should alway
    call with a fallback, such as...

    ::

      try:
        authorities = stem.directory.Authority.from_remote()
      except IOError:
        authorities = stem.directory.Authority.from_cache()

    .. versionadded:: 1.5.0

    .. versionchanged:: 1.7.0
       Support added to the :class:`~stem.directory.Authority` class.

    :param int timeout: seconds to wait before timing out the request

    :returns: **dict** of **str** identifiers to their
      :class:`~stem.directory.Directory`

    :raises: **IOError** if unable to retrieve the fallback directories
    r:   r;   timeouts    r   from_remotezDirectory.from_remote   s    6 k
llr   c           	      L    t         j                  j                  | dddddd      S )Nr/   r1   r2   r3   r4   r5   )stemutil
_hash_attrr6   s    r   __hash__zDirectory.__hash__   s%    99iJWacnoor   c                 T    t        |t              rt        |       t        |      k(  S dS NF)r+   r"   hashr6   others     r   __eq__zDirectory.__eq__   #    (25)(D4:e$O%Or   c                     | |k(   S r   r=   rL   s     r   __ne__zDirectory.__ne__       u}r   N<   )__name__
__module____qualname____doc__r8   staticmethodr>   rB   rH   rN   rQ   r=   r   r   r"   r"   z   sJ    .N: m m" m m8pPr   r"   c                   h     e Zd ZdZd	 fd	Zed        Zed
d       Zed        Zd Z	d Z
d Z xZS )	Authoritya  
  Tor directory authority, a special type of relay `hardcoded into tor
  <https://gitweb.torproject.org/tor.git/plain/src/or/auth_dirs.inc>`_
  to enumerate the relays in the network.

  .. versionchanged:: 1.3.0
     Added the is_bandwidth_authority attribute.

  .. versionchanged:: 1.7.0
     Added the orport_v6 attribute.

  .. deprecated:: 1.7.0
     The is_bandwidth_authority attribute is deprecated and will be removed in
     the future.

  :var str v3ident: identity key fingerprint used to sign votes and consensus
  c	                     t         t        |   ||||||       |r1t        j                  |      s|r|d|dn|}	t        |	d|      || _        || _        y )Nr$   r%   z has an invalid v3ident: )superr[   r8   r   r)   r   v3identis_bandwidth_authority)r6   r/   r1   r2   r3   r4   r5   r^   r_   r7   	__class__s             r   r8   zAuthority.__init__   sZ    	)T#GWhXW`ay55g>:BX6j*gNOODL"8Dr   c                       t        t              S r   )dictDIRECTORY_AUTHORITIESr=   r   r   r>   zAuthority.from_cache  s    %&&r   c                    	 t        j                  t        j                  t        |       j                               j                         }|st        d      	 	 i }t        |t        j                  t        t         t"        t$        ft        t$        f      D ]~  }|j'                  t              \  }}|j'                  t$              \  }	}
}t        |	||
|j)                  dd	      ||j'                  t"              |j'                  t               
      ||<    	 |S #  t        j                         dd \  }}dt        d|}t        j                  t        |||      xY w# t*        $ r}t        t-        |            d }~ww xY w)Nr@   
no contentr      z4Unable to download tor's directory authorities from : r     )r/   r1   r2   r3   r4   r5   r^   )r   r   urlliburlopenGITWEB_AUTHORITY_URLread
splitlinesIOErrorsysexc_inforD   DownloadFailedr    r[   _pop_sectionAUTHORITY_NAMEAUTHORITY_V3IDENTAUTHORITY_IPV6AUTHORITY_ADDRgetreplacer   r-   )rA   r   exc
stacktracemessageresultsr   r4   r1   r/   r2   r3   s               r   rB   zAuthority.from_remote	  sr   P##FNN3GSZ$[$`$`$bcnnpel## g'y/E/EXiky  |J  HK  Yg  iw  Xx  y 
'#KK7')0^)D&;%#++C4kk.1KK 12
	
  N=Pq+oc:Pdfijg 4c:wOO0  CHs&   AD B>E# AE #	F,F  Fc                     g }| rm|j                  | j                  d             | rK| d   j                  d      r7|j                  | j                  d             | r| d   j                  d      r7|S )z,
    Provides the next authority entry.
    r   ri   )appendpop
startswith)r   section_liness     r   rt   zAuthority._pop_section0  sj     M599Q<(eAh))#.UYYq\* eAh))#. r   c                 R    t         j                  j                  | ddt        d      S )Nr^   r_   TparentcacherD   rE   rF   r"   rG   s    r   rH   zAuthority.__hash__@  s%    99i1IT]gkllr   c                 T    t        |t              rt        |       t        |      k(  S dS rJ   )r+   r[   rK   rL   s     r   rN   zAuthority.__eq__C  rO   r   c                     | |k(   S r   r=   rL   s     r   rQ   zAuthority.__ne__F  rR   r   )NNNNNNNFrS   )rU   rV   rW   rX   r8   rY   r>   rB   rt   rH   rN   rQ   __classcell__r`   s   @r   r[   r[      s[    $9 ' ' $ $L  mPr   r[   c                   ~     e Zd ZdZd
 fd	Zedd       Zedd       Zed        Zee	fd       Z
d Zd Zd	 Z xZS )Fallbacka  
  Particularly stable relays tor can instead of authorities when
  bootstrapping. These relays are `hardcoded in tor
  <https://gitweb.torproject.org/tor.git/tree/src/or/fallback_dirs.inc>`_.

  For example, the following checks the performance of tor's fallback directories...

  ::

    import time
    from stem.descriptor.remote import get_consensus
    from stem.directory import Fallback

    for fallback in Fallback.from_cache().values():
      start = time.time()
      get_consensus(endpoints = [(fallback.address, fallback.dir_port)]).run()
      print('Downloading the consensus took %0.2f from %s' % (time.time() - start, fallback.fingerprint))

  ::

    % python example.py
    Downloading the consensus took 5.07 from 0AD3FA884D18F89EEA2D89C019379E0E7FD94417
    Downloading the consensus took 3.59 from C871C91489886D5E2E94C13EA1A5FDC4B6DC5204
    Downloading the consensus took 4.16 from 74A910646BCEEFBCD2E874FC1DC997430F968145
    ...

  .. versionadded:: 1.5.0

  .. versionchanged:: 1.7.0
     Added the has_extrainfo and header attributes which are part of
     the `second version of the fallback directories
     <https://lists.torproject.org/pipermail/tor-dev/2017-December/012721.html>`_.

  :var bool has_extrainfo: **True** if the relay should be able to provide
    extrainfo descriptors, **False** otherwise.
  :var collections.OrderedDict header: metadata about the fallback directory file this originated from
  c	                     t         t        |   ||||||       || _        |rt	        |      | _        y t	               | _        y r   )r]   r   r8   has_extrainfor   header)
r6   r/   r1   r2   r3   r4   r   r5   r   r`   s
            r   r8   zFallback.__init__q  s;    	(D"7GX{HV_`&D)/+f%DK[]DKr   c                    | t         } t        j                  j                  j	                         }|j                  |        t        |j                         D cg c]:  }|j                  d      s|j                  dd      d   |j                  |      f< c}      }i }t        |j                         D cg c]  }|j                  d      d    c}      D ]  }|dv ri }dD ]<  }|d|}|j                  |      ||<   ||   r$|dvs)t        d|d	t                |d
   r|d   r|d
   t        |d         f}	nd }	t        |d   t        |d         t        |d         ||d   |d   dk(  |	|      ||<    |S c c}w c c}w )Nzheader..r   r   )
tor_commitstem_commitr   )r/   r1   r2   r4   r   orport6_addressorport6_port)r4   r   r   r   'z' is missing from r   r   r/   r1   r2   r4   r   truer/   r1   r2   r3   r4   r   r5   r   )FALLBACK_CACHE_PATHrD   rE   confConfigloadr   keysr   splitry   setrp   r0   r   )
pathr   kheadersr~   keyr3   attr	attr_namer5   s
             r   r>   zFallback.from_cachev  s   |"99>>  "DIIdOdiikmUVUaUabkUlAGGCOA.<mnGGE#CIIcN1-EF 	?	?dy P)$i0((3-YI94t#tS:MNO
OP 
	 T.%9+,c$~2F.GH		%y/d9o&tJ'(!
#_-7	gk%: NC n Fs   F0(F8Fc                    	 t        j                  t        j                  t        |       j                               j                         }|st        d      	 |d   dk7  rt        d	t        z        i }t        j                  |      D ]J  }t        j                  |      }|r$|j!                  d
      ||j!                  d      <   >t        d|z         t        j                  |       	 i }t#        |t        j                  t$        t&        t(        t*        ft$        f      D ]t  }	|	t$           \  }
}}}t        |
t-        |      t-        |      ||	j/                  t&              |	j/                  t(              dk(  |	j/                  t*              |      ||<   v 	 |S #  t        j                         dd \  }}dt        d|}t        j                  t        |||      xY w# t0        $ r}t        t3        |            d }~ww xY w)Nr@   re   r   rf   z3Unable to download tor's fallback directories from rg   r   z/* type=fallback */zJ%s does not have a type field indicating it is fallback directory metadatar&   z,Malformed fallback directory header line: %srh   1r   )r   r   rk   rl   GITWEB_FALLBACK_URLrn   ro   rp   rq   rr   rD   rs   r   rt   FALLBACK_MAPPINGmatchgroupr    FALLBACK_ADDRFALLBACK_NICKNAMEFALLBACK_EXTRAINFOFALLBACK_IPV6r0   ry   r   r-   )rA   r   r{   r|   r}   r   r   mappingr~   r   r/   r2   r1   r3   s                 r   rB   zFallback.from_remote  s   O##FNN3FRY$Z$_$_$abmmoel##  Qx((`cvvwwF%%e, M &&t,g	#*==#3w}}Q DtKLLM % g'x/D/D}Vgi{  ~K  GL  Zg  Yi  j 
'29-2H/7K'L]#[[!23!++&89S@kk-0	 

  NaOq+oc:Obdghg 3S*gNNT  CHs&   AF  .B/G'  AG$'	H	0HH	c                     g }| r]| j                  d      }| rJ|t        k7  rA|j                         dk7  r|j                  |       | j                  d      }| r
|t        k7  rA|S )z
    Provides lines up through the next divider. This excludes lines with just a
    comma since they're an artifact of these being C strings.
    r   ,)r   FALLBACK_DIVstripr   )r   r   r   s      r   rt   zFallback._pop_section  s`     MYYq\ddl*::<3


t
$yy|	 dl* r   c                    t         j                  j                  j                         }|j	                  d|       |j	                  d|       |j                         D ]  \  }}|j	                  d|z  |        t        | j                         d       D ]#  }|j                  }	|j	                  d|	z  |j                         |j	                  d|	z  t        |j                               |j	                  d|	z  t        |j                               |j	                  d	|	z  |j                         |j	                  d
|	z  |j                  rdnd       |j                  s|j	                  d|	z  t        |j                  d                |j	                  d|	z  t        |j                  d                & |j!                  |       y)a  
    Persists fallback directories to a location in a way that can be read by
    from_cache().

    :param dict fallbacks: mapping of fingerprints to their fallback directory
    :param str tor_commit: tor commit the fallbacks came from
    :param str stem_commit: stem commit the fallbacks came from
    :param dict headers: metadata about the file these came from
    :param str path: location fallbacks will be persisted to
    r   r   z	header.%sc                     | j                   S r   )r3   )xs    r   <lambda>z!Fallback._write.<locals>.<lambda>   s
     r   )r   z
%s.addressz
%s.or_portz%s.dir_portz%s.nicknamez%s.has_extrainfor   falsez%s.orport6_addressr   z%s.orport6_portr   N)rD   rE   r   r   r   itemssortedvaluesr3   r/   r-   r1   r2   r4   r   r5   save)
	fallbacksr   r   r   r   r   r   v	directoryr3   s
             r   _writezFallback._write  s{    99>>  "DHH\:&HH]K( #1
hh{Q"# I,,.6MN 
O	))k
hh|k)9+<+<=
hh|k)3y/@/@+AB
hh}{*C	0B0B,CD
hh}{*I,>,>?
hh!K/9;R;RX_`			%3S9L9LQ9O5PQ"[0#i6I6I!6L2MN
O 	IIdOr   c                 R    t         j                  j                  | ddt        d      S )Nr   r   Tr   r   rG   s    r   rH   zFallback.__hash__  s#    99ox)]abbr   c                 T    t        |t              rt        |       t        |      k(  S dS rJ   )r+   r   rK   rL   s     r   rN   zFallback.__eq__  s#    (25((C4:e$NNr   c                     | |k(   S r   r=   rL   s     r   rQ   zFallback.__ne__  rR   r   )NNNNNFNNr   rS   )rU   rV   rW   rX   r8   rY   r>   rB   rt   r   r   rH   rN   rQ   r   r   s   @r   r   r   J  sv    $LC
 & &P 6 6p  & AT  BcOr   r   c                    g }t        |j                               j                  | j                               }t        | j                               j                  |j                               }|D ]  }||   }|j                  rd|j                  z  nd}|d|j                  z  d|j
                  z  d|j                  z  d|j                  z  d|j                  z  d|j                  z  d	|z  d
gz  } |D ]  }|j                  d|z          |D ]]  }||v s||v r| |   }||   }	||	k7  sdD ]=  }
t        ||
      }t        |	|
      }||k7  s!|j                  d|
d|d|d|       ? _ dj                  |      S )z@
  Provides a description of how fallback directories differ.
  z%s:%sz[none]z'* Added %s as a new fallback directory:z  address: %sz  or_port: %sz  dir_port: %sz  nickname: %sz  has_extrainfo: %sz  orport_v6: %srj   z$* Removed %s as a fallback directory)r/   r1   r2   r3   r5   z* Changed the z of z from z to r	   )r   r   
differencer5   r3   r/   r1   r2   r4   r   r   getattrr   )previous_directoriesnew_directoriesr   added_fp
removed_fpfpr   r5   previous_directorynew_directoryr   old_attrnew_attrs                r   _fallback_directory_differencesr     s   
 %%%'(334H4M4M4OP(',,./::?;O;O;QR* b#I1:1D1D)---(I	/)2G2GG	)))	)))++++++i555)#	 	E	  >b	LL7"<=>  `b	X~z)-b1#B'M]*P `$-t4=$/x
,,r8U]^
_`` 
5	r   moria1z128.31.0.39i#  i#  (9695DFC35FFEB861329B9F1AB04C46397020CE31(D586D18309DED4CD6D57C18FDB97EFA96D330566)r4   r/   r1   r2   r3   r^   tor26z86.59.21.38  P   (847B1F850344D7876491A54892F904934E4EB85D)z2001:858:2:2:aabb:0:563b:1526r   (14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4)r4   r/   r1   r2   r3   r5   r^   dizumz45.66.33.45(7EA6EAD6FD83083C538F44038BBFA077587DD755(E8A9C45EDE6D711294FADF8E7951F4DE6CA56B58gabelmooz131.188.40.189(F2044413DAC2E02E3D6BCF4735A19BCA1DE97281)z2001:638:a000:4140::ffff:189r   (ED03BB616EB2F60BEC80151114BB25CEF515B226
dannenbergz193.23.244.244)z2001:678:558:1000::244r   (7BE683E65D48141321C5ED92F075C55364AC7123(0232AF901C31A04EE9848595AF9BB7620D4C5B2E)r4   r/   r1   r2   r5   r3   r^   maatuskaz171.25.193.9(BD6A829255CB08E66FBE7D3748363586E46B3810)z2001:67c:289c::9r   (49015F787433103580E3B66A1707A00E60F2D15B	Faravaharz154.35.175.225(CF6D0AAFB385BE71B8E111FC5CFF4B47923733BC(EFCBE720AB3A82B99F9E953CD5BF50F7EEFC7B97longclawz199.58.81.140(74A910646BCEEFBCD2E874FC1DC997430F968145(23D15D965BC35114467363C165C4F724B64B4F66bastetz204.13.164.118(24E2F139121D4394C54B5BCC368B3B411857C413)z2620:13:4000:6000::1000:118r   (27102BC123E7AF1D4741AE047E160C91ADC76B21Sergez66.111.2.131i)#  iF#  (BA44A889E64B93FAA2B114E02C2A279A8555C533)
r   r   r   r   r   r   r   r   r   r   r   ),rX   osrerq   rD   	stem.utilstem.util.confr   r   r   collectionsr   ImportErrorstem.util.ordereddicturllib.requestrequestrk   urllib2rm   r   r   r   dirname__file__r   compileru   rv   rw   rx   r   r   r   r   r   r   r   r    objectr"   r[   r   r   rc   r=   r   r   <module>r     s  #J 
 	 
    6 60%! b d ggll277??8#<>TU 78BJJ:; ?@CD2::=> 

RSBJJ:; RZZ => 

>?B+k k\_	 _DKy K\+^ <8 <68 <8 <58 /<8 <(8 <8 <8 <48 <]V a  0//0  s"   G G GG	G&%G&