Format: application/x-python-code
⬇ Download File (10.8 KB)
   1  o
   2  
   3  ;'h�3�@sPddlmZddlZddlZddlZddlZddlmZmZddl	m
   4  Z
   5  ddlmZddl
   6  mZmZddlmZmZmZmZmZdd	lmZmZdd
   7  lmZmZmZmZejdkrbddl
   8  mZm Z nddl!mZm Z ed
   9  �Z"ed�Z#e$e$e%e%fdfZ&e$e&dfZ'Gdd�de�Z(e
  10  dd�Gdd�de��Z)e
  11  dd�Gdd�dee)��Z*dS)�)�annotationsN)�Callable�Mapping)�	dataclass)�wraps)�Any�TypeVar�)�BrokenResourceError�EndOfStream�aclose_forcefully�get_cancelled_exc_class�	to_thread)�TypedAttributeSet�typed_attribute)�
  12  AnyByteStream�
  13  ByteStream�Listener�	TaskGroup)��)�TypeVarTuple�Unpack�T_Retval�PosArgsT.c@s�eZdZUdZe�Zded<e�Zded<e�Zded<e�Z	ded	<e�Z
  14  d
  15  ed<e�Zded
  16  <e�Zded<e�Z
  17  ded<e�Zded<e�Zded<dS)�TLSAttributez5Contains Transport Layer Security related attributes.�
  18  str | None�
  19  alpn_protocol�bytes�channel_binding_tls_uniqueztuple[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�
  20  ssl.SSLObject�
  21  ssl_object�standard_compatible�str�tls_versionN)�__name__�
  22  __module__�__qualname__�__doc__rr�__annotations__rr r!r"r$r%r'r(r*�r0r0�GC:\Users\shubh\Videos\agent\nova\Lib\site-packages\anyio/streams/tls.pyr!s
  23  rF)�eqc@s�eZdZUdZded<ded<ded<ded	<ded
  24  <eddddd
  25  �d2dd��Zd3dd�Zd4dd �Zd5d"d#�Z	d6d7d(d)�Z
  26  d8d+d,�Zd5d-d.�Ze
  27  d9d0d1��ZdS):�	TLSStreama
  28      A stream wrapper that encrypts all sent data and decrypts received data.
  29  
  30      This class has no public initializer; use :meth:`wrap` instead.
  31      All extra attributes from :class:`~TLSAttribute` are supported.
  32  
  33      :var AnyByteStream transport_stream: the wrapped stream
  34  
  35      r�transport_streamr#r(r&�_ssl_objectz
  36  ssl.MemoryBIO�	_read_bio�
  37  _write_bioNT)r$�hostname�ssl_contextr(r$�bool | Noner8rr9�ssl.SSLContext | None�returnc�s��|dur|}|s'|rtjjntjj}t�|�}ttd�r'|jtjM_t��}t��}t	|�tj
  38  ur@|j||||d�}	nt�
  39  |j||||d�IdH}	||||	||d�}
  40  |
  41  �|	j�IdH|
  42  S)a�
  43          Wrap an existing stream with Transport Layer Security.
  44  
  45          This performs a TLS handshake with the peer.
  46  
  47          :param transport_stream: a bytes-transporting stream to wrap
  48          :param server_side: ``True`` if this is the server side of the connection,
  49              ``False`` if this is the client side (if omitted, will be set to ``False``
  50              if ``hostname`` has been provided, ``False`` otherwise). Used only to create
  51              a default context when an explicit context has not been provided.
  52          :param hostname: host name of the peer (if host name checking is desired)
  53          :param ssl_context: the SSLContext object to use (if not provided, a secure
  54              default will be created)
  55          :param standard_compatible: if ``False``, skip the closing handshake when
  56              closing the connection, and don't raise an exception if the peer does the
  57              same
  58          :raises ~ssl.SSLError: if the TLS handshake fails
  59  
  60          N�OP_IGNORE_UNEXPECTED_EOF)r$�server_hostname)r4r(r5r6r7)�ssl�Purpose�CLIENT_AUTH�SERVER_AUTH�create_default_context�hasattr�optionsr=�	MemoryBIO�type�
  61  SSLContext�wrap_biorZrun_sync�_call_sslobject_method�do_handshake)�clsr4r$r8r9r(�purposeZbio_inZbio_outr'�wrapperr0r0r1�wrapOs@��
  62  
  63  �
  64  �	�zTLSStream.wrap�func�&Callable[[Unpack[PosArgsT]], T_Retval]�args�Unpack[PosArgsT]rc
  65  �s��	z||�}Wn�tjyYz|jjr!|j�|j���IdH|j��IdH}Wn&ty8|j	�
  66  �YntyP}z
  67  |j	�
  68  �|j�
  69  �t|�d}~ww|j	�
  70  |�Ynotjyn|j�|j���IdHYnZtjy�}z
  71  |j	�
  72  �|j�
  73  �t|�d}~wtjy�}z"|j	�
  74  �|j�
  75  �t|tj�s�|jr�d|jvr�|jr�t|�td��d}~ww|jjr�|j�|j���IdH|Sq)NTZUNEXPECTED_EOF_WHILE_READING)r?�SSLWantReadErrorr7�pendingr4�send�read�receiverr6�	write_eof�OSErrorr
  76  �write�SSLWantWriteError�SSLSyscallError�SSLError�
  77  isinstance�SSLEOFError�strerrorr()�selfrPrR�result�data�excr0r0r1rJ�sV�
  78  
  79  ���
  80  
  81  �
  82  
  83  �
  84  ���z TLSStream._call_sslobject_method�tuple[AnyByteStream, bytes]c�s:�|�|jj�IdH|j��|j��|j|j��fS)z�
  85          Does the TLS closing handshake.
  86  
  87          :return: a tuple of (wrapped byte stream, bytes left in the read buffer)
  88  
  89          N)rJr5�unwrapr6rYr7r4rW�rbr0r0r1rg�s
  90  �
  91  
  92  zTLSStream.unwrap�Nonec�sP�|jrz	|��IdHWntyt|j�IdH�w|j��IdHdS�N)r(rg�
  93  BaseExceptionrr4�acloserhr0r0r1rl�s��zTLSStream.aclose��	max_bytes�intrc�s$�|�|jj|�IdH}|st�|Srj)rJr5rWr)rbrnrdr0r0r1rX�s
  94  �zTLSStream.receive�itemc�s�|�|jj|�IdHdSrj)rJr5r[)rbrpr0r0r1rV�s�zTLSStream.sendc�sb�|�tj�}t�d|�}|r-t|�d��t|�d�pd�}}||fdkr-td|����td��)NzTLSv(\d+)(?:\.(\d+))?�r	r)rqrz;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)�extrarr*�re�matchro�group�NotImplementedError)rbr*rt�major�minorr0r0r1�send_eof�s�"���zTLSStream.send_eof�Mapping[Any, Callable[[], Any]]cs�i�jj�tj�jjtj�jjtj�jjtj	�fdd�tj
  95  �fdd�tj�fdd�tj�fdd�tj
  96  �fdd�tj�fdd�tj�jji
  97  �S)Nc��j�d�S)NF�r5�getpeercertr0rhr0r1�<lambda>�sz,TLSStream.extra_attributes.<locals>.<lambda>cr{)NTr|r0rhr0r1r~�s�cs�jjSrj)r5r$r0rhr0r1r~scs�jjr	�j��SdSrj)r5r$r%r0rhr0r1r~s�c��jSrj�r(r0rhr0r1r~�crrj)r5r0rhr0r1r~r�)r4�extra_attributesrrr5�selected_alpn_protocolr�get_channel_bindingr r!r"r$r%r(r'r*�versionrhr0rhr1r��s�
  98  
  99  
 100  �zTLSStream.extra_attributes)r4rr$r:r8rr9r;r(r#r<r3)rPrQrRrSr<r)r<rf�r<ri)rm)rnror<r)rprr<ri�r<rz)r+r,r-r.r/�classmethodrOrJrgrlrXrVry�propertyr�r0r0r0r1r3=s*
 101  
 102  �
 103  F
 104  .
 105  
 106  
 107  
 108  r3c@sneZdZUdZded<ded<dZded<d	Zd
 109  ed<ed dd��Z	d!d"dd�Z	d#dd�Z
 110  ed$dd��ZdS)%�TLSListenera�
 111      A convenience listener that wraps another listener and auto-negotiates a TLS session
 112      on every accepted connection.
 113  
 114      If the TLS handshake times out or raises an exception,
 115      :meth:`handle_handshake_error` is called to do whatever post-mortem processing is
 116      deemed necessary.
 117  
 118      Supports only the :attr:`~TLSAttribute.standard_compatible` extra attribute.
 119  
 120      :param Listener listener: the listener to wrap
 121      :param ssl_context: the SSL context object
 122      :param standard_compatible: a flag passed through to :meth:`TLSStream.wrap`
 123      :param handshake_timeout: time limit for the TLS handshake
 124          (passed to :func:`~anyio.fail_after`)
 125      z
 126  Listener[Any]�listenerzssl.SSLContextr9Tr#r(��float�handshake_timeoutrerk�streamrr<ric�sL�t|�IdHt|t��st�t�jd|d�t|t�r#t|t��r$�dS)a�
 127          Handle an exception raised during the TLS handshake.
 128  
 129          This method does 3 things:
 130  
 131          #. Forcefully closes the original stream
 132          #. Logs the exception (unless it was a cancellation exception) using the
 133             ``anyio.streams.tls`` logger
 134          #. Reraises the exception if it was a base exception or a cancellation exception
 135  
 136          :param exc: the exception
 137          :param stream: the original stream
 138  
 139          NzError during TLS handshake)�exc_info)rr_r
 140  �logging�	getLoggerr+�	exception�	Exception)rer�r0r0r1�handle_handshake_error"s�
 141  ��z"TLSListener.handle_handshake_errorN�handler�Callable[[TLSStream], Any]�
 142  task_group�TaskGroup | Nonec�s2�t��d��fdd��}�j�||�IdHdS)Nr�rr<ric
 143  �s��ddlm}z$|�j��tj|�j�jd�IdH}Wd�n1s%wYWntyG}z��||�IdHWYd}~dSd}~ww�|�IdHdS)Nr	)�
 144  fail_after)r9r()	�r�r�r3rOr9r(rkr�)r�r�Zwrapped_streamre�r�rbr0r1�handler_wrapperGs ���� ��z*TLSListener.serve.<locals>.handler_wrapper)r�rr<ri)rr��serve)rbr�r�r�r0r�r1r�Bs�zTLSListener.servec�s�|j��IdHdSrj)r�rlrhr0r0r1rlYs�zTLSListener.acloserzcstj�fdd�iS)Ncrrjr�r0rhr0r1r~_r�z.TLSListener.extra_attributes.<locals>.<lambda>)rr(rhr0rhr1r�\s�zTLSListener.extra_attributes)rerkr�rr<rirj)r�r�r�r�r<rir�r�)
 145  r+r,r-r.r/r(r��staticmethodr�r�rlr�r�r0r0r0r1r�
 146  s
 147  "�
 148  r�)+�
 149  __future__rr�rsr?�sys�collections.abcrr�dataclassesr�	functoolsr�typingrrr�r
 150  rrr
 151  rZ_core._typedattrrr�abcrrrr�version_inforrZtyping_extensionsrr�tupler)Z_PCTRTTZ_PCTRTTTrr3r�r0r0r0r1�<module>s2
 152  M