o
    Òó·iÇ  ã                   @   sz   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ dd	„ Zd
d„ ZG dd„ dejƒZdS )z
hosts(5) support.
é    )Údefer)ÚisIPAddressÚisIPv6Address)ÚcommonÚdns)Úfailure)ÚnativeString)ÚFilePathc              	   C   sÊ   g }z|   ¡  ¡ }W n ty   | Y S w | ¡ }|D ]F}| d¡}|dkr-|d|… }|s0q| ¡ }| ¡ dd„ |dd… D ƒv rbzt|d ƒ}W n	 tyT   Y qw t|ƒs]t	|ƒrb| 
|¡ q|S )a±  
    Search the given file, which is in hosts(5) standard format, for addresses
    associated with a given name.

    @param hostsFile: The name of the hosts(5)-format file to search.
    @type hostsFile: L{FilePath}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the address in the file associated with the name.
    ó   #éÿÿÿÿNc                 S   s   g | ]}|  ¡ ‘qS © )Úlower)Ú.0Úsr   r   úYC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/names/hosts.pyÚ
<listcomp>/   s    z$searchFileForAll.<locals>.<listcomp>é   r   )Z
getContentÚ
splitlinesÚBaseExceptionr   ÚfindÚsplitr   Ú
ValueErrorr   r   Úappend)Z	hostsFileÚnameÚresultsÚlinesÚlineÚidxÚpartsZmaybeIPr   r   r   ÚsearchFileForAll   s0   ÿ
ÿ
€r   c                 C   s   t t| ƒ|ƒ}|r|d S dS )a²  
    Grep given file, which is in hosts(5) standard format, for an address
    entry with a given name.

    @param file: The name of the hosts(5)-format file to search.
    @type file: C{str} or C{bytes}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the first address in the file associated with
        the name.
    r   N)r   r	   )Úfiler   Ú	addressesr   r   r   ÚsearchFileFor9   s   r"   c                   @   sJ   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zddd„Zddd„Z	eZ
dS )ÚResolverz9
    A resolver that services hosts(5) format files.
    ó
   /etc/hostsé  c                 C   s   t j | ¡ || _|| _d S ©N)r   ÚResolverBaseÚ__init__r    Úttl)Úselfr    r)   r   r   r   r(   S   s   
zResolver.__init__c                    ó$   t ‡ ‡fdd„ttˆjƒˆ ƒD ƒƒS )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv4
        addresses in the hosts file.
        c                 3   ó:   � | ]}t |ƒrt ˆ tjtjˆjt |ˆj¡¡V  qd S r&   )r   r   ÚRRHeaderÚAÚINr)   ZRecord_A©r   Úaddr©r   r*   r   r   Ú	<genexpr>]   s   € ý 
ÿz%Resolver._aRecords.<locals>.<genexpr>N©Útupler   r	   r    ©r*   r   r   r2   r   Ú	_aRecordsX   s   þzResolver._aRecordsc                    r+   )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv6
        addresses in the hosts file.
        c                 3   r,   r&   )r   r   r-   ZAAAAr/   r)   ZRecord_AAAAr0   r2   r   r   r3   h   s   € ûÿ
ÿz(Resolver._aaaaRecords.<locals>.<genexpr>Nr4   r6   r   r2   r   Ú_aaaaRecordsc   s   üzResolver._aaaaRecordsc                 C   s*   |r
t  |ddf¡S t  t t |¡¡¡S )aF  
        Generate a response for the given name containing the given result
        records, or a failure if there are no result records.

        @param name: The DNS name the response is for.
        @type name: C{str}

        @param records: A tuple of L{dns.RRHeader} instances giving the results
            that will go into the response.

        @return: A L{Deferred} which will fire with a three-tuple of result
            records, authority records, and additional records, or which will
            fail with L{dns.DomainError} if there are no result records.
        r   N)r   ZsucceedZfailr   ZFailurer   ZDomainError)r*   r   Úrecordsr   r   r   Ú_respondp   s   zResolver._respondNc                 C   ó   t  |¡}|  ||  |¡¡S )zm
        Read any IPv4 addresses from C{self.file} and return them as
        L{Record_A} instances.
        N)r   ÚdomainStringr:   r7   ©r*   r   Útimeoutr   r   r   ÚlookupAddressƒ   ó   
zResolver.lookupAddressc                 C   r;   )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        N)r   r<   r:   r8   r=   r   r   r   ÚlookupIPV6Address‹   r@   zResolver.lookupIPV6Address)r$   r%   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r7   r8   r:   r?   rA   ZlookupAllRecordsr   r   r   r   r#   N   s    


r#   N)rE   Ztwisted.internetr   Ztwisted.internet.abstractr   r   Ztwisted.namesr   r   Ztwisted.pythonr   Ztwisted.python.compatr   Ztwisted.python.filepathr	   r   r"   r'   r#   r   r   r   r   Ú<module>   s   '