
    hg                         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dS )    N)datetime)har)RequestResponseWebSocketMessage)HTTPResponse)
websockets)Headers)is_list_alikec                   P    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dS )InterceptRequestHandlerzTMitmproxy add-on which is responsible for request modification
    and capture.
    c                     || _         d S N)proxy)selfr   s     M/var/www/fb-scrape/myenv/lib/python3.11/site-packages/seleniumwire/handler.py__init__z InterceptRequestHandler.__init__   s    


    c                 V    |                      |j                  rd|j        _        d S d S NF)in_scoperequeststreamr   flows     r   requestheadersz&InterceptRequestHandler.requestheaders   s2    ==&& 	("'DL	( 	(r   c                 N   |j         j        rN|j         j        j        | j        j        j        j        j        j        k    r|j        	                                 d S | j        j
                            |j        d           |                     |          }|                     |          s(t                              d|j        |j                   d S | j        j        | j                            |           |j        rdt+          j        t/          |j        j                  |j        j        d |j        j                                        D                       |_        nl|j        |j        _        |j                            ddd          |j        _        |                     |j                  |j        _        |j        |j        _        t                              d	|j                   | j        j         !                    |           |j"        |j"        |j        _"        |j        r*| j        j         #                    |j"        |j                   | j        j$        %                    d
          du rd|j        j        d<   d|j        j        v r|j        j        d= d S d S )Nraw_content)bodyattrzNot capturing %s request: %sc                 h    g | ]/\  }}|                     d           |                     d           f0S zutf-8)encode.0kvs      r   
<listcomp>z3InterceptRequestHandler.request.<locals>.<listcomp>9   s9    qqq1ahhw//'1B1BCqqqr   )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replace_to_headers_objr   infostoragesave_requestidsave_responseoptionsget)r   r   r   s      r   r   zInterceptRequestHandler.request   sc    	#+tz/@/G/N/^/fff  ''))) 	
**4<-*PPP &&t,,}}W%% 	II4gngkRRRF :)5J**7333 8 , 1 #G$4$@ A A#,1qqPWP`PhPnPnPpPpqqq! ! ! '.n##*;#6#6xQ#O#O '+';';GO'L'L$+2<(('+666
''000:!%jDLO 	KJ,,WZ9IJJJ :!!"455==6@DL !23 !555$%7888 65r   c                     |j         | j        j                            ddg          v rdS | j        j        }|sdS t          |          s|g}|D ]!}t          j        ||j                  }|r dS "dS )Nignore_http_methodsOPTIONSFT)	r>   r   rM   rN   scopesr   researchr?   )r   r   rR   scopematchs        r   r   z InterceptRequestHandler.in_scopeT   s    >TZ/334II;WWWW5" 	4v&& 	XF 	 	EIeW[11E tt ur   c                 V    |                      |j                  rd|j        _        d S d S r   )r   r   rA   r   r   s     r   responseheadersz'InterceptRequestHandler.responseheadersf   s2    ==&& 	)#(DM   	) 	)r   c                 H   | j         j                            |j        |j                   t          |j        d          sd S |                     |          }| j         j        | j                             |                     ||          |           |j	        |j        _	        |j
        |j        _
        |                     |j                  |j        _        |j        |j        _        t                              d|j        j        |j	        |j
                   | j         j                            |j        j        |           | j         j                            dd          r>| j         j                            |j        j        t1          j        |                     d S d S )NrK   zCapturing response: %s %s %s
enable_harF)r   r9   modify_responserA   r   hasattr_create_responseresponse_interceptorr;   r(   reasonrG   r*   rD   r   r<   rH   r?   rI   rL   rK   rM   rN   save_har_entryr   create_har_entry)r   r   rA   s      r   rA   z InterceptRequestHandler.responsek   sf    	
++DM4<HHHt|T** 	F ((.. :*6J++D,@,@x,P,PRZ[[[(0(<DM%#+?DM $($8$89I$J$JDM!(0DM%/1A8CWYaYhiii
(((CCC:!!,66 	[J--dlos?STX?Y?YZZZZZ	[ 	[r   Nc                 h   t          |j        j        |j        j        d |j        j                                        D             |j        j                  }t          j        |j                  r:t          j	        |j                  r!|j        
                    ddd          |_        ||_        |S )Nc                     g | ]	\  }}||f
S  rd   r#   s      r   r'   z;InterceptRequestHandler._create_request.<locals>.<listcomp>   s     EEE1aVEEEr   )r>   r?   r*   rD   r,   r+   r-   )r   r   r>   r?   r*   rE   r   r	   check_handshakecheck_client_versionrF   rA   )r   r   rA   r   s       r   r;   z'InterceptRequestHandler._create_request   s    <& EE(<(B(B(D(DEEE)	
 
 
 %go66 	G:;Z[b[j;k;k 	G!+--j(AFFGK#r   c                    t          |j        j        |j        j        d |j        j                            d          D             |j        j                  }|j        j        }|ht          |j
        |j        |j        |j                                        |j        |j        |j        |j        |j        |j        |j                  |_        |S )Nc                     g | ]	\  }}||f
S rd   rd   r#   s      r   r'   z<InterceptRequestHandler._create_response.<locals>.<listcomp>   s     PPP1aVPPPr   T)multi)r(   r_   r*   rD   )subjectserialkeysignature_algorithmexpiredissuer	notbeforenotafterorganizationcnaltnames)r   rA   r(   r_   r*   rE   r   r0   certdictrj   rk   keyinfox509get_signature_algorithmhas_expiredro   rp   rq   rr   rs   rt   )r   r   rA   ru   s       r   r]   z(InterceptRequestHandler._create_response   s    1='PP(=(C(C$(C(O(OPPP*	
 
 
 $ {L$(I$E$E$G$G({.!.7  HM r   c                 X    t          d |                                D                       S )Nc                     g | ]<\  }}|                     d           t          |                               d           f=S r!   )r"   strr#   s      r   r'   z;InterceptRequestHandler._to_headers_obj.<locals>.<listcomp>   s?    ```1'**CFFMM',B,BC```r   )r
   rE   )r   r*   s     r   rG   z'InterceptRequestHandler._to_headers_obj   s,    ``PWP]P]P_P_```aaar   c                 l   t          |j        j        d          r|j        d         }t	          |j        |j        t          j        |j	                            }| j
        j                            |j        j        j        |           |j        rd}nd}t                              d||           d S d S )NrK   )from_clientr)   datez(client -> server)z(server -> client)z"Capturing websocket message %s: %s)r\   handshake_flowr   messagesr   r   r)   r   fromtimestamp	timestampr   rI   save_ws_messagerK   r<   r=   )r   r   message
ws_message	directions        r   websocket_messagez)InterceptRequestHandler.websocket_message   s    4&.55 	SmB'G)#/+G,=>>  J J..t/B/J/MzZZZ" 10		0	II:IzRRRRR	S 	Sr   r   )__name__
__module____qualname____doc__r   r   r   r   rX   rA   r;   r]   rG   r   rd   r   r   r   r      s           ( ( (
69 69 69p  $) ) )
[ [ [8   "  4b b bS S S S Sr   r   )loggingrS   r   seleniumwirer   seleniumwire.requestr   r   r   &seleniumwire.thirdparty.mitmproxy.httpr   %seleniumwire.thirdparty.mitmproxy.netr	   2seleniumwire.thirdparty.mitmproxy.net.http.headersr
   seleniumwire.utilsr   	getLoggerr   r<   r   rd   r   r   <module>r      s     				             D D D D D D D D D D ? ? ? ? ? ? < < < < < < F F F F F F , , , , , ,g!!vS vS vS vS vS vS vS vS vS vSr   