o
    ACe                     @  s   d dl mZ d dlZd dlmZmZ d dlmZmZ d dl	Z	ddl
mZ ddlmZ edZd	ded
d+ddZdd	ddd,dd Zdd	dde	jd!d-d)d*ZdS ).    )annotationsN)	AwaitableCallable)NoReturnTypeVar   )DEFAULT_DELAY)SocketStreamTF)https_compatiblessl_contexthappy_eyeballs_delayhoststr | bytesportintr   boolr   ssl.SSLContext | Noner   float | Nonereturntrio.SSLStream[SocketStream]c                  sV   t j| ||dI dH }|du r"t }ttdr"| jtj M  _t j||| |dS )aT  Make a TLS-encrypted Connection to the given host and port over TCP.

    This is a convenience wrapper that calls :func:`open_tcp_stream` and
    wraps the result in an :class:`~trio.SSLStream`.

    This function does not perform the TLS handshake; you can do it
    manually by calling :meth:`~trio.SSLStream.do_handshake`, or else
    it will be performed automatically the first time you send or receive
    data.

    Args:
      host (bytes or str): The host to connect to. We require the server
          to have a TLS certificate valid for this hostname.
      port (int): The port to connect to.
      https_compatible (bool): Set this to True if you're connecting to a web
          server. See :class:`~trio.SSLStream` for details. Default:
          False.
      ssl_context (:class:`~ssl.SSLContext` or None): The SSL context to
          use. If None (the default), :func:`ssl.create_default_context`
          will be called to create a context.
      happy_eyeballs_delay (float): See :func:`open_tcp_stream`.

    Returns:
      trio.SSLStream: the encrypted connection to the server.

    )r   NOP_IGNORE_UNEXPECTED_EOF)Zserver_hostnamer   )trioZopen_tcp_streamsslZcreate_default_contexthasattroptionsr   Z	SSLStream)r   r   r   r   r   Z
tcp_stream r   ?usr/lib/python3.10/site-packages/trio/_highlevel_ssl_helpers.pyopen_ssl_over_tcp_stream   s   "
r   r   r   backlogssl.SSLContextstr | bytes | Noner    
int | None$list[trio.SSLListener[SocketStream]]c                  s0   t j| ||dI dH } fdd|D }|S )a*  Start listening for SSL/TLS-encrypted TCP connections to the given port.

    Args:
      port (int): The port to listen on. See :func:`open_tcp_listeners`.
      ssl_context (~ssl.SSLContext): The SSL context to use for all incoming
          connections.
      host (str, bytes, or None): The address to bind to; use ``None`` to bind
          to the wildcard address. See :func:`open_tcp_listeners`.
      https_compatible (bool): See :class:`~trio.SSLStream` for details.
      backlog (int or None): See :func:`open_tcp_listeners` for details.

    )r   r    Nc                   s   g | ]
}t j| d qS ))r   )r   ZSSLListener).0Ztcp_listenerr   r   r   r   
<listcomp>]   s    z/open_ssl_over_tcp_listeners.<locals>.<listcomp>)r   Zopen_tcp_listeners)r   r   r   r   r    Ztcp_listenersZssl_listenersr   r&   r   open_ssl_over_tcp_listenersH   s   r(   )r   r   r    handler_nurserytask_statushandler;Callable[[trio.SSLStream[SocketStream]], Awaitable[object]]r)   trio.Nursery | Noner*   5trio.TaskStatus[list[trio.SSLListener[SocketStream]]]r   c          	        s8   t j|||||dI dH }t j| |||dI dH  dS )uO  Listen for incoming TCP connections, and for each one start a task
    running ``handler(stream)``.

    This is a thin convenience wrapper around
    :func:`open_ssl_over_tcp_listeners` and :func:`serve_listeners` – see them
    for full details.

    .. warning::

       If ``handler`` raises an exception, then this function doesn't do
       anything special to catch it – so by default the exception will
       propagate out and crash your server. If you don't want this, then catch
       exceptions inside your ``handler``, or use a ``handler_nursery`` object
       that responds to exceptions in some other way.

    When used with ``nursery.start`` you get back the newly opened listeners.
    See the documentation for :func:`serve_tcp` for an example where this is
    useful.

    Args:
      handler: The handler to start for each incoming connection. Passed to
          :func:`serve_listeners`.

      port (int): The port to listen on. Use 0 to let the kernel pick
          an open port. Ultimately passed to :func:`open_tcp_listeners`.

      ssl_context (~ssl.SSLContext): The SSL context to use for all incoming
          connections. Passed to :func:`open_ssl_over_tcp_listeners`.

      host (str, bytes, or None): The address to bind to; use ``None`` to bind
          to the wildcard address. Ultimately passed to
          :func:`open_tcp_listeners`.

      https_compatible (bool): Set this to True if you want to use
          "HTTPS-style" TLS. See :class:`~trio.SSLStream` for details.

      backlog (int or None): See :class:`~trio.SSLStream` for details.

      handler_nursery: The nursery to start handlers in, or None to use an
          internal nursery. Passed to :func:`serve_listeners`.

      task_status: This function can be used with ``nursery.start``.

    Returns:
      This function only returns when cancelled.

    r   N)r)   r*   )r   r(   Zserve_listeners)	r+   r   r   r   r   r    r)   r*   Z	listenersr   r   r   serve_ssl_over_tcpd   s   <r/   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r!   r   r"   r   r   r    r#   r   r$   )r+   r,   r   r   r   r!   r   r"   r   r   r    r#   r)   r-   r*   r.   r   r   )
__future__r   r   collections.abcr   r   typingr   r   r   Z_highlevel_open_tcp_streamr   Z_highlevel_socketr	   r
   r   r(   ZTASK_STATUS_IGNOREDr/   r   r   r   r   <module>   s,    4!