
    Eg                         d dl Z d dlmZ d dlmZmZ d dlmZ  G d d          Z G d de          Z G d	 d
          Z	dS )    N)
controller)connections
exceptions)configc                   "     e Zd ZdZ fdZ xZS )_LayerCodeCompletionz`
    Dummy class that provides type hinting in PyCharm, which simplifies development a lot.
    c                 <     t                      j        di | 	 d S )NT )super__init__r   client_connserver_connchannelctx)self
mixin_args	__class__s     k/var/www/sysmax/venv/lib/python3.11/site-packages/seleniumwire/thirdparty/mitmproxy/server/protocol/base.pyr   z_LayerCodeCompletion.__init__   s*    &&:&&&	F    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r      sK         	M 	M 	M 	M 	M 	M 	M 	M 	Mr   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )Layerz^
    Base class for all layers. All other protocol layers should inherit from this class.
    c                 J    || _         	  t                      j        di | dS )a?  
        Each layer usually passes itself to its child layers as a context. Properties of the
        context are transparently mapped to the layer, so that the following works:

        .. code-block:: python

            root_layer = Layer(None)
            root_layer.client_conn = 42
            sub_layer = Layer(root_layer)
            print(sub_layer.client_conn) # 42

        The root layer is passed a :py:class:`seleniumwire.thirdparty.mitmproxy.server.RootContext` object,
        which provides access to
        :py:attr:`.client_conn <seleniumwire.thirdparty.mitmproxy.server.RootContext.client_conn>`,
        :py:attr:`.next_layer <seleniumwire.thirdparty.mitmproxy.server.RootContext.next_layer>`
        and other basic attributes.

        Args:
            ctx: The (read-only) parent layer / context.
        Nr
   )r   r   r   )r   r   r   r   s      r   r   zLayer.__init__    s6    * 	
 	&&:&&&&&r   c                     t                      )a  Logic of the layer.

        Returns:
            Once the protocol has finished without exceptions.

        Raises:
            ~seleniumwire.thirdparty.mitmproxy.exceptions.ProtocolException: if an exception occurs.
            No other exceptions must be raised.
        )NotImplementedError)r   s    r   __call__zLayer.__call__=   s     "###r   c                 ,    t          | j        |          S )z[
        Attributes not present on the current layer are looked up on the context.
        )getattrr   )r   names     r   __getattr__zLayer.__getattr__I   s     tx&&&r   )r   r   r   r   r   r!   r%   r   r   s   @r   r   r      s`         ' ' ' ' ':
$ 
$ 
$' ' ' ' ' ' 'r   r   c                   B     e Zd ZdZd	 fd	Zd Zd Zd Zd Zd Z	 xZ
S )
ServerConnectionMixinaI  
    Mixin that provides a layer with the capabilities to manage a server connection.
    The server address can be passed in the constructor or set by calling :py:meth:`set_server`.
    Subclasses are responsible for calling :py:meth:`disconnect` before returning.

    Recommended Usage:

    .. code-block:: python

        class MyLayer(Layer, ServerConnectionMixin):
            def __call__(self):
                try:
                    # Do something.
                finally:
                    if self.server_conn.connected():
                        self.disconnect()
    Nc                     t                                                       |                     |          | _        |                                  d S N)r   r   (_ServerConnectionMixin__make_server_connr   *_ServerConnectionMixin__check_self_connect)r   server_addressr   s     r   r   zServerConnectionMixin.__init__d   sG    22>BB!!#####r   c                 N   | j         j        }|rg d}| j        j        j        r$|                    | j        j        j                   |d         | j        j        j        k    o	|d         |v }|r6t          j        d	                    t          |                              dS dS )z
        We try to protect the mitmproxy from _accidentally_ connecting to itself,
        e.g. because of a failed transparent lookup or an invalid configuration.
        )	localhostz	127.0.0.1z::1   r   zFInvalid server address: {}
The mitmproxy shall not connect to itself.N)r   addressr   optionslisten_hostappendlisten_portr   ProtocolExceptionformatrepr)r   r0   forbidden_hostsself_connects       r   __check_self_connectz*ServerConnectionMixin.__check_self_connectk   s    
 "* 	???O{". H&&t{':'FGGG 
dk1== .
o-    2AAGWAVAV  	 	 r   c                    | j         j        j        rB| j         j        j        dk    r-t	          j        || j        j        j        d         dfd          S t	          j        || j         j        j        df| j         j        j                  S )N r   T)	r   r1   spoof_source_addressupstream_bind_addressr   ServerConnectionr   r   r0   )r   r,   s     r   __make_server_connz(ServerConnectionMixin.__make_server_conn   s    ;3 	8K8aeg8g8g/!5!=a!@! DdL L L /!4!JA N#8  r   c                    | j                                         r|                                  |                     d                    |d         |d                   d           || j         _        |                                  dS )zc
        Sets a new server address. If there is an existing connection, it will be closed.
        zSet new server address: {}:{}r   r/   debugN)r   	connected
disconnectlogr6   r0   r+   r   r0   s     r   
set_serverz ServerConnectionMixin.set_server   s|     %%'' 	OO077
GAJOOQXYYY#* !!#####r   c                 T   |                      ddt          | j        j                  g           | j        j        }| j                                         | j                                         | j                            d| j                   |                     |          | _        dS )z
        Deletes (and closes) an existing server connection.
        Must not be called if there is no existing connection.
        serverdisconnectrB   N)	rE   r7   r   r0   finishcloser   tellr*   rF   s     r   rD   z ServerConnectionMixin.disconnect   s    
 	#WtD4D4L/M/M.NOOO"*!!!   ,d.>???227;;r   c           	         | j         j        st          j        d          	 | j                                          |                     ddt          | j         j                  g           | j                            d| j                    dS # t          j	        $ r}| j
        j        j        r$|                     t          |          d           nJ|                     t          |          d           |                     t          j                    d           t          j        d                    t          | j         j                  t!          |                              d}~ww xY w)a  
        Establishes a server connection.
        Must not be called if there is an existing connection.

        Raises:
            ~seleniumwire.thirdparty.mitmproxy.exceptions.ProtocolException:
            if the connection could not be established.
        z2Cannot connect to server, no server address given.serverconnectrB   errorz"Server connection to {} failed: {}N)r   r0   r   r5   connectrE   r7   r   askTcpExceptionr   r1   suppress_connection_errors	traceback
format_excr6   str)r   es     r   rP   zServerConnectionMixin.connect   sN    ' 	e./cddd	$$&&&HH_gT5E5M0N0N/OPPPL_d.>?????& 
	 
	 
	{"= :a'****a'***-//999.4;;)122CFF   
	s   A(B E+CE&&E+r)   )r   r   r   r   r   r+   r*   rG   rD   rP   r   r   s   @r   r'   r'   P   s         $$ $ $ $ $ $  ,  $ $ $< < <      r   r'   )
rT   !seleniumwire.thirdparty.mitmproxyr   r   r   (seleniumwire.thirdparty.mitmproxy.serverr   r   r   r'   r
   r   r   <module>rZ      s        8 8 8 8 8 8 E E E E E E E E ; ; ; ; ; ;M M M M M M M M$3' 3' 3' 3' 3'  3' 3' 3'lk k k k k k k k k kr   