U
    <{fh                     @   sf  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mZmZm	Z	m
Z
m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ZddlZddlZddlZddlmZ ddlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# e"rddl$Z$ej%j&Z'dd Z(d6dd	Z)d7ej*j+eej,j-e.f ee	e/ e
e0e/f d
ddZ1d8ej*j+e	e e	e/ e2e2e	eej3j4ej5j6f  e	e. e2e2e2e	ej,j- edddZ7d9ej,j-e8e	e/ e0e	e8 e0e2e2e2e2e	ej*j+ e	ej*j9 e2ej,j-dddZ:d:ej,j-e8e	e/ e0e	e8 e0e2e2e2e	ej*j+ e	ej*j; e	ej*j9 e2e
ej,j-e2f dddZ<d;ej*j;eej,j-e.f e	e/ e
e0e/f dddZ=dd Z>d<ej*j;e	e/ e2e	eej3j4ej5j6f  e	e. e2e
ej,j-e/f ddd Z?d=ej,j-e8e	e/ e0e	e8 e0e2e2e	ej*j; e	ej*j9 ej,j-d!d"d#Z@d>ej,j-e8e	e/ e0e	e8 e0e2e2e	ej*j; e	ej*j9 e	e#jA e	e8 ee2e8f ej,j-d&d'd(ZBdd)dddddd*d%d%ddejCfej,j-e8e	e/ e0e	e8 e0e2e2e	d+ e8e2ee2e8f e	e8 e	d, e	e0 ej,j-d-d.d/ZDddddddejEdfe8ejFjGe	ej,j- e0e	e/ e	e/ e	e8 e0ee	ej*j9 dd0d1d2ZHd?ej,j-e8e	e/ e0e	e8 e0e2e2e	ejIjJ ee2e8f e	ej*j9 e	e8 ej,j-d3d4d5ZIdS )@zTalk to a DNS server.    N)AnyDictOptionalTupleUnion)NullContext)
BadResponseNoDOHNoDOQUDPMode_compute_times_make_dot_ssl_context_matches_destination
_remaininghave_dohsslc                 C   sN   |s|rF|d kr>| t jkr d}n| t jkr0d}ntd|  ||fS d S d S )Nz0.0.0.0z::zunknown address family )socketAF_INETAF_INET6NotImplementedError)afaddressport r   M/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/asyncquery.py_source_tuple:   s    

r   c                 C   s*   | d k	r"|st   }t| | dS d S d S )Nr   )timemax)
expirationnowr   r   r   _timeoutJ   s
    r    )sockwhatdestinationr   returnc                    s@   t |tjjr| }t }| ||t||I dH }||fS )a  Send a DNS message to the specified UDP socket.

    *sock*, a ``dns.asyncbackend.DatagramSocket``.

    *what*, a ``bytes`` or ``dns.message.Message``, the message to send.

    *destination*, a destination tuple appropriate for the address family
    of the socket, specifying where to send the query.

    *expiration*, a ``float`` or ``None``, the absolute time at which
    a timeout exception should be raised.  If ``None``, no timeout will
    occur.  The expiration value is meaningless for the asyncio backend, as
    asyncio's transport sendto() never blocks.

    Returns an ``(int, float)`` tuple of bytes sent and the sent time.
    N)
isinstancednsmessageMessageto_wirer   sendtor    )r!   r"   r#   r   	sent_timenr   r   r   send_udpS   s
    r-   F    )r!   r#   r   ignore_unexpectedone_rr_per_rrsetkeyringrequest_macignore_trailingraise_on_truncationignore_errorsqueryr$   c              
      s   d}|  dt|I dH \}}t| j|||s0qt }ztjj||||||d}W nj tjjk
r } z*|	r|
dk	r|
	| sW Y 
qn W 5 d}~X Y n  t
k
r   |	rY qn Y nX |	r|
dk	r|
	|sq|||fS )aj  Read a DNS message from a UDP socket.

    *sock*, a ``dns.asyncbackend.DatagramSocket``.

    See :py:func:`dns.query.receive_udp()` for the documentation of the other
    parameters, and exceptions.

    Returns a ``(dns.message.Message, float, tuple)`` tuple of the received message, the
    received time, and the address where the message arrived from.
    r.     N)r1   r2   r0   r3   r4   )recvfromr    r   familyr   r&   r'   	from_wire	Truncatedis_response	Exception)r!   r#   r   r/   r0   r1   r2   r3   r4   r5   r6   wirefrom_addressreceived_timerer   r   r   receive_udpq   sF       

rC   5   )qwheretimeoutr   sourcesource_portr/   r0   r3   r4   r!   backendr5   r$   c                    s  |   }t|\}}tj|}t||f|}|
r<t|
}nJ|sJtj }t	|||}|
 rh||f}nd}||tjd||I dH }|4 I dH t}t||||I dH  t|||||| j| j||	|| I dH \}}}|| |_|s| |st|W  5 Q I dH R  S Q I dH R X dS )aF  Return the response obtained after sending a query via UDP.

    *sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``,
    the socket to use for the query.  If ``None``, the default, a
    socket is created.  Note that if a socket is provided, the
    *source*, *source_port*, and *backend* are ignored.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.udp()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    Nr   )r)   r   r&   inetaf_for_address_lltupler   asyncbackendget_default_backendr   Zdatagram_connection_requiredmake_socketr   
SOCK_DGRAMr-   rC   r1   macr   r<   r   )rE   rF   rG   r   rH   rI   r/   r0   r3   r4   r!   rJ   r5   r>   
begin_timer   r   r#   cmstupledtuplesrA   r@   _r   r   r   udp   s@    



rY   )rE   rF   rG   r   rH   rI   r/   r0   r3   udp_socktcp_sockrJ   r5   r$   c                    sv   z0t | ||||||||d|	||I dH }|dfW S  tjjk
rp   t| ||||||||
|
I dH }|df Y S X dS )a  Return the response to the query, trying UDP first and falling back
    to TCP if UDP results in a truncated response.

    *udp_sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``,
    the socket to use for the UDP query.  If ``None``, the default, a
    socket is created.  Note that if a socket is provided the *source*,
    *source_port*, and *backend* are ignored for the UDP query.

    *tcp_sock*, a ``dns.asyncbackend.StreamSocket``, or ``None``, the
    socket to use for the TCP query.  If ``None``, the default, a
    socket is created.  Note that if a socket is provided *where*,
    *source*, *source_port*, and *backend*  are ignored for the TCP query.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.udp_with_fallback()` for the documentation
    of the other parameters, exceptions, and return type of this
    method.
    TNF)rY   r&   r'   r;   tcp)rE   rF   rG   r   rH   rI   r/   r0   r3   rZ   r[   rJ   r5   responser   r   r   udp_with_fallback   s>    #


r^   )r!   r"   r   r$   c                    s\   t |tjjr|jdd}nt|dd| }t }| |t	||I dH  t||fS )zSend a DNS message to the specified TCP socket.

    *sock*, a ``dns.asyncbackend.StreamSocket``.

    See :py:func:`dns.query.send_tcp()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    T)Zprepend_length   bigN)
r%   r&   r'   r(   r)   lento_bytesr   sendallr    )r!   r"   r   tcpmsgr+   r   r   r   send_tcp4  s    re   c                    sH   d}|dkrD|  |t|I dH }|dkr.t|t| }|| }q|S )z|Read the specified number of bytes from stream.  Keep trying until we
    either get the desired amount, or we hit EOF.
    r.   r   N)recvr    EOFErrorra   )r!   countr   rW   r,   r   r   r   _read_exactlyM  s    
ri   )r!   r   r0   r1   r2   r3   r$   c                    sX   t | d|I dH }td|\}t | ||I dH }t }	tjj|||||d}
|
|	fS )zRead a DNS message from a TCP socket.

    *sock*, a ``dns.asyncbackend.StreamSocket``.

    See :py:func:`dns.query.receive_tcp()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    r_   N!Hr1   r2   r0   r3   )ri   structunpackr   r&   r'   r:   )r!   r   r0   r1   r2   r3   ldatalr>   r@   rA   r   r   r   receive_tcp[  s    rp   )rE   rF   rG   r   rH   rI   r0   r3   r!   rJ   r$   c
                    s   |   }
t|\}}|r0| I dH  t|}nJtj|}t|||}||f}|	s^tj	 }	|	
|tjd|||I dH }|4 I dH b}t||
|I dH  t|||| j| j|I dH \}}|| |_| |st|W  5 Q I dH R  S Q I dH R X dS )aO  Return the response obtained after sending a query via TCP.

    *sock*, a ``dns.asyncbacket.StreamSocket``, or ``None``, the
    socket to use for the query.  If ``None``, the default, a socket
    is created.  Note that if a socket is provided
    *where*, *port*, *source*, *source_port*, and *backend* are ignored.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.tcp()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    Nr   )r)   r   getpeernamer   r&   rK   rL   r   rN   rO   rP   r   SOCK_STREAMre   rp   r1   rR   r   r<   r   )rE   rF   rG   r   rH   rI   r0   r3   r!   rJ   r>   rS   r   rT   r   rU   rV   rW   rA   r@   r   r   r   r\   y  s@    

     
     

r\   U  T)rE   rF   rG   r   rH   rI   r0   r3   r!   rJ   ssl_contextserver_hostnameverifyr$   c                    s   t |\}}|rt|}n`|
dkr,t||}
tj|}t|||}||f}|	sZtj }	|		|t
jd||||
|I dH }|4 I dH R}t|}t| |||||||||	
I dH }t }|| |_|W  5 Q I dH R  S Q I dH R X dS )a  Return the response obtained after sending a query via TLS.

    *sock*, an ``asyncbackend.StreamSocket``, or ``None``, 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
    connected SSL stream socket, and *where*, *port*,
    *source*, *source_port*, *backend*, *ssl_context*, and *server_hostname*
    are ignored.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.tls()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    Nr   )r   r   r   r&   rK   rL   r   rN   rO   rP   r   rr   r    r\   r   )rE   rF   rG   r   rH   rI   r0   r3   r!   rJ   rt   ru   rv   rS   r   rT   r   rU   rV   rW   r]   end_timer   r   r   tls  sJ    






rx   i  z
/dns-queryzhttpx.AsyncClientzdns.asyncresolver.Resolver)rE   rF   rG   r   rH   rI   r0   r3   clientpathpostrv   bootstrap_addressresolverr9   r$   c              
      s
  t st|r t|tjs td|  }ztj	|}W n tk
rP   d}Y nX d}ddi}|dk	rtj
|r|tjkrd|||	}q|tjkrd|||	}n|}tj }|dkrd}d}n|}|}| |dd|||||d	}|rt|}ntjdd||d
}|4 I dH ~}|
rV|dtt|d ||j|||d|I dH }n:t|d}| }||j||d|id|I dH }W 5 Q I dH R X |jdk s|jdkrtd||j|jtjj |j| j!| j"||d}|j#$ |_%| &|st'|S )a  Return the response obtained after sending a query via DNS-over-HTTPS.

    *client*, a ``httpx.AsyncClient``.  If provided, the client to use for
    the query.

    Unlike the other dnspython async functions, a backend cannot be provided
    in this function because httpx always auto-detects the async backend.

    See :py:func:`dns.query.https()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    z.session parameter must be an httpx.AsyncClientNacceptzapplication/dns-messagezhttps://{}:{}{}zhttps://[{}]:{}{}r   T)local_addresshttp1http2rv   
local_portr|   r}   r9   )r   r   rv   	transport)zcontent-typezcontent-length)headerscontent   =r&   )r   params   i+  z4{} responded with status code {}
Response body: {!r}rk   )(r   r	   r%   httpxZAsyncClient
ValueErrorr)   r&   rK   rL   Z
is_addressr   r   formatr   rN   rO   Zget_transport_classr   updatestrra   wait_forr{   base64urlsafe_b64encoderstripdecodegetstatus_coder   r'   r:   r1   r2   elapsedtotal_secondsr   r<   r   )rE   rF   rG   r   rH   rI   r0   r3   ry   rz   r{   rv   r|   r}   r9   r>   r   r   r   urlrJ   r   r   rT   Z
the_clientr]   ZtwirerA   r   r   r   https  s    




   
    r   )rF   txn_managerr6   r   rG   lifetimerH   rI   udp_moderJ   r$   c
           #         s  |dkrt j|\}}
nt j|}
|jd j}|t jjk}| }|	 }t j
| }t|||}| |f}t|\}}d}|rd}|r|tjkrtj}d}n
tj}d}|	st j }	|	||d||t|I dH }|4 I dH  |r|||t|I dH  n&tdt|| }|||I dH  t j|||
|p}d}d}|st|\}}|dks||dk	r||kr|}|rt| |f|}t|}|d|I dH \}}t|||drqqn2t |d|I dH } t!d| \}!t ||!|I dH }|t jjk}t j"j#||j$|j%d||| |d}"z|&|"}W nF t jj'k
r   |sZt(|tj)krh d}d}tj}Y qLY nX |"j*}qL|s|j$r|"j+st j,-d	W 5 Q R X W 5 Q I dH R X q~dS )
am  Conduct an inbound transfer and apply it via a transaction from the
    txn_manager.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.inbound_xfr()` for the documentation of
    the other parameters, exceptions, and return type of this method.
    Nr   TFrj   r7   r_   )r1   r2   xfrorigintsig_ctxmultir0   zmissing TSIG).r&   r   Z
make_queryZextract_serial_from_queryquestionrdtypeZ	rdatatypeZIXFRZfrom_wire_originr)   rK   rL   r   r   r   NEVERr   rQ   rr   rN   rO   rP   r    r*   rl   packra   rc   ZInboundrM   r8   r   ri   rm   r'   r:   r1   rR   Zprocess_messageZUseTCPAssertionErrorZONLYr   Zhad_tsig	exceptionZ	FormError)#rF   r   r6   r   rG   r   rH   rI   r   rJ   serialr   Zis_ixfrr   r>   r   rU   rV   rX   r   retryZ	sock_typeZis_udprW   rd   Zinbounddoner   Zmexpirationr#   Zrwirer?   rn   ro   rA   r   r   r   inbound_xfrl  s    
     

   



r   )rE   rF   rG   r   rH   rI   r0   r3   
connectionrv   rJ   ru   r$   c                    sJ  t jjstdd| _|  }|r8t jj}t jj}|}nt j|
\}}| 4 I dH }|||	|d4 I dH }|s|||||}t	|\}}|
|I dH }|4 I dH , ||dI dH  |t|I dH }W 5 Q I dH R X t }W 5 Q I dH R X t jj|| j| j||d}W 5 Q I dH R X t|| d|_| |sFt|S )ai  Return the response obtained after sending an asynchronous query via
    DNS-over-QUIC.

    *backend*, a ``dns.asyncbackend.Backend``, or ``None``.  If ``None``,
    the default, then dnspython will use the default backend.

    See :py:func:`dns.query.quic()` for the documentation of the other
    parameters, exceptions, and return type of this method.
    zDNS-over-QUIC is not available.r   N)verify_modeZserver_nameTrk   g        )r&   quicZ	have_quicr
   idr)   Znull_factoryZfactories_for_backendconnectr   Zmake_streamsendZreceiver   r   r'   r:   r1   r2   r   r<   r   )rE   rF   rG   r   rH   rI   r0   r3   r   rv   rJ   ru   r>   ZcfactoryZmfactoryZthe_connectioncontextZthe_managerstartr   streamfinishrA   r   r   r   r     sF      $r   )N)N)
NNFFNr.   FFFN)NrD   Nr   FFFFNNF)NrD   Nr   FFFNNNF)N)NFNr.   F)NrD   Nr   FFNN)Nrs   Nr   FFNNNNT)
Nrs   Nr   FFNTNN)K__doc__r   
contextlibr   rl   r   typingr   r   r   r   r   Zdns.asyncbackendr&   Zdns.exceptionZdns.inetZdns.messageZdns.nameZdns.quicZ	dns.rcodeZdns.rdataclassZdns.rdatatypeZdns.transactionZdns._asyncbackendr   Z	dns.queryr   r	   r
   r   r   r   r   r   r   r   r   rK   Zlow_level_address_tuplerM   r   r    rN   ZDatagramSocketr'   r(   bytesfloatintr-   boolnameNameZtsigKeyrC   r   BackendrY   ZStreamSocketr^   re   ri   rp   r\   
SSLContextrx   	AF_UNSPECr   r   ZtransactionZTransactionManagerr   r   ZAsyncQuicConnectionr   r   r   r   <module>   s   0
 
           
@           

E           


G 
     !        

;           


I
x

h          


