
    \h-4                     b    d dl Z 	 d dlmZmZ d dlZ G d de      Zy# e$ r d dlmZmZ Y w xY w)    N)	urlencodeunquotec                       e Zd Zd#dZd Zd Zd Zd Zd Ze	d        Z
e	d	        Zd$d
Zd#dZd Zd Zd Zd Zd Zd ZddddZd ZdddddZd Zd$dZd Zd Zd  Zd! Zd" Zy)%ClientNc                    ||ni }||ni }d|z   | _         |rdt        t        |            z   }nd}d|v r|d   | _        n`t	        j
                  d| j                   z  |z         }|j                  j                  d      }	 t        j                  |      }|d
   | _        | j                   j                  d      }	|	d   dd dz   t        | j                        z   | _        y# t        $ r}t        d|dd	 z        d}~ww xY w)aY  
        Initialises a new Client object


        :param url: This is where the BrowserMob Proxy lives
        :param params: URL query (for example httpProxy and httpsProxy vars)
        :param options: Dictionary that can contain the port of an existing
                        proxy to use (for example 'existing_proxy_port_to_use')
        Nzhttp://? existing_proxy_port_to_use%s/proxyzutf-8zOCould not read Browsermob-Proxy json
Another server running on this port?
%s...i   port:      )hostr   r   r   requestspostcontentdecodejsonloads	Exceptionsplitstrproxy)
selfurlparamsoptions	urlparamsrespr   jcontente	url_partss
             R/var/www/html/trade_iq/venv/lib/python3.12/site-packages/browsermobproxy/client.py__init__zClient.__init__   s    "-2$0'bO	gi&788II'72 <=DI==dii!7)!CDDll))'2G_::g. !(DIIIOOC(	q\!"%+c$))n<
  _ !NPWX\Y\P]!^ _ __s   C$ $	D-C>>Dc                 v    t        j                  | j                  d| j                        }|j                  S )z:
        shuts down the proxy and closes the port
        /proxy/r   deleter   r   status_coder   rs     r$   closezClient.close*   s)     OOTYY		BC}}    c                 ^    ddl m} |j                  | j                  | j                  d      S )Y
        Returns a Selenium WebDriver Proxy class with details of the HTTP Proxy
        r   )	webdriver)	httpProxysslProxy)seleniumr1   Proxyr   )r   r1   s     r$   selenium_proxyzClient.selenium_proxy3   s+     	'

 
  	r.   c                 "    | j                         S )r0   )r6   )r   s    r$   webdriver_proxyzClient.webdriver_proxy=   s     ""$$r.   c                 >    d| j                   | j                   d|d<   y)z
        Adds an 'proxy' entry to a desired capabilities dictionary with the
        BrowserMob proxy information


        :param capabilities: The Desired capabilities object from Selenium WebDriver
        MANUAL)	proxyTyper2   r3   r   N)r   r   capabilitiess     r$   add_to_capabilitieszClient.add_to_capabilitiesD   s!     "

!
Wr.   c                 &    | j                  |       y )N)r>   r<   s     r$   add_to_webdriver_capabilitiesz$Client.add_to_webdriver_capabilitiesR   s      .r.   c                     t        j                  d| j                  z        j                         }|d   D cg c]  }|d   	 }}|S c c}w )z8
        Return a list of proxy ports available
        r   	proxyListr   )r   getr   r   )r   r,   r   portss       r$   proxy_portszClient.proxy_portsV   sI     LLdii/0557*+K.9$f99 :s   Ac                     t        j                  | j                  d| j                  d      }|j	                         S )z5
        Gets the HAR that has been recorded
        r'   /har)r   rC   r   r   r   r+   s     r$   harz
Client.hara   s*    
 LLdiiCDvvxr.   c                 @   ||ni }|d|ini }||j                  d|i       |r|j                  |       t        j                  | j                  d| j                  d|      }|j
                  dk(  r|j
                  |j                         fS |j
                  dfS )a
  
        This sets a new HAR to be recorded

        :param str ref: A reference for the HAR. Defaults to None
        :param dict options: A dictionary that will be passed to BrowserMob
            Proxy with specific keywords. Keywords are:

                - captureHeaders: Boolean, capture headers
                - captureContent: Boolean, capture content bodies
                - captureBinaryContent: Boolean, capture binary content

        :param str title: the title of first har page. Defaults to ref.
        NinitialPageRefinitialPageTitler'   rG      )updater   putr   r   r*   r   )r   refr   titlepayloadr,   s         r$   new_harzClient.new_harj   s     %0'b-0_#S)"NN.67NN7#LLdiiCWM==CMM1668,,MM4((r.   c                     |d|ini }||j                  d|i       t        j                  | j                  d| j                  d|      }|j
                  S )z
        This sets a new page to be recorded

        :param str ref: A reference for the new page. Defaults to None
        :param str title: the title of new har page. Defaults to ref.
        pageRef	pageTitler'   z/har/pageRef)rM   r   rN   r   r   r*   )r   rO   rP   rQ   r,   s        r$   new_pagezClient.new_page   sU     '*o9c"2NNK/0LLdiiK "}}r.   c                     t        j                  | j                  d| j                  d||d      }|j                  S )z
        Sets a list of URL patterns to blacklist

        :param str regex: a comma separated list of regular expressions
        :param int status_code: the HTTP status code to return for URLs
            that do not match the blacklist
        r'   z
/blacklistregexstatusr   rN   r   r   r*   r   regexpr*   r,   s       r$   	blacklistzClient.blacklist   4     LLDIItyyI#)[AC}}r.   c                     t        j                  | j                  d| j                  d||d      }|j                  S )z
        Sets a list of URL patterns to whitelist

        :param str regex: a comma separated list of regular expressions
        :param int status_code: the HTTP status code to return for URLs
            that do not match the whitelist
        r'   z
/whitelistrX   r[   r\   s       r$   	whitelistzClient.whitelist   r_   r.   c                     t        j                  | j                  d| j                  d|t	        j
                  ||d      ddi      }|j                  S )a  
        This add automatic basic authentication

        :param str domain: domain to set authentication credentials for
        :param str username: valid username to use when authenticating
        :param  str password: valid password to use when authenticating
        r'   z/auth/basic/)usernamepasswordcontent-typeapplication/jsonr   dataheadersr   r   r   r   r   dumpsr*   )r   domainrc   rd   r,   s        r$   basic_authenticationzClient.basic_authentication   sK     MMTYY		SYZ#zzxX*VW#13E"FH }}r.   c                     t        |t              st        d      t        j                  | j
                  d| j                  dt        j                  |      ddi      }|j                  S )z
        This sets the headers that will set by the proxy on all requests

        :param dict headers: this is a dictionary of the headers to be set
        zheaders needs to be dictionaryr'   z/headersre   rf   rg   )

isinstancedict	TypeErrorr   r   r   r   r   rk   r*   )r   ri   r,   s      r$   ri   zClient.headers   sY     '4(<==MMtyy$))L#zz'2#13E"FH }}r.   c                     t        j                  | j                  d| j                  d|ddi      }|j                  S )  
        Executes the java/js code against each response
        `HttpRequest request <https://netty.io/4.1/api/io/netty/handler/codec/http/HttpRequest.html>`_,
        `HttpMessageContents contents <https://raw.githubusercontent.com/lightbody/browsermob-proxy/master/browsermob-core/src/main/java/net/lightbody/bmp/util/HttpMessageContents.java>`_,
        `HttpMessageInfo messageInfo <https://raw.githubusercontent.com/lightbody/browsermob-proxy/master/browsermob-core/src/main/java/net/lightbody/bmp/util/HttpMessageInfo.java>`_
        are available objects to interact with.
        :param str js: the js/java code to execute
        r'   z/filter/responsere   
text/plainrg   r   r   r   r   r*   r   jsr,   s      r$   response_interceptorzClient.response_interceptor   s9     MMtyy$))T!#1<"@B }}r.   c                     t        j                  | j                  d| j                  d|ddi      }|j                  S )rs   r'   z/filter/requestre   rt   rg   ru   rv   s      r$   request_interceptorzClient.request_interceptor   s9     MMdiiS!#1<"@B }}r.   upstreamKbpsdownstreamKbpslatency)upstream_kbpsdownstream_kbpsr}   c                    i }t        |j                               D ]<  \  }}|| j                  vrt        d|z        t	        |      || j                  |   <   > t        t        |j                                     dk(  rt        d      t        j                  | j                  d| j                  d|      }|j                  S )a4  
        Limit the bandwidth through the proxy.

        :param dict options: A dictionary with all the details you want to set.
            downstream_kbps - Sets the downstream kbps
            upstream_kbps - Sets the upstream kbps
            latency - Add the given latency to each HTTP request
        invalid key: %sr   )You need to specify one of the valid Keysr'   z/limit)listitemsLIMITSKeyErrorintlenr   rN   r   r   r*   r   r   r   kvr,   s         r$   limitszClient.limits   s     7==?+ 	,FQ#01455%(VF4;;q>"		, tFLLN#$)FGGLL		499E!}}r.   requestTimeoutreadTimeoutconnectionTimeoutdnsCacheTimeout)requestread
connectiondnsc                    i }t        |j                               D ]<  \  }}|| j                  vrt        d|z        t	        |      || j                  |   <   > t        t        |j                                     dk(  rt        d      t        j                  | j                  d| j                  d|      }|j                  S )aU  
        Configure various timeouts in the proxy

        :param dict options: A dictionary with all the details you want to set.
            request - request timeout (in seconds)
            read - read timeout (in seconds)
            connection - connection timeout (in seconds)
            dns - dns lookup timeout (in seconds)
        r   r   r   r'   z/timeout)r   r   TIMEOUTSr   r   r   r   rN   r   r   r*   r   s         r$   timeoutszClient.timeouts  s     7==?+ 	.FQ%01455'*1vF4==#$		. tFLLN#$)FGGLL$))TYYG!}}r.   c                     ||ni }|||||<   t        j                  | j                  d| j                  dt	        j
                  |      ddi      }|j                  S )a   
        Remap the hosts for a specific URL

        :param str address: url that you wish to remap
        :param str ip_address: IP Address that will handle all traffic for
            the address passed in
        :param **hostmap: Other hosts to be added as keyword arguments
        r'   z/hostsre   rf   )ri   rj   )r   address
ip_addresshostmapr,   s        r$   remap_hostszClient.remap_hosts!  sd     %0'bJ$:)GGMMDIIF**W-#13E"FH }}r.   c                     t        j                  | j                  d| j                  d||d      }|j                  S )z
        Waits for the network to be quiet

        :param int quiet_period: number of milliseconds the network needs
            to be quiet for
        :param int timeout: max number of milliseconds to wait
        r'   z/wait)quietPeriodInMstimeoutInMsr[   )r   quiet_periodtimeoutr,   s       r$   wait_for_traffic_to_stopzClient.wait_for_traffic_to_stop3  s4     LLtyy$))D-9'RT}}r.   c                 x    t        j                  | j                  d| j                  d      }|j                  S )zI
        Clears the DNS cache associated with the proxy instance
        r'   z
/dns/cacher(   r+   s     r$   clear_dns_cachezClient.clear_dns_cache?  s)     OOtyy$))LM}}r.   c                     ||d}t        j                  | j                  d| j                  d|      }|j                  S )z
        Rewrites the requested url.

        :param match: a regex to match requests with
        :param replace: unicode                    a string to replace the matches with
        )
matchRegexreplacer'   /rewriter[   )r   matchr   r   r,   s        r$   rewrite_urlzClient.rewrite_urlF  s=      
 LL$))TYYG!}}r.   c                 x    t        j                  | j                  d| j                  d      }|j                  S )zK
        Clears all URL rewrite rules
        :return: status code
        r'   r   r(   r+   s     r$   clear_all_rewrite_url_rulesz"Client.clear_all_rewrite_url_rulesV  s)     OOTYY		JK}}r.   c                 ~    t        j                  | j                  d| j                  dd|i      }|j                  S )z
        Retries. No idea what its used for, but its in the API...

        :param int retry_count: the number of retries
        r'   z/retry
retrycountr[   )r   retry_countr,   s      r$   retryzClient.retry_  s3     LL		499E&46}}r.   )NN)NNN)__name__
__module____qualname__r%   r-   r6   r8   r>   r@   propertyrE   rH   rR   rV   r^   ra   rm   ri   rx   rz   r   r   r   r   r   r   r   r   r   r    r.   r$   r   r   
   s    =>%
/    )8

 (+F2 $) 	H2$
 r.   r   )	r   urllib.parser   r   ImportErrorurllibr   objectr   r   r.   r$   <module>r      s:    */ ]V ]  *))*s    ..