
    'hA`                     ~   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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&                  Z eedd      Z G d	 d
      Z G d de      Z G d de      Zd Zd Z G d d      Z G d d      Z G d de      Zd Z G d de      Z  G d d      Z! G d d      Z"y)    N)Optional)SSL)certs
exceptions)
basethread)tlsIPPROTO_IPV6)   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)	_FileLikei   c                 .    || _         d | _        d | _        y N)o_logfirst_byte_timestampselfr   s     i/var/www/html/scrapers/gsc/venv/lib/python3.12/site-packages/seleniumwire/thirdparty/mitmproxy/net/tcp.py__init__z_FileLike.__init__   s    	$(!    c                     || _         y r   )r   r   s     r   set_descriptorz_FileLike.set_descriptor    s	    r   c                 .    t        | j                  |      S r   )getattrr   )r   attrs     r   __getattr__z_FileLike.__getattr__#   s    tvvt$$r   c                     g | _         y)zg
            Starts or resets the log.

            This will store all bytes read or written.
        Nr   r   s    r   	start_logz_FileLike.start_log&   s     	r   c                     d| _         y)z$
            Stops the log.
        Nr   r   s    r   stop_logz_FileLike.stop_log.   s     	r   c                     | j                   d uS r   r   r   s    r   
is_loggingz_FileLike.is_logging4   s    yy$$r   c                 n    | j                         st        d      dj                  | j                        S )z2
            Returns the log as a string.
        zNot logging!r   )r$   
ValueErrorjoinr   r   s    r   get_logz_FileLike.get_log7   s,      ^,,xx		""r   c                 \    | j                         r| j                  j                  |       y y r   )r$   r   appendr   vs     r   add_logz_FileLike.add_log?   s#    ??IIQ r   c                     d | _         y r   )r   r   s    r   reset_timestampsz_FileLike.reset_timestampsC   s
    $(!r   N)__name__
__module____qualname__	BLOCKSIZEr   r   r   r    r"   r$   r(   r-   r/    r   r   r   r      s4    I)
%%# )r   r   c                       e Zd Zd Zd Zy)Writerc                     t        | j                  d      r	 | j                  j                          yy# t        j                  t
        f$ r#}t        j                  t        |            d}~ww xY w)8
            May raise exceptions.TcpDisconnect
        flushN)	hasattrr   r9   socketerrorIOErrorr   TcpDisconnectstrr+   s     r   r9   zWriter.flushI   sY     4667#7 $ LL'* 7 ..s1v667s   4 A0A++A0c                    |r| j                   xs t        j                         | _         	 t        | j                  d      r,| j	                  |       | j                  j                  |      S | j                  j                  |      }| j	                  |d|        |S y# t        j                  t        j                  f$ r#}t        j                  t        |            d}~ww xY w)r8   sendallN)r   timer:   r   r-   rA   writer   Errorr;   r<   r   r>   r?   )r   r,   res       r   rC   zWriter.writeS   s     (,(A(A(PTYY[D%	74669-LLO66>>!,,QALL2A'H  IIv||, 7 ..s1v667s   AB -0B #C%C  C%N)r0   r1   r2   r9   rC   r4   r   r   r6   r6   G   s    77r   r6   c                   &    e Zd Zd ZddZd Zd Zy)Readerc                 n   d}t        j                          }|dk(  s|dkD  r|dk(  s|| j                  kD  r| j                  }n|}	 | j                  j                  |      }| j*                  xs t        j                          | _        |sn$||z  }|dk7  r|t-        |      z  }|dk(  r|dkD  r| j/                  |       |S # t        j
                  $ r Y (t        j                  t        j                  f$ re | j                  j                         xs d}t        j                          |z
  |k  rt        j                  d       Y 1t        j                         t        j                  $ r t        j                         t        j                  $ r#}t        j                  t!        |            d}~wt        j"                  $ r8}|j$                  dk(  rY d}~/t        j&                  t!        |            d}~wt        j(                  $ r#}t        j&                  t!        |            d}~ww xY w)zG
            If length is -1, we read until connection closes.
        r   r   i,  皙?N)rJ   zUnexpected EOF)rB   r3   r   readr   ZeroReturnErrorWantWriteErrorWantReadError
gettimeoutsleepr   
TcpTimeoutr;   timeoutr<   r>   r?   SysCallErrorargsTlsExceptionrD   r   lenr-   )r   lengthresultstartrlendatarS   rF   s           r   rL   zReader.readg   s    		lfqj|v6~~6vv{{4(6 )-(A(A(PTYY[D%dNF|#d)#M lfqjN 	VC && &&(9(9: 2 &&++-4IIK%'72JJsO$//11>> . ++--<< 7 ..s1v66## 66633 --c!f5599 6 --c!f556sD   C H4A/H4AH4F22H4G;G;;H4H//H4Nc                 r    d}d}	 |||k\  r	 |S | j                  d      }|dz  }|s	 |S ||z  }|dk(  r	 |S 3)Nr   r         
)rL   )r   sizerY   
bytes_readchs        r   readlinezReader.readline   sj    
J$$6  1B!OJ
  "; r   c                     | j                  |      }|dk7  rHt        |      |k7  r:|st        j                         t        j                  d|dt        |            |S )zu
            Like .read, but is guaranteed to either return length bytes, or
            raise an exception.
        rJ   z	Expected z bytes, got )rL   rW   r   r>   TcpReadIncomplete)r   rX   rY   s      r   	safe_readzReader.safe_read   s\    
 6"R<CK61 ..00 22393v;G  r   c                 8   t        | j                  t              r5	 | j                  j                  j	                  |t
        j                        S t        | j                  t        j                        r+	 | j                  j	                  |t
        j                        S t!        d      # t
        j                  $ r#}t        j                  t        |            d}~ww xY w# t        j                  $ r#}t        j                  t        |            d}~ww xY w)a  
        Tries to peek into the underlying file object.

        Returns:
            Up to the next N bytes if peeking is successful.

        Raises:
            exceptions.TcpException if there was an error with the socket
            TlsException if there was an error with pyOpenSSL.
            NotImplementedError if the underlying file object is not a [pyOpenSSL] socket
        Nz&Can only peek into (pyOpenSSL) sockets)
isinstancer   socket_fileobject_sockrecvr;   MSG_PEEKr<   r   TcpExceptionreprr   
ConnectionrD   rV   r?   NotImplementedError)r   rX   rF   s      r   peekzReader.peek   s     dff/07vv||((AA /6vv{{66??;; &&NOO << 7 --d1g667
 99 6 --c!f556s/   3B* 5)C# *C =CC #D6DDr   )r0   r1   r2   rL   rc   rf   rq   r4   r   r   rH   rH   e   s    .` Pr   rH   c                     | D cg c]1  }t        |t        j                        r|j                         dkD  r|3 c}xs t	        j                  | dd|      d   S c c}w )a  
    This is a wrapper around select.select() which also works for SSL.Connections
    by taking ssl_connection.pending() into account.

    Caveats:
        If .pending() > 0 for any of the connections in rlist, we avoid the select syscall
        and **will not include any other connections which may or may not be ready**.

    Args:
        rlist: wait until ready for reading

    Returns:
        subset of rlist which is ready for reading.
    r   r4   )rh   r   ro   pendingselect)rlistrS   conns      r   ssl_read_selectrw      s]      dCNN+0B 	 2 
ub"g	.q	12 s   6Ac                    	 | j                  t        j                         t        j                  dk(  rF| j                  | j                         xs d       t        d      D ]  }| j                  d      r n | j                  t        j                         | j                          y# t        j                  $ r Y &w xY w)z@
    Does a hard close of a socket, without emitting a RST.
    nt   i   i   N)shutdownr;   SHUT_WRosname
settimeoutrP   rangerk   SHUT_RDr<   close)sock_s     r   close_socketr      s    $ 	fnn% 77d? OODOO-34 ,- yy 	fnn%
 	JJL << s   A4B* 7"B* *C ?C c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)_ConnectionrJ   c                     t        t        j                  | j                  d            | _        t        t        j                  | j                  d            | _        y)zF
        Set up .rfile and .wfile attributes from .connection
        rbwbN)rH   r;   SocketIO
connectionrfiler6   wfiler   s    r   	_makefilez_Connection._makefile  s:     FOODOOTBC
FOODOOTBC
r   c                     |r-|| _         |j                         | _        | j                          nd | _         d | _        d | _        d | _        d| _        d| _        y NF)r   getpeername
ip_addressr   r   r   tls_establishedfinished)r   r   s     r   r   z_Connection.__init__#  sQ    (DO(446DONN"DO"DODJDJ$r   c                     | j                   sy | j                  j                         }| j                  j                         }| j                  j	                         }|||fS r   )r   r   get_cipher_nameget_cipher_bitsget_cipher_version)r   r~   bitsversions       r   get_current_cipherz_Connection.get_current_cipher1  sQ    ##..0..0//446T7""r   c                    d| _         t        | j                  t        j                        sgt        | j                  dd      s5	 | j                  j                          | j                  j                          | j                  j                          y 	 | j                  j                          y # t        j                  $ r Y Lw xY w# t        j                  $ r Y y w xY w)NTclosedF)r   rh   r   r   ro   r   r   r9   r   r   r>   r   r{   rD   r   s    r   finishz_Connection.finish:  s    
 $//3>>:4::x7JJ$$&JJ$$& JJ((* "//  99 s$   4B/ C /CCCCN)	r0   r1   r2   rbufsizewbufsizer   r   r   r   r4   r   r   r   r     s!    HH
D#r   r   c                   $    e Zd Zd Zd Zd Zd Zy)ConnectionCloserc                      || _         d| _        y r   )rv   	_canceled)r   rv   s     r   r   zConnectionCloser.__init__Q  s    	r   c                 :    d| _         t        | j                        S )zH
            Cancel the current closer, and return a fresh one.
        T)r   r   rv   r   s    r   popzConnectionCloser.popU  s     		**r   c                     | S r   r4   r   s    r   	__enter__zConnectionCloser.__enter__\  s    r   c                 R    | j                   s| j                  j                          y y r   )r   rv   r   r   rU   s     r   __exit__zConnectionCloser.__exit___  s    ~~IIOO r   N)r0   r1   r2   r   r   r   r   r4   r   r   r   r   P  s    +r   r   c                        e Zd Zd fd	Zedeej                     fd       Zd Z	ddZ
d Zd ZddZd	 Zd
 Zd Zd Z xZS )	TCPClientc                 x    t         |   d        || _        || _        d | _        g | _        d | _        || _        y r   )superr   addresssource_addresscertserver_certssnispoof_source_address)r   r   r   r   	__class__s       r   r   zTCPClient.__init__f  s=    ,	$8!r   returnc                 0    t        | j                  dd       S )N
cert_error)r   r   r   s    r   ssl_verification_errorz TCPClient.ssl_verification_erroro  s    td;;r   c                     | j                   rZt        | j                   t        j                        r t	        | j                   j
                         y t	        | j                          y y r   )r   rh   r   ro   r   _socketr   s    r   r   zTCPClient.closes  s@     ??$//3>>:T__445T__-	 r   c                    t        j                  d||d|}| j                  }t        j                  || j                        | _        |r1|| _        | j                  j                  |j                  d             | j                  j                          t        || j                         t        j                  | j                  j                               | _        | j                  j                         D ]0  }| j                  j!                  t        j                  |             2 d| _        | j$                  j'                  | j                         | j(                  j'                  | j                         y )N)alpn_protosr   idnaTr4   )r   create_client_contextr   r   ro   r   set_tlsext_host_nameencodeset_connect_statedo_ssl_handshaker   Certget_peer_certificater   get_peer_cert_chainr   r*   r   r   r   r   )r   r   r   sslctx_kwargscontextr   is          r   convert_to_tlszTCPClient.convert_to_tls}  s   ++ 
#
 

 ..$//BDHOO00F1CD))+t/JJtCCEF	 446 	4A$$UZZ]3	4  $

!!$//2

!!$//2r   c                 0    t        j                   |||      S r   )r;   )r   familytypeprotos       r   
makesocketzTCPClient.makesocket  s    }}VT511r   c                 ,    t        j                  |i |S r   )r;   getaddrinfo)r   rU   kwargss      r   r   zTCPClient.getaddrinfo  s    !!42622r   c                 X   d }| j                  | j                  d   | j                  d   dt        j                        D ]  }|\  }}}}}d }		 | j	                  |||      }	|r|	j                  |       | j                  r|	j                  | j                         | j                  r^	 |	j                  t        j                  t        j                        s/|	j                  t        j                  t        j                  d       |	j%                  |       |	c S  ||t        j&                  d      # t        $ r8}
|	|	j                          t        j                   dt#        |
      z         d }
~
ww xY w# t        j&                  $ r}|}|	|	j                          Y d }~ed }~ww xY w)Nr   r^   z$Failed to spoof the source address: z!getaddrinfo returns an empty list)r   r   r;   SOCK_STREAMr   r   r   bindr   
getsockoptSOL_IPIP_TRANSPARENT
setsockopt	Exceptionr   r   rm   r?   connectr<   )r   rS   errresafsocktyper   	canonnamesar   rF   r   s               r   create_connectionzTCPClient.create_connection  so    ##DLLOT\\!_aI[I[\ 	!C14.B%BD!r8U;OOG,&&IId112,,	#v}}f>S>ST OOFMM6;P;PRST R +	!8 ?I,,BCC% % + JJL(55BSVK 	 << !#JJL!s>   AE7'AD3E73	E4<3E//E44E77F)
F$$F)c                 N   	 | j                         }|| _        |j                         | _	        |j                         | _        | j                          t        |       S # t        j                  t        f$ r-}t	        j
                  d| j                  d   d|      d }~ww xY w)NzError connecting to "r   z": )r   r;   r<   r=   r   rm   r   r   getsocknamer   r   r   r   r   )r   r   r   s      r   r   zTCPClient.connect  s    	//1J %(446$002%% g& 	))a#' 	s   A B$7(BB$c                 :    | j                   j                  |       y r   r   r   r   ns     r   r   zTCPClient.settimeout      ""1%r   c                 6    | j                   j                         S r   )r   rP   r   s    r   rP   zTCPClient.gettimeout  s    ))++r   c                 P    | j                   r| j                  j                         S yNr   r   r   get_alpn_proto_negotiatedr   s    r   r   z#TCPClient.get_alpn_proto_negotiated  !    ??<<>>r   )NNr   )r0   r1   r2   r   propertyr   r   InvalidCertificateExceptionr   r   r   r   r   r   r   r   rP   r   __classcell__r   s   @r   r   r   d  s]    9 <1W1W(X < <.3023$DL&&,r   r   c                 n   	 	 |j                          y# t        j                  $ rE t        j                  | gg g | j	                               \  }}}|st        j                  d      Y jt        j                  $ r7}t        |dd      }|r|t        j                  dt        |      z        d}~ww xY w)a  Peform the SSL handshake.

    If a timeout has been set on the socket externally, it causes OpenSSL
    to raise EWOULDBLOCK which breaks the handshake. This function will
    catch that condition and will retry the operation until it succeeds or
    some other error occurs.

    See https://github.com/pyca/pyopenssl/issues/168 for more information.

    Args:
        sock: The underlying socket.
        ssl_connection: The OpenSSL Connection object.
    zSelect timed outr   NSSL handshake error: %s)do_handshaker   rO   rt   rP   r   rR   rD   r   rV   rn   )r   ssl_connectionrdr   rF   verification_errors         r   r   r     s     	S'') 	    	}}dVRT__5FGHB1 ++,>??yy 	S!(t!L!(( --.G$q'.QRR	Ss    AB4+B4=2B//B4c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )BaseHandlerzU
        The instantiator is expected to call the handle() and finish() methods.
    c                 N    t         |   |       || _        || _        d | _        y r   )r   r   r   server
clientcert)r   r   r   r   r   s       r   r   zBaseHandler.__init__  s%    $r   c                 ~   t        j                  d||d|}| j                  }t        j                  || j                        | _        | j                  j                          	 t        || j                         d| _        | j                  j                         }|rt        j                  |      | _        | j                   j#                  | j                         | j$                  j#                  | j                         y# t        j                  $ r&}t        j                  dt        |      z        d}~ww xY w)zq
        Convert connection to SSL.
        For a list of parameters, see tls.create_server_context(...)
        )r   keyr   NTr4   )r   create_server_contextr   r   ro   set_accept_stater   rD   r   rV   rn   r   r   r   r   r   r   r   r   )r   r   r  r   r   r   r,   s          r   r   zBaseHandler.convert_to_tls  s     ++   ..$//B((*	OT4??3  $335#jj.DO

!!$//2

!!$//2 yy 	O))*Cd1g*MNN	Os   %D D<!D77D<c                     t         r   rp   r   s    r   handlezBaseHandler.handle   s    !!r   c                 :    | j                   j                  |       y r   r   r   s     r   r   zBaseHandler.settimeout#  r   r   c                 P    | j                   r| j                  j                         S yr   r   r   s    r   r   z%BaseHandler.get_alpn_proto_negotiated&  r   r   )
r0   r1   r2   __doc__r   r   r  r   r   r   r   s   @r   r   r     s!    30"&r   r   c                   .    e Zd Zd Zed        Zd Zd Zy)Counterc                 D    d| _         t        j                         | _        y )Nr   )_count	threadingLock_lockr   s    r   r   zCounter.__init__.  s    ^^%
r   c                 ^    | j                   5  | j                  cd d d        S # 1 sw Y   y xY wr   r  r  r   s    r   countzCounter.count2  s%    ZZ 	;;	 	 	s   #,c                 p    | j                   5  | xj                  dz  c_        d d d        y # 1 sw Y   y xY wNr^   r  r   s    r   r   zCounter.__enter__7  ,    ZZ 	KK1K	 	 	   ,5c                 p    | j                   5  | xj                  dz  c_        d d d        y # 1 sw Y   y xY wr  r  r   s     r   r   zCounter.__exit__;  r  r  N)r0   r1   r2   r   r   r  r   r   r4   r   r   r  r  -  s%    &  r   r  c                   X    e Zd Zd Zd Zd
dZd Zej                  fdZ	d Z
d ZddZy	)	TCPServerc                 T   || _         t        j                         | _        | j                  j	                          d| _        | j                   d   dk(  rt        j                  d      d | _        	 t        j                  t        j                  t        j                        | _        | j                  j                  t        j                  t        j                  d       | j                  j                  t        j                  t        j                  d       | j                  j                  t        t        j                  d       | j                  j!                  | j                          | j                  s	 t        j                  t        j&                  t        j                        | _        | j                  j                  t        j                  t        j                  d       | j                  j                  t        j                  t        j                  d       | j                  j!                  | j                          | j                  st        j                  t        j                  t        j                        | _        | j                  j                  t        j                  t        j                  d       | j                  j                  t        j                  t        j                  d       | j                  j!                  | j                          | j                  j)                         | _         | j                  j+                          t-               | _        y # t        j                  $ rI}| j                  r| j                  j#                          d | _        |j$                  dk(  r|Y d }~Ud }~ww xY w# t        j                  $ rI}| j                  r| j                  j#                          d | _        |j$                  dk(  r|Y d }~d }~ww xY w)NFr   	localhostzOBinding to 'localhost' is prohibited. Please use '::1' or '127.0.0.1' directly.r^   b   )r   r  Event_TCPServer__is_shut_downset_TCPServer__shutdown_requestr;   r<   AF_INET6r   r   
SOL_SOCKETSO_REUSEADDRSO_KEEPALIVEr	   IPV6_V6ONLYr   r   errnoAF_INETr   listenr  handler_counter)r   r   rF   s      r   r   zTCPServer.__init__B  s   'oo/!"'<<?k),,pqq	 !--9K9KLDKKK""6#4#4f6I6I1MKK""6#4#4f6I6I1MKK""<1C1CQGKKT\\* {{$mmFNNF<N<NO&&v'8'8&:M:MqQ&&v'8'8&:M:MqQ  . {{ --9K9KLDKKK""6#4#4f6I6I1MKK""6#4#4f6I6I1MKKT\\*{{..0&y= || 	{{!!#DKww"} 		 << ;;KK%%'"77b=G !	s2   1C=M, ;CO ,O?>OOP'>P""P'c                 4   | j                   5  	 | j                  ||       t        |       	 d d d        y # t        $ r(}|j                  t        j                  k(  s Y d }~Ad }~w | j                  ||       Y ZxY w# t        |       w xY w# 1 sw Y   y xY wr   )r+  handle_client_connectionOSErrorr(  EINVALhandle_errorr   )r   r   client_addressrF   s       r   connection_threadzTCPServer.connection_threadv  s    !! 	))--j.I Z(	) 	)  
 ww%,,. />!!*n=Z(	) 	)s<   B6B	A;A"A>"A;9A>>BBBc                    | j                   j                          	 | j                  st        j                  | j                  gg g |      \  }}}| j                  |v r| j                  j                         \  }}t        j                  d| j                  j                  d|d   d|d   d| j                  d   d| j                  d   d| j                  ||f      }d	|_        	 |j                          | j                  sd
| _        | j                   j%                          y # t        j                  $ r% | j!                  ||       |j#                          Y fw xY w# d
| _        | j                   j%                          w xY w)NzTCPConnectionHandler (z: r   :r^   z -> ))targetrU   TF)r   clearr"  rt   r;   acceptr   
BaseThreadr   r0   r   r2  daemonrZ   r  ThreadErrorr0  r   r!  )r   poll_intervalrE   w_e_r   r1  ts           r   serve_foreverzTCPServer.serve_forever  sN   !!#	&--"MM4;;-RO	2r;;!#151C1C1E.J"-- NN33*1-*1- LLO LLO  $55(.9
A  $AH+	# --, ',D###% %00 +))*nE"((*+ ',D###%s0   CE$ *D) :E$ )5E!E$  E!!E$ $#Fc                     d| _         | j                  j                          | j                  j	                          | j                          y )NT)r"  r   waitr;   r   handle_shutdownr   s    r   r{   zTCPServer.shutdown  s8    "&  "r   c                     t         r^t        t        j                               }t        d|       t        dt	        |      z  |       t        ||       t        d|       yy)zO
            Called when handle_client_connection raises an exception.
        z(----------------------------------------)filez&Error in processing of request from %sN)	tracebackr?   
format_excprintrn   )r   connection_r1  fpexcs        r   r0  zTCPServer.handle_error  sV     i**,-C)"%9D<PPWY[#B)"% r   c                     t         )z5
            Called after client connection.
        r  )r   rv   r1  s      r   r-  z"TCPServer.handle_client_connection  s
     "!r   c                      y)z3
            Called after server shutdown.
        Nr4   r   s    r   rC  zTCPServer.handle_shutdown  s    r   c                     t        j                          }	 t        j                          |z
  |k\  r,t        j                  d| j                  j                  z        | j                  j                  dk(  ry a)Nz%s service threads still aliver   )rB   r   Timeoutr+  r  )r   rS   rZ   s      r   wait_for_silencezTCPServer.wait_for_silence  sk    		yy{U"g- ((4((../  ##))Q. r   N)rK   )   )r0   r1   r2   r   r2  r@  r{   sysstderrr0  r-  rC  rP  r4   r   r   r  r  @  s4    2)h) &8 <?:: &"
	r   r  )#r(  r}   rt   r;   rR  r  rB   rF  typingr   OpenSSLr   !seleniumwire.thirdparty.mitmproxyr   r   +seleniumwire.thirdparty.mitmproxy.coretypesr   %seleniumwire.thirdparty.mitmproxy.netr   r   ri   r   r	   r   r6   rH   rw   r   r   r   r   r   r   r  r  r4   r   r   <module>rY     s     	   
      ? B 5OO  v~r2,) ,)^7Y 7<hPY hPV2**Z; ;| (v vr>.+ .b &J Jr   