
    Dg                         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"y)    N)
namedtuple)Path)Dict
NamedTuple)_parse_proxy)encodingca.crtzca.keyzseleniumwire-ca.pemmodeupstream_authupstream_custom_authno_proxyc                 Z   | xs i j                  di       }t        j                  j                  d      }t        j                  j                  d      }t        j                  j                  d      }i }|r||d<   |r||d<   |r||d<   |j	                  |       |j                  d      }t        |t              r0|j                  d      D cg c]  }|j                          c}|d<   t        d	d
      }dD ](  }|j                  |       |t        ||          ||<   * |S c c}w )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   ,	ProxyConfz!scheme username password hostport)r   r   )poposenvirongetupdate
isinstancestrsplitstripr   r   )	optionsproxy_options
http_proxyhttps_proxyr   mergedhconf
proxy_types	            R/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/seleniumwire/utils.pyget_upstream_proxyr)      s   $ ]''4M-J**../Kzz~~j)HF#v%w%z
MM- zz*%H(C 191DEAaggiEzk#FGD' I
 ::j!-!%|F:4F'G!HF:	I M Fs   D(proxy_configreturnc                    | j                  d      }| j                  d      }d}|r)|r'|j                  |j                  k7  rt        d      |}n	|r|}n|r|}i }|rs|\  }}}}dj                  ||      |t        <   |rdj                  ||      |t
        <   | j                  d      }	|	r	|	|t        <   | j                  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passwordr.   custom_authr   s              r(   build_proxy_argsr:   J   s     !!&)J""7+KDk+"6"66`aa		D/3,(H,33FHEY'.~~h'ID#$"&&'=>.9D*+##J/"*DK    c                    	 t        j                  t        |       }t        t	        t        j                         |       d      5 }|j                  |       ddd       t        j                  dj                  |              y# 1 sw Y   .xY w# t        $ r' t        j                  dj                  |              Y y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infor0   FileNotFoundErrorerror)	cert_namecertouts      r(   extract_certrK   w   s    \Y7 $ryy{I.5 	IIdO	HOOPYZ[	 	  @		,33I>?@s   B BB-B?>B?c                    t        j                  | d       t        | t              }|r|j	                         ry|5|3t        |      j                         }t        |      j                         }nK||t        d      t        j                  t        t              }t        j                  t        t              }t        |d      5 }|j                  |dz   |z          ddd       y# 1 sw Y   yxY w)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 suppliedr=      
)r   makedirsr   COMBINED_CERTexists
read_bytesr/   r>   r?   r@   	ROOT_CERTROOT_KEYrA   rC   )dest_folder	cert_pathkey_pathcheck_existscombined_path	root_certroot_keyf_outs           r(   extract_cert_and_keyr]      s      KKd+m4M,,.!5O..0	>,,.		("6FGG$$[)<	##K:	mT	" 2eI%012 2 2s   	C**C3c                 p    t        | t        j                  j                        xr t        | t               S )N)r   collectionsabcSequencer   )	containers    r(   is_list_alikerc      s)    i!9!9:]:iY\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)
    [] )addressaddrportrests       r(   urlsafe_addressrl      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)rm   r   s     r(   rp   rp      s     >>$))r;   )r	   )NNT)#collections.abcr_   loggingr   r>   r   pathlibr   typingr   r   urllib.requestr   *seleniumwire.thirdparty.mitmproxy.net.httpr   ro   	getLogger__name__rD   rS   rT   rP   r1   r2   r3   r4   r)   r   r:   rK   r]   rc   rl   bytesrp   rg   r;   r(   <module>rz      s      	  "  # ' Jg!	%	$ 2 /d*4Z#8 *T#s(^ *Z
\2D^"* *# *% *r;   