o
    í6Wj¹=  ã                   @  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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rqd 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Ú	TypeAliasÚTypeVaré   )ÚBrokenResourceErrorÚEndOfStreamÚaclose_forcefullyÚget_cancelled_exc_classÚ	to_thread)ÚTypedAttributeSetÚtyped_attribute)ÚAnyByteStreamÚAnyByteStreamConnectableÚ
ByteStreamÚByteStreamConnectableÚListenerÚ	TaskGroup)é   é   )Ú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-   r/   r0   r2   r3   r5   © r;   r;   ú`/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/anyio/streams/tls.pyr   5   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.   r3   r1   Ú_ssl_objectzssl.MemoryBIOÚ	_read_bioÚ
_write_bioNT)r/   ÚhostnameÚssl_contextr3   r/   úbool | NonerB   r'   rC   ú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  ¡ }|dur>ddl	m
}	 |	|ƒ}
nd}
t|ƒt ju rQ|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_EOFr   )Úidna2008_resolve)r/   Úserver_hostname)r>   r3   r?   r@   rA   )ÚsslÚPurposeÚCLIENT_AUTHÚSERVER_AUTHÚcreate_default_contextÚhasattrÚoptionsrG   Ú	MemoryBIOÚ_core._socketsrH   Útyper   Úwrap_bior   Úrun_syncÚ_call_sslobject_methodÚdo_handshake)Úclsr>   r/   rB   rC   r3   ÚpurposeÚbio_inÚbio_outrH   rI   r2   Úwrapperr;   r;   r<   Úwrapc   sN   €ÿ


ü
ú	û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r°d|jv r°| jr­t|‚td ‚‚ d }~ww | jjrÆ| j | j ¡ ¡I d H  |S q)NTÚUNEXPECTED_EOF_WHILE_READING)rJ   ÚSSLWantReadErrorrA   Úpendingr>   ÚsendÚreadÚreceiver   r@   Ú	write_eofÚOSErrorr   ÚwriteÚSSLWantWriteErrorÚSSLSyscallErrorÚSSLErrorÚ
isinstanceÚSSLEOFErrorÚstrerrorr3   )Úselfr^   r`   ÚresultÚdataÚexcr;   r;   r<   rV   ¶   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)rV   r?   Úunwrapr@   rh   rA   r>   rf   ©rq   r;   r;   r<   rv   ä   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)r3   rv   ÚBaseExceptionr   r>   Úacloserw   r;   r;   r<   r{   ð   s   €þzTLSStream.acloseé   Ú	max_bytesÚintr)   c                 Ã  s4   �|dk r	t dƒ‚|  | jj|¡I d H }|st‚|S )Né   z$max_bytes must be a positive integer)Ú
ValueErrorrV   r?   rf   r   )rq   r}   rs   r;   r;   r<   rg   ú   s   €zTLSStream.receiveÚitemc                 Ã  s   �|   | jj|¡I d H  d S ry   )rV   r?   rj   )rq   r�   r;   r;   r<   re     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   r   z;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)Úextrar   r5   ÚreÚmatchr~   ÚgroupÚNotImplementedError)rq   r5   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;   rw   r;   r<   Ú<lambda>  s    z,TLSStream.extra_attributes.<locals>.<lambda>c                     r‹   )NTrŒ   r;   rw   r;   r<   rŽ      s    ÿc                     s   ˆ j jS ry   )r?   r/   r;   rw   r;   r<   rŽ   #  s    c                     s   ˆ j jr	ˆ j  ¡ S d S ry   )r?   r/   r0   r;   rw   r;   r<   rŽ   $  s   ÿc                     ó   ˆ j S ry   ©r3   r;   rw   r;   r<   rŽ   )  ó    c                     r�   ry   )r?   r;   rw   r;   r<   rŽ   *  r‘   )r>   Úextra_attributesr   r(   r?   Úselected_alpn_protocolr*   Úget_channel_bindingr+   r,   r-   r/   r0   r3   r2   r5   Úversionrw   r;   rw   r<   r’     s   ÿ


ízTLSStream.extra_attributes)r>   r   r/   rD   rB   r'   rC   rE   r3   r.   rF   r   )r^   r_   r`   ra   rF   r#   )rF   ru   ©rF   rx   )r|   )r}   r~   rF   r)   )r�   r)   rF   rx   ©rF   rŠ   )r6   r7   r8   r9   r:   Úclassmethodr]   rV   rv   r{   rg   re   r‰   Úpropertyr’   r;   r;   r;   r<   r   Q   s*   
 
ù
R
.




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.SSLContextrC   Tr.   r3   é   ÚfloatÚhandshake_timeoutrt   rz   Ústreamr   rF   rx   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   rn   r   ÚloggingÚ	getLoggerr6   Ú	exceptionÚ	Exception)rt   rž   r;   r;   r<   Úhandle_handshake_errorG  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   rF   rx   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)rC   r3   )	Ú r©   r�   r   r]   rC   r3   rz   r¤   )rž   r©   Úwrapped_streamrt   ©r¥   rq   r;   r<   Úhandler_wrapperl  s    €ýÿ€ €ÿz*TLSListener.serve.<locals>.handler_wrapper)rž   r   rF   rx   )r
   rš   Úserve)rq   r¥   r§   r­   r;   r¬   r<   r®   g  s   €zTLSListener.servec                 Ã  s   �| j  ¡ I d H  d S ry   )rš   r{   rw   r;   r;   r<   r{   ~  s   €zTLSListener.acloserŠ   c                   s   t j‡ fdd„iS )Nc                     r�   ry   r�   r;   rw   r;   r<   rŽ   „  r‘   z.TLSListener.extra_attributes.<locals>.<lambda>)r   r3   rw   r;   rw   r<   r’   �  s   ÿzTLSListener.extra_attributes)rt   rz   rž   r   rF   rx   ry   )r¥   r¦   r§   r¨   rF   rx   r–   r—   )r6   r7   r8   r9   r:   r3   r�   Ústaticmethodr¤   r®   r{   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©rB   rC   r3   Úconnectabler   rB   r'   rC   rE   r3   r.   rF   rx   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   rC   rn   r   Ú	TypeErrorrS   r6   rB   r3   )rq   r±   rB   rC   r3   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]   rB   rC   r3   rz   r   )rq   rž   r;   r;   r<   r´   ¨  s   €üþzTLSConnectable.connect)
r±   r   rB   r'   rC   rE   r3   r.   rF   rx   )rF   r   )r6   r7   r8   r9   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   r   Ú_core._typedattrr   r   Úabcr   r   r   r   r   r   Úversion_infor   r    Útyping_extensionsr"   r#   r$   Útupler4   r%   r:   r&   r   r   r   r   r;   r;   r;   r<   Ú<module>   s>     
	
 ^X