Ë
    oj   ã                   ó:  — 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       ú>/usr/lib/python3/dist-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Ó-ˆØˆŒØ—Y‘Y §	¡	Ð*ˆ	Ü/°·±¸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   € Ø�	‰	�‰Ð7¸D¿G¹GˆÔDÜˆt�{‰{�‰Ó !Ò#Ø—‘—‘  Ð#ˆAØ�J‰J�qŒMØˆ�‰�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˜Ô"Ø�I‰I�N‰NÐBÀtÇwÁwˆNÔOØ�K‰K×!Ñ!×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Ð6¨4Ò6°2Ò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‰   € ð 	�	‰	�‰Ø)°·±¸aÑ0@ÀtÇ}Á}ÐUVÑGWð 	ô 	
ô ˜dŸm™m¨T¯]©]¸1Ñ-=¸t¿}¹}ÈQÑ?OÓPˆÜ˜BÔ 3°DÓ 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Ñ/?ÀdÇmÁmÐTUÑFVð 	ô 	
ð �>Š>Ø�K‰K×!Ñ!×'Ñ'¨¯©Ô7Ø!ˆDŒNØ�;‰;�?‰?˜1˜2ÑØ�J‰J�t—{‘{—‘ q rÐ*Ô+Øˆ�‰�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   € ð �;Š;Ø�K‰K×!Ñ!×'Ñ'¨Õ-à�NŠN˜dÑ"Ž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]   € ð �;Š;Ø�I‰I�N‰NÐBÀtÇwÁwˆNÔOØ�{‰{×"Ò"Ø×#Ñ#Ô%Ø�K‰K×!Ñ!×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Ñ€HˆfÜ&ØØ!Ø'Øô	ð 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)   € Ø�8Š8Ø�HŠH˜ÑŽHà�L‰L×Ñ˜tÕ$r   c                 ó`   — | j                   r"| j                   j                  «        d | _         y y r   )r   r=   r4   s     r   ÚconnectionLostz"SSHForwardingClient.connectionLost»   s%   € Ø�<Š<Ø�L‰L×'Ñ'Ô)ØˆD�Lð 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Ž   € ð 'Ñ€XˆxØ!Ñ€XˆxÜ�(œCÔ Ø—?‘? 7Ó+ˆÜ�(œCÔ Ø—?‘? 7Ó+ˆÜ�9‰9�XÓ¤§¡¨T°8Ó!<Ñ<€DÜ�9‰9�XÓ¤§¡¨T°8Ó!<Ñ<€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±   € ä—\‘\ $Ó'�N€HˆdÜ�(œEÔ"Ø—?‘? 7Ó+ˆÜ”6—=‘=  t¨B¨Q xÓ0°Ñ3Ó4€HÜ—\‘\ $ q r (Ó+�N€HˆdÜ�(œEÔ"Ø—?‘? 7Ó+ˆÜ”6—=‘=  t¨B¨Q xÓ0°Ñ3Ó4€HØ�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+   € ð �L€Tˆ4Ü�9‰9�T‹?œ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Ó#�J€Dˆ$Ü�$œÔØ�{‰{˜7Ó#ˆÜŒv�}‰}˜T 4¨¨ 8Ó,¨QÑ/Ó0€DØ�ˆ:Ð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   