a
    ”˜ëj<  ã                   @  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rÚd dlm#Z# nd dl$m#Z# ej"dk�rd dlm%Z%m&Z& nd dl$m%Z%m&Z& ej"dk�r.d 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<   úP/mnt/bigstor/www/usecases/.venv/lib/python3.9/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œdddddd dœdd„ƒZddddœdd„Zddœdd„Zddœdd „Z	d/d"d#d$œd%d&„Z
d#dd'œd(d)„Zddœd*d+„Zed,dœd-d.„ƒZdS )0r   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   zbool | Noner(   ússl.SSLContext | None)r?   r0   rC   rD   r4   Úreturnc                Ã  sÂ   |du r| }|sL|rt jjnt jj}t  |¡}tt dƒrL| 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ÚoptionsrG   Ú	MemoryBIOÚtyper   Úwrap_bior   Zrun_syncÚ_call_sslobject_methodÚdo_handshake)Úclsr?   r0   rC   rD   r4   ÚpurposeZbio_inZbio_outr3   Úwrapperr<   r<   r=   Úwraph   s>    ÿ

ÿú
	ûzTLSStream.wrapz&Callable[[Unpack[PosArgsT]], T_Retval]zUnpack[PosArgsT]r$   )ÚfuncÚargsrF   c                 Ç  sÈ  z||Ž }W �nŒ t jy¼   z4| jjr@| j | j ¡ ¡I d H  | j ¡ I d H }W nX tyn   | j	 
¡  Y nJ tyª } z&| j	 
¡  | j 
¡  t|‚W Y d }~nd }~0 0 | j	 |¡ Y q  t jyæ   | j | j ¡ ¡I d H  Y q  t j�y& } z&| j	 
¡  | j 
¡  t|‚W Y d }~q d }~0  t j�yš } zZ| j	 
¡  | j 
¡  t|t jƒ�sn|j�r„d|jv �r„| j�r~t|‚ntd ‚‚ W Y d }~q d }~0 0 | jj�r¾| j | j ¡ ¡I d H  |S q d S )NZUNEXPECTED_EOF_WHILE_READING)rI   ÚSSLWantReadErrorrB   Úpendingr?   ÚsendÚreadÚreceiver   rA   Ú	write_eofÚOSErrorr   ÚwriteÚSSLWantWriteErrorÚSSLSyscallErrorÚSSLErrorÚ
isinstanceÚSSLEOFErrorÚstrerrorr4   )ÚselfrY   rZ   ÚresultÚdataÚexcr<   r<   r=   rS   ¯   sF    





ÿÿ
z TLSStream._call_sslobject_methodztuple[AnyByteStream, bytes]©rF   c                 Ã  s8   |   | 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)rS   r@   ÚunwraprA   r`   rB   r?   r^   ©ri   r<   r<   r=   rn   Ý   s    

zTLSStream.unwrapÚNonec                 Ã  sR   | j r>z|  ¡ I d H  W n$ ty<   t| jƒI d H  ‚ Y n0 | j ¡ I d H  d S ©N)r4   rn   ÚBaseExceptionr   r?   Úaclosero   r<   r<   r=   rs   é   s    zTLSStream.acloseé   Úintr*   )Ú	max_bytesrF   c                 Ã  s"   |   | jj|¡I d H }|st‚|S rq   )rS   r@   r^   r   )ri   rv   rk   r<   r<   r=   r_   ó   s    zTLSStream.receive)ÚitemrF   c                 Ã  s   |   | jj|¡I d H  d S rq   )rS   r@   rb   )ri   rw   r<   r<   r=   r]   ú   s    zTLSStream.sendc                 Ã  sd   |   tj¡}t d|¡}|rXt| d¡ƒt| d¡p6dƒ }}||fdk rXtd|› �ƒ‚tdƒ‚d S )NzTLSv(\d+)(?:\.(\d+))?é   r   r   )rx   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Úmatchru   ÚgroupÚNotImplementedError)ri   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                     s   ˆ j  d¡S )NF©r@   Úgetpeercertr<   ro   r<   r=   Ú<lambda>  ó    z,TLSStream.extra_attributes.<locals>.<lambda>c                     s   ˆ j  d¡S )NTr‚   r<   ro   r<   r=   r„     s   ÿc                     s   ˆ j jS rq   )r@   r0   r<   ro   r<   r=   r„     r…   c                     s   ˆ j jrˆ j  ¡ S d S rq   )r@   r0   r1   r<   ro   r<   r=   r„     s    ÿc                     s   ˆ j S rq   ©r4   r<   ro   r<   r=   r„     r…   c                     s   ˆ j S rq   )r@   r<   ro   r<   r=   r„     r…   )r?   Úextra_attributesr   r)   r@   Úselected_alpn_protocolr+   Úget_channel_bindingr,   r-   r.   r0   r1   r4   r3   r6   Úversionro   r<   ro   r=   r‡     s    ÿ


ïzTLSStream.extra_attributes)rt   )r7   r8   r9   r:   r;   ÚclassmethodrX   rS   rn   rs   r_   r]   r€   Úpropertyr‡   r<   r<   r<   r=   r   V   s(   

ùF.
r   c                   @  s„   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dœdd„ƒZdddddœdd„Z	ddœdd„Z
ed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_timeoutrr   r   rp   )rl   ÚstreamrF   c                 Ã  sJ   t |ƒI dH  t| tƒ ƒs.t t¡jd| d� t| tƒrDt| tƒ ƒrF‚ 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   rf   r   ÚloggingÚ	getLoggerr7   Ú	exceptionÚ	Exception)rl   r‘   r<   r<   r=   Úhandle_handshake_error;  s    
ÿz"TLSListener.handle_handshake_errorNzCallable[[TLSStream], Any]zTaskGroup | None)ÚhandlerÚ
task_grouprF   c                 ƒ  s6   t ˆ ƒdddœ‡ ‡fdd„ƒ}ˆj ||¡I d H  d S )Nr   rp   )r‘   rF   c              
   “  sž   ddl m} zH|ˆjƒ�* tj| ˆjˆjd�I d H }W d   ƒ n1 sH0    Y  W n6 tyŠ } zˆ || ¡I d H  W Y d }~nd }~0 0 ˆ |ƒI d H  d S )Nr   )Ú
fail_after)rD   r4   )	Ú rš   r�   r   rX   rD   r4   rr   r—   )r‘   rš   Zwrapped_streamrl   ©r˜   ri   r<   r=   Úhandler_wrapper`  s    ý.(z*TLSListener.serve.<locals>.handler_wrapper)r
   r�   Úserve)ri   r˜   r™   r�   r<   rœ   r=   rž   [  s    zTLSListener.serverm   c                 Ã  s   | j  ¡ I d H  d S rq   )r�   rs   ro   r<   r<   r=   rs   r  s    zTLSListener.acloser�   c                   s   t j‡ fdd„iS )Nc                     s   ˆ j S rq   r†   r<   ro   r<   r=   r„   x  r…   z.TLSListener.extra_attributes.<locals>.<lambda>)r   r4   ro   r<   ro   r=   r‡   u  s    ÿzTLSListener.extra_attributes)N)r7   r8   r9   r:   r;   r4   r�   Ústaticmethodr—   rž   rs   rŒ   r‡   r<   r<   r<   r=   r   #  s   
" ýr   c                   @  sB   e Zd ZdZddddœddddd	d
œdd„Zed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   r   r(   rE   r/   rp   )ÚconnectablerC   rD   r4   rF   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¡   rI   rM   rJ   rL   rD   rf   r   Ú	TypeErrorrQ   r7   rC   r4   )ri   r¡   rC   rD   r4   r<   r<   r=   Ú__init__ˆ  s    ÿ
ÿÿzTLSConnectable.__init__r   rm   c                 Ã  sX   | j  ¡ I d H }z tj|| j| j| jd�I d H W S  tyR   t|ƒI d H  ‚ Y n0 d S )Nr    )	r¡   Úconnectr   rX   rC   rD   r4   rr   r   )ri   r‘   r<   r<   r=   r¤   œ  s    üzTLSConnectable.connect)r7   r8   r9   r:   r£   r#   r¤   r<   r<   r<   r=   r   |  s   úr   )3Ú
__future__r   Ú__all__r“   rz   rI   ÚsysÚcollections.abcr   r   Zdataclassesr	   Ú	functoolsr
   r   Útypingr   r   r›   r   r   r   r   r   Z_core._typedattrr   r   Úabcr   r   r   r   r   r   Úversion_infor   Ztyping_extensionsr    r!   r#   r$   r%   Útupler5   r&   r;   r'   r   r   r   r   r<   r<   r<   r=   Ú<module>   sB    	
 MX