
    > /i<                    ^   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jD                  dk\  rd dlm#Z# nd dl$m#Z# ejD                  dk\  r	d dlm%Z%m&Z& nd dl$m%Z%m&Z& ejD                  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e      Z/ ed       G d de             Z0 ed       G d d e e0                Z1 G d! d"e      Z2y)#    )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                     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<   y)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/   r0   r2   r3   r5   r6   r8        G/srv/sistema/bot/venv/lib/python3.12/site-packages/anyio/streams/tls.pyr   r   :   s    ? !0 1M:1(7(99#2#4F 4 FUEVBV,;,=\=')K) 9H8IN5I / 1J1 !0 11&(K(r?   r   F)eqc                      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       Z	 	 	 	 	 	 ddZddZddZ	dddZ
ddZddZedd       Zy)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_streamr1   r6   r4   _ssl_objectzssl.MemoryBIO	_read_bio
_write_bioNT)r2   hostnamessl_contextr6   c                 K   || }|s|rt         j                  j                  nt         j                  j                  }t        j                  |      }t        t         d      r$|xj                  t         j                   z  c_        t        j                         }t        j                         }t        |      t         j                  u r|j                  ||||      }	n,t        j                  |j                  ||||d       d{   }	 | |||	||      }
|
j                  |	j                         d{    |
S 7 67 w)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)r2   server_hostname)rC   r6   rD   rE   rF   )sslPurposeCLIENT_AUTHSERVER_AUTHcreate_default_contexthasattroptionsrJ   	MemoryBIOtyper   wrap_bior   run_sync_call_sslobject_methoddo_handshake)clsrC   r2   rG   rH   r6   purposebio_inbio_outr5   wrappers              r@   wrapzTLSStream.wraph   s/    : &,K+6''CKK<S<S  44W=K s67##(D(D'DD#--/ .$--[( . J  )11$$  J - 3"
 ,,Z-D-DEEE#  	Fs$   DE	E 
/E9E:EEc                t  K   	 	  || }| j                   j                  r;| j                  j                  | j                   j	                                d {    |S 7 # t
        j                  $ r 	 | j                   j                  r<| j                  j                  | j                   j	                                d {  7   | j                  j                          d {  7  }| j                  j                  |       np# t        $ r | j                  j                          Y nKt        $ r@}| j                  j                          | j                   j                          t        |d }~ww xY wY n>t
        j                  $ r? | j                  j                  | j                   j	                                d {  7   Y nt
        j                  $ r@}| j                  j                          | j                   j                          t        |d }~wt
        j                   $ r}| j                  j                          | j                   j                          t#        |t
        j$                        s|j&                  r(d|j&                  v r| j(                  rt        |t        d  d }~ww xY ww)NUNEXPECTED_EOF_WHILE_READING)rF   pendingrC   sendreadrL   SSLWantReadErrorreceiverE   writer   	write_eofOSErrorr   SSLWantWriteErrorSSLSyscallErrorSSLError
isinstanceSSLEOFErrorstrerrorr6   )selffuncargsresultdataexcs         r@   rW   z TLSStream._call_sslobject_method   s
     (tH ??**//44T__5I5I5KLLL MI '' //.."33889M9M9OPPP!%!6!6!>!>!@@@D NN((. # /NN,,. 7NN,,.OO--/-367 (( I++001E1E1GHHH&& 3((*))+)s2<< ((*))+c3??3LL%Cs||%S//1s:)t31 s   J8A AJ8AJ8J34AD C"D#C&$D)J3#E2(J3*E22;E--E22J35J88AJ3 GJ3J8J3;HJ3+BJ..J33J8c                  K   | j                  | j                  j                         d{    | j                  j	                          | j
                  j	                          | j                  | j                  j                         fS 7 ^w)z
        Does the TLS closing handshake.

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

        N)rW   rD   unwraprE   rg   rF   rC   rc   ro   s    r@   rv   zTLSStream.unwrap   sl      ))$*:*:*A*ABBB  "!!#$$dnn&9&9&;;; 	Cs   )BB
ABc                   K   | j                   r	 | j                          d {    | j                  j                          d {    y 7 '# t        $ r  t        | j                         d {  7    w xY w7 3wN)r6   rv   BaseExceptionr   rC   acloserw   s    r@   r{   zTLSStream.aclose   sm     ##kkm##
 ##**,,, $  '(=(=>>> 	-sC   A;A AA A;A9A;A !A6.A1/A66A;c                ~   K   | j                  | j                  j                  |       d {   }|st        |S 7 wry   )rW   rD   rc   r   )ro   	max_bytesrs   s      r@   re   zTLSStream.receive   s;     001A1A1F1F	RR	 Ss   *=;=c                l   K   | j                  | j                  j                  |       d {    y 7 wry   )rW   rD   rf   )ro   items     r@   rb   zTLSStream.send   s(     ))$*:*:*@*@$GGGs   *424c                (  K   | j                  t        j                        }t        j                  d|      }|rMt        |j                  d            t        |j                  d      xs d      }}||fdk  rt        d|       t        d      w)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   r8   rematchintgroupNotImplementedError)ro   r8   r   majorminors        r@   send_eofzTLSStream.send_eof   s     jj!9!9:1;?u{{1~.EKKN4Ga0H5Eu~&)$$/=2 
 "E
 	
s   BB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                 :     j                   j                  d      S )NFrD   getpeercertrw   s   r@   <lambda>z,TLSStream.extra_attributes.<locals>.<lambda>  s    43C3C3O3OPU3V r?   c                 :     j                   j                  d      S )NTr   rw   s   r@   r   z,TLSStream.extra_attributes.<locals>.<lambda>  s    $:J:J:V:V; r?   c                 0     j                   j                  S ry   )rD   r2   rw   s   r@   r   z,TLSStream.extra_attributes.<locals>.<lambda>  s    d.>.>.J.J r?   c                 h     j                   j                  r j                   j                         S d S ry   )rD   r2   r3   rw   s   r@   r   z,TLSStream.extra_attributes.<locals>.<lambda>  s0    ++ 261A1A1P1P1R  r?   c                      j                   S ry   r6   rw   s   r@   r   z,TLSStream.extra_attributes.<locals>.<lambda>      d6N6N r?   c                      j                   S ry   )rD   rw   s   r@   r   z,TLSStream.extra_attributes.<locals>.<lambda>  s    T-=-= r?   )rC   extra_attributesr   r+   rD   selected_alpn_protocolr-   get_channel_bindingr.   r/   r0   r2   r3   r6   r5   r8   versionrw   s   `r@   r   zTLSStream.extra_attributes  s    
##44
&&(8(8(O(O33  44!1!1!8!8))+V00 3 $$&J'' * ,,.N##%=$$d&6&6&>&>#
 	
r?   )rC   r   r2   zbool | NonerG   r*   rH   ssl.SSLContext | Noner6   r1   returnr   )rp   z&Callable[[Unpack[PosArgsT]], T_Retval]rq   zUnpack[PosArgsT]r   r%   )r   ztuple[AnyByteStream, bytes]r   None)i   )r}   r   r   r,   )r   r,   r   r   r   zMapping[Any, Callable[[], Any]])r9   r:   r;   r<   r=   classmethodr^   rW   rv   r{   re   rb   r   propertyr   r>   r?   r@   r   r   V   s     $#
 $(#-1$(D'D !	D
 D +D "D 
D DL,:,CS,	,\
<-H
 
 
r?   r   c                      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       Z	 d	 	 	 	 	 ddZ	ddZ
edd       Zy)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.SSLContextrH   Tr1   r6      floathandshake_timeoutc                   K   t        |       d{    t        | t                     s*t        j                  t
              j                  d|        t        | t              rt        | t                     r y7 hw)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   rl   r   logging	getLoggerr9   	exception	Exception)rt   streams     r@   handle_handshake_errorz"TLSListener.handle_handshake_error;  sr        ''' #689
 h'11,s 2 
 #y)Z=T=V-W .X 	(s   A<A:A)A<Nc                    K   t              d fd       } j                  j                  ||       d {    y 7 w)Nc                \  K   ddl m} 	  |j                        5  t        j	                  | j
                  j                         d {   }d d d                d {    y 7 # 1 sw Y   xY w7 # t        $ r%}j                  ||        d {  7   Y d }~y d }~ww xY ww)Nr   )
fail_after)rH   r6   )	 r   r   r   r^   rH   r6   rz   r   )r   r   wrapped_streamrt   handlerro   s       r@   handler_wrapperz*TLSListener.serve.<locals>.handler_wrapper`  s     %
. 6 67 +4>>$($4$4,0,D,D ,: , &N n---&  . ! ?11#v>>>?su   B,A; 0A-A+A-A; B,%A9&B,+A--A62A; 9B,;	B)B$BB$B,$B))B,)r   r   r   r   )r   r   serve)ro   r   
task_groupr   s   ``  r@   r   zTLSListener.serve[  s;     
 
w	. 
	. mm!!/:>>>s   5A?Ac                T   K   | j                   j                          d {    y 7 wry   )r   r{   rw   s    r@   r{   zTLSListener.acloser  s     mm""$$$s   (&(c                .     t         j                   fdiS )Nc                      j                   S ry   r   rw   s   r@   r   z.TLSListener.extra_attributes.<locals>.<lambda>x  r   r?   )r   r6   rw   s   `r@   r   zTLSListener.extra_attributesu  s     ,,.N
 	
r?   )rt   rz   r   r   r   r   ry   )r   zCallable[[TLSStream], Any]r   zTaskGroup | Noner   r   r   r   )r9   r:   r;   r<   r=   r6   r   staticmethodr   r   r{   r   r   r>   r?   r@   r   r   #  s}    "  $$!u! D (,?+? %? 
	?.% 
 
r?   r   c                  F    e Zd ZdZdddd	 	 	 	 	 	 	 	 	 ddZedd       Zy)	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rG   rH   r6   c               8   || _         |xs- t        j                  t        j                  j                        | _        t        | j
                  t        j                        s+t        dt        | j
                        j                         || _        || _        y )Nz7ssl_context must be an instance of ssl.SSLContext, not )connectablerL   rP   rM   rO   rH   rl   r   	TypeErrorrT   r9   rG   r6   )ro   r   rG   rH   r6   s        r@   __init__zTLSConnectable.__init__  s     ''2 (
c6P6PKK##7
 $**CNN;I(()2235  !#6 r?   c                  K   | j                   j                          d {   }	 t        j                  || j                  | j
                  | j                         d {   S 7 D7 # t        $ r t        |       d {  7    w xY ww)Nr   )	r   connectr   r^   rG   rH   r6   rz   r   )ro   r   s     r@   r   zTLSConnectable.connect  s     ''//11		" ,,$($<$<	 (    2  	#F+++	sD   BA%B:A)  A'!A) $B'A) )B BBB)
r   r   rG   r*   rH   r   r6   r1   r   r   )r   r   )r9   r:   r;   r<   r   r$   r   r>   r?   r@   r   r   |  s[    	  $-1$(7-7 	7
 +7 "7 
7(  r?   r   )3
__future__r   __all__r   r   rL   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&   tupler7   r'   r=   r(   r   r   r   r   r>   r?   r@   <module>r      s1   "  	 
 
 - !     B  w +w++6w*:
#5c?C/0 0GSL)) ))$ )8 eI

 I
 I
X eU
(9% U
 U
p,* ,r?   