
    "d/                        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m	Z	m
Z
mZmZmZmZmZmZ ddl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  ed	          Zeeeef         d
f         Z ee d
f         Z! G d de          Z" ed           G d de                      Z# ed           G d dee#                               Z$dS )    N)	dataclass)wraps)	AnyCallableDictListMappingOptionalTupleTypeVarUnion   )BrokenResourceErrorEndOfStreamaclose_forcefullyget_cancelled_exc_class)TypedAttributeSettyped_attribute)AnyByteStream
ByteStreamListener	TaskGroupT_Retval.c                      e Zd ZU dZ e            Zee         ed<    e            Z	e
ed<    e            Zeeeef         ed<    e            Zeeeeeeef         f                  ed<    e            Zee
         ed<    e            Zeed<    e            Zeeeeef                  ed<    e            Zej        ed	<    e            Zeed
<    e            Zeed<   dS )TLSAttributez5Contains Transport Layer Security related attributes.alpn_protocolchannel_binding_tls_uniquecipherpeer_certificatepeer_certificate_binaryserver_sideshared_ciphers
ssl_objectstandard_compatibletls_versionN)__name__
__module____qualname____doc__r   r   r
   str__annotations__r   bytesr   r   intr   r   r   _PCTRTTT_PCTRTTr    r!   boolr"   r   r#   ssl	SSLObjectr$   r%        5D:\api_v1\venv\Lib\site-packages\anyio/streams/tls.pyr   r      s_        ?? $3?#4#4M8C=444(7(9(9999#2?#4#4FE#sC- 444
 	 hS%Xw.//0    0?/@/@Xe_@@@'))K)))1@1B1BNDsC}-.BBB / 1 1J111 !0 1 1111&((K(((((r4   r   F)eqc                   |   e Zd ZU dZeed<   eed<   ej        ed<   ej	        ed<   ej	        ed<   e
ddddd	ded
ee         dee         deej                 dedd fd            Zdedef         dedefdZdeeef         fdZddZddedefdZdeddfdZddZedeeeg ef         f         fd            ZdS )	TLSStreama  
    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

    transport_streamr$   _ssl_object	_read_bio
_write_bioNT)r!   hostnamessl_contextr$   r!   r=   r>   returnc                  K   || }|sg|rt           j        j        nt           j        j        }t          j        |          }t          t           d          r|xj        t           j        z  c_        t          j                    }t          j                    }|	                    ||||          }	 | |||	||          }
|

                    |	j                   d{V  |
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)r!   server_hostname)r9   r$   r:   r;   r<   )r1   PurposeCLIENT_AUTHSERVER_AUTHcreate_default_contexthasattroptionsrA   	MemoryBIOwrap_bio_call_sslobject_methoddo_handshake)clsr9   r!   r=   r>   r$   purposebio_inbio_outr#   wrappers              r5   wrapzTLSStream.wrapE   s	     8 &,K 	D+6S''CK<S  4W==K s677 D##s'CC##-// ))Gh * 
 

 #- 3"
 
 
 ,,Z-DEEEEEEEEEr4   func.argsc                   K   	 	  || }| j         j        r7| j                            | j                                                    d {V  |S # t
          j        $ r 	 | j         j        r7| j                            | j                                                    d {V  | j                                         d {V }| j        	                    |           np# t          $ r | j                                         Y nKt          $ r?}| j                                         | j                                          t          |d }~ww xY wY nt
          j        $ r: | j                            | j                                                    d {V  Y nt
          j        $ r?}| j                                         | j                                          t          |d }~wt
          j        $ rr}| j                                         | j                                          t#          |t
          j                  s	d|j        v r| j        rt          |t          d  d }~ww xY wc)NTUNEXPECTED_EOF_WHILE_READING)r<   pendingr9   sendreadr1   SSLWantReadErrorreceiver;   writer   	write_eofOSErrorr   SSLWantWriteErrorSSLSyscallErrorSSLError
isinstanceSSLEOFErrorstrerrorr$   )selfrS   rT   resultdataexcs         r5   rK   z TLSStream._call_sslobject_method}   s     *	)tJ ?* M/44T_5I5I5K5KLLLLLLLLLO ' / / //. Q"3889M9M9O9OPPPPPPPPP!%!6!>!>!@!@@@@@@@D N((.... # / / /N,,..... 7 7 7N,,...O--///-367 ( I I I+001E1E1G1GHHHHHHHHHHH& 3 3 3((***))+++)s2<   ((***))+++sCO4445EE/ 41s:)t31*	sb   A I"A"CI"#E	?I"	E	
:EE		I"AI"I"$:GI"0A-II"c                    K   |                      | j        j                   d{V  | 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)rK   r:   unwrapr;   r]   r<   r9   rY   re   s    r5   rj   zTLSStream.unwrap   sx       ))$*:*ABBBBBBBBB  """!!###$dn&9&9&;&;;;r4   c                    K   | j         rE	 |                                  d {V  n)# t          $ r t          | j                   d {V   w xY w| j                                         d {V  d S N)r$   rj   BaseExceptionr   r9   acloserk   s    r5   ro   zTLSStream.aclose   s      # 	kkmm########    '(=>>>>>>>>> #**,,,,,,,,,,,s	   & &A   	max_bytesc                 h   K   |                      | j        j        |           d {V }|st          |S rm   )rK   r:   rY   r   )re   rq   rg   s      r5   r[   zTLSStream.receive   sF      001A1F	RRRRRRRR 	r4   itemc                 V   K   |                      | j        j        |           d {V  d S rm   )rK   r:   r\   )re   rs   s     r5   rX   zTLSStream.send   s:      ))$*:*@$GGGGGGGGGGGr4   c                 P  K   |                      t          j                  }t          j        d|          }|r`t          |                    d                    t          |                    d          pd          }}||fdk     rt          d|           t          d          )NzTLSv(\d+)(?:\.(\d+))?   r   r   )rv      z;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)extrar   r%   rematchr-   groupNotImplementedError)re   r%   rz   majorminors        r5   send_eofzTLSStream.send_eof   s      jj!9::1;?? 	u{{1~~..EKKNN4Ga0H0H5Eu~&&)2$/2 2  
 "E
 
 	
r4   c                     i  j         j        t          j         j        j        t          j         j        j        t          j         j        j        t          j	         fdt          j
         fdt          j         fdt          j         fdt          j         fdt          j         fdt          j         j        j        i
S )Nc                  8     j                             d          S )NFr:   getpeercertrk   s   r5   <lambda>z,TLSStream.extra_attributes.<locals>.<lambda>   s    43C3O3OPU3V3V r4   c                  8     j                             d          S )NTr   rk   s   r5   r   z,TLSStream.extra_attributes.<locals>.<lambda>   s    $:J:V:V; ; r4   c                       j         j        S rm   )r:   r!   rk   s   r5   r   z,TLSStream.extra_attributes.<locals>.<lambda>   s    d.>.J r4   c                  6     j                                         S rm   )r:   r"   rk   s   r5   r   z,TLSStream.extra_attributes.<locals>.<lambda>   s    1A1P1P1R1R r4   c                       j         S rm   r$   rk   s   r5   r   z,TLSStream.extra_attributes.<locals>.<lambda>   
    d6N r4   c                       j         S rm   )r:   rk   s   r5   r   z,TLSStream.extra_attributes.<locals>.<lambda>   s
    T-= r4   )r9   extra_attributesr   r   r:   selected_alpn_protocolr   get_channel_bindingr   r   r    r!   r"   r$   r#   r%   versionrk   s   `r5   r   zTLSStream.extra_attributes   s    
#4
&(8(O3T5E5Y!1!8)+V+V+V+V0 3 3 3 3 $&J&J&J&J')R)R)R)R,.N.N.N.N#%=%=%=%=$d&6&>
 
 	
r4   r?   N)rp   )r&   r'   r(   r)   r   r+   r0   r1   r2   rI   classmethodr
   r*   
SSLContextrR   r   r   objectrK   r   r,   rj   ro   r-   r[   rX   r   propertyr	   r   r   r3   r4   r5   r8   r8   3   s          $###}
 '+"&04$(5 5 5'5 d^	5
 3-5 cn-5 "5 
5 5 5 [5n-S(]+-4:-	- - - -^
<eM5$89 
< 
< 
< 
<- - - - s u    Hu H H H H H
 
 
 
 
'#xC/@*@"A 
 
 
 X
 
 
r4   r8   c                       e Zd ZU dZee         ed<   ej        ed<   dZ	e
ed<   dZeed<   eded	ed
dfd            Z	 ddeegef         dee         d
dfdZddZed
eeeg ef         f         fd            ZdS )TLSListenera  
    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`)
    listenerr>   Tr$      handshake_timeoutrh   streamr?   Nc                 >  K   dt            d t          |           d {V  t          | t                                s,t	          j        t                                         d           t          | t                    rt          | t                                r d S )Nz
        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 ``z``
           logger
        #. Reraises the exception if it was a base exception or a cancellation exception

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

        zError during TLS handshake)r&   r   rb   r   logging	getLogger	exception	Exception)rh   r   s     r5   handle_handshake_errorz"TLSListener.handle_handshake_error  s      	 U]	 	 	 	  ''''''''' #68899 	Ph''112NOOO #y)) 	Z=T=V=V-W-W 		 	r4   handler
task_groupc                     K   t                    dt          dd f fd            } j                            ||           d {V  d S )Nr   r?   c                 N  K   ddl m} 	  |j                  5  t                              | j        j                   d {V }d d d            n# 1 swxY w Y    |           d {V  d S # t          $ r'}                    ||            d {V  Y d }~d S d }~ww xY w)Nr   )
fail_after)r>   r$   )	 r   r   r8   rR   r>   r$   rn   r   )r   r   wrapped_streamrh   r   re   s       r5   handler_wrapperz*TLSListener.serve.<locals>.handler_wrapper$  sP     %%%%%%
.Z 677  +4>>$($4,0,D ,: , , & & & & & &N               gn----------- ! ? ? ?11#v>>>>>>>>>>>>>>>?s:   A3 .A	A3 AA3 AA3 3
B$=BB$)r   r   r   serve)re   r   r   r   s   ``  r5   r   zTLSListener.serve  s}      
 
w	.- 	.D 	. 	. 	. 	. 	. 	. 
	. m!!/:>>>>>>>>>>>r4   c                 H   K   | j                                          d {V  d S rm   )r   ro   rk   s    r5   ro   zTLSListener.aclose6  s2      m""$$$$$$$$$$$r4   c                 &     t           j         fdiS )Nc                       j         S rm   r   rk   s   r5   r   z.TLSListener.extra_attributes.<locals>.<lambda><  r   r4   )r   r$   rk   s   `r5   r   zTLSListener.extra_attributes9  s!     ,.N.N.N.N
 	
r4   rm   r   )r&   r'   r(   r)   r   r   r+   r1   r   r$   r0   r   floatstaticmethodrn   r   r   r   r8   r
   r   r   ro   r   r	   r   r3   r4   r5   r   r      s/           sm $$$$!u!!!-  SW    \8 +/? ?9+s*+? Y'? 
	? ? ? ?.% % % % 
'#xC/@*@"A 
 
 
 X
 
 
r4   r   )%r   ry   r1   dataclassesr   	functoolsr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   _core._typedattrr   r   abcr   r   r   r   r   r*   r/   r.   r   r8   r   r3   r4   r5   <module>r      s
    				 



 ! ! ! ! ! !       V V V V V V V V V V V V V V V V V V V V V V            B A A A A A A A @ @ @ @ @ @ @ @ @ @ @ @7:
c3h$
%#) ) ) ) )$ ) ) ): ew
 w
 w
 w
 w

 w
 w
 w
t eN
 N
 N
 N
 N
(9% N
 N
 N
 N
 N
r4   