
    kj                     J   d Z ddlZddlZddlmZ ddlmZ ddlZddlm	Z	 ddlm
Z
 erddlZ G d d      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d deej&                  j(                        Z G d de      Zy)a&  
Commands make it possible for layers to communicate with the "outer world",
e.g. to perform IO or to ask the master.
A command is issued by a proxy layer and is then passed upwards to the proxy server, and from there
possibly to the master and addons.

The counterpart to commands are events.
    N)TYPE_CHECKING)Union)
Connection)Serverc                   2    e Zd ZU dZdZeedf   ed<   	 d Zy)Commandz%
    Base class for all commands
    Fzmitmproxy.proxy.layer.Layerblockingc                     | j                   j                         }|j                  dd        t        |       j                   d|dS )Nr	   ())__dict__copypoptype__name__)selfxs     a/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/proxy/commands.py__repr__zCommand.__repr__)   s@    MM 	j$t*%%&auA..    N)	r   
__module____qualname____doc__r	   r   bool__annotations__r    r   r   r   r      s)     <AHeD778@
/r   r   c                   (    e Zd ZU dZeed<   defdZy)RequestWakeupzI
    Request a `Wakeup` event after the specified amount of seconds.
    delayc                     || _         y N)r   )r   r   s     r   __init__zRequestWakeup.__init__6   s	    
r   N)r   r   r   r   floatr   r"   r   r   r   r   r   /   s     Le r   r   c                   (    e Zd ZU dZeed<   defdZy)ConnectionCommandz2
    Commands involving a specific connection
    
connectionc                     || _         y r!   )r&   )r   r&   s     r   r"   zConnectionCommand.__init__A   s	    $r   N)r   r   r   r   r   r   r"   r   r   r   r%   r%   :   s     %: %r   r%   c                   >     e Zd ZU dZeed<   dedef fdZd Z xZ	S )SendDataz$
    Send data to a remote peer
    datar&   c                 2    t         |   |       || _        y r!   )superr"   r*   )r   r&   r*   	__class__s      r   r"   zSendData.__init__L   s    $	r   c                     t        | j                        j                  dd      d   j                         }d| d| j                  dS )Nr      r   z	SendData(, r   )strr&   splitlowerr*   )r   targets     r   r   zSendData.__repr__P   sD    T__%++C3A6<<>6("TYYM33r   )
r   r   r   r   bytesr   r   r"   r   __classcell__r-   s   @r   r)   r)   E   s'     K: U 4r   r)   c                        e Zd ZU dZeed<   dZy)OpenConnectionz
    Open a new connection
    r&   TN)r   r   r   r   r   r   r	   r   r   r   r9   r9   U   s     Hr   r9   c                       e Zd ZdZy)CloseConnectionz
    Close a connection. If the client connection is closed,
    all other connections will ultimately be closed during cleanup.
    N)r   r   r   r   r   r   r   r;   r;   ^   s    r   r;   c                   8     e Zd ZU eed<   	 ddedef fdZ xZS )CloseTcpConnection
half_closer&   c                 2    t         |   |       || _        y r!   )r,   r"   r>   )r   r&   r>   r-   s      r   r"   zCloseTcpConnection.__init__m   s    $$r   )F)r   r   r   r   r   r   r"   r6   r7   s   @r   r=   r=   e   s&    %: %4 % %r   r=   c                   *     e Zd ZdZdZdZ fdZ xZS )	StartHookz
    Start an event hook in the mitmproxy core.
    This triggers a particular function (derived from the class name) in all addons.
     Tc                 P    | t         u rt        d      t        |   | g|i |S )Nz+StartHook may not be instantiated directly.)rA   	TypeErrorr,   __new__)clsargskwargsr-   s      r   rE   zStartHook.__new__{   s0    )IJJws4T4V44r   )r   r   r   r   namer	   rE   r6   r7   s   @r   rA   rA   r   s    
 DH5 5r   rA   c                   T    e Zd ZU dZeed<   eed<   ej                  fdedefdZ	d Z
y)Loga  
    Log a message.

    Layers could technically call `logging.log` directly, but the use of a command allows us to
    write more expressive playbook tests. Put differently, by using commands we can assert that
    a specific log message is a direct consequence of a particular I/O event.
    This could also be implemented with some more playbook magic in the future,
    but for now we keep the current approach as the fully sans-io one.
    messagelevelc                     t        |t              r:t        j                  dt        d       t        t        |j                               }|| _        || _	        y )Nz>commands.Log() now expects an integer log level, not a string.   )
stacklevel)

isinstancer1   warningswarnDeprecationWarninggetattrloggingupperrL   rM   )r   rL   rM   s      r   r"   zLog.__init__   sE    
 eS!MMP"
 GU[[]3E
r   c                 ~    d| j                   dt        j                  | j                        j	                          dS )NzLog(r0   r   )rL   rV   getLevelNamerM   r3   )r   s    r   r   zLog.__repr__   s6    dll%R(<(<TZZ(H(N(N(P'QQRSSr   N)r   r   r   r   r1   r   intrV   INFOr"   r   r   r   r   rK   rK      s9     LJ
 \\ Tr   rK   )r   rV   rR   typingr   r   mitmproxy.hooks	mitmproxymitmproxy.connectionr   r   mitmproxy.proxy.layerr   r   r%   r)   r9   r;   r=   hooksHookrA   rK   r   r   r   <module>rc      s          + ' / /0G % %4  4 & ' 
% 
%5-- 5T' Tr   