§
    Ètºf   ã                   ó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       ú[/var/www/surfInsights/venv3-11/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   € ØŒ	�ŠÐ7¸D¼GˆÑDÔDÐDÝˆtŒ{ŒÑÔ !Ò#Ð#Ø””   Ô#ˆAØ�JŠJ�q‰MŒMˆMØˆŒŒˆˆ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˜Ñ"Ô"ð 	ØŒI�NŠNÐBÀtÄwˆNÑ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   € € € € € Ø€D€D€Dr   rC   c                   ó   — e Zd ZdZdS )Ú SSHListenServerForwardingChannels   forwarded-tcpipNrD   r#   r   r   rG   rG   ?   s   € € € € € Ø€D€D€Dr   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Ð6¨4Ð6Ð6Ð6°2Ð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˜   € ð 	Œ	�ŠØ)°´¸aÔ0@ÀtÄ}ÐUVÔGWð 	ñ 	
ô 	
ð 	
õ ˜dœm¨T¬]¸1Ô-=¸t¼}ÈQÔ?OÑPÔPˆÝ˜BÕ 3°DÑ 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Ô/?ÀdÄmÐTUÔFVð 	ñ 	
ô 	
ð 	
ð Œ>ð 	"ØŒKÔ!×'Ò'¨¬Ñ7Ô7Ð7Ø!ˆDŒNØŒ;Œ?˜1˜2˜2Ôð 	,Ø�JŠJ�t”{” q r rÔ*Ñ+Ô+Ð+ØˆŒŒˆˆ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Ô!×'Ò'¨Ñ-Ô-Ð-Ð-Ð-àˆNŒN˜dÑ"ˆNŒNˆNˆNr   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   € ð Œ;ð 	ØŒI�NŠNÐBÀtÄwˆNÑ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Ñ€HˆfÝ&ØØ!Ø'Øð	ñ ô ð 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ð 	%ØˆHŒH˜ÑˆHŒHˆHˆHàŒL×Ò˜tÑ$Ô$Ð$Ð$Ð$r   c                 óX   — | j         r"| j                              ¦   «          d | _         d S d S r   )r   r=   r4   s     r   ÚconnectionLostz"SSHForwardingClient.connectionLost»   s5   € ØŒ<ð 	 ØŒL×'Ò'Ñ)Ô)Ð)ØˆDŒLˆLˆLð	 ð 	 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¨   € ð 'Ñ€XˆxØ!Ñ€XˆxÝ�(�CÑ Ô ð ,Ø—?’? 7Ñ+Ô+ˆÝ�(�CÑ Ô ð ,Ø—?’? 7Ñ+Ô+ˆÝŒ9�XÑÔ¥¤¨T°8Ñ!<Ô!<Ñ<€DÝŒ9�XÑÔ¥¤¨T°8Ñ!<Ô!<Ñ<€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×   € å”\ $Ñ'Ô'�N€HˆdÝ�(�EÑ"Ô"ð ,Ø—?’? 7Ñ+Ô+ˆÝ•6”=  t¨B¨Q¨B¤xÑ0Ô0°Ô3Ñ4Ô4€HÝ”\ $ q r r¤(Ñ+Ô+�N€HˆdÝ�(�EÑ"Ô"ð ,Ø—?’? 7Ñ+Ô+ˆÝ•6”=  t¨B¨Q¨B¤xÑ0Ô0°Ô3Ñ4Ô4€HØ�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,   € ð �L€Tˆ4ÝŒ9�T‰?Œ?�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Ñ#Ô#�J€Dˆ$Ý�$�ÑÔð $Ø�{Š{˜7Ñ#Ô#ˆÝ�vŒ}˜T 4¨¨¨¤8Ñ,Ô,¨QÔ/Ñ0Ô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Ð HÐ HÐ HÐ HÐ HÐ HÐ Hðð ð ð ð  Ô!1ñ ô ð ð ð ð ð ð  Ô!3ñ ô ð ð0ð ð ð ð Ð'Añ ô ð ðð ð ð ð Ð'Añ ô ð ð`ð `ð `ð `ð ` 'Ô"4ñ `ô `ð `ðFð ð ð ð  ð  ð  ð  ˜(Ô+ñ  ô  ð  ð"ð ð ð* 1Ð ð
6ð 
6ð 
6ð 5Ð ð5ð 5ð 5ðð ð ðð r   