
    @OOf3                     6   d dl Z d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl!m#Z# ddl!m$Z$ d dl%m&Z& d dl%m'Z' d d l%m(Z( d d!l)m*Z* d d"l+m,Z, d d#l+m-Z- d d$l+m.Z. d d%l+m/Z/ d d&l0m1Z1 d d'l0m2Z2 d d(l0m3Z3 d d)l0m4Z4 d d*l5m6Z6  G d+ d,e      Z7 G d- d.e7      Z8 G d/ d0e7      Z9d0d.gZ:y)1    N)abstractmethod)	ErrorCode)FrameUnexpected)DataReceived)HeadersReceived)PushPromiseReceived   )RequestData)RequestEndOfMessage)RequestHeaders)RequestProtocolError)RequestTrailers)ResponseData)ResponseEndOfMessage)ResponseHeaders)ResponseProtocolError)ResponseTrailers)format_error)HttpConnection)	HttpEvent)ReceiveHttp)format_h2_request_headers)format_h2_response_headers)parse_h2_request_headers)parse_h2_response_headers)LayeredH3Connection)StreamReset)TrailersReceived)
connection)http)version)status_codes)commands)context)events)layer)error_code_to_str)QuicConnectionClosed)QuicStreamEvent)StopQuicStream)expectc                   R    e Zd ZU eed<   eeez     ed<   eee	z     ed<   ee
ez     ed<   eeez     ed<   dej                  dej"                  f fdZd	ej(                  d
ej,                  d   fdZ eeee      d
ej,                  d   fd       Zed	ed
ee z  fd       Z! xZ"S )Http3Connectionh3_connReceiveDataReceiveEndOfMessageReceiveProtocolErrorReceiveTrailersr$   connc                     t         |   ||       t        | j                  | j                  | j                  j
                  u       | _        i | _        y )N)	is_client)super__init__r   r3   r$   serverr.   _stream_protocol_errors)selfr$   r3   	__class__s      c/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/proxy/layers/http/_http3.pyr7   zHttp3Connection.__init__6   sD    $'*IIdll.A.A!A
 8:$    eventreturnNc           	   #     K   t        |t        j                        r#| j                  j	                         E d {    y t        |t
              rB	 t        |t        t        f      r2| j                  j                  |j                  |j                         nt        |t        t        f      rxt        |t              rt        | j                  |      nt        | j                  |      E d {   }| j                  j!                  |j                  ||j"                         nHt        |t$        t&        f      r>| j                  j)                  |j                  g |j*                  j,                         nt        |t.        t0        f      r'| j                  j#                  |j                         nt        |t2        t4        f      rt6        j8                  t:        j<                  j>                  ijA                  |jB                  t:        jD                  j>                        }|| jF                  |j                  <   t        |t4              xrE | j                  jI                  |j                         xr |jB                  t6        jJ                  k7  }|r| j                  j!                  |j                  dd|jB                  z  fdtL        jN                  jQ                         fdg       | j                  j                  |j                  tS        |jB                  |jT                        d       n5| j                  jW                  |j                  |       ntY        d|      | j                  j	                         E d {    y t        |t`              r| j                  jc                  |      }|j                  | jF                  v r<te        | jf                  |j                  | jF                  |j                            nz|D ]t  }t        |th              r|jj                  !tm        |jn                        }t:        j<                  j>                  t6        j8                  ijA                  |jn                  | jp                  jB                        }	ts        | jq                  |j                  d
| d|	             t        |tt              r|jj                  |j                  r1ts        | jw                  |j                  |j                               |jx                  s$ts        | j{                  |j                               Lt        |t|              rc|jj                  j	 | j                  |      }
ts        |
       |jx                  sts        | j{                  |j                               t        |t              r|jj                  ts        | j                  |j                  t        j                  |j*                                     |jx                  s/ts        | j{                  |j                               Wt        |t              ritY        d|       | j                  j	                         E d {    y t        |t              r| j                  | _J        | j                  j                  |       |j                  xs tm        |jn                        }| j                  j                  d       D ]  }ts        | jq                  ||             ! y tY        d|      7 "7 p7 # tZ        $ r'}t]        j^                  d|d	|        Y d }~y d }~ww xY w# t        $ r9}| j                  j                  t:        j                  d|        Y d }~d }~ww xY w7 3w)N
end_streams   :statuss   %ds   server)s   content-types	   text/htmlTzUnexpected event: z	Received z unexpectedly: zstream reset by client ())codez Invalid HTTP/3 request headers: )
error_codereason_phrase)push_id)N
isinstancer%   Startr.   transmitr   r
   r   	send_data	stream_iddatar   r   r   r$   r   send_headersrB   r   r   send_trailerstrailersfieldsr   r   r   r   r"   CLIENT_CLOSED_REQUESTH3ErrorCodeH3_REQUEST_CANCELLEDvaluegetrD   H3_INTERNAL_ERRORr9   has_sent_headersNO_RESPONSEr!   	MITMPROXYencoder   messagereset_streamAssertionErrorH3FrameUnexpectedr#   Logr)   handle_stream_eventr*   r3   r   rG   r'   rE   r1   r   r   r/   stream_endedr0   r   parse_headers
ValueErrorclose_connectionH3_GENERAL_PROTOCOL_ERRORr   r2   r    Headersr   r(   done_handle_eventhandle_connection_closedrF   get_open_stream_ids)r:   r>   headersrD   send_error_messagee	h3_eventsh3_eventerr_strerr_codereceive_eventmsgrL   s                r<   ri   zHttp3Connection._handle_event=   sm    eV\\*||,,... y)53ek<%@ALL**5??EJJG'HI &e^< 2$,,F7eLG
 LL--U=M=M .   9I'JKLL..)A5>>+@+@)A  (;=Q'RSLL++EOO<(<>S'TU$::K<\<\<b<bc%**k&C&C&I&IJ  EID00A"5*?@ C $ = =eoo NNC!JJ,*B*BB '
 *11!OO!+UUZZ-? @!*G,=,=,D,D,F G ? ..!OO(U]]C'+ /  11%//4H(+=eY)GHH  <<00222 /88?I$">">> %IIOO00A  !* 6MH!(K8#++3&78K8K&LG + @ @ F FHjHj(!c("5"5t7P7P7U7UV % #. $ 9 9$,$6$6&>wiq$I)1 !: !"#  $Hl;#++3'}}&1$($4$4X5G5G$W'" !"  (44&1$($<$<X=O=O$P'" !" $Ho>#++3&040B0B80L '2-&@ @#+#8#8*5(,(@(@ASAS(T+& %& $H.>?#++3"- $ 4 4$,$6$6XEVEV8W!"# 
  (44&1$($<$<X=O=O$P'" !" $H.AB,/A%-KLLm6Mn ||,,... 34!%DLL11%8%%L):5;K;K)LC!\\==d=K M	!$";";Is"KLLM !#5eY!?@@S /b 3 % LllYuiqc#JKKL` $. " $ = =/:/T/T4TUVTW2X !> !" !""6 /s   8_ ]_ B]% /]0J ]% 0_ ]"B'_ 6B7_ .A	_ 9A_  ^_ ,A_ 2A_ A&_ *_+B2_ ]% "_ %	^.^_ ^_ 	_!.__ __ c              #   $   K   dE d {    y 7 w)N rv   )r:   _s     r<   rh   zHttp3Connection.done   s     s   c                      y Nrv   )r:   r>   s     r<   rc   zHttp3Connection.parse_headers   s    r=   )#__name__
__module____qualname__r   __annotations__typer
   r   r   r   r   r   r   r   r$   Contextr   
Connectionr7   r%   Eventr&   CommandGeneratorri   r+   r   r)   r(   rh   r   r   r   r   rc   __classcell__r;   s   @r<   r-   r-   .   s      kL01114HHII36KKLL/,<<==: :z7L7L :KA6<< KAE4J4J44P KAZ I(<=//5  > ? ~7W  r=   r-   c                   ^     e Zd ZeZeZeZe	Z
dej                  f fdZdedeez  fdZ xZS )Http3Serverr$   c                 :    t         |   ||j                         y ry   )r6   r7   clientr:   r$   r;   s     r<   r7   zHttp3Server.__init__   s    '..1r=   r>   r?   c                     t        |j                        \  }}}}}}}t        j                  ||||||d|d d t	        j                         d       }	t        |j                  |	|j                        S )N   HTTP/3)hostportmethodscheme	authoritypathhttp_versionrl   contentrP   timestamp_starttimestamp_endrA   )r   rl   r    Requesttimer   rL   rb   )
r:   r>   r   r   r   r   r   r   rl   requests
             r<   rc   zHttp3Server.parse_headers   s}     %U]]3	
,," IIK
 eoow5CUCUVVr=   )rz   r{   r|   r
   r/   r   r0   r   r1   r   r2   r$   r   r7   r   r   r   rc   r   r   s   @r<   r   r      sD    K-/%O2 2W? W~7W Wr=   r   c                        e Zd ZU eZeZeZe	Z
eeef   ed<   eeef   ed<   dej                  f fdZdej$                  dej(                  d   f fdZdedeez  fd	Z xZS )
Http3Clientour_stream_idtheir_stream_idr$   c                 V    t         |   ||j                         i | _        i | _        y ry   )r6   r7   r8   r   r   r   s     r<   r7   zHttp3Client.__init__  s&    '..1!r=   r>   r?   Nc              #     K   t        |t              r{| j                  j                  |j                  d       }|L| j
                  j                         }|| j                  |j                  <   |j                  | j                  |<   ||_        t        | %  |      D ]H  }t        |t              r2| j                  |j                  j                     |j                  _        | J y wry   )rH   r   r   rV   rL   r.   get_next_available_stream_idr   r6   ri   r   r>   )r:   r>   ourscmdr;   s       r<   ri   zHttp3Client._handle_event  s      eY'%%))%//4@D|||@@B6:""5??3-2__$$T*"EO7(/ 	C#{+&*&:&:399;N;N&O		#I	s   C'C*c           
          t        |j                        \  }}t        j                  d|d|d d t	        j                         d       }t        |j                  ||j                        S )Nr   r=   )r   status_codereasonrl   r   rP   r   r   )r   rl   r    Responser   r   rL   rb   )r:   r>   r   rl   responses        r<   rc   zHttp3Client.parse_headers  s\    8GW=="# IIK	
 u%:L:LMMr=   )rz   r{   r|   r   r/   r   r0   r   r1   r   r2   dictintr}   r$   r   r7   r%   r   r&   r   ri   r   r   r   rc   r   r   s   @r<   r   r      s    K.0&OS>!#s(^#" "
6<< E4J4J44P "N? N~7W Nr=   r   );r   abcr   aioquic.h3.connectionr   rS   r   r_   aioquic.h3.eventsr   r   r    r
   r   r   r   r   r   r   r   r   r   _baser   r   r   r   _http2r   r   r   r   _http_h3r   r   r   	mitmproxyr   r    r!   mitmproxy.net.httpr"   mitmproxy.proxyr#   r$   r%   r&   mitmproxy.proxy.layers.quicr'   r(   r)   r*   mitmproxy.proxy.utilsr+   r-   r   r   __all__rv   r=   r<   <module>r      s      : F * - 1  !  "   "  #   !   - . , - ) ! &     + $ # " ! 9 < 7 6 (bn bJ"W/ "WJ,N/ ,N` r=   