o
    +i<                     @  s  U d dl mZ dZd dlZd dlZd dlZd dl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lmZmZ d	d
lmZmZmZmZmZ d	dlmZmZ d	dlmZmZmZmZm Z m!Z! ej"dkrmd dlm#Z# nd dl$m#Z# ej"dkrd dlm%Z%m&Z& nd dl$m%Z%m&Z& ej"dkrd dlm'Z' nd dl$m'Z' edZ(e%dZ)e*e*e+e+f df Z,de-d< e*e,df Z.de-d< G dd deZ/eddG dd deZ0eddG dd  d e e0 Z1G d!d" d"eZ2dS )#    )annotations)TLSAttributeTLSConnectableTLSListener	TLSStreamN)CallableMapping)	dataclass)wraps)
SSLContext)AnyTypeVar   )BrokenResourceErrorEndOfStreamaclose_forcefullyget_cancelled_exc_class	to_thread)TypedAttributeSettyped_attribute)AnyByteStreamAnyByteStreamConnectable
ByteStreamByteStreamConnectableListener	TaskGroup)   
   )	TypeAlias)r      )TypeVarTupleUnpack)r      )overrideT_RetvalPosArgsT.r   _PCTRTT_PCTRTTTc                   @  s   e Zd ZU dZe Zded< e Zded< e Zded< e Z	ded	< e Z
d
ed< e Zded< e Zded< e Zded< e Zded< e Zded< dS )r   z5Contains Transport Layer Security related attributes.
str | Nonealpn_protocolbyteschannel_binding_tls_uniqueztuple[str, str, int]cipherz*None | dict[str, str | _PCTRTTT | _PCTRTT]peer_certificatezbytes | Nonepeer_certificate_binaryboolserver_sidez!list[tuple[str, str, int]] | Noneshared_ciphersssl.SSLObject
ssl_objectstandard_compatiblestrtls_versionN)__name__
__module____qualname____doc__r   r)   __annotations__r+   r,   r-   r.   r0   r1   r3   r4   r6    r<   r<   D/var/www/html/venv/lib/python3.10/site-packages/anyio/streams/tls.pyr   :   s   
 r   F)eqc                   @  s   e Zd ZU dZded< ded< ded< ded	< ded
< edddddd2ddZd3ddZd4dd Zd5d"d#Z	d6d7d(d)Z
d8d+d,Zd5d-d.Zed9d0d1ZdS ):r   a  
    A stream wrapper that encrypts all sent data and decrypts received data.

    This class has no public initializer; use :meth:`wrap` instead.
    All extra attributes from :class:`~TLSAttribute` are supported.

    :var AnyByteStream transport_stream: the wrapped stream

    r   transport_streamr/   r4   r2   _ssl_objectzssl.MemoryBIO	_read_bio
_write_bioNT)r0   hostnamessl_contextr4   r0   bool | NonerC   r(   rD   ssl.SSLContext | Nonereturnc                  s   |du r| }|s'|rt jjnt jj}t |}tt dr'| jt j M  _t  }t  }t	|t j
u r@|j||||d}	nt|j||||dI dH }	| |||	||d}
|
|	jI dH  |
S )a  
        Wrap an existing stream with Transport Layer Security.

        This performs a TLS handshake with the peer.

        :param transport_stream: a bytes-transporting stream to wrap
        :param server_side: ``True`` if this is the server side of the connection,
            ``False`` if this is the client side (if omitted, will be set to ``False``
            if ``hostname`` has been provided, ``False`` otherwise). Used only to create
            a default context when an explicit context has not been provided.
        :param hostname: host name of the peer (if host name checking is desired)
        :param ssl_context: the SSLContext object to use (if not provided, a secure
            default will be created)
        :param standard_compatible: if ``False``, skip the closing handshake when
            closing the connection, and don't raise an exception if the peer does the
            same
        :raises ~ssl.SSLError: if the TLS handshake fails

        NOP_IGNORE_UNEXPECTED_EOF)r0   server_hostname)r?   r4   r@   rA   rB   )sslPurposeCLIENT_AUTHSERVER_AUTHcreate_default_contexthasattroptionsrH   	MemoryBIOtyper   wrap_bior   run_sync_call_sslobject_methoddo_handshake)clsr?   r0   rC   rD   r4   purposebio_inbio_outr3   wrapperr<   r<   r=   wraph   s@   


	zTLSStream.wrapfunc&Callable[[Unpack[PosArgsT]], T_Retval]argsUnpack[PosArgsT]r$   c                   s  	 z|| }W n t jyY   z| jjr!| j| j I d H  | j I d H }W n& ty8   | j	
  Y n tyP } z| j	
  | j
  t|d }~ww | j	| Y no t jyn   | j| j I d H  Y nZ t jy } z| j	
  | j
  t|d }~w t jy } z"| j	
  | j
  t|t js|jrd|jv r| jrt|td  d }~ww | jjr| j| j I d H  |S q)NTUNEXPECTED_EOF_WHILE_READING)rJ   SSLWantReadErrorrB   pendingr?   sendreadreceiver   rA   	write_eofOSErrorr   writeSSLWantWriteErrorSSLSyscallErrorSSLError
isinstanceSSLEOFErrorstrerrorr4   )selfr]   r_   resultdataexcr<   r<   r=   rU      sV   






z TLSStream._call_sslobject_methodtuple[AnyByteStream, bytes]c                   s:   |  | jjI dH  | j  | j  | j| j fS )z
        Does the TLS closing handshake.

        :return: a tuple of (wrapped byte stream, bytes left in the read buffer)

        N)rU   r@   unwraprA   rg   rB   r?   re   rp   r<   r<   r=   ru      s
   

zTLSStream.unwrapNonec                   sP   | j rz	|  I d H  W n ty   t| jI d H   w | j I d H  d S N)r4   ru   BaseExceptionr   r?   acloserv   r<   r<   r=   rz      s   zTLSStream.aclose   	max_bytesintr*   c                   s$   |  | jj|I d H }|st|S rx   )rU   r@   re   r   )rp   r|   rr   r<   r<   r=   rf      s
   zTLSStream.receiveitemc                   s   |  | jj|I d H  d S rx   )rU   r@   ri   )rp   r~   r<   r<   r=   rd      s   zTLSStream.sendc                   sb   |  tj}td|}|r-t|dt|dpd}}||fdk r-td| td)NzTLSv(\d+)(?:\.(\d+))?   r   r   )r   r   z;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)extrar   r6   rematchr}   groupNotImplementedError)rp   r6   r   majorminorr<   r<   r=   send_eof   s   "zTLSStream.send_eofMapping[Any, Callable[[], Any]]c                   s   i  j jtj jjtj jjtj jjtj	 fddtj
 fddtj fddtj fddtj fddtj fddtj jji
S )Nc                         j dS )NFr@   getpeercertr<   rv   r<   r=   <lambda>  s    z,TLSStream.extra_attributes.<locals>.<lambda>c                     r   )NTr   r<   rv   r<   r=   r     s    c                     s    j jS rx   )r@   r0   r<   rv   r<   r=   r     s    c                     s    j jr	 j  S d S rx   )r@   r0   r1   r<   rv   r<   r=   r     s   c                         j S rx   r4   r<   rv   r<   r=   r         c                     r   rx   )r@   r<   rv   r<   r=   r     r   )r?   extra_attributesr   r)   r@   selected_alpn_protocolr+   get_channel_bindingr,   r-   r.   r0   r1   r4   r3   r6   versionrv   r<   rv   r=   r     s   


zTLSStream.extra_attributes)r?   r   r0   rE   rC   r(   rD   rF   r4   r/   rG   r   )r]   r^   r_   r`   rG   r$   )rG   rt   rG   rw   )r{   )r|   r}   rG   r*   )r~   r*   rG   rw   rG   r   )r7   r8   r9   r:   r;   classmethodr\   rU   ru   rz   rf   rd   r   propertyr   r<   r<   r<   r=   r   V   s*   
 

F
.



r   c                   @  sn   e Zd ZU dZded< ded< dZded< d	Zd
ed< ed ddZ	d!d"ddZ	d#ddZ
ed$ddZdS )%r   a  
    A convenience listener that wraps another listener and auto-negotiates a TLS session
    on every accepted connection.

    If the TLS handshake times out or raises an exception,
    :meth:`handle_handshake_error` is called to do whatever post-mortem processing is
    deemed necessary.

    Supports only the :attr:`~TLSAttribute.standard_compatible` extra attribute.

    :param Listener listener: the listener to wrap
    :param ssl_context: the SSL context object
    :param standard_compatible: a flag passed through to :meth:`TLSStream.wrap`
    :param handshake_timeout: time limit for the TLS handshake
        (passed to :func:`~anyio.fail_after`)
    zListener[Any]listenerzssl.SSLContextrD   Tr/   r4      floathandshake_timeoutrs   ry   streamr   rG   rw   c                   sL   t |I dH  t| t sttjd| d t| tr#t| t r$ dS )a  
        Handle an exception raised during the TLS handshake.

        This method does 3 things:

        #. Forcefully closes the original stream
        #. Logs the exception (unless it was a cancellation exception) using the
           ``anyio.streams.tls`` logger
        #. Reraises the exception if it was a base exception or a cancellation exception

        :param exc: the exception
        :param stream: the original stream

        NzError during TLS handshake)exc_info)r   rm   r   logging	getLoggerr7   	exception	Exception)rs   r   r<   r<   r=   handle_handshake_error;  s   
z"TLSListener.handle_handshake_errorNhandlerCallable[[TLSStream], Any]
task_groupTaskGroup | Nonec                   s2   t  d fdd}j||I d H  d S )Nr   r   rG   rw   c              
     s   ddl m} z$|j tj| jjdI d H }W d    n1 s%w   Y  W n tyG } z|| I d H  W Y d }~d S d }~ww  |I d H  d S )Nr   )
fail_after)rD   r4   )	 r   r   r   r\   rD   r4   ry   r   )r   r   wrapped_streamrs   r   rp   r<   r=   handler_wrapper`  s     z*TLSListener.serve.<locals>.handler_wrapper)r   r   rG   rw   )r
   r   serve)rp   r   r   r   r<   r   r=   r   [  s   zTLSListener.servec                   s   | j  I d H  d S rx   )r   rz   rv   r<   r<   r=   rz   r  s   zTLSListener.acloser   c                   s   t j fddiS )Nc                     r   rx   r   r<   rv   r<   r=   r   x  r   z.TLSListener.extra_attributes.<locals>.<lambda>)r   r4   rv   r<   rv   r=   r   u  s   zTLSListener.extra_attributes)rs   ry   r   r   rG   rw   rx   )r   r   r   r   rG   rw   r   r   )r7   r8   r9   r:   r;   r4   r   staticmethodr   r   rz   r   r   r<   r<   r<   r=   r   #  s   
 "
r   c                   @  s2   e Zd ZdZdddddddZedddZdS )r   a  
    Wraps another connectable and does TLS negotiation after a successful connection.

    :param connectable: the connectable to wrap
    :param hostname: host name of the server (if host name checking is desired)
    :param ssl_context: the SSLContext object to use (if not provided, a secure default
        will be created)
    :param standard_compatible: if ``False``, skip the closing handshake when closing
        the connection, and don't raise an exception if the server does the same
    NTrC   rD   r4   connectabler   rC   r(   rD   rF   r4   r/   rG   rw   c                C  sN   || _ |pttjj| _t| jtjstdt	| jj
 || _|| _d S )Nz7ssl_context must be an instance of ssl.SSLContext, not )r   rJ   rN   rK   rM   rD   rm   r   	TypeErrorrR   r7   rC   r4   )rp   r   rC   rD   r4   r<   r<   r=   __init__  s   

zTLSConnectable.__init__r   c                   sR   | j  I d H }ztj|| j| j| jdI d H W S  ty(   t|I d H   w )Nr   )	r   connectr   r\   rC   rD   r4   ry   r   )rp   r   r<   r<   r=   r     s   zTLSConnectable.connect)
r   r   rC   r(   rD   rF   r4   r/   rG   rw   )rG   r   )r7   r8   r9   r:   r   r#   r   r<   r<   r<   r=   r   |  s    r   )3
__future__r   __all__r   r   rJ   syscollections.abcr   r   dataclassesr	   	functoolsr
   r   typingr   r   r   r   r   r   r   r   _core._typedattrr   r   abcr   r   r   r   r   r   version_infor   typing_extensionsr    r!   r#   r$   r%   tupler5   r&   r;   r'   r   r   r   r   r<   r<   r<   r=   <module>   sD     
	

 MX