
    Dg                         d dl Z d dlZd dlm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
      Zy)    N)datetime)har)RequestResponseWebSocketMessage)HTTPResponse)
websockets)Headers)is_list_alikec                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	dd	Z
d
 Zd Zd Zy)InterceptRequestHandlerzTMitmproxy add-on which is responsible for request modification
    and capture.
    c                     || _         y N)proxy)selfr   s     T/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/seleniumwire/handler.py__init__z InterceptRequestHandler.__init__   s	    
    c                 ^    | j                  |j                        rd|j                  _        y y NF)in_scoperequeststreamr   flows     r   requestheadersz&InterceptRequestHandler.requestheaders   s#    ==&"'DLL 'r   c                 N   |j                   j                  rz|j                   j                  j                  | j                  j                  j
                  j                  j                  j                  k7  r|j                  j                          y | j                  j                  j                  |j                  d       | j                  |      }| j                  |      s,t        j!                  d|j"                  |j$                         y | j                  j&                  W| j                  j'                  |       |j(                  rt+        j,                  t/        |j(                  j0                        |j(                  j2                  |j(                  j4                  j7                         D cg c]'  \  }}|j9                  d      |j9                  d      f) c}}      |_        n|j"                  |j                  _        |j$                  j;                  ddd      |j                  _        | j=                  |j4                        |j                  _        |j2                  |j                  _        t        jA                  d	|j$                         | j                  jB                  jE                  |       |jF                  |jF                  |j                  _#        |j(                  r:| j                  jB                  jI                  |jF                  |j(                         | j                  jJ                  jM                  d
      du rd|j                  j4                  d<   d|j                  j4                  v r|j                  j4                  d= y y c c}}w )Nraw_content)bodyattrzNot capturing %s request: %sutf-8)status_codecontentheaderswss://https://   zCapturing request: %sdisable_encodingTidentityzAccept-EncodingzProxy-Connection)'server_connviaaddressr   masterserverconfigupstream_serverclient_connfinishmodifiermodify_requestr   _create_requestr   logdebugmethodurlrequest_interceptorresponser   makeintr!   bodyr#   itemsencodereplace_to_headers_objr   infostoragesave_requestidsave_responseoptionsget)r   r   r   kvs        r   r   zInterceptRequestHandler.request   s   ##++tzz/@/@/G/G/N/N/^/^/f/ff   '') 	

**4<<-*P &&t,}}W%II4gnngkkR ::))5JJ**73 , 1 1 #G$4$4$@$@ A#,,11PWP`P`PhPhPnPnPpq1ahhw/'1BCq! '.nn##*;;#6#6xQ#O '+';';GOO'L$+2<<(('++6

''0::!%jjDLLOJJ,,WZZ9I9IJ ::!!"45=6@DLL  !23 !5!55$$%78 61 rs   7,N!c                    |j                   | j                  j                  j                  ddg      v ry| j                  j                  }|syt        |      s|g}|D ]&  }t        j                  ||j                        }|s& y y)Nignore_http_methodsOPTIONSFT)	r7   r   rG   rH   scopesr   researchr8   )r   r   rN   scopematchs        r   r   z InterceptRequestHandler.in_scopeT   s}    >>TZZ//334II;WW""v&XF 	EIIeW[[1E	
 r   c                 ^    | j                  |j                        rd|j                  _        y y r   )r   r   r:   r   r   s     r   responseheadersz'InterceptRequestHandler.responseheadersf   s#    ==&#(DMM  'r   c                    | j                   j                  j                  |j                  |j                         t        |j                  d      sy | j                  |      }| j                   j                  | j                   j                  | j                  ||      |       |j                  |j                  _	        |j                  |j                  _
        | j                  |j                        |j                  _        |j                  |j                  _        t        j!                  d|j                  j"                  |j                  |j                         | j                   j$                  j'                  |j                  j(                  |       | j                   j*                  j-                  dd      rN| j                   j$                  j/                  |j                  j(                  t1        j2                  |             y y )NrE   zCapturing response: %s %s %s
enable_harF)r   r2   modify_responser:   r   hasattr_create_responseresponse_interceptorr4   r!   reasonrA   r#   r=   r   r5   rB   r8   rC   rF   rE   rG   rH   save_har_entryr   create_har_entry)r   r   r:   s      r   r:   z InterceptRequestHandler.responsek   se    	

++DMM4<<Ht||T* ((. ::**6JJ++D,@,@x,PRZ[(0(<(<DMM%#+??DMM $($8$89I9I$JDMM!(0DMM%/1A1A8CWCWYaYhYhi

(((C::!!,6JJ--dlloos?S?STX?YZ 7r   Nc           
         t        |j                  j                  |j                  j                  |j                  j                  j                         D cg c]	  \  }}||f c}}|j                  j                        }t        j                  |j                        rAt        j                  |j                        r"|j                  j                  ddd      |_        ||_        |S c c}}w )N)r7   r8   r#   r=   r%   r$   r&   )r   r   r7   r8   r#   r>   r   r	   check_handshakecheck_client_versionr@   r:   )r   r   r:   rI   rJ   r   s         r   r4   z'InterceptRequestHandler._create_request   s    <<&&  (,(<(<(B(B(DE1aVE))	
 %%goo6:;Z;Z[b[j[j;k!++--j(AFGK# Fs   C,c                 x   t        |j                  j                  |j                  j                  |j                  j                  j                  d      D cg c]	  \  }}||f c}}|j                  j                        }|j                  j                  }|t        |j                  |j                  |j                  |j                  j                         |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                        |_        |S c c}}w )NT)multi)r!   r[   r#   r=   )subjectserialkeysignature_algorithmexpiredissuer	notbeforenotafterorganizationcnaltnames)r   r:   r!   r[   r#   r>   r   r)   certdictrc   rd   keyinfox509get_signature_algorithmhas_expiredrh   ri   rj   rk   rl   rm   )r   r   rI   rJ   r:   rn   s         r   rY   z(InterceptRequestHandler._create_response   s    11==''(,(=(=(C(C$(C(OP1aVP**	
 $$ {{LL$(II$E$E$G(({{..!..77HM ) Qs   D6c           
          t        |j                         D cg c]0  \  }}|j                  d      t        |      j                  d      f2 c}}      S c c}}w )Nr    )r
   r>   r?   str)r   r#   rI   rJ   s       r   rA   z'InterceptRequestHandler._to_headers_obj   sD    PWP]P]P_`1'*CFMM',BC`aa`s   5A
c                    t        |j                  j                  d      r|j                  d   }t	        |j
                  |j                  t        j                  |j                              }| j                  j                  j                  |j                  j                  j                  |       |j
                  rd}nd}t        j                  d||       y y )NrE   )from_clientr"   datez(client -> server)z(server -> client)z"Capturing websocket message %s: %s)rX   handshake_flowr   messagesr   rx   r"   r   fromtimestamp	timestampr   rC   save_ws_messagerE   r5   r6   )r   r   message
ws_message	directions        r   websocket_messagez)InterceptRequestHandler.websocket_message   s    4&&..5mmB'G)#//++G,=,=>J JJ..t/B/B/J/J/M/MzZ""0	0	II:IzR 6r   r   )__name__
__module____qualname____doc__r   r   r   r   rT   r:   r4   rY   rA   r    r   r   r   r      s=    (
69p$)
[8"4bSr   r   )loggingrO   r   seleniumwirer   seleniumwire.requestr   r   r   &seleniumwire.thirdparty.mitmproxy.httpr   %seleniumwire.thirdparty.mitmproxy.netr	   2seleniumwire.thirdparty.mitmproxy.net.http.headersr
   seleniumwire.utilsr   	getLoggerr   r5   r   r   r   r   <module>r      sC     	   D D ? < F ,g!vS vSr   