U
    @f                     @   s  d Z ddlZddl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	 ddlm
Z
 ddlmZ d	d
 ZedZede_ejjdddg ejjD ]Zeejeede  qejjjD ]Zeejjeede  qejjjD ]Zeejjeede  qejD ](Zedkr&qeeeede  q[eZdZdZeejdZeejdZeZejdd  dkre!edreej"dZdd Z#dd  Z$d!d" Z%d#d$ Z&e!ej'd%rd&d' Z(nd(d' Z(G d)d* d*ej)j*Z+G d+d, d,Z,G d-d. d.Z-e-e, d/Z)ej.d0dd0fd1d2Z/d3d4 Z0d5d6 Z1d7d8 Z2dNd9d:Z3d;d< Z4d=d> Z5d?d@ Z6dAdB Z7dCdD Z8zej9j:dd0dE W n6 ej9j;k
r   d0Z<Y n e=k
r   dFZ<Y nX edGddddFdFdFdFddFfdHdIZ>edGddddFdFdfdJdKZ?dLdM Z@e?ej'_?e>ej'_>dS )Oz1greendns - non-blocking DNS support for Eventlet
    N)patcher)_socket_nodns)os)time)select)sslc                 C   s   t ttttd}tj| f|S )N)r   r   r   socketr   )r   r   r   r   r   r   import_patched)module_namemodules r   S/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/support/greendns.pyr	   0   s    r	   dnszdns.rdtypesZ
dnskeybaseZdsbaseZtxtbasezdns.rdtypes.zdns.rdtypes.IN.zdns.rdtypes.ANY.rdtypeszdns.g      $@zLookup timed outzName or service not knownZEVENTLET_DEPRECATED_EAI_NODATA )1yyes
EAI_NODATA#No address associated with hostnamec                 C   s   | j | j d S N)	__class__args)Zerror_instancer   r   r   _raise_new_errorc   s    r   c                 C   sB   t | tsdS ztj|  W n tjjk
r8   Y dS X dS dS )z+Return True if host is a valid IPv4 addressFTN)
isinstancestrr   Zipv4	inet_aton	exceptionSyntaxErrorhostr   r   r   is_ipv4_addrg   s    
r!   c                 C   sR   t | tsdS | ddd } ztj|  W n tjjk
rH   Y dS X dS dS )z+Return True if host is a valid IPv6 addressF%   r   TN)r   r   splitr   ipv6r   r   r   r   r   r   r   is_ipv6_addrs   s    
r&   c                 C   s   t | pt| S )z3Return True if host is a valid IPv4 or IPv6 address)r!   r&   r   r   r   r   
is_ip_addr   s    r'   _compute_expirationc                 C   s
   |  |S r   )r(   querytimeoutr   r   r   compute_expiration   s    r,   c                 C   s   |  |d S )Nr#   )Z_compute_timesr)   r   r   r   r,      s    c                   @   s   e Zd ZdZdddZdS )HostsAnswerz%Answer class for HostsResolver objectTc                 C   sX   d| _ || _|| _|| _|| _|s0|r0tj || _t	|drNt

 |j nd| _dS )a^  Create a new answer

        :qname: A dns.name.Name instance of the query name
        :rdtype: The rdatatype of the query
        :rdclass: The rdataclass of the query
        :rrset: The dns.rrset.RRset with the response, must have ttl attribute
        :raise_on_no_answer: Whether to raise dns.resolver.NoAnswer if no
           answer.
        Nttlr   )responseqnamerdtyperdclasscanonical_namer   resolverNoAnswerrrsethasattrr   r.   
expiration)selfr0   r1   r2   r6   raise_on_no_answerr   r   r   __init__   s    

zHostsAnswer.__init__N)T)__name__
__module____qualname____doc__r;   r   r   r   r   r-      s   r-   c                   @   s`   e Zd ZdZedejZdefddZ	dd Z
dd	 Zejjejjd
ddfddZdd ZdS )HostsResolverzClass to parse the hosts file

    Attributes
    ----------

    :fname: The filename of the hosts file in use.
    :interval: The time between checking for hosts file modification
    z
        \s*  # Leading space
        ([^\r\n#]*?)  # The actual match, non-greedy so as not to include trailing space
        \s*  # Trailing space
        (?:[#][^\r\n]+)?  # Comments
        (?:$|[\r\n]+)  # EOF or newline
    Nc                 C   sh   i | _ i | _i | _|| _|| _|d krPtjdkr8d| _ntjdkrPtjd| _d| _	| jrd| 
  d S )Nposixz
/etc/hostsntz'%SystemRoot%\system32\drivers\etc\hostsr   )_v4_v6_aliasesintervalfnamer   namepath
expandvars
_last_load_load)r9   rG   rF   r   r   r   r;      s    

zHostsResolver.__init__c              	   C   s\   z$t | jd}| }W 5 Q R X W n tk
r<   g  Y S X |jdd}td| j|S )zRead the contents of the hosts file

        Return list of lines, comment lines and empty lines are
        excluded.

        Note that this performs disk I/O so can be blocking.
        rbignore)errorsN)openrG   readOSErrordecodefilterLINES_REfindall)r9   fpfdataZudatar   r   r   
_readlines   s    
zHostsResolver._readlinesc                 C   s   |   }| j  | j  | j  |D ]}| }t|dk rDq*|d}t|r^| j}nt	|r*|
drrq*| j}nq*|d }|||< |D ]}| }|||< || j|< qq*t | _dS )zjLoad hosts file

        This will unconditionally (re)load the data from the hosts
        file.
           r   Zfe80N)rY   rC   clearrD   rE   r$   lenpopr!   r&   
startswithlowerr   rK   )r9   lineslinepartsipZipmapcnamealiasr   r   r   rL      s.    




zHostsResolver._loadFTc              	   C   s  t   }| j| j |k r |   tjj}t|trD|}tj	
|}n*t|trf|d}tj	
|}nt|}| }tj|||}	| j| j | |	_|tjjkr|tjjkr| j|}
|
s| r| j|dd }
|
r|	tjjj|||
 n|tjjkrj|tjjkrj| j|}
|
sH| rH| j|dd }
|
r|	tjjj|||
 nt|tjjkr|tjjkr| j|}|s| r| j|dd }|r|	tjjj||tj	
| t||||	|S )aY  Query the hosts file

        The known rdtypes are dns.rdatatype.A, dns.rdatatype.AAAA and
        dns.rdatatype.CNAME.

        The ``rdclass`` parameter must be dns.rdataclass.IN while the
        ``tcp`` and ``source`` parameters are ignored.

        Return a HostAnswer instance or raise a dns.resolver.NoAnswer
        exception.
        asciiN)r   rK   rF   rL   r   
rdataclassINr   r   rH   	from_textbytesrS   r_   r6   ZRRsetr.   	rdatatypeArC   getis_absoluteaddr   AAAArD   CNAMErE   ANYr-   )r9   r0   r1   r2   tcpsourcer:   nowrH   r6   addrrd   r   r   r   r*      sJ    


  
zHostsResolver.queryc                 C   s\   g }|| j kr| j | }n|}|| | j  D ]\}}||kr2|| q2|| |S )z1Return a list of all the aliases of a given cname)rE   appenditemsremove)r9   hostnamealiasesZcannonre   rd   r   r   r   
getaliases-  s    


zHostsResolver.getaliases)r<   r=   r>   r?   recompileVERBOSErU   	HOSTS_TTLr;   rY   rL   r   rl   rm   rh   ri   r*   r}   r   r   r   r   r@      s   	   
1r@   c                   @   sp   e Zd ZdZdddZedd Zejdd Zd	d
 Ze	j
je	jjddde	j
je	j
jfdfddZdd ZdS )ResolverProxyzResolver class which can also use /etc/hosts

    Initialise with a HostsResolver instance in order for it to also
    use the hosts file.
    N/etc/resolv.confc                 C   s   || _ || _d| _dS )ah  Initialise the resolver proxy

        :param hosts_resolver: An instance of HostsResolver to use.

        :param filename: The filename containing the resolver
           configuration.  The default value is correct for both UNIX
           and Windows, on Windows it will result in the configuration
           being read from the Windows registry.
        N)_hosts	_filename_cached_resolver)r9   hosts_resolverfilenamer   r   r   r;   F  s    
zResolverProxy.__init__c                 C   s   | j d kr|   | j S r   )r   r[   r9   r   r   r   	_resolverX  s    
zResolverProxy._resolverc                 C   s
   || _ d S r   )r   )r9   valuer   r   r   r   ^  s    c                 C   s$   t jj| jd| _t j | j_d S )N)r   )r   r4   Resolverr   r   ZLRUCachecacher   r   r   r   r[   b  s    zResolverProxy.clearFTc	              	      s   dddg dkrd t  ts*t  tr8tj d fdd}	 fdd}
| jr|tjjkr||kr|	| jj	 |dd	rd dk	sd
 dk	s|s|
 S |	| j
j	 ||||dd	 t d
kr|	| j
j	 tjj||||dd	 |
 S )aP  Query the resolver, using /etc/hosts if enabled.

        Behavior:
        1. if hosts is enabled and contains answer, return it now
        2. query nameservers for qname if use_network is True
        3. if qname did not contain dots, pretend it was top-level domain,
           query "foobar." and append to previous result
        Nr   z0.0.0.0c              
      s   z| ||}W n0 t k
r> } z| d< W Y dS d }~X Y nX |jd k	rt|jr d d krj| d< n d j|j  d  t|j7  < dS )Nr#   Fr   rZ   T)	Exceptionr6   r\   Zunion_update)Zfunr   kwargsae)resultr   r   stepy  s    
z!ResolverProxy.query.<locals>.stepc                      sj   d d k	r,r$d dkr$t jjd S d d k	rVsNtd t jjsVd t jj fdd S )Nr   rZ   r#   )Zqnames)r   r4   r5   r   NXDOMAINr   r0   r:   r   r   r   end  s    z ResolverProxy.query.<locals>.endF)r:   r#   )r   r   rk   r   rH   rj   r   rh   ri   r*   r   r\   Zconcatenateroot)r9   r0   r1   r2   rt   ru   r:   Z_hosts_rdtypesuse_networkr   r   r   r   r   r*   f  s*    

    zResolverProxy.queryc              	   C   sz   | j r| j |}ng }z| j|tjj}W n$ tjjtjj	fk
rR   Y qvY qX |
dd |jD  |d j}q|S )z4Return a list of all the aliases of a given hostnamec                 s   s   | ]}t |jV  qd S r   )r   target.0Zrrr   r   r   	<genexpr>  s     z+ResolverProxy.getaliases.<locals>.<genexpr>r   )r   r}   r   r*   r   rl   rr   r4   r5   r   extendr6   r   )r9   r{   r|   ansr   r   r   r}     s    
zResolverProxy.getaliases)Nr   )r<   r=   r>   r?   r;   propertyr   setterr[   r   rl   rm   rh   ri   rq   r*   r}   r   r   r   r   r   ?  s   


  
?r   )r   Tc              	   C   s   |t jkrtjj}n"|t jkr(tjj}nt t jd|dkrBt	}z\z|j
| |||dW W S  tj	jk
r   |sttj| |tjjdd Y W S  Y nX W n> tjjk
r   tt Y n  tjjk
r   tt Y nX dS )a8  Resolve a name for a given family using the global resolver proxy.

    This method is called by the global getaddrinfo() function. If use_network
    is False, only resolution via hosts file will be performed.

    Return a dns.resolver.Answer instance.  If there is no answer it's
    rrset will be emtpy.
    zAddress family not supportedN)r:   r   F)r   AF_INETr   rl   rm   AF_INET6rq   gaierror
EAI_FAMILYr4   r*   r   r-   rH   Namerh   ri   r   Timeoutr   EAI_EAGAIN_ERRORDNSExceptionEAI_NODATA_ERROR)rH   familyZraises_proxyr   r1   r   r   r   resolve  s6    






   
r   c                 C   s   zt | tjj}W nX tj jk
r0   |  Y S  tjjk
rN   tt	 Y n. tjj
k
rl   tt Y nX t|d jS dS )z'Return the canonical name of a hostnamer   N)r4   r*   r   rl   rr   r5   r   r   r   r   r   r   r   r   )r    r   r   r   r   resolve_cname  s    r   c                 C   sP   zt | W S  tjjk
r,   tt Y n  tjjk
rJ   tt Y nX dS )zReturn a list of for aliases for the given hostname

    This method does translate the dnspython exceptions into
    socket.gaierror exceptions.  If no aliases are available an empty
    list will be returned.
    N)	r4   r}   r   r   r   r   r   r   r   r   r   r   r   r}     s    r}   c           	      C   sv  |t j@ rtt g }|t jkrd}dD ]}t jt jfD ]v}zt| |d|d}W n@ t jk
r } z |j	t j
tj	tj	fkr| |}W 5 d}~X Y q8X |jr8|dd |jD  q8|r( qq(|dk	r|s|n|t jkrD|t j@ rDt| t jd}|jr
dd |jD }|r|t j@ rht| t jd}|jrhd	d |jD }n$t| |d}|jrhd
d |jD }t|j|fS )zTResolve a hostname to a list of addresses

    Helper function for getaddrinfo.
    N)FTF)r   c                 s   s   | ]}|j V  qd S r   addressr   r   r   r   r     s     z&_getaddrinfo_lookup.<locals>.<genexpr>c                 S   s   g | ]
}|j qS r   r   r   r   r   r   
<listcomp>  s     z'_getaddrinfo_lookup.<locals>.<listcomp>c                 S   s   g | ]}d |j  qS )z::ffff:r   r   r   r   r   r     s     c                 S   s   g | ]
}|j qS r   r   r   r   r   r   r     s     )r   AI_NUMERICHOSTr   EAI_NONAME_ERROR	AF_UNSPECr   r   r   r   errno	EAI_AGAINr   r6   r   AI_V4MAPPEDAI_ALLr   r0   )	r    r   flagsaddrserrr   ZqfamilyZanswerr   r   r   r   _getaddrinfo_lookup  s>    

r   c                 C   sR  t | tr| dd} nt | tr0| d} | dk	rRt| sRt| ||\}}n
| }| g}|tjB dtj	A @ }g }	d}
|D ]b}zt
||||||}W n: tk
r } z|tj@ r|}
W Y q| W 5 d}~X Y nX |	| q||	s|
r|
ttjd|tj	@ rNt|s$t|dd}|	d }|d |d |d ||d	 f|	d< |	S )
aC  Replacement for Python's socket.getaddrinfo

    This does the A and AAAA lookups asynchronously after which it
    calls the OS' getaddrinfo(3) using the AI_NUMERICHOST flag.  This
    flag ensures getaddrinfo(3) does not use the network itself and
    allows us to respect all the other arguments like the native OS.
    idnarf   N  zNo address foundr   r#   rZ      )r   r   encoderS   rk   r'   r   r   r   AI_CANONNAMEgetaddrinforR   AI_ADDRCONFIGr   r   
EAI_NONAMEr   )r    portr   typeprotor   r0   r   Zaiflagsresr   rw   Zair   r   r   r   r   !  sD    



  

"r   c                 C   s   t | r| S t| }|d jS )z-Replacement for Python's socket.gethostbynamer   )r!   r   r   )r{   r6   r   r   r   gethostbynameK  s    r   c                 C   s`   t | r| g | gfS t| }t| }dd |jD }t|j}|d dkrV|dd }|||fS )z0Replacement for Python's socket.gethostbyname_exc                 S   s   g | ]
}|j qS r   r   r   r   r   r   r   Y  s     z$gethostbyname_ex.<locals>.<listcomp>rg   .N)r!   r   r}   r6   r   r0   )r{   r   r|   r   r0   r   r   r   gethostbyname_exS  s    
r   c              	   C   s  z| \}}W n6 t tfk
rB   t| ts6~ tdntt Y nX |tj@ r`|tj@ r`tt t	|rzBt
tj|tjj}t|dkrtd|d jjdd}W nR tjjk
r   |tj@ rtt Y n* tjjk
r   |tj@ rtt Y nX nz:t
|}t|dkr$td|tj@ r:|d j}W nJ tjjk
r^   tt Y n* tjjk
r   ttjdfY nX |tj@ s|tj@ rdpd	}t||}||fS )
zTReplacement for Python's socket.getnameinfo.

    Currently only supports IPv4.
    z(getnameinfo() argument 1 must be a tupler#   z'sockaddr resolved to multiple addressesr   T)Zomit_final_dotr   udprt   )
ValueError	TypeErrorr   tupler   r   r   NI_NAMEREQDNI_NUMERICHOSTr!   r4   r*   r   Zreversenamefrom_addressrl   ZPTRr\   rR   r   Zto_textr   r   r   r   r   r   r   NI_NUMERICSERVNI_DGRAMgetservbyport)sockaddrr   r    r   r6   r   r   r   r   getnameinfo`  sP    



 



r   c                 C   s   t  }|dkr|z| |}W n< tjk
rX   |t  dkrFtjjt	d Y qY nX |dkrft
|t| }||7 }q|S )a  coro friendly replacement for dns.query._net_read
    Read the specified number of bytes from sock.  Keep trying until we
    either get the desired amount, or we hit EOF.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    r           {Gz?    )	bytearrayrecvr   r+   r   r   r   r   eventletsleepEOFErrorr\   )sockcountr8   snr   r   r   	_net_read  s    


r   c                 C   sd   d}t |}||k r`z|| ||d 7 }W q tjk
r\   |t  dkrXtjjY qX qdS )zcoro friendly replacement for dns.query._net_write
    Write the specified data to the socket.
    A Timeout exception will be raised if the operation is not completed
    by the expiration time.
    r   Nr   )r\   sendr   r+   r   r   r   r   )r   datar8   currentlr   r   r   
_net_write  s    r   )raise_on_truncationF5   c              
   C   s  |   }|dkr6ztj|}W n   tjj}Y nX |tjjkr\||f}|dk	r||f}n@|tjjkrtjtj|}||ddf}|dk	r||ddf}|r|}nt		|t	j
}|| z2ttj|}|dk	r|| z||| W q:W q t	jk
r6   |t  dkr$tjjtd Y qY qX qd}|r^|t  dkr^tjj|rntd d}z|d\}}W n t	jk
r   Y q>Y nX tj|d tjjkr|d }tjtj|}||d |d	 |d
 f}||kr|rq>ntjd||f z^trBtjj|| j| j||	|
d}ntjj|| j| j||	d}| |srtj W qW np tjj k
r } z(|r| | sW Y q>n W 5 d}~X Y n& t!k
r   |rY q>n Y nX q>W 5 |  X |S )aX  coro friendly replacement for dns.query.udp
    Return the response obtained after sending a query via UDP.

    @param q: the query
    @type q: dns.message.Message
    @param where: where to send the message
    @type where: string containing an IPv4 or IPv6 address
    @param timeout: The number of seconds to wait before the query times out.
    If None, the default, wait forever.
    @type timeout: float
    @param port: The port to which to send the message.  The default is 53.
    @type port: int
    @param af: the address family to use.  The default is None, which
    causes the address family to use to be inferred from the form of of where.
    If the inference attempt fails, AF_INET is used.
    @type af: int
    @rtype: dns.message.Message object
    @param source: source address.  The default is the IPv4 wildcard address.
    @type source: string
    @param source_port: The port from which to send the message.
    The default is 0.
    @type source_port: int
    @param ignore_unexpected: If True, ignore responses from unexpected
    sources.  The default is False.
    @type ignore_unexpected: bool
    @param one_rr_per_rrset: If True, put each RR into its own
    RRset.
    @type one_rr_per_rrset: bool
    @param ignore_trailing: If True, ignore trailing
    junk at end of the received message.
    @type ignore_trailing: bool
    @param raise_on_truncation: If True, raise an exception if
    the TC bit is set.
    @type raise_on_truncation: bool
    @param sock: the socket to use for the
    query.  If None, the default, a socket is created.  Note that
    if a socket is provided, it must be a nonblocking datagram socket,
    and the source and source_port are ignored.
    @type sock: socket.socket | None
    @param ignore_errors: if various format errors or response mismatches occur,
    continue listening.
    @type ignore_errors: boolNr   r   r   FTr   r#   rZ      z$got a response from %s instead of %s)keyringrequest_macone_rr_per_rrsetignore_trailingr   r   r   r   r   )"to_wirer   inetaf_for_addressr   r   r%   	inet_ntoar   r   
SOCK_DGRAM
settimeoutcloser,   r*   bindsendtor+   r   r   r   r   r   recvfromZUnexpectedSource_handle_raise_on_truncationmessage	from_wirer   macis_responseBadResponseZ	Truncatedr   )qwherer+   r   afru   source_portZignore_unexpectedr   r   r   r   ignore_errorswiredestinationZwhere_truncr   r8   Ztriedr   rw   rr   r   r   r   r     s    /









r   c
                 C   s  |   }
|dkr6ztj|}W n   tjj}Y nX |tjjkr\||f}|dk	r||f}n,|tjjkr||ddf}|dk	r||ddf}|	r|	}nt|tj}|| zt
tj|}|dk	r|| z|| W q"W q tjk
r   |t  dkrtjjtd Y qY qX qt|
}td||
 }t||| t|d|}td|\}tt|||}
W 5 |	  X tjj|
| j| j||d}| |stj  |S )a  coro friendly replacement for dns.query.tcp
    Return the response obtained after sending a query via TCP.

    @param q: the query
    @type q: dns.message.Message object
    @param where: where to send the message
    @type where: string containing an IPv4 or IPv6 address
    @param timeout: The number of seconds to wait before the query times out.
    If None, the default, wait forever.
    @type timeout: float
    @param port: The port to which to send the message.  The default is 53.
    @type port: int
    @param af: the address family to use.  The default is None, which
    causes the address family to use to be inferred from the form of of where.
    If the inference attempt fails, AF_INET is used.
    @type af: int
    @rtype: dns.message.Message object
    @param source: source address.  The default is the IPv4 wildcard address.
    @type source: string
    @param source_port: The port from which to send the message.
    The default is 0.
    @type source_port: int
    @type ignore_unexpected: bool
    @param one_rr_per_rrset: If True, put each RR into its own
    RRset.
    @type one_rr_per_rrset: bool
    @param ignore_trailing: If True, ignore trailing
    junk at end of the received message.
    @type ignore_trailing: bool
    @param sock: the socket to use for the
    query.  If None, the default, a socket is created.  Note that
    if a socket is provided, it must be a nonblocking datagram socket,
    and the source and source_port are ignored.
    @type sock: socket.socket | NoneNr   r   r   z!HrZ   r   )!r   r   r   r   r   r   r   SOCK_STREAMr   r   r,   r*   r   connectr+   r   r   r   r   r   r\   structpackr   r   unpackrk   r   r   r   r   r   r   )r  r  r+   r   r  ru   r  r   r   r   r  r  r   r8   r   ZtcpmsgZldatar  r   r   r   rt   ^  sX    &







rt   c                   C   s   t   d S r   )r4   r[   r   r   r   r   reset  s    r  )r   r   r   r   )Ar?   r~   r  sysr   r   Zeventlet.greenr   r   r   r   r   r	   r   r   __all__r   pkgsetattrri   rs   r   ZDNS_QUERY_TIMEOUTr   r   r   r   r   r   r   environrn   r_   r7   r   r   r!   r&   r'   r*   r,   r4   ZAnswerr-   r@   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   ZShortHeaderr   r   r   rt   r  r   r   r   r   <module>   s   "



 w
#(
*5
      
     
[