
    F2j*#                        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ej                  Z e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                  R    e Zd Zedddddd#dZd Zd$dZd%dZd&d Zd'd!Z	d(d"Z
dS ))
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                   || _         || _        t          j                    | _        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   s          Z/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/qh3/asyncio/server.py__init__zQuicServer.__init__   sn     , /-//
?A'=$'=$<@- 	/11DKKKDKKK    c                    t          | j                                                  D ]}|                                 | j                                         | j                                         d S r'   )setr-   valuescloseclearr0   )r3   protocols     r4   r:   zQuicServer.close2   sh    DO224455 	 	HNNr6   	transportasyncio.BaseTransportc                D    t          t          j        |          | _        d S r'   )r   r*   DatagramTransportr0   )r3   r=   s     r4   connection_madezQuicServer.connection_made8   s    w8)DDr6   databytes | straddrr
   c                   t          t          |          }	 t          |d| j        j                  \
  }}}}}}}	}
}}n# t
          $ r Y d S w xY wt          |t          |          |||||	t          |
                    }|j	        T|j	        | j        j
        vrA| j                            t          |j        |j        | j        j
                  |           d S | j                            |j        d           }d }d }|t%          |          dk    r|j        t          j        k    r| j        |j        sqt/          j        d          }| j                            t3          |j	        ||j        |j        | j                            ||j        |                    |           d S 	 | j                            ||j                  \  }}n# t
          $ r Y d S w xY w|j        }t9          | j        ||| j        | j                  }|                     || j                   }|!                    | j                   tE          | j#        |	          |_$        tE          | j%        |	          |_&        tE          | j'        |	          |_(        || j        |j        <   || j        |j)        <   ||*                    ||           d S d S )
Nr   )versionpacket_typepacket_lengthdestination_cid
source_cidtokenintegrity_tagsupported_versions)rJ   rI   rM   i     )rF   rJ   rI   original_destination_cidretry_token)r   "original_destination_connection_idretry_source_connection_idr   r   )r   )r<   )+r   bytes_pull_quic_header_rawr(   connection_id_length
ValueErrorr   r   listrF   rM   r0   sendtor   rI   rJ   r-   getlenrG   INITIALr2   rK   osurandomr   create_tokenvalidate_tokenr   r.   r/   r)   r1   rA   r   _connection_id_issued_connection_id_issued_handler_connection_id_retired_connection_id_retired_handler_connection_terminated_connection_terminated_handlerhost_ciddatagram_received)r3   rB   rD   _version_packet_type_packet_length_destination_cid_source_cid_token_integrity_tag_supported_versions_encrypted_offset_end_offsetheaderr<   rQ   rR   rJ   
connections                      r4   rg   zQuicServer.datagram_received;   sg   E4  	 &dAt/B/WXX #! 	 	 	FF	 &|44(,"(#$788	
 	
 	
 N&d&9&LLLO""/%5$*$5'+':'M  
    F?&&v'=tDD;?*37"D		T!!"n&<<< {&| !#AJO**)$*N'1,2,=5;5K(,(@(@ $f&<j) )      F !K66tV\JJ>66%    6<5K2 ("13U+E'+'C'+'C  J ,,4+? -  H $$T_555 6=*X6 6 6H2 7>+h7 7 7H3 7>+h7 7 7H3 7?DOF233;DOJ/0&&tT22222  s!   &> 
AA,#G 
GGcidrS   r<   r   c                    || j         |<   d S r'   r-   r3   rt   r<   s      r4   r`   z QuicServer._connection_id_issued   s    'r6   c                <    | j         |         |k    sJ | j         |= d S r'   rv   rw   s      r4   rb   z!QuicServer._connection_id_retired   s,     s#x////OC   r6   c                z    t          | j                                                  D ]\  }}||k    r| j        |= d S r'   )rW   r-   items)r3   r<   rt   protos       r4   rd   z!QuicServer._connection_terminated   sK    t446677 	) 	)JC  OC(	) 	)r6   )r   r	   r   r   r   r    r   r!   r   r"   r   r#   r$   r%   )r=   r>   r$   r%   )rB   rC   rD   r
   r$   r%   )rt   rS   r<   r   )rt   rS   r<   r   r$   r%   )r<   r   )__name__
__module____qualname__r   r5   r:   rA   rg   r`   rb   rd    r6   r4   r   r      s        
 %;>B>B37     2     E E E Eo3 o3 o3 o3b( ( ( (! ! ! !) ) ) ) ) )r6   r   Fr   hoststrportintr   r	   r   r   r   r    r   r!   r   r"   r   r   r$   c                  K   t          j                    }|                    fd| |f           d{V \  }	}
|
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                 .    t                     S )N)r   r   r   r   r   r   )r   )r   r   r   r   r   r   s   r4   <lambda>zserve.<locals>.<lambda>   s(    
'+#9#9)
 
 
 r6   )
local_addrN)r*   r+   create_datagram_endpoint)r   r   r   r   r   r   r   r   loop_r<   s     ``````   r4   r   r      s      H #%%D55	
 	
 	
 	
 	
 	
 	
 	
 	
 $< 6 
 
 
 
 
 
 
 
KAx Or6   )r   r   r   r   r   r	   r   r   r   r    r   r!   r   r"   r   r   r$   r   )"
__future__r   r*   r\   	functoolsr   typingr   r   _hazmatr   rT   quic.configurationr	   quic.connectionr
   r   quic.packetr   r   r   r   
quic.retryr   tlsr   r   r<   r   r   __all__DatagramProtocolr   r   r   r6   r4   <module>r      s   " " " " " "  				       ! ! ! ! ! ! ! ! ? ? ? ? ? ? 2 2 2 2 2 2 < < < < < < < <            / . . . . . < < < < < < < < ? ? ? ? ? ? ? ?)`) `) `) `) `)) `) `) `)P !7:>:>(,1 1 1 1 1 1 1 1r6   