
    <g                     r    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	y)    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        |   di | 	 y )N )super__init__r   client_connserver_connchannelctx)self
mixin_args	__class__s     j/var/www/html/myenv/lib/python3.12/site-packages/seleniumwire/thirdparty/mitmproxy/server/protocol/base.pyr   z_LayerCodeCompletion.__init__   s    &:&    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r      s    	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                 4    || _         	 t        |   di | y)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__    s#    * 	
 	&:&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     txx&&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()
    c                 n    t         |           | j                  |      | _        | j	                          y N)r   r   (_ServerConnectionMixin__make_server_connr   *_ServerConnectionMixin__check_self_connect)r   server_addressr   s     r   r   zServerConnectionMixin.__init__d   s-    22>B!!#r   c                    | j                   j                  }|rg d}| j                  j                  j                  r/|j                  | j                  j                  j                         |d   | j                  j                  j                  k(  xr |d   |v }|r-t        j                  dj                  t        |                  yy)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{{""..&&t{{':':'F'FG 
dkk11=== .
o-   22AAGWAV   r   c                    | j                   j                  j                  r]| 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38K8K8a8aeg8g//!5!5!=!=a!@! DdL L //!4!4!J!JA N##88 r   c                     | j                   j                         r| j                          | j                  dj	                  |d   |d         d       || j                   _        | j                          y)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OQXY#* !!#r   c                 r   | j                  ddt        | j                  j                        g       | j                  j                  }| j                  j	                          | j                  j                          | j                  j                  d| j                         | j                  |      | _        y)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    
 	#WtD4D4D4L4L/M.NO""**! ,d.>.>?227;r   c           	          | j                   j                  st        j                  d      	 | j                   j	                          | j                  ddt        | j                   j                        g       | j                  j                  d| j                          y# t        j                  $ r}| j                  j                  j                  r| j                  t        |      d       n?| j                  t        |      d       | j                  t        j                         d       t        j                  dj                  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   s    ''../cdd	$$&HH_gT5E5E5M5M0N/OPLL_d.>.>?&& 
	{{""==a'*a'*--/9..4;;))112CF 
	s   A1B E=2CE88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      s7     8 E ;M M$3'  3'lk kr   