
    Vh                      :   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y)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y)SSHListenForwardingFactoryc                 .    || _         || _        || _        y N)connhostportklass)self
connectionr   r   s       L/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/ssh/forwarding.py__init__z#SSHListenForwardingFactory.__init__   s    	 
    c                     | j                  | j                        }t        |      }||_        |j                  |j
                  f}t        | j                  |      }| j                  j                  ||       |S )N)r   )	r   r   SSHForwardingClientclienthostportpackOpen_direct_tcpipr   openChannel)r   addrr   r   	addrTuplechannelOpenDatas         r   buildProtocolz(SSHListenForwardingFactory.buildProtocol   sb    **$))*,$W-YY		*	/yI		g7r   N)__name__
__module____qualname__r   r    r   r   r
   r
      s    
r   r
   c                   *    e Zd Zd Zd Zd Zd Zd Zy)SSHListenForwardingChannelc                    | j                   j                  d| j                         t        | j                  j
                        dkD  r*| j                  j
                  dd  }| j                  |       d| j                  _        y )Nzopened forwarding channel {id}id   r   )_loginfor(   lenr   bufwrite)r   specificDatabs      r   channelOpenz&SSHListenForwardingChannel.channelOpen$   sZ    		7DGGDt{{!##AJJqMr   c                 $    | j                          y r   )closedr   reasons     r   
openFailedz%SSHListenForwardingChannel.openFailed+   s    r   c                 N    | j                   j                  j                  |       y r   )r   	transportr.   r   datas     r   dataReceivedz'SSHListenForwardingChannel.dataReceived.   s    ##D)r   c                 L    | j                   j                  j                          y r   )r   r8   loseConnectionr   s    r   eofReceivedz&SSHListenForwardingChannel.eofReceived1   s    ,,.r   c                     t        | d      rN| j                  j                  d| j                         | j                  j
                  j                          | `y y )Nr   z%closing local forwarding channel {id}r'   )hasattrr*   r+   r(   r   r8   r=   r>   s    r   r3   z!SSHListenForwardingChannel.closed4   sE    4"IINNBtwwNOKK!!00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y) SSHListenClientForwardingChannels   direct-tcpipNr    r!   r"   namer#   r   r   rC   rC   ;   s    Dr   rC   c                       e Zd ZdZy) SSHListenServerForwardingChannels   forwarded-tcpipNrD   r#   r   r   rG   rG   ?   s    Dr   rG   c                   8    e Zd ZdZeZd Zd Zd Zd Z	d Z
d Zy)	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                 t    t        j                  j                  | g|i | || _        d | _        d| _        y )Nr   )r   
SSHChannelr   r   r   	clientBuf)r   r   argskws       r   r   z$SSHConnectForwardingChannel.__init__]   s5    ##D64626 r   c                 Z   | j                   j                  d| j                  d   | j                  d          t        | j                  | j                  d   | j                  d         }t        |t        |             }|j                  | j                  | j                         || _
        y),
        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 	 	
 dmmT]]1-=t}}Q?OPB 3D 9:	t4$%!r   c                    || _         | j                  j                  d| j                  d   | j                  d          | j                  r6| j                   j
                  j                  | j                         d| _        | j                   j                  dd r(| j                  | j                   j                  dd        d| j                   _        y)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/?dmmTUFV 	 	
 >>KK!!''7!DN;;??12JJt{{qr*+r   c                     | j                   j                  d| j                  d   | j                  d   |       | j                          y)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   sE     			:q!q!	 	 	
 	r   c                     | j                   r&| j                   j                  j                  |       y| xj                  |z  c_        y)rP   N)r   r8   r.   rL   r9   s     r   r;   z(SSHConnectForwardingChannel.dataReceived   s1     ;;KK!!''-NNd"Nr   c                    | j                   rt| j                  j                  d| j                         | j                   j                  r| j                          | j                   j                  j                          | ` yy)rP   z%closed remote forwarding channel {id}r'   N)r   r*   r+   r(   r   r=   r8   r>   s    r   r3   z"SSHConnectForwardingChannel.closed   s]     ;;IINNBtwwNO{{""##%KK!!00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                 <    t        |      \  }}t        || ||      S )N)remoteWindowremoteMaxPacketavatar)unpackOpen_direct_tcpiprI   )r`   ra   r:   rb   remoteHPorigHPs         r   openConnectForwardingClientrf      s*    .t4Hf&!'	 r   c                       e Zd Zd Zd Zd Zy)r   c                      || _         d| _        y )N    )r   r-   )r   r   s     r   r   zSSHForwardingClient.__init__   s    r   c                 ~    | j                   r| xj                   |z  c_         y | j                  j                  |       y r   )r-   r   r.   r9   s     r   r;   z SSHForwardingClient.dataReceived   s)    88HHHLLt$r   c                 `    | j                   r"| j                   j                          d | _         y y r   )r   r=   r4   s     r   connectionLostz"SSHForwardingClient.connectionLost   s%    <<LL'')DL r   N)r    r!   r"   r   r;   rl   r#   r   r   r   r      s    % r   r   c                 T   | \  }}|\  }}t        |t              r|j                  d      }t        |t              r|j                  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+99XT8!<<D99XT8!<<D$;r   c                    t        j                  |       \  }}t        |t              r|j	                  d      }t        t        j                  d|dd       d         }t        j                  |dd       \  }}t        |t              r|j	                  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==tBQx034H\\$qr(+NHd(E"??7+6==tBQx034Hh(H!555r   c                 d    | \  }}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99T?V[[t444r   c                     t        j                  |       \  }}t        |t              r|j	                  d      }t        t        j                  d|d d       d         }||fS )Nrn   ro   r~   r   r   )r:   r   r   r   s       r   unpackGlobal_tcpip_forwardr      sV    d#JD$${{7#v}}T48,Q/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!1!1  !3!3 0'A 'A `'"4"4 `F (++  "* 1 
6 5 5r   