
    'h+                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZ d dlmZmZ  G d dej                   e	j"                        Z G d dej&                  e	j"                        Zeej*                  d	<    G d
 de      Zy)    N)certs
exceptionsstateobject)tlstcp)humanstrutilsc                        e Zd ZdZ fdZd Zd Zd Zd Z e	e
eeej                  ej                  eeee
e
ee
ej$                  ej&                  eef            Zd Zed	        Zed
        Z fdZ fdZ xZS )ClientConnectiona  
    A client connection

    Attributes:
        address: Remote address
        tls_established: True if TLS is established, False otherwise
        clientcert: The TLS client certificate
        mitmcert: The MITM'ed TLS server certificate presented to the client
        timestamp_start: Connection start timestamp
        timestamp_tls_setup: TLS established timestamp
        timestamp_end: Connection end timestamp
        sni: Server Name Indication sent by client during the TLS handshake
        cipher_name: The current used cipher
        alpn_proto_negotiated: The negotiated application protocol
        tls_version: TLS version
        tls_extensions: TLS ClientHello extensions
    c                 v   |rt         |   |||       n1d | _        d | _        d | _        d | _        d | _        d | _        d | _        t        t        j                               | _        d | _        t        j                         | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        y N)super__init__
connectionserverwfilerfileaddress
clientcerttls_establishedstruuiduuid4idmitmcerttimetimestamp_starttimestamp_endtimestamp_tls_setupsnicipher_namealpn_proto_negotiatedtls_versiontls_extensions)selfclient_connectionr   r   	__class__s       m/var/www/html/scrapers/gsc/venv/lib/python3.12/site-packages/seleniumwire/thirdparty/mitmproxy/connections.pyr   zClientConnection.__init__!   s     G.@"DODKDJDJDL"DO#'D djjl##yy{!#' %)""    c                 J    t        | j                        xr | j                   S r   boolr   finishedr%   s    r(   	connectedzClientConnection.connected:       DOO$:T]])::r)   c                 2   | j                   rdj                  | j                        }nd}| j                  r/dj                  t	        j
                  | j                              }nd}dj                  ||t        j                  | j                              S )N[{}]  [ALPN: {}] z(<ClientConnection: {tls}{alpn}{address}>r   alpnr   )	r   formatr#   r"   r	   bytes_to_escaped_strr   format_addressr   r%   r   r6   s      r(   __repr__zClientConnection.__repr__=   s    ..!1!12CC%% ''--d.H.HID D9@@((6 A 
 	
r)   c                 V    t        |t              r| j                  |j                  k(  S yNF)
isinstancer   r   r%   others     r(   __eq__zClientConnection.__eq__P   #    e-.77ehh&&r)   c                 ,    t        | j                        S r   hashr   r.   s    r(   __hash__zClientConnection.__hash__U       DGG}r)   )r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   c                     t        |t              rdj                  |      }| j                  j	                  |       | j                  j                          y Nr)   r>   listjoinr   writeflushr%   messages     r(   sendzClientConnection.sendh   <    gt$hhw'G

!

r)   c                 L     | d t               d       }|j                  |       |S r   tuple	set_stateclsstatefs      r(   
from_statezClientConnection.from_staten   s#    egt$	Er)   c                     | j                  t        t        t        j                               |d d dd d d d d d d d             S )NF)r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r[   dictr   r   r   rX   r   s     r(   
make_dummyzClientConnection.make_dummyt   sM    ~~tzz|$ % $"$( &* #
 	
r)   c                 .   	 t         j                  j                  | j                        }|j                  | _        t        | $  |g|i | t        j                         | _        || _        | j                  j                         }|r|j                  d      | _        nd | _        | j                  j#                         | _        | j'                         | _        | j                  j+                         | _        y # t        j                  $ r Y w xY w)Nidna)r   ClientHello	from_filer   
extensionsr$   r   TlsProtocolExceptionr   convert_to_tlsr   r   r   r   get_servernamedecoder    get_cipher_namer!   get_alpn_proto_negotiatedr"   get_protocol_version_namer#   )r%   certargskwargsclient_hellor    r'   s         r(   rg   zClientConnection.convert_to_tls   s    	:??44TZZ@L #/"9"9Dt5d5f5#'99; oo,,.zz&)DHDH??::<%)%C%C%E"??DDF .. 		s   )C> >DDc                 T    t         |           t        j                         | _        y r   )r   finishr   r   )r%   r'   s    r(   rr   zClientConnection.finish   s    !YY[r)   )__name__
__module____qualname____doc__r   r/   r;   rA   rF   r^   r   rU   r,   r   CertfloatbytestypingListTupleint_stateobject_attributesrQ   classmethodr[   r`   rg   rr   __classcell__r'   s   @r(   r   r      s    $#2;
&
 #::!#{{6<<U
#;<   
 
 
&G,) )r)   r   c                       e Zd ZdZddZd Zd Zd Zd Z e	e
eeeeej                  e
ee
eeee      Zed	        Zed
        Zd Zd ZddddZd Zy)ServerConnectiona  
    A server connection

    Attributes:
        address: Remote address. Can be both a domain or an IP address.
        ip_address: Resolved remote IP address.
        source_address: Local IP address or client's source IP address.
        tls_established: True if TLS is established, False otherwise
        cert: The certificate presented by the remote during the TLS handshake
        sni: Server Name Indication sent by the mitmproxy during the TLS handshake
        alpn_proto_negotiated: The negotiated application protocol
        tls_version: TLS version
        via: The underlying server connection (e.g. the connection to the upstream mitmproxy in upstream mitmproxy mode)
        timestamp_start: Connection start timestamp
        timestamp_tcp_setup: TCP ACK received timestamp
        timestamp_tls_setup: TLS established timestamp
        timestamp_end: Connection end timestamp
    Nc                     t         j                  j                  | |||       t        t	        j
                               | _        d | _        d | _        d | _	        d | _
        d | _        d | _        d | _        y r   )r   	TCPClientr   r   r   r   r   r"   r#   viar   r   timestamp_tcp_setupr   )r%   r   source_addressspoof_source_addresss       r(   r   zServerConnection.__init__   sd    tWn>RSdjjl#%)"#!#' #' r)   c                 J    t        | j                        xr | j                   S r   r+   r.   s    r(   r/   zServerConnection.connected   r0   r)   c                    | j                   r7| j                  r+dj                  | j                  xs d| j                        }n.| j                   r dj                  | j                  xs d      }nd}| j                  r/dj                  t        j                  | j                              }nd}dj                  ||t        j                  | j                              S )Nz	[{}: {}] TLSr2   r3   r4   z(<ServerConnection: {tls}{alpn}{address}>r5   )
r   r    r7   r#   r"   r	   r8   r   r9   r   r:   s      r(   r;   zServerConnection.__repr__   s    DHH$$T%5%5%>IC!!..!1!1!:U;CC%% ''--d.H.HID D9@@((6 A 
 	
r)   c                 V    t        |t              r| j                  |j                  k(  S yr=   )r>   r   r   r?   s     r(   rA   zServerConnection.__eq__   rB   r)   c                 ,    t        | j                        S r   rD   r.   s    r(   rF   zServerConnection.__hash__   rG   r)   )r   r   
ip_addressr   r   rm   r    r"   r#   r   r   r   r   c                 H     | t                     }|j                  |       |S r   rT   rW   s      r(   r[   zServerConnection.from_state   s    L	Er)   c                     | j                  t        t        t        j                               ||d |d   d d ddd d d d d             S )Nr   )r3   r   F)r   r   r   rm   r    r"   r#   r   r   r   r   r   r   r   r]   r_   s     r(   r`   zServerConnection.make_dummy   sT    ~~tzz|$"AJ&* & % $$($("
 	
r)   c                     t        j                          | _        t        j                  j	                  |        t        j                          | _        y r   )r   r   r   r   connectr   r.   s    r(   r   zServerConnection.connect  s0    #yy{d##'99; r)   c                     t        |t              rdj                  |      }| j                  j	                  |       | j                  j                          y rI   rJ   rO   s     r(   rQ   zServerConnection.send  rR   r)   )r    client_certsc                   |r1t        |t              s!t        dt        |      j                  z         d }|rt
        j                  j                  |      }t
        j                  j                  |      r|}nrt
        j                  j                  ||xs, | j                  d   j                  d      j                         dz         }t
        j                  j                  |      r|} | j                  d||d| || _        | j!                         | _        | j$                  j'                         | _        t+        j*                         | _        y )Nzsni must be str, not r   rb   z.pem)rm   r     )r>   r   
ValueErrortypers   ospath
expanduserisfilerL   r   encoderi   existsrg   r    rk   r"   r   rl   r#   r   r   )r%   r    r   ro   client_certr   s         r(   establish_tlszServerConnection.establish_tls  s   z#s+4tCy7I7IIJJ77--l;Lww~~l+*ww|| CDLLO226:AACvM 77>>$'"&K@#@@%)%C%C%E"??DDF#'99; r)   c                 t    t         j                  j                  |        t        j                         | _        y r   )r   r   rr   r   r   r.   s    r(   rr   zServerConnection.finish-  s"    T"!YY[r)   )NN)rs   rt   ru   rv   r   r/   r;   rA   rF   r^   r   rU   r,   r   rw   ry   rx   r~   r   r[   r`   r   rQ   r   rr   r   r)   r(   r   r      s    &
(;
&
 #ZZ#!!   
 
 
(/
 $(d /,)r)   r   r   c                   .     e Zd Z fdZ fdZd Z xZS )SocksServerConnectionc                 2    t        |   |i | || _        y r   )r   r   socks_config)r%   r   rn   ro   r'   s       r(   r   zSocksServerConnection.__init__6  s    $)&)(r)   c                     | j                   j                  dk(  r&t        j                  t        j                  dd||ffgS t        |   ||g|i |S )Nsocks5h   r3   )r   schemesocketAF_INETSOCK_STREAMr   getaddrinfo)r%   hostportrn   ro   r'   s        r(   r   z!SocksServerConnection.getaddrinfo:  sU    ##y0^^V%7%7BtMNNw"4????r)   c                 D   	 t        t        j                  t        j                  t        j                        | j                  j
                     }t        j                  |||      }|j                  || j                  j                  | j                  j                  | j                  j
                  dk(  | j                  j                  | j                  j                         |S # t        $ r/ t        dj                  | j                  j
                              w xY w)N)socks4socks5r   zInvalid SOCKS scheme: {}r   )r^   socksPROXY_TYPE_SOCKS4PROXY_TYPE_SOCKS5r   r   KeyError	TypeErrorr7   
socksocket	set_proxyr   r   usernamepassword)r%   familyr   proto
socks_typess         r(   
makesocketz SocksServerConnection.makesocket?  s    	Y....// &&	(J VT51	%%""$$	1&&&&	
   	Y6==d>O>O>V>VWXX	Ys   AC' '8D)rs   rt   ru   r   r   r   r   r   s   @r(   r   r   5  s    )@
r)   r   )r   r   rz   r   r   r   !seleniumwire.thirdparty.mitmproxyr   r   r   %seleniumwire.thirdparty.mitmproxy.netr   r   'seleniumwire.thirdparty.mitmproxy.utilsr   r	   BaseHandlerStateObjectr   r   r   r~   r   r   r)   r(   <module>r      sz    	      L L : CR)s(?(? R)jL)s}}k&=&= L)^ 3C  ( ( /, r)   