
    hg                        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 d dlm	Z	m
Z
 d dlmZ d dlmZ  ej        e          ZdZdZd	Zd
ZdZdZdZd Zde	ee
f         de	eef         fdZddZddZd Zd Z de!dede!fdZ"dS )    N)
namedtuple)Path)Dict
NamedTuple)_parse_proxy)encodingca.crtzca.keyzseleniumwire-ca.pemmodeupstream_authupstream_custom_authno_proxyc                 j   | pi                      di           }t          j                            d          }t          j                            d          }t          j                            d          }i }|r||d<   |r||d<   |r||d<   |                    |           |                    d          }t          |t                    r"d |                    d	          D             |d<   t          d
d          }dD ]2}|                    |           |t          ||                    ||<   3|S )a  Get the upstream proxy configuration from the options dictionary.
    This will be overridden with any configuration found in the environment
    variables HTTP_PROXY, HTTPS_PROXY, NO_PROXY

    The configuration will be returned as a dictionary with keys 'http',
    'https' and 'no_proxy'. The value of the 'http' and 'https' keys will
    be a named tuple with the attributes:
        scheme, username, password, hostport
    The value of 'no_proxy' will be a list.

    Note that the keys will only be present in the dictionary when relevant
    proxy configuration exists.

    Args:
        options: The selenium wire options.
    Returns: A dictionary.
    proxy
HTTP_PROXYHTTPS_PROXYNO_PROXYhttphttpsr   c                 6    g | ]}|                                 S  )strip).0hs     K/var/www/fb-scrape/myenv/lib/python3.11/site-packages/seleniumwire/utils.py
<listcomp>z&get_upstream_proxy.<locals>.<listcomp>=   s     EEEAaggiiEEE    ,	ProxyConfz!scheme username password hostport)r   r   )
poposenvirongetupdate
isinstancestrsplitr   r   )optionsproxy_options
http_proxyhttps_proxyr   mergedconf
proxy_types           r   get_upstream_proxyr.      sB   $ ]''44M--J*..//Kz~~j))HF $#v &%w &%z
MM-   zz*%%H(C   FEE1D1DEEEzk#FGGD' I I
 ::j!!-!%|F:4F'G'G!HF:Mr   proxy_configreturnc                    |                      d          }|                      d          }d}|r$|r"|j        |j        k    rt          d          |}n	|r|}n|r|}i }|r|\  }}}}d                    ||          |t          <   |rd                    ||          |t
          <   |                      d          }	|	r
|	|t          <   |                      d          }
|
r
|
|t          <   |S )	zBuild the arguments needed to pass an upstream proxy to mitmproxy.

    Args:
        proxy_config: The proxy config parsed out of the Selenium Wire options.
    Returns: A dictionary of arguments suitable for passing to mitmproxy.
    r   r   NzADifferent settings for http and https proxy servers not supportedzupstream:{}://{}z{}:{}custom_authorizationr   )r"   hostport
ValueErrorformat	MITM_MODEMITM_UPSTREAM_AUTHMITM_UPSTREAM_CUSTOM_AUTHMITM_NO_PROXY)r/   r)   r*   r,   argsschemeusernamepasswordr3   custom_authr   s              r   build_proxy_argsr?   J   s(    !!&))J""7++KD 	k 	+"666`aaa	 	 D +/3,(H,33FHEEY 	J'.~~h'I'ID#$"&&'=>> 	:.9D*+##J// 	+"*DKr   c                    	 t          j        t          |           }t          t	          t          j                    |           d          5 }|                    |           ddd           n# 1 swxY w Y   t          	                    d
                    |                      dS # t          $ r1 t                              d
                    |                      Y dS w xY w)z?Extracts the root certificate to the current working directory.wbNz5{} extracted. You can now import this into a browser.zInvalid certificate '{}')pkgutilget_data__package__openr   r    getcwdwriteloginfor5   FileNotFoundErrorerror)	cert_namecertouts      r   extract_certrO   w   s   \Y77 $ry{{I..55 	IIdOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	HOOPYZZ[[[[[  @ @ @		,33I>>??????@s#   B' A--A14A1'7C"!C"Tc                 D   t          j        | d           t          | t                    }|r|                                rdS |E|Ct          |                                          }t          |                                          }nQ||t          d          t          j        t          t                    }t          j        t          t                    }t          |d          5 }|                    |dz   |z              ddd           dS # 1 swxY w Y   dS )a  Extracts the root certificate and key and combines them into a
    single file called seleniumwire-ca.pem in the specified destination
    folder.

    Args:
        dest_folder: The destination folder that the combined certificate
            and key will be written to.
        cert_path: Optional path to the root certificate. When not supplied
            selenium wire's own root certificate will be used.
        key_path: Optional path to the private key. When not supplied
            selenium wire's own private key will be used. Note that the key
            must always be supplied when a certificate is supplied.
        check_exists: If True the combined file will not be overwritten
            if it already exists in the destination folder.
    T)exist_okNz+A certificate and key must both be suppliedrA      
)r    makedirsr   COMBINED_CERTexists
read_bytesr4   rB   rC   rD   	ROOT_CERTROOT_KEYrE   rG   )dest_folder	cert_pathkey_pathcheck_existscombined_path	root_certroot_keyf_outs           r   extract_cert_and_keyra      sN     Kd++++m44M ,,.. !5OO..00	>>,,..		("6FGGG$[)<<	#K::	mT	"	" 2eI%01112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   ,DDDc                 l    t          | t          j        j                  ot          | t                     S )N)r$   collectionsabcSequencer%   )	containers    r   is_list_alikerg      s*    i!9::]:iY\C]C]?]]r   c                 $    | ^}}}|rd| d}||fS )zMake an address safe to use in a URL.

    Args:
        address: A tuple of address information.
    Returns:
        A 2-tuple of url-safe (address, port)
    []r   )addressaddrportrests       r   urlsafe_addressro      s/      D$ 4{{{:r   datar   c                 ,    t          j        | |          S )a  Attempt to decode data based on the supplied encoding.

    If decoding fails a ValueError is raised.

    Args:
        data: The encoded data.
        encoding: The encoding type.
    Returns: The decoded data.
    Raises: ValueError if the data could not be decoded.
    )decoderdecode)rp   r   s     r   rs   rs      s     >$)))r   )r	   )NNT)#collections.abcrc   loggingr    rB   r   pathlibr   typingr   r   urllib.requestr   *seleniumwire.thirdparty.mitmproxy.net.httpr   rr   	getLogger__name__rH   rW   rX   rT   r6   r7   r8   r9   r.   r%   r?   rO   ra   rg   ro   bytesrs   r   r   r   <module>r}      s        				  " " " " " "       # # # # # # # # ' ' ' ' ' ' J J J J J Jg!!	%	$ 2 / / /d*4Z#8 *T#s(^ * * * *Z
\ 
\ 
\ 
\2 2 2 2D^ ^ ^  "* *# *% * * * * * *r   