o
    .jW                  
   @  sb  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m	Z
 d dlmZmZ ejr6d dlmZ d dlmZmZmZmZmZmZmZmZmZ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-m.Z. ddl/m0Z0 ej+ej&ej1ej2hZ3dddddddddd	Z4e5edZ6de7d< G dd  d e0Z8dS )!    )annotationsN)deque)environ)time)AnyIterable)Literal)CipherSuiteH3ConnectionH3ErrorProtocolErrorQuicConfigurationQuicConnectionQuicConnectionErrorQuicFileLoggerSessionTicket	h3_eventsquic_events)QuicConnectionState   )QuicTLSConfig)StreamMatrix)AddressTypeHeadersType)ConnectionTerminatedDataReceivedEarlyHeadersReceivedEventGoawayReceived)HandshakeCompletedHeadersReceivedStreamResetReceived   )HTTP3Protocol
commonNamelocalityNamestateOrProvinceNameorganizationNameorganizationalUnitNamecountryNamestreetAddressdomainComponentemail)	CNLSTOOUCSTREETDCEr   bool_QH3_H3_HAVE_GA_EVc                   @  s  e Zd ZU dZded< dqddZdrddZedsddZdtddZ	dtddZ
dtddZeduddZdvddZdwdxd"d#Z	$dydzd)d*Z	$dyd{d-d.Zdwd|d/d0Zd}d~d4d5Zd1d1d6dd9d:Zdd;d<Zdd=d>Zdd?d@ZddAdBZddFdGZddJdKZ	1d}ddNdOZejddSdTZejdUdVddYdTZd$dVdd[dTZejdd\d]ZejdUdVdd_d]Zd$dVddad]ZddcddZ dtdedfZ!ddidjZ"ddkdlZ#dtdmdnZ$dvdodpZ%d1S )HTTP3ProtocolAioQuicImplqh3strimplementationremote_addressr   server_name
tls_configr   returnNonec          	      C  s  t dd }t dd }td&i ddd|jrtjntjd|jd|jd|j	d	d
gd|j
d|d|jd|jd|jd|rGt|dnd d|rPt|nd d|jdddd| _t| jdrj|j| j_|jrdd tD }g }|jD ]}d|v rt|d tr|||d dd  qy|std|j dd d! | D  d"|| j_|jr| j|j|j|j  t!| jd#| _"t# | _$|| _%t& | _'t( | _)d | _*d$| _+d%| _,d%| _-d$| _.d | _/d | _0d%| _1t( | _2d S )'NSSLKEYLOGFILE
QUICLOGDIR	is_clientTverify_modecafilecapathcadataalpn_protocolsh3session_ticketr=   hostname_checks_common_nameassert_fingerprintverify_hostnamesecrets_log_filewquic_loggeridle_timeoutmax_datai   max_stream_datai  ` ech_config_listc                 S  s   i | ]}|j |qS  )name).0crU   rU   _/home/thesage/.local/lib/python3.10/site-packages/urllib3/contrib/hface/protocols/http3/_qh3.py
<dictcomp>|       z5HTTP3ProtocolAioQuicImpl.__init__.<locals>.<dictcomp>rV   TLS_ z'Unable to find a compatible cipher in 'z7' to establish a QUIC connection. QUIC support one of 'c                 S  s   g | ]}d | qS )r\   rU   rW   erU   rU   rY   
<listcomp>   r[   z5HTTP3ProtocolAioQuicImpl.__init__.<locals>.<listcomp>z' only.)configurationFr   rU   )3r   getr   insecuressl	CERT_NONECERT_REQUIREDrE   rF   rG   rJ   cert_use_common_namecert_fingerprintrM   openr   rQ   _configurationhasattrrT   ciphersr	   
isinstancer:   appendreplace
ValueErrorkeyscipher_suitescertfileload_cert_chainkeyfilekeypasswordr   _quicset_connection_ids_remote_addressr   _eventsr   _packets_http_terminated_open_stream_count_total_stream_count_goaway_to_honor_max_frame_size_next_timer_last_ping_uid_pending_ping_ack)	selfr<   r=   r>   keylogfile_pathqlogdir_pathavailable_cipherschosen_cipherscipherrU   rU   rY   __init__Y   s   	



z!HTTP3ProtocolAioQuicImpl.__init__float | Nonec                 C  s   | j  | _| jS N)rw   	get_timerr   r   rU   rU   rY   
next_timer   s   z#HTTP3ProtocolAioQuicImpl.next_timertuple[type[BaseException], ...]c                   C  s   t tttfS r   )r   r   r   AssertionErrorrU   rU   rU   rY   
exceptions   s   z#HTTP3ProtocolAioQuicImpl.exceptionsr6   c                 C  s,   | j s| jrdS | jjd }| jj}||kS )NF   )r~   r   rw   _local_next_stream_id_bidimax_concurrent_bidi_streams)r   openedgrantedrU   rU   rY   is_available   s
   z%HTTP3ProtocolAioQuicImpl.is_availablec                 C  s   | j jrdS | j o| jdkS )NFr   )r{   stream_countr~   r   r   rU   rU   rY   is_idle   s   z HTTP3ProtocolAioQuicImpl.is_idlec                 C  s   | j sO| jsOt| jdkr| j  d| _ n9t }z| j| W n	 ty*   Y nw | jj	t
jt
jhv r8d| _ t| jdd d urO| j| | jj d| _ | j pT| joY| jj S )Nr"   T_close_event)r~   r   lenr   rw   close	monotonichandle_timer	TypeError_stater   CLOSING
TERMINATEDgetattrr{   extend_map_quic_eventr   r   )r   nowrU   rU   rY   has_expired   s,   

z$HTTP3ProtocolAioQuicImpl.has_expiredSessionTicket | Nonec                 C  s   | j r| j jr| j jjS d S r   )rw   tlsrJ   r   rU   rU   rY   rJ      s   z'HTTP3ProtocolAioQuicImpl.session_ticketintc                 C  s
   | j  S r   )rw   get_next_available_stream_idr   rU   rU   rY   get_available_stream_id      
z0HTTP3ProtocolAioQuicImpl.get_available_stream_idr   
error_codec                 C  s    |rdnd}| j j||d d S )N      )r   
frame_type)rw   r   )r   r   r   rU   rU   rY   submit_close   s   
z%HTTP3ProtocolAioQuicImpl.submit_closeF	stream_idheadersr   
end_streamc                 C  s>   | j d usJ |  jd7  _|  jd7  _| j ||| d S N   )r}   r   r   send_headers)r   r   r   r   rU   rU   rY   submit_headers   s   z'HTTP3ProtocolAioQuicImpl.submit_headersdatabytesc                 C  s"   | j d usJ | j ||| d S r   )r}   	send_data)r   r   r   r   rU   rU   rY   submit_data   s   z$HTTP3ProtocolAioQuicImpl.submit_datac                 C  s   | j || d S r   )rw   reset_stream)r   r   r   rU   rU   rY   submit_stream_reset   s   z,HTTP3ProtocolAioQuicImpl.submit_stream_resetN
int | NoneEvent | Nonec                 C  s   | j j|dS )N)r   )r{   popleft)r   r   rU   rU   rY   
next_event   s   z#HTTP3ProtocolAioQuicImpl.next_eventr   
excl_eventr   tuple[type[Event], ...] | Nonec                C  s   | j j||dS )Nr   )r{   has)r   r   r   rU   rU   rY   has_pending_event   s   z*HTTP3ProtocolAioQuicImpl.has_pending_eventc                 C  s   d| _ | jt  d S )NT)r~   r{   rn   r   r   rU   rU   rY   connection_lost   s   z(HTTP3ProtocolAioQuicImpl.connection_lostc                 C  sH   | j }|j|| jt d |   |j}|r || jkr"|| _d S d S d S )Nr   )rw   receive_datagramrz   r   _fetch_events#_remote_max_stream_data_bidi_remoter   )r   r   quic
remote_maxrU   rU   rY   bytes_received  s   
z'HTTP3ProtocolAioQuicImpl.bytes_receivedc                 C  s   t  }| jd u r| jj| j|d t| j| _| jd uo || jk}| jr&|rP|r:z| j| W n	 t	y9   Y nw | j
dd | jj|dD  | j | _| jsUdS | j S )Nr   c                 s  s    | ]}|d  V  qdS )r   NrU   r^   rU   rU   rY   	<genexpr>  s    z9HTTP3ProtocolAioQuicImpl.bytes_to_send.<locals>.<genexpr>    )r   r}   rw   connectrz   r
   r   r|   r   r   r   datagrams_to_sendr   r   )r   r   timer_expiredrU   rU   rY   bytes_to_send  s"   

 
z&HTTP3ProtocolAioQuicImpl.bytes_to_sendc                 C  s   | j d usJ t| jjd D ]}| j| | | j |D ]}| j| | qqt	| jdd d urB| j| | jj
 d S d S )Nr   )r}   iterrw   r   r{   r   r   handle_event_map_h3_eventr   r   )r   
quic_eventh3_eventrU   rU   rY   r   *  s   z&HTTP3ProtocolAioQuicImpl._fetch_eventsr   quic_events.QuicEventIterable[Event]c                 c  s    |j }|tvr
d S |tju rt|jV  d S |tju r+d| _t|j|j	p&dV  d S |tj
u rA|  jd8  _t|j|jV  d S |tju rU|j| jv rW| j|j d S d S d S )NTz-Remote end closed connection (not gracefully)r   )	__class__QUIC_RELEVANT_EVENT_TYPESr   r   _HandshakeCompletedalpn_protocolr   r~   r   reason_phraseStreamResetr   r!   r   PingAcknowledgeduidr   remove)r   r   ev_typerU   rU   rY   r   5  s*   




z(HTTP3ProtocolAioQuicImpl._map_quic_eventr   h3_events.H3Eventc                 c  s    |j }|tju r|jr|  jd8  _t|j|j|jV  d S |tju r:|jr.|  jd8  _t|j|j|jV  d S |tj	u rIt
|j|jV  d S tr[|tju r]d| _t|jV  d S d S d S )Nr   T)r   r   r    stream_endedr   r   r   r   r   InformationalHeadersReceivedr   r7   r   r   )r   r   r   rU   rU   rY   r   M  s2   





z&HTTP3ProtocolAioQuicImpl._map_h3_eventamtbool | Nonec                 C  sR   | j j}tdd |jD }|dkrdS t }|dkr'|j|j }||kr'dS dS )Nc                 s  s    | ]}|j V  qd S r   )ack_eliciting_in_flight)rW   srU   rU   rY   r   s  s    zKHTTP3ProtocolAioQuicImpl.should_wait_remote_flow_control.<locals>.<genexpr>r"   Tr   F)rw   _losssumspacesr   '_time_of_last_sent_ack_eliciting_packetmax_ack_delay)r   r   r   lossn_outstandingr   deadlinerU   rU   rY   should_wait_remote_flow_controlg  s   z8HTTP3ProtocolAioQuicImpl.should_wait_remote_flow_controlbinary_formLiteral[True]bytes | Nonec                C     d S r   rU   r   r   rU   rU   rY   getissuercert     z&HTTP3ProtocolAioQuicImpl.getissuercert.)r   Literal[False]dict[str, Any] | Nonec                C  r   r   rU   r   rU   rU   rY   r     s   $bytes | dict[str, typing.Any] | Nonec             	   C  s  | j  }|d u rtd| j  sd S | j  d }|r!| S |jd |j g g tjj	|j
tjjddd tjj	|jtjjddd d}|jD ]\}}}|tvrYqO|d t| | ff qO|jD ]\}}}|tvruqk|d	 t| | ff qk|S )
N#TLS handshake has not been done yetr   r   tz%b %d %H:%M:%S %Y UTC)versionserialNumbersubjectissuer	notBeforenotAfterr  r  )rw   get_peercertrp   get_issuercertspublic_bytesr
  serial_numberupperdatetimefromtimestampnot_valid_beforetimezoneutcstrftimenot_valid_afterr  _SHORT_NAME_ASSOCrn   decoder  )r   r   x509_certificateissuer_inforaw_oidrfc4514_attribute_namevaluerU   rU   rY   r     sX   



		c                C  r   r   rU   r   rU   rU   rY   getpeercert  r  z$HTTP3ProtocolAioQuicImpl.getpeercertdict[str, Any]c                C  r   r   rU   r   rU   rU   rY   r#    r  bytes | dict[str, typing.Any]c                C  s  | j  }|d u rtd|r| S |jd |j g g tjj|j	tj
jddd tjj|jtj
jddd g g g g d
}|jD ]\}}}|tvrOqE|d t| | ff qE|jD ]\}}}|tvrkqa|d t| | ff qa| D ]Q}| }||d	d |d
 }	|dr|d d|	f q~ddlm}
m} t|	dkr|
t|	dd}n|t|	dd}|d d|f q~| D ]}| }|d ||d	d d  q| D ]}| }|d ||d	d d  q| D ]}| }|d ||d	d d  q
g }|D ]}t || t!rBt"|| ||< || sB|| q&|D ]}|#| qF|S )Nr  r   r  r  r	  )
r
  r  r  r  r  r  subjectAltNameOCSP	caIssuerscrlDistributionPointsr  r  ()DNSr&  r   )
inet4_ntoa
inet6_ntoa   :r]   z
IP Addressr'  r(  r)  )$rw   r  rp   r  r
  r  r  r  r  r  r  r  r  r  r  r  rn   r  r  get_subject_alt_namesindex
startswithresolver.utilsr-  r.  r   r   fromhexro   get_ocsp_endpointsget_issuer_endpointsget_crl_endpointsrm   listtuplepop)r   r   r  	peer_infor   r!  r"  alt_namedecoded_alt_namein_parenthesisr-  r.  ip_address_decodedendpointdecoded_endpointpop_keyskrU   rU   rY   r#    s   


		



str | Nonec                 C  s&   | j  }|d u rtdd|j S )Nr  r\   )rw   
get_cipherrp   rV   )r   cipher_suiterU   rU   rY   r   4  s   
zHTTP3ProtocolAioQuicImpl.cipherc                 C  s   t | jdsdS | jjS )Nech_acceptedF)rk   rw   rI  r   rU   rU   rY   rI  <  s   z%HTTP3ProtocolAioQuicImpl.ech_acceptedeventsr   c                 G  s   t |D ]}| j| qd S r   )reversedr{   
appendleft)r   rJ  evrU   rU   rY   reshelveA  s   z!HTTP3ProtocolAioQuicImpl.reshelvec                 C  s.   | j | j | j| j |  jd7  _d S r   )r   rn   r   rw   	send_pingr   rU   rU   rY   pingE  s   zHTTP3ProtocolAioQuicImpl.pingc                 C  s
   t | jS r   )r6   r   r   rU   rU   rY   expect_pongJ  r   z$HTTP3ProtocolAioQuicImpl.expect_pongc                 C  s   | j d ur| j S tr   )r   NotImplementedErrorr   rU   rU   rY   max_frame_sizeM  s   
z'HTTP3ProtocolAioQuicImpl.max_frame_size)r<   r   r=   r:   r>   r   r?   r@   )r?   r   )r?   r   )r?   r6   )r?   r   )r?   r   )r   )r   r   r?   r@   )F)r   r   r   r   r   r6   r?   r@   )r   r   r   r   r   r6   r?   r@   )r   r   r   r   r?   r@   r   )r   r   r?   r   )r   r   r   r   r?   r6   )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   r6   r?   r  )r   r   r?   r   )r   r  r?   r$  )r   r6   r?   r%  )r?   rF  )rJ  r   r?   r@   )&__name__
__module____qualname__r;   __annotations__r   r   staticmethodr   r   r   r   propertyrJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typingoverloadr   r#  r   rI  rN  rP  rQ  rS  rU   rU   rU   rY   r8   V   sb   
 

K



	






9
j



r8   )9
__future__r   r  rd   rZ  collectionsr   osr   r   r   r   r   TYPE_CHECKINGtyping_extensionsr   r9   r	   r
   r   r   r   r   r   r   r   r   r   qh3.quic.connectionr   rj   r   _stream_matrixr   _typingr   r   rJ  r   r   r   r   r   r   r   r    r!   
_protocolsr#   r   r   r   r  rk   r7   rW  r8   rU   rU   rU   rY   <module>   sF   4