
    <gCY                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl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  ej(                  e      Z G d d	ej.                        Z G d
 d      Z G d dej.                        Zd Z G d dej8                        Zej<                  dej>                  dej@                  diZ!d Z" G d dej.                        Z#y)    N)connections)
exceptionsflowhttp)
websockets)base)WebSocketLayer)strutilsc                   Z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)_HttpTransmissionLayerc                     t               NNotImplementedErrorselfr   s     j/var/www/html/myenv/lib/python3.12/site-packages/seleniumwire/thirdparty/mitmproxy/server/protocol/http.pyread_request_headersz+_HttpTransmissionLayer.read_request_headers       !##    c                     t               r   r   r   requests     r   read_request_bodyz(_HttpTransmissionLayer.read_request_body   r   r   c                     t               r   r   r   s     r   read_request_trailersz,_HttpTransmissionLayer.read_request_trailers   r   r   c                     t               r   r   r   s     r   send_requestz#_HttpTransmissionLayer.send_request   r   r   c                     t               r   r   r   s    r   read_response_headersz,_HttpTransmissionLayer.read_response_headers    r   r   c              #      K   t               wr   r   r   r   responses      r   read_response_bodyz)_HttpTransmissionLayer.read_response_body#   s     !##s   c                     t               r   r   r#   s      r   read_response_trailersz-_HttpTransmissionLayer.read_response_trailers'   r   r   c                     | j                         }dj                  | j                  ||            |j                  _        | j                  ||      |j                  _        |S )Nr   )r!   joinr%   datacontentr'   trailersr#   s      r   read_responsez$_HttpTransmissionLayer.read_response*   sU    --/ ###GX6!
 "&!<!<Wh!Or   c                     |j                   j                  t        j                  d      | j	                  |       | j                  ||j                   j                  g       | j                  |       y )Nz)Cannot assemble flow with missing content)r*   r+   r   HttpExceptionsend_response_headerssend_response_bodysend_response_trailersr   r$   s     r   send_responsez$_HttpTransmissionLayer.send_response2   s]    ==  (**+VWW""8,8==+@+@*AB##H-r   c                     t               r   r   r3   s     r   r0   z,_HttpTransmissionLayer.send_response_headers9   r   r   c                     t               r   r   r   r$   chunkss      r   r1   z)_HttpTransmissionLayer.send_response_body<   r   r   c                     t               r   r   r7   s      r   r2   z-_HttpTransmissionLayer.send_response_trailers?   r   r   c                     t               r   r   )r   fs     r   check_close_connectionz-_HttpTransmissionLayer.check_close_connectionB   r   r   N)__name__
__module____qualname__r   r   r   r   r!   r%   r'   r-   r4   r0   r1   r2   r<    r   r   r   r      sC    $$$$$$$.$$$$r   r   c                   2    e Zd ZdZd Zed        Zd Zd Zy)ConnectServerConnectionzf
    "Fake" ServerConnection to represent state after a CONNECT request to an upstream mitmproxy.
    c                      || _         || _        y r   )address_ctx)r   rD   ctxs      r   __init__z ConnectServerConnection.__init__L   s    	r   c                 .    | j                   j                  S r   )rE   server_connr    s    r   viazConnectServerConnection.viaP   s    yy$$$r   c                 .    t        | j                  |      S r   )getattrrJ   )r   items     r   __getattr__z#ConnectServerConnection.__getattr__T   s    txx&&r   c                 6    | j                   j                         S r   )rJ   	connectedr    s    r   rP   z!ConnectServerConnection.connectedW   s    xx!!##r   N)	r=   r>   r?   __doc__rG   propertyrJ   rN   rP   r@   r   r   rB   rB   F   s*     % %'$r   rB   c                   <     e Zd Z fdZd Zd Zd Zd Zd Z xZ	S )UpstreamConnectLayerc                     t         |   |       || _        t        |j                  |j
                  f| j                        | _        y r   )superrG   connect_requestrB   hostportrF   rI   )r   rF   rW   	__class__s      r   rG   zUpstreamConnectLayer.__init__]   s@    .2!!?#7#78HH
r   c                 H    | j                   j                  |       } |        y r   )rF   
next_layer)r   layers     r   __call__zUpstreamConnectLayer.__call__e   s    ##D)r   c           	         | j                  dddj                  | j                  j                  j                        dj                  | j
                  j                  | j
                  j                        g       | j                  | j
                         | j                  | j
                        }|j                  dk7  rt        j                  d      y )NzSending CONNECT requestdebugzProxy Server: {}zConnect to: {}:{}   z2Reconnect: Upstream server refuses CONNECT request)logformatrF   rI   rD   rW   rX   rY   r   r-   status_coder   ProtocolException)r   resps     r   _send_connect_requestz*UpstreamConnectLayer._send_connect_requesti   s    *G%%dhh&:&:&B&BC&&t';';'@'@$BVBVB[B[\6
 	 	$../!!$"6"67s"../cdd #r   c                     | j                   j                         s+| j                  j                          | j	                          y y r   )rI   rP   rF   connectrg   r    s    r   ri   zUpstreamConnectLayer.connects   s3    ))+HH&&(r   c                     | j                  dj                  t        |            d       || j                  j                  j
                  k7  r| j                  j                  |       y y )Nz-Changing upstream mitmproxy to {} (CONNECTed)r`   )rb   rc   reprrI   rJ   rD   rF   
set_serverr   rD   s     r   change_upstream_proxy_serverz1UpstreamConnectLayer.change_upstream_proxy_serverz   sR    @GGWVX_`d&&**222HH( 3r   c                     | j                   j                  j                         r| j                   j                          |d   | j                  _        |d   | j                  _        || j                  _        y )Nr      )rF   rI   rP   
disconnectrW   rX   rY   rD   rm   s     r   rl   zUpstreamConnectLayer.set_server   sZ    88))+HH!$+AJ!$+AJ!#* r   )
r=   r>   r?   rG   r^   rg   ri   rn   rl   __classcell__rZ   s   @r   rT   rT   [   s"    
e)
+r   rT   c                 "    d| cxk  xr dk  S c S )Nra   i,  r@   )statuss    r   is_okrv      s    &3r   c                       e Zd ZdZdZdZy)HTTPModerp         N)r=   r>   r?   regulartransparentupstreamr@   r   r   rx   rx      s    GKHr   rx   )	authorityabsolute)relativec                 (   |j                   dk(  r0|j                  dvr"t        j                  d|j                  z        t        |    }|j                   |vr|j
                  r"| t        j                  u r|j                   dk(  ry | t        j                  k(  rDt        j                  d      j                         j                  |j                   dk(  rdnd      }n"dd	j                  |      d
|j                   d}t        j                  |      y )Nr   )r   httpszInvalid request scheme: %sz
                Mitmproxy received an {} request even though it is not running
                in regular mode. This usually indicates a misconfiguration,
                please see the mitmproxy mode documentation for details.
                r~   zHTTP CONNECTzabsolute-formz%Invalid HTTP request form (expected: z or z, got: ))first_line_formatschemer   r/   MODE_REQUEST_FORMSis_http2rx   r|   textwrapdedentstriprc   r)   )moder   allowed_request_formserr_messages       r   validate_request_formr      s      J.7>>IZ3Z&&(7>>9
 	
 /t4  (==(<(< <AZAZ^hAh8'''"// uwvv0I0I[0Xn^mn  ( 12G4M4MK &&{33 >r   c                   j     e Zd ZU 	  fdZd Zd Z	d Z
d ZdddZd Zded	ed
efdZd Zd Z xZS )	HttpLayerc                 `    t         |   |       || _        d | _        	 	 d | _        d| _        y )NF)rV   rG   r   "_HttpLayer__initial_server_address_HttpLayer__initial_server_tlsrW   )r   rF   r   rZ   s      r   rG   zHttpLayer.__init__   s7    	/3%[R %)!$r   c                 >   | j                   t        j                  k(  r,| j                  | _        | j
                  j                  | _        	 t        j                  | j                  | j
                  | | j                   j                        }| j                  |      sy T)N)liver   )r   rx   r|   
server_tlsr   rI   rD   r   r   HTTPFlowclient_connname_process_flowr   s     r   r^   zHttpLayer.__call__   s}    99,,,(,D%,0,<,<,D,DD)==    YY^^	D %%d+ r   c                    d| _         	 | j                  |j                  j                  |j                  j                  f       |j
                  r|j
                  }n3t        j                  |j                  j                  j                        }| j                  |       t        |j                        r"| j                  j                  |       } |        y# t        j                   t        j"                  f$ rd}| j%                  dt'        |             t)        j*                  t-        |            |_        | j0                  j3                  d|       Y d }~yd }~ww xY w)NT  errorF)rW   rl   r   rX   rY   r$   r   make_connect_responser*   http_versionr4   rv   rd   rF   r\   r   re   NetlibExceptionsend_error_responserk   r   Errorstrr   channelask)r   r;   rf   r]   es        r   handle_regular_connectz HttpLayer.handle_regular_connect   s    #	OOQYY^^QYY^^<=zzzz11!))..2M2MNt$T%%&++D1  ((**D*D
 	
 $$S$q'2jjQ(AGLLWa(	s   CC #E <AEE c                    |j                   s| j                  |j                  j                  |j                  j                  |j                  j
                         | j                  |j                         | j                         |_         dj                  | j                  |j                  |j                               |j                   j                  _        | j                  |j                          t        |j                   j                        rt        | |j                        } |       S |j                   j                  dk(  rt         j#                  d       y)Nr   i  z)Invalid proxy server credentials suppliedF)r$   establish_server_connectionr   rX   rY   r   r   r!   r)   r%   r*   r+   r4   rv   rd   rT   rb   r   )r   r;   r]   s      r   handle_upstream_connectz!HttpLayer.handle_upstream_connect   s     zz,,						  
 aii(335AJ&)hh''		1::>'AJJOO# 	1::&''((qyy9E7NZZ##s*IIABr   c                     	 	  j                        }|_         j                  t
        j                  u rk j                  j                        rP j                  j                  j                  j                  j                  f       t
        j                   _        |j                  dk(  r6dj                   j                  j                              j                  j                  _         j#                  j                        j                  j                  _        t'        j&                         j                  _         j*                  j-                  d        j                  t
        j                  u r j/                        S  j                  t
        j                  u r" j1                          j3                        S d} j5                  d|       y j6                  j8                  j:                  st=         j                  |        j*                  j-                  d        j                  t
        j                  u r'j>                  j@                  s j1                          j6                  j8                  j:                  st=         j                  |       |jB                  jE                  dd	      jG                         d
k(  r> jI                  tK        jL                                |jB                  jO                  d       j                  jP                  rd j                  j                  _        n9dj                   j                  |            j                  j                  _         j#                  j                        j                  j                  _        t'        j&                         |_         jc                  ddtU        |      g        j                  t
        j                  u r|j                  dk(  rd	|_2         j6                  j8                  j                  jg                  d      rR j6                  j8                  jh                  s2 j6                  jj                  jl                  d   j                  _7         j                  t
        jp                  u ry jr                  d   j                  j                  _	         jr                  d   j                  j                  _
         jt                  rdndj                  j                  _;         j*                  j-                  d       	 ty        jz                  |jB                        rJty        j|                  |jB                        r+dj~                  d<    j*                  j-                  d       j                  s. j                  j                  j                  j                  j                  j                  jv                          fd}	  |         j*                  j-                  d       j                  jP                  rd j                  j                  _        nNdj                   j                  j                  j                              j                  j                  _        t'        j&                         j                  _         j>                  _        n j*                  j-                  d        j                  j                  j                        j                  j                  _         jc                  d dtU        j                        g        j*                  j-                  d        j                  jP                  s jI                  j                         n j                  j                          j                  j                  j                        }t        j                  jP                        rj                  jQ                  |      } j                  j                  |       t'        j&                         j                  _         j                        r	 rd_F        yyj                  j                  d!k(  rty        jz                  j                  jB                        xr) ty        jz                  j                  jB                        }|r2 j6                  j8                  j                  s jc                  d"d#       |r- j6                  j8                  j                  rt               }n j                  j                         } |        	 rd_F        yy	 rd_F        y# t        j                  $ r Y yw xY w# t        jR                  $ r} j5                  dtU        |             d _        tW        jX                  t[        |            _.         j*                  j-                  d       dj_                  |      } j6                  j8                  j`                  r jc                  dd|g       n jc                  dd|g       Y d }~yd }~ww xY w# t        j                  $ r} jc                  dtU        |      z  d       t        |t        j                        rt        j                  d      j                  jP                  r` j5                  dd       tW        jX                  tU        |            _.         j*                  j-                  d       Y d }~rd_F        yy j                           j                           |        Y d }~yd }~ww xY w# t        j                  t        j                  f$ r}j                  si j5                  d$tU        |             tW        jX                  t[        |            _.         j*                  j-                  d       Y d }~rd_F        yyt        j                  d%tU        |      z        d }~ww xY w# rd_F        w w xY w)&NFr~   r   http_connectzUnexpected CONNECT request.i  requestheadersexpect z100-continuer   z)HTTP protocol error in client request: {}r   r`   warnr   zreverse:r   rp   s   httpss   httpT	websocketwebsocket_handshakec                  "   j                  j                         j                  j                  rrj                  j                        } t	        j                  j                        rj                  j                  |       } j                  j                  |        n;j                  j                  j                  j                  j                  g       j                  j                         j                         _
        y r   )send_request_headersr   streamr   callablesend_request_bodyr*   r+   send_request_trailersr!   r$   )r8   r;   r   s    r   get_responsez-HttpLayer._process_flow.<locals>.get_responset  s    --aii8yy''!%!7!7		!B#AII$4$45%&YY%5%5f%=F..qyy&A..qyy199>>;Q;Q:RS..qyy9!%!;!;!=AJr   zserver communication error: %s)levelz8First and only attempt to get response via HTTP2 failed.i  zRequest Timeoutresponseheadersr$   e   zDClient requested WebSocket connection, but the protocol is disabled.infor   zError in HTTP connection: %s)Tr   r   HttpReadDisconnectr   r   rx   r}   should_bypass_upstream_proxyrl   rX   rY   r{   r   r)   r   r*   r+   r   r,   timetimestamp_endr   r   r   apply_proxy_authr   r   configoptionsrelax_http_form_validationr   rI   rJ   headersgetlowerr4   r   make_expect_continue_responsepopr   r/   rk   r   r   r   r   rc   suppress_connection_errorsrb   r~   
startswithkeep_host_headerupstream_serverrD   host_headerr|   r   r   r   r   check_handshakecheck_client_versionmetadatar$   r   r   
isinstanceHttp2ProtocolExceptionre   r   rq   ri   r%   r'   r0   r   r1   r<   rd   r   r	   rF   r\   )	r   r;   r   msgr   r   r8   is_websocketr]   s	   ``       r   r   zHttpLayer._process_flow  s7	   H	,0,E,Ea,H  AIyyH---$2S2STUT]T]2^ @A$,,	((K7 *-**1995*		& +/*D*DQYY*O		'*.))+		'  399 0 0066q99YY("3"33))!,77::7C,,S#6 ;;&&AA%dii9LL-q1yyH---amm6G6G%%a( ;;&&AA%dii9""8R0668NJ ""4#E#E#GH##H-yy)-		&),$2H2H2Q)R		&&*&@&@&KAIINN#$(IIKG!  	Gd7m_5 99(((W-F-F*-T "G ;;##..z:4;;CVCVCgCg$(KK$?$?$G$G$JAII! 99,,, #'"?"?"BAIINN"&"?"?"BAIINN040I0IHwAIINN!A&Q	))'//:z?^?^_f_n_n?o*.

;'   !6:::00IINNIINNII$$>"# NJ   !2A6::$$.2AJJOO+.1hh//		1::F/AJJOO+ ,099;

( !% 0 0   !2A6'+'B'B199ajj'YAJJOO$HHZ4

+;*<=LLZ+::$$ ""1::. **1::600IIJJ AJJ--.ZZ..v6F''

F;+/99;

(**1-F  A zz%%,
 ..qyy/@/@A C..qzz/A/AB   (;(;(E(EHH^
  DKK$7$7$A$A*43E HH//5E  A -@ e 00  p '' 	 $$S$q'2AIjjQ(AGLLWa(=DDQGC{{""==GcU3FSE2	L "11  #HH847B%    "!Z%F%FG(::V  )) 006GH"&**T!W"5((!4$j  g OO%LLN NNA #L ,,j.H.HI 		::((d1g6**SV,  !,  	 !222T!W< 		  s   f. F g =g g *G.g Cn. &j" -I,n. %C2n. .gg gg jB;jj"n+5B5n&*n. 9'n& n. &n++n. .#q,A&q'7q/ !q''q,,q/ /q;c                    	 t        j                  |||      }| j                  |       y # t        j                  t
        j                  j                  t        j                  f$ r$ | j                  dj                  |      d       Y y w xY w)Nz+Failed to send error response to client: {}r`   )
r   make_error_responser4   r   r   h2H2Errorr   rb   rc   )r   codemessager   r$   s        r   r   zHttpLayer.send_error_response  ss    	]//gwGHx(**BMM,A,A:CdCde 	]HHBII'RT[\	]s   (+ AB
Bc                    t        | j                  d      r| j                  j                  |       y || j                  j                  k7  r<| j                  dj                  t        |            d       | j                  |       y y )Nrn   z1Changing upstream mitmproxy to {} (not CONNECTed)r`   )	hasattrrF   rn   rI   rD   rb   rc   rk   rl   rm   s     r   rn   z&HttpLayer.change_upstream_proxy_server  sl     488;<HH11':((000HHHOOPTU\P]^`ghOOG$ 1r   rX   rY   r   c                    |dk(  }| j                   t        j                  u s| j                   t        j                  u r~||f}|| j                  j
                  k7  s|| j                  k7  r&| j                  |       | j                  ||d          | j                  j                         s| j                          y y | j                  j                         s| j                          |rt        j                  d      y )Nr   r   z0Cannot change scheme in upstream mitmproxy mode.)r   rx   r{   r|   rI   rD   r   rl   set_server_tlsrP   ri   r   HttpProtocolException)r   rX   rY   r   tlsrD   s         r   r   z%HttpLayer.establish_server_connection  s     99(((DII9M9M,MTlG$**222cT__6L(##C4##--/ 0 ##--/ 667ijj r   c                     d}| j                   j                  j                  D ]T  }|j                  d      ^}}|j                  j                  |      rd}|rt        |d         |j                  k(  }|sS |S  |S )zWhether we should bypass any upstream proxy.

        This checks whether the request address is in the no_proxy list.
        F:Tr   )r   r   no_proxysplitrX   endswithintrY   )r   r   r   addrrX   rY   s         r   r   z&HttpLayer.should_bypass_upstream_proxy  s{    
 KK''00 	D**S/KD4||$$T*"47|w||;H	 r   c                 ^   d}	 | j                   j                  j                  }|sN	 | j                   j                  j                  }|r+dt        j                  t        j                  |            z   }|r||j                  j                  d<   yy# t        $ r Y xw xY w# t        $ r Y 7w xY w)z7Apply proxy authorization to the request if configured.Ns   Basic zProxy-Authorization)r   r   upstream_custom_authAttributeErrorupstream_authbase64	b64encoder
   always_bytesr   r   )r   r;   auths      r   r   zHttpLayer.apply_proxy_auth3  s    	;;&&;;D {{**88*V-=-=h>S>STX>Y-ZZD 7;AII34   		 " s#    B AB  	BB 	B,+B,r   )returnN)r=   r>   r?   rI   r   ServerConnection__annotations__rG   r^   r   r   r   r   rn   r   r   r   r   r   rr   rs   s   @r   r   r      sT    
%8,vp]%k k3 k k$,<r   r   )$r   enumloggingr   r   h2.exceptionsr   !seleniumwire.thirdparty.mitmproxyr   r   r   r   %seleniumwire.thirdparty.mitmproxy.netr   1seleniumwire.thirdparty.mitmproxy.server.protocolr   ;seleniumwire.thirdparty.mitmproxy.server.protocol.websocketr	   'seleniumwire.thirdparty.mitmproxy.utilsr
   	getLoggerr=   rb   Layerr   rB   rT   rv   Enumrx   r{   r|   r}   r   r   r   r@   r   r   <module>r      s          9 D D < B V <g!0$TZZ 0$f$ $*)+4:: )+Xtyy  /-0 40U<

 U<r   