
    Yf                      8   d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	m
Z
  G d dej                  Z G d d	ej                  Z G d
 de          Z G d de          Z G d dej                  Zd Z G d dej                  Zd ZeZd ZeZd Zd ZdS )z
This module contains the implementation of the TCP forwarding, which allows
clients and servers to forward arbitrary TCP data across the connection.

Maintainer: Paul Swartz
    N)channelcommon)protocolreactor)HostnameEndpointconnectProtocolc                       e Zd Zd Zd ZdS )SSHListenForwardingFactoryc                 0    || _         || _        || _        d S N)connhostportklass)self
connectionr   r   s       N/var/www/html/env/lib/python3.11/site-packages/twisted/conch/ssh/forwarding.py__init__z#SSHListenForwardingFactory.__init__   s    	 


    c                     |                      | j                  }t          |          }||_        |j        |j        f}t          | j        |          }| j                            ||           |S )N)r   )	r   r   SSHForwardingClientclienthostportpackOpen_direct_tcpipr   openChannel)r   addrr   r   	addrTuplechannelOpenDatas         r   buildProtocolz(SSHListenForwardingFactory.buildProtocol   sg    **$)*,,$W--Y	*	/yII	g777r   N)__name__
__module____qualname__r   r    r   r   r
   r
      s2          
    r   r
   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )SSHListenForwardingChannelc                     | j                             d| j                   t          | j        j                  dk    r)| j        j        dd          }|                     |           d| j        _        d S )Nzopened forwarding channel {id}id   r   )_loginfor(   lenr   bufwrite)r   specificDatabs      r   channelOpenz&SSHListenForwardingChannel.channelOpen$   sb    	7DGDDDt{!###AJJqMMMr   c                 .    |                                   d S r   )closedr   reasons     r   
openFailedz%SSHListenForwardingChannel.openFailed+   s    r   c                 D    | j         j                            |           d S r   )r   	transportr.   r   datas     r   dataReceivedz'SSHListenForwardingChannel.dataReceived.   s!    ##D)))))r   c                 B    | j         j                                         d S r   )r   r8   loseConnectionr   s    r   eofReceivedz&SSHListenForwardingChannel.eofReceived1   s    ,,.....r   c                     t          | d          rC| j                            d| j                   | j        j                                         | `d S d S )Nr   z%closing local forwarding channel {id}r'   )hasattrr*   r+   r(   r   r8   r=   r>   s    r   r3   z!SSHListenForwardingChannel.closed4   sY    4"" 	INNBtwNOOOK!00222	 	r   N)r    r!   r"   r1   r6   r;   r?   r3   r#   r   r   r%   r%   #   s_            * * */ / /    r   r%   c                       e Zd ZdZdS ) SSHListenClientForwardingChannels   direct-tcpipNr    r!   r"   namer#   r   r   rC   rC   ;   s        DDDr   rC   c                       e Zd ZdZdS ) SSHListenServerForwardingChannels   forwarded-tcpipNrD   r#   r   r   rG   rG   ?   s        DDDr   rG   c                   :    e Zd ZdZeZd Zd Zd Zd Z	d Z
d ZdS )	SSHConnectForwardingChannela  
    Channel used for handling server side forwarding request.
    It acts as a client for the remote forwarding destination.

    @ivar hostport: C{(host, port)} requested by client as forwarding
        destination.
    @type hostport: L{tuple} or a C{sequence}

    @ivar client: Protocol connected to the forwarding destination.
    @type client: L{protocol.Protocol}

    @ivar clientBuf: Data received while forwarding channel is not yet
        connected.
    @type clientBuf: L{bytes}

    @var  _reactor: Reactor used for TCP connections.
    @type _reactor: A reactor.

    @ivar _channelOpenDeferred: Deferred used in testing to check the
        result of C{channelOpen}.
    @type _channelOpenDeferred: L{twisted.internet.defer.Deferred}
    c                 d    t          j        j        | g|R i | || _        d | _        d| _        d S )Nr   )r   
SSHChannelr   r   r   	clientBuf)r   r   argskws       r   r   z$SSHConnectForwardingChannel.__init__]   s?    #D646662666 r   c                 L   | j                             d| j        d         | j        d                    t          | j        | j        d         | j        d                   }t          |t          |                     }|                    | j        | j	                   || _
        dS ),
        See: L{channel.SSHChannel}
        zconnecting to {host}:{port}r   r)   r   r   N)r*   r+   r   r   _reactorr   r   addCallbacks
_setClient_close_channelOpenDeferred)r   r/   epds       r   r1   z'SSHConnectForwardingChannel.channelOpenc   s     		)a0@t}UVGW 	 	
 	
 	
 dmT]1-=t}Q?OPPB 3D 9 9::	t444$%!!!r   c                 l   || _         | j                            d| j        d         | j        d                    | j        r+| j         j                            | j                   d| _        | j         j        dd         r'|                     | j         j        dd                    d| j         _        dS )z
        Called when the connection was established to the forwarding
        destination.

        @param client: Client protocol connected to the forwarding destination.
        @type  client: L{protocol.Protocol}
        zconnected to {host}:{port}r   r)   rQ   Nr   )r   r*   r+   r   rL   r8   r.   r-   )r   r   s     r   rT   z&SSHConnectForwardingChannel._setCliento   s     	(t}Q/?dmTUFV 	 	
 	
 	
 > 	"K!''777!DN;?122 	,JJt{qrr*+++r   c                     | j                             d| j        d         | j        d         |           |                                  dS )z
        Called when failed to connect to the forwarding destination.

        @param reason: Reason why connection failed.
        @type  reason: L{twisted.python.failure.Failure}
        z,failed to connect to {host}:{port}: {reason}r   r)   )r   r   r5   N)r*   errorr   r=   r4   s     r   rU   z"SSHConnectForwardingChannel._close   sU     		:q!q!	 	 	
 	
 	
 	r   c                 v    | j         r!| j         j                            |           dS | xj        |z  c_        dS )rP   N)r   r8   r.   rL   r9   s     r   r;   z(SSHConnectForwardingChannel.dataReceived   sA     ; 	#K!''-----NNd"NNNNr   c                     | j         rc| j                            d| j                   | j         j        r|                                  | j         j                                         | ` dS dS )rP   z%closed remote forwarding channel {id}r'   N)r   r*   r+   r(   r   r=   r8   r>   s    r   r3   z"SSHConnectForwardingChannel.closed   sr     ; 	INNBtwNOOO{" &##%%%K!00222	 	r   N)r    r!   r"   __doc__r   rR   r   r1   rT   rU   r;   r3   r#   r   r   rI   rI   C   s         . H  
& 
& 
&  &  # # #	 	 	 	 	r   rI   c                 L    t          |          \  }}t          || ||          S )N)remoteWindowremoteMaxPacketavatar)unpackOpen_direct_tcpiprI   )r`   ra   r:   rb   remoteHPorigHPs         r   openConnectForwardingClientrf      s7    .t44Hf&!'	   r   c                        e Zd Zd Zd Zd ZdS )r   c                 "    || _         d| _        d S )N    )r   r-   )r   r   s     r   r   zSSHForwardingClient.__init__   s    r   c                 l    | j         r| xj         |z  c_         d S | j                            |           d S r   )r-   r   r.   r9   s     r   r;   z SSHForwardingClient.dataReceived   s<    8 	%HHHHHHLt$$$$$r   c                 X    | j         r"| j                                          d | _         d S d S r   )r   r=   r4   s     r   connectionLostz"SSHForwardingClient.connectionLost   s5    < 	 L'')))DLLL	  	 r   N)r    r!   r"   r   r;   rl   r#   r   r   r   r      sA          % % %         r   r   c                 p   | \  }}|\  }}t          |t                    r|                    d          }t          |t                    r|                    d          }t          j        |          t          j        d|          z   }t          j        |          t          j        d|          z   }||z   S )a  
    Pack the data suitable for sending in a CHANNEL_OPEN packet.

    @type destination: L{tuple}
    @param destination: A tuple of the (host, port) of the destination host.

    @type source: L{tuple}
    @param source: A tuple of the (host, port) of the source host.
    utf-8>L)
isinstancestrencoder   NSstructpack)destinationsourceconnHostconnPortorigHostorigPortr   origs           r   r   r      s     'Xx!Xx(C   ,??7++(C   ,??7++9XT8!<!<<D9XT8!<!<<D$;r   c                    t          j        |           \  }}t          |t                    r|                    d          }t          t          j        d|dd                   d                   }t          j        |dd                   \  }}t          |t                    r|                    d          }t          t          j        d|dd                   d                   }||f||ffS )z#Unpack the data to a usable format.rn   ro   N   r   r   getNSrp   bytesdecodeintrt   unpack)r:   rx   restry   rz   r{   s         r   rc   rc      s    \$''NHd(E"" ,??7++6=tBQBx00344H\$qrr(++NHd(E"" ,??7++6=tBQBx00344Hh(H!555r   c                 `    | \  }}t          j        |          t          j        d|          z   S )zv
    Pack the data for tcpip forwarding.

    @param peer: A tuple of the (host, port) .
    @type peer: L{tuple}
    ro   )r   rs   rt   ru   )peerr   r   s      r   packGlobal_tcpip_forwardr      s,     LT49T??V[t4444r   c                     t          j        |           \  }}t          |t                    r|                    d          }t          t          j        d|d d                   d                   }||fS )Nrn   ro   r~   r   r   )r:   r   r   r   s       r   unpackGlobal_tcpip_forwardr      sh    d##JD$$ ${{7##v}T48,,Q/00D:r   )r^   rt   twisted.conch.sshr   r   twisted.internetr   r   twisted.internet.endpointsr   r   Factoryr
   rK   r%   rC   rG   rI   rf   Protocolr   r   packOpen_forwarded_tcpiprc   unpackOpen_forwarded_tcpipr   r   r#   r   r   <module>r      s     - - - - - - - - . . . . . . . . H H H H H H H H    !1        !3   0    'A       'A   ` ` ` ` `'"4 ` ` `F           (+      "  * 1 
6 
6 
6 5 5 5 5   r   