o
    ßOæ_|[  ã                   @   s4  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	 zddl
mZ W n ey;   ddlmZ Y nw zddlmZ W n eyQ   ddlZY nw dZdZej ej e¡d¡Ze d¡Ze d	¡Ze d
¡Ze d¡ZdZe d¡Ze d¡Z e d¡Z!e d¡Z"e d¡Z#dTdd„Z$dTdd„Z%G dd„ de&ƒZ'G dd„ de'ƒZ(G dd„ de'ƒZ)dd„ Z*e(ddd d!d"d#d$e(d%d&d'd(d)d*d+d,e(d-d.d'd(d/d0d$e(d1d2d'd(d3d4d5d,e(d6d7d'd(d8d9d:d;e(d<d=d(d'd>d?d@d,e(dAdBd'd(dCdDd$e(dEdFd'd(dGdHd$e(dIdJd'd(dKdLdMd,e(dNdOdPdQdRdd$dSœ
Z+dS )Uaç  
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           	      C   s|   i }| D ]#}|D ]}|  t |¡¡}|r&| ¡ }t|ƒdkr |n|d ||< qq|r<|D ]}||vr;tdd | ¡ ƒ‚q,|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ÚlineZmatcherÚmZmatch_groupsZrequired_matcher© r   ú0/usr/lib/python3/dist-packages/stem/directory.pyÚ_match_withQ   s   €ûÿr   c                 c   s0    || ƒ}|rt |||ƒV  || ƒ}|sd S d S ©N)r   )r   Zpop_section_funcr   r   Znext_sectionr   r   r   Ú_directory_entriesr   s   €þr   c                   @   sJ   e Zd ZdZdd„ Zedd„ ƒZeddd„ƒZd	d
„ Zdd„ Z	dd„ Z
dS )Ú	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                 C   sZ  |rd||f n|}t  |¡std||f ƒ‚t  |¡s$td||f ƒ‚t  |¡s1td||f ƒ‚t |¡s>td||f ƒ‚|rMt |¡sMtd||f ƒ‚|r†t|tƒrZt	|ƒdkrdtd|t
|ƒf ƒ‚t  |d	 ¡sutd
||d	 f ƒ‚t  |d ¡s†td||d f ƒ‚|| _t|ƒ| _t|ƒ| _|| _|| _|r¨|d	 t|d ƒf| _d S d | _d S )Nú%s (%s)z"%s has an invalid IPv4 address: %sz%s has an invalid ORPort: %sz%s has an invalid DirPort: %sz!%s has an invalid fingerprint: %sz%s has an invalid nickname: %sé   z,%s orport_v6 should be a two value tuple: %sr   z"%s has an invalid IPv6 address: %sr   z%s has an invalid IPv6 port: %s)r   Zis_valid_ipv4_addressr   Zis_valid_portr   Úis_valid_fingerprintZis_valid_nicknameÚ
isinstanceÚtupler   ÚstrZis_valid_ipv6_addressÚaddressÚintÚor_portÚdir_portÚfingerprintÚnicknameÚ	orport_v6)Úselfr    r"   r#   r$   r%   r&   Ú
identifierr   r   r   Ú__init__’   s0   





(zDirectory.__init__c                   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©ÚNotImplementedErrorr   r   r   r   Ú
from_cache¯   s   zDirectory.from_cacheé<   c                 C   r*   )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,   ©Útimeoutr   r   r   Úfrom_remoteÁ   s   zDirectory.from_remotec              	   C   s   t j | dddddd¡S )Nr    r"   r#   r$   r%   r&   )ÚstemÚutilÚ
_hash_attr©r'   r   r   r   Ú__hash__Þ   s   zDirectory.__hash__c                 C   ó   t |tƒrt| ƒt|ƒkS dS ©NF)r   r   Úhash©r'   Úotherr   r   r   Ú__eq__á   ó   zDirectory.__eq__c                 C   ó
   | |k S r   r   r;   r   r   r   Ú__ne__ä   ó   
zDirectory.__ne__N©r/   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   Ústaticmethodr.   r2   r7   r=   r@   r   r   r   r   r   z   s    
r   c                       s`   e Zd ZdZd‡ fdd„	Zedd„ ƒZedd	d
„ƒZedd„ ƒZdd„ Z	dd„ Z
dd„ Z‡  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
  NFc	           
         s\   t t| ƒ ||||||¡ |r&t |¡s&|rd||f n|}	td|	|f ƒ‚|| _|| _d S )Nr   z%s has an invalid v3ident: %s)ÚsuperrH   r)   r   r   r   Úv3identÚis_bandwidth_authority)
r'   r    r"   r#   r$   r%   r&   rJ   rK   r(   ©Ú	__class__r   r   r)   û   s   
zAuthority.__init__c                   C   s   t tƒS r   )ÚdictÚDIRECTORY_AUTHORITIESr   r   r   r   r.     s   zAuthority.from_cacher/   c                 C   s  zt  tjt| d ¡ ¡ ¡ }|stdƒ‚W n   t 	¡ dd… \}}dt|f }t
 t|||¡‚z=i }t|tjttttfttfdD ](}| t¡\}}| t¡\}	}
}t|	||
| dd¡|| t¡| t¡d	||< qDW |S  ty } ztt|ƒƒ‚d }~ww )
Nr0   ú
no contentr   é   z:Unable to download tor's directory authorities from %s: %s©r   ú Ú )r    r"   r#   r$   r%   r&   rJ   )r   r	   ÚurllibÚurlopenÚGITWEB_AUTHORITY_URLÚreadÚ
splitlinesÚIOErrorÚsysÚexc_infor3   ÚDownloadFailedr   rH   Ú_pop_sectionÚAUTHORITY_NAMEÚAUTHORITY_V3IDENTÚAUTHORITY_IPV6ÚAUTHORITY_ADDRÚgetÚreplacer   r   )r1   r   ÚexcÚ
stacktraceÚmessageÚresultsr   r%   r"   r    r#   r$   r   r   r   r2   	  s:   ÿ"
ùüý€ÿzAuthority.from_remotec                 C   sP   g }| r&|  |  d¡¡ | r&| d  d¡r&|  |  d¡¡ | r&| d  d¡s|S )z,
    Provides the next authority entry.
    r   rS   )ÚappendÚpopÚ
startswith)r   Úsection_linesr   r   r   r^   0  s   ÿzAuthority._pop_sectionc                 C   ó   t jj| ddtddS )NrJ   rK   T©ÚparentÚcache©r3   r4   r5   r   r6   r   r   r   r7   @  ó   zAuthority.__hash__c                 C   r8   r9   )r   rH   r:   r;   r   r   r   r=   C  r>   zAuthority.__eq__c                 C   r?   r   r   r;   r   r   r   r@   F  rA   zAuthority.__ne__)NNNNNNNFrB   )rC   rD   rE   rF   r)   rG   r.   r2   r^   r7   r=   r@   Ú__classcell__r   r   rL   r   rH   è   s    

&
rH   c                       sr   e Zd ZdZd‡ fdd„	Zeddd„ƒZedd	d
„ƒZedd„ ƒZee	fdd„ƒZ
dd„ Zdd„ Zdd„ Z‡  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
  NFc	           	         s>   t t| ƒ ||||||¡ || _|rt|ƒ| _d S tƒ | _d S r   )rI   rt   r)   Úhas_extrainfor   Úheader)	r'   r    r"   r#   r$   r%   ru   r&   rv   rL   r   r   r)   q  s   zFallback.__init__c                    s  | d u rt } tjj ¡ ‰ ˆ  | ¡ t‡ fdd„ˆ  ¡ D ƒƒ}i }tdd„ ˆ  ¡ D ƒƒD ]^}|dv r2q+i }dD ]}d||f }ˆ  	|¡||< || sU|dvrUt
d|t f ƒ‚q6|d	 ri|d
 ri|d	 t|d
 ƒf}nd }t|d t|d ƒt|d ƒ||d |d dk||d||< q+|S )Nc                    s0   g | ]}|  d ¡r| dd¡d ˆ  |¡f‘qS )zheader.Ú.r   )rk   Úsplitrc   )Ú.0Úk©Úconfr   r   Ú
<listcomp>|  s   0 z'Fallback.from_cache.<locals>.<listcomp>c                 S   s   g | ]	}|  d ¡d ‘qS )rw   r   )rx   )ry   Úkeyr   r   r   r}   €  s    )Ú
tor_commitÚstem_commitrv   )r    r"   r#   r%   ru   Úorport6_addressÚorport6_portz%s.%s)r%   ru   r   r‚   z'%s' is missing from %sr   r‚   r    r"   r#   r%   ru   Útrue©r    r"   r#   r$   r%   ru   r&   rv   )ÚFALLBACK_CACHE_PATHr3   r4   r|   ÚConfigÚloadr   ÚkeysÚsetrc   rZ   r!   rt   )ÚpathÚheadersrh   r$   ÚattrZ	attr_namer~   r&   r   r{   r   r.   v  s<   
€


øzFallback.from_cacher/   c                 C   sd  zt  tjt| d ¡ ¡ ¡ }|stdƒ‚W n   t 	¡ dd… \}}dt|f }t
 t|||¡‚|d dkr>tdt ƒ‚i }t |¡D ]}t |¡}|rY| d	¡|| d¡< qEtd
| ƒ‚t |¡ z;i }t|tjttttftfdD ]'}	|	t \}
}}}t|
t|ƒt|ƒ||	 t¡|	 t¡dk|	 t¡|d||< quW |S  ty± } ztt|ƒƒ‚d }~ww )Nr0   rP   r   rQ   z9Unable to download tor's fallback directories from %s: %sr   z/* type=fallback */zJ%s does not have a type field indicating it is fallback directory metadatar   z,Malformed fallback directory header line: %srR   Ú1r„   )r   r	   rU   rV   ÚGITWEB_FALLBACK_URLrX   rY   rZ   r[   r\   r3   r]   rt   r^   ÚFALLBACK_MAPPINGÚmatchÚgroupr   ÚFALLBACK_ADDRÚFALLBACK_NICKNAMEÚFALLBACK_EXTRAINFOÚFALLBACK_IPV6r!   rc   r   r   )r1   r   re   rf   rg   rv   r   Úmappingrh   r   r    r#   r"   r$   r   r   r   r2   Ÿ  sL   ÿ

	 øýý€ÿzFallback.from_remotec                 C   sN   g }| r%|   d¡}| r%|tkr%| ¡ dkr| |¡ |   d¡}| r%|tks|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   ú,)rj   ÚFALLBACK_DIVÚstripri   )r   rl   r   r   r   r   r^   Ø  s   


üzFallback._pop_sectionc           
      C   s  t jj ¡ }| d|¡ | d|¡ | ¡ D ]\}}| d| |¡ qt|  ¡ dd„ dD ]W}|j}	| d|	 |j	¡ | d|	 t
|jƒ¡ | d	|	 t
|jƒ¡ | d
|	 |j¡ | d|	 |jrddnd¡ |jr„| d|	 t
|jd ƒ¡ | d|	 t
|jd ƒ¡ q-| |¡ dS )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                 S   s   | j S r   )r$   )Úxr   r   r   Ú<lambda>   s    z!Fallback._write.<locals>.<lambda>)r~   z
%s.addressz
%s.or_portz%s.dir_portz%s.nicknamez%s.has_extrainforƒ   Zfalsez%s.orport6_addressr   z%s.orport6_portr   N)r3   r4   r|   r†   r‰   ÚitemsÚsortedÚvaluesr$   r    r   r"   r#   r%   ru   r&   Zsave)
Z	fallbacksr   r€   r‹   rŠ   r|   rz   ÚvÚ	directoryr$   r   r   r   Ú_writeì  s"   €zFallback._writec                 C   rm   )Nru   rv   Trn   rq   r6   r   r   r   r7     rr   zFallback.__hash__c                 C   r8   r9   )r   rt   r:   r;   r   r   r   r=     r>   zFallback.__eq__c                 C   r?   r   r   r;   r   r   r   r@     rA   zFallback.__ne__)NNNNNFNNr   rB   )rC   rD   rE   rF   r)   rG   r.   r2   r^   r…   r¡   r7   r=   r@   rs   r   r   rL   r   rt   J  s    &(8
!rt   c              
   C   s(  g }t | ¡ ƒ |  ¡ ¡}t |  ¡ ƒ | ¡ ¡}|D ]0}|| }|jr(d|j nd}|d|j d|j d|j d|j d|j d|j	 d	| d
g7 }q|D ]	}| 
d| ¡ qM|D ]5}||v sc||v rdqY| | }|| }	||	krŽdD ]}
t||
ƒ}t|	|
ƒ}||kr| 
d|
|||f ¡ qrqYd |¡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: %srT   z$* Removed %s as a fallback directory)r    r"   r#   r$   r&   z$* Changed the %s of %s from %s to %sr   )r‰   rˆ   Ú
differencer&   r$   r    r"   r#   r%   ru   ri   Úgetattrr   )Zprevious_directoriesZnew_directoriesr   Zadded_fpZ
removed_fpÚfpr    r&   Zprevious_directoryZnew_directoryrŒ   Zold_attrZnew_attrr   r   r   Ú_fallback_directory_differences  s>   ø

€
r¥   Úmoria1z128.31.0.39i#  i«#  Z(9695DFC35FFEB861329B9F1AB04C46397020CE31Z(D586D18309DED4CD6D57C18FDB97EFA96D330566)r%   r    r"   r#   r$   rJ   Útor26z86.59.21.38é»  éP   Z(847B1F850344D7876491A54892F904934E4EB85D)z2001:858:2:2:aabb:0:563b:1526r¨   Z(14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4)r%   r    r"   r#   r$   r&   rJ   Údizumz45.66.33.45Z(7EA6EAD6FD83083C538F44038BBFA077587DD755Z(E8A9C45EDE6D711294FADF8E7951F4DE6CA56B58Úgabelmooz131.188.40.189Z(F2044413DAC2E02E3D6BCF4735A19BCA1DE97281)z2001:638:a000:4140::ffff:189r¨   Z(ED03BB616EB2F60BEC80151114BB25CEF515B226Ú
dannenbergz193.23.244.244)z2001:678:558:1000::244r¨   Z(7BE683E65D48141321C5ED92F075C55364AC7123Z(0232AF901C31A04EE9848595AF9BB7620D4C5B2E)r%   r    r"   r#   r&   r$   rJ   Úmaatuskaz171.25.193.9Z(BD6A829255CB08E66FBE7D3748363586E46B3810)z2001:67c:289c::9r©   Z(49015F787433103580E3B66A1707A00E60F2D15BÚ	Faravaharz154.35.175.225Z(CF6D0AAFB385BE71B8E111FC5CFF4B47923733BCZ(EFCBE720AB3A82B99F9E953CD5BF50F7EEFC7B97Úlongclawz199.58.81.140Z(74A910646BCEEFBCD2E874FC1DC997430F968145Z(23D15D965BC35114467363C165C4F724B64B4F66Úbastetz204.13.164.118Z(24E2F139121D4394C54B5BCC368B3B411857C413)z2620:13:4000:6000::1000:118r¨   Z(27102BC123E7AF1D4741AE047E160C91ADC76B21ÚSergez66.111.2.131i)#  iF#  Z(BA44A889E64B93FAA2B114E02C2A279A8555C533)
r¦   r§   rª   r«   r¬   r­   r®   r¯   r°   r±   r   ),rF   ÚosÚrer[   r3   Z	stem.utilZstem.util.confr   r   r   Úcollectionsr   ÚImportErrorZstem.util.ordereddictZurllib.requestZrequestrU   Zurllib2rW   rŽ   rŠ   r   ÚdirnameÚ__file__r…   Úcompiler_   r`   ra   rb   r˜   r   r’   r“   r”   r•   r   r   Úobjectr   rH   rt   r¥   rO   r   r   r   r   Ú<module>   sø   %ÿÿ










!nb O/úù	úù	ù	ù	úúù	ú
²