o
    .j*#                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ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 dd
lmZ ddlmZmZ ddlmZmZ dgZG dd dejZ edddddd%d$dZ!dS )&    )annotationsN)partial)Callablecast   )pull_quic_header)QuicConfiguration)NetworkAddressQuicConnection)
QuicHeaderQuicPacketTypeencode_quic_retryencode_quic_version_negotiation)QuicRetryTokenHandler)SessionTicketFetcherSessionTicketHandler   )QuicConnectionProtocolQuicStreamHandlerservec                   @  s^   e Zd Zedddddd*ddZdd Zd+ddZd,ddZd-d$d%Zd.d&d'Z	d/d(d)Z
dS )0
QuicServerNFcreate_protocolsession_ticket_fetchersession_ticket_handlerretrystream_handlerconfigurationr   r   r   r   SessionTicketFetcher | Noner   SessionTicketHandler | Noner   boolr   QuicStreamHandler | NonereturnNonec                C  sN   || _ || _t | _i | _|| _|| _d | _|| _	|r"t
 | _d S d | _d S N)_configuration_create_protocolasyncioget_running_loop_loop
_protocols_session_ticket_fetcher_session_ticket_handler
_transport_stream_handlerr   _retry)selfr   r   r   r   r   r    r1   G/home/thesage/.local/lib/python3.10/site-packages/qh3/asyncio/server.py__init__   s   


zQuicServer.__init__c                 C  s4   t | j D ]}|  q| j  | j  d S r$   )setr*   valuescloseclearr-   )r0   protocolr1   r1   r2   r6   2   s   

zQuicServer.close	transportasyncio.BaseTransportc                 C  s   t tj|| _d S r$   )r   r'   DatagramTransportr-   )r0   r9   r1   r1   r2   connection_made8   s   zQuicServer.connection_madedatabytes | straddrr	   c                 C  s  t t|}zt|d| jj\
}}}}}}}	}
}}W n
 ty#   Y d S w t|t||||||	t|
d}|j	d urR|j	| jj
vrR| jt|j|j| jj
d| d S | j|jd }d }d }|d u rt|dkr|jtjkr| jd ur|jstd}| jt|j	||j|j| j||j|d| d S z| j||j\}}W n ty   Y d S w |j}t| j||| j| jd}| j|| j d}|!| j t"| j#|d	|_$t"| j%|d	|_&t"| j'|d	|_(|| j|j< || j|j)< |d ur|*|| d S d S )
Nr   )versionpacket_typepacket_lengthdestination_cid
source_cidtokenintegrity_tagsupported_versions)rD   rC   rG   i     )r@   rD   rC   original_destination_cidretry_token)r   "original_destination_connection_idretry_source_connection_idr   r   )r   )r8   )+r   bytes_pull_quic_header_rawr%   connection_id_length
ValueErrorr   r   listr@   rG   r-   sendtor   rC   rD   r*   getlenrA   INITIALr/   rE   osurandomr   create_tokenvalidate_tokenr
   r+   r,   r&   r.   r<   r   _connection_id_issued_connection_id_issued_handler_connection_id_retired_connection_id_retired_handler_connection_terminated_connection_terminated_handlerhost_ciddatagram_received)r0   r=   r?   _version_packet_type_packet_length_destination_cid_source_cid_token_integrity_tag_supported_versions_encrypted_offset_end_offsetheaderr8   rK   rL   rD   
connectionr1   r1   r2   ra   ;   s   



	zQuicServer.datagram_receivedcidrM   r8   r   c                 C  s   || j |< d S r$   r*   r0   rn   r8   r1   r1   r2   rZ      s   z QuicServer._connection_id_issuedc                 C  s   | j | |ks	J | j |= d S r$   ro   rp   r1   r1   r2   r\      s   z!QuicServer._connection_id_retiredc                 C  s,   t | j D ]\}}||kr| j|= qd S r$   )rQ   r*   items)r0   r8   rn   protor1   r1   r2   r^      s
   z!QuicServer._connection_terminated)r   r   r   r   r   r   r   r   r   r    r   r!   r"   r#   )r9   r:   r"   r#   )r=   r>   r?   r	   r"   r#   )rn   rM   r8   r   )rn   rM   r8   r   r"   r#   )r8   r   )__name__
__module____qualname__r   r3   r6   r<   ra   rZ   r\   r^   r1   r1   r1   r2   r      s    


q
r   Fr   hoststrportintr   r   r   r   r   r   r   r   r   r    r   r   r"   c                  s<   t  }|j fdd| |fdI dH \}	}
|
S )aQ  
    Start a QUIC server at the given `host` and `port`.

    :func:`serve` requires a :class:`~qh3.quic.configuration.QuicConfiguration`
    containing TLS certificate and private key as the ``configuration`` argument.

    :func:`serve` also accepts the following optional arguments:

    * ``create_protocol`` allows customizing the :class:`~asyncio.Protocol` that
      manages the connection. It should be a callable or class accepting the same
      arguments as :class:`~qh3.asyncio.QuicConnectionProtocol` and returning
      an instance of :class:`~qh3.asyncio.QuicConnectionProtocol` or a subclass.
    * ``session_ticket_fetcher`` is a callback which is invoked by the TLS
      engine when a session ticket is presented by the peer. It should return
      the session ticket with the specified ID or `None` if it is not found.
    * ``session_ticket_handler`` is a callback which is invoked by the TLS
      engine when a new session ticket is issued. It should store the session
      ticket for future lookup.
    * ``retry`` specifies whether client addresses should be validated prior to
      the cryptographic handshake using a retry packet.
    * ``stream_handler`` is a callback which is invoked whenever a stream is
      created. It must accept two arguments: a :class:`asyncio.StreamReader`
      and a :class:`asyncio.StreamWriter`.
    c                     s   t  dS )N)r   r   r   r   r   r   )r   r1   r   r   r   r   r   r   r1   r2   <lambda>   s    zserve.<locals>.<lambda>)
local_addrN)r'   r(   create_datagram_endpoint)rv   rx   r   r   r   r   r   r   loop_r8   r1   rz   r2   r      s   $)rv   rw   rx   ry   r   r   r   r   r   r   r   r   r   r    r   r   r"   r   )"
__future__r   r'   rV   	functoolsr   typingr   r   _hazmatr   rN   quic.configurationr   quic.connectionr	   r
   quic.packetr   r   r   r   
quic.retryr   tlsr   r   r8   r   r   __all__DatagramProtocolr   r   r1   r1   r1   r2   <module>   s*     )