o b! @sdZddlZddlmZmZddlmZmZddlm Z m Z Gdddej Z Gdd d ej ZGd d d eZGd d d eZGdddej ZddZGdddejZddZeZddZeZddZddZdS)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)channelcommon)protocolreactor)HostnameEndpointconnectProtocolc@seZdZddZddZdS)SSHListenForwardingFactorycCs||_||_||_dSN)connhostportklass)self connectionr r r>/usr/lib/python3/dist-packages/twisted/conch/ssh/forwarding.py__init__s z#SSHListenForwardingFactory.__init__cCsF|j|jd}t|}||_|j|jf}t|j|}|j|||S)N)r ) r r SSHForwardingClientclienthostportpackOpen_direct_tcpipr openChannel)r addrrr addrTuplechannelOpenDatarrr buildProtocols  z(SSHListenForwardingFactory.buildProtocolN)__name__ __module__ __qualname__rrrrrrrs rc@s4eZdZddZddZddZddZd d Zd S) SSHListenForwardingChannelcCsH|jjd|jdt|jjdkr|jjdd}||d|j_dS)Nzopened forwarding channel {id}id)_loginfor!lenrbufwrite)r specificDatabrrr channelOpen$s   z&SSHListenForwardingChannel.channelOpencCs |dSr )closedr reasonrrr openFailed+s z%SSHListenForwardingChannel.openFailedcCs|jj|dSr )r transportr(r datarrr dataReceived.sz'SSHListenForwardingChannel.dataReceivedcCs|jjdSr )rr0loseConnectionr rrr eofReceived1sz&SSHListenForwardingChannel.eofReceivedcCs4t|dr|jjd|jd|jj|`dSdS)Nrz%closing local forwarding channel {id}r )hasattrr$r%r!rr0r4r5rrrr,4s  z!SSHListenForwardingChannel.closedN)rrrr+r/r3r6r,rrrrr#s  rc@eZdZdZdS) SSHListenClientForwardingChannels direct-tcpipNrrrnamerrrrr9;r9c@r8) SSHListenServerForwardingChannelsforwarded-tcpipNr:rrrrr=@r<r=c@sDeZdZdZeZddZddZddZdd Z d d Z d d Z dS)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} cOs0tjj|g|Ri|||_d|_d|_dS)Nr#)r SSHChannelrr r clientBuf)r r argskwrrrr_s z$SSHConnectForwardingChannel.__init__cCs`|jjd|jd|jddt|j|jd|jd}t|t|}||j|j ||_ dS), See: L{channel.SSHChannel} zconnecting to {host}:{port}rr"rrN) r$r%r r_reactorrr addCallbacks _setClient_close_channelOpenDeferred)r r)epdrrrr+es z'SSHConnectForwardingChannel.channelOpencCsr||_|jjd|jd|jdd|jr |jj|jd|_|jjddr3||jjddd|j_dS)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} zconnected to {host}:{port}rr"rDNr#)rr$r%r r@r0r(r')r rrrrrGqs z&SSHConnectForwardingChannel._setClientcCs,|jjd|jd|jd|d|dS)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}rr")rrr.N)r$errorr r4r-rrrrHs z"SSHConnectForwardingChannel._closecCs*|jr |jj|dS|j|7_dS)rCN)rr0r(r@r1rrrr3sz(SSHConnectForwardingChannel.dataReceivedcCs@|jr|jjd|jd|jjr||jj|`dSdS)rCz%closed remote forwarding channel {id}r N)rr$r%r!rr4r0r5rrrr,s z"SSHConnectForwardingChannel.closedN) rrr__doc__rrErr+rGrHr3r,rrrrr>Es  r>cCst|\}}t||||dS)N) remoteWindowremoteMaxPacketavatar)unpackOpen_direct_tcpipr>)rNrOr2rPremoteHPorigHPrrropenConnectForwardingClients rTc@s$eZdZddZddZddZdS)rcCs||_d|_dS)N)rr')r rrrrrs zSSHForwardingClient.__init__cCs(|jr |j|7_dS|j|dSr )r'rr(r1rrrr3sz SSHForwardingClient.dataReceivedcCs|jr |jd|_dSdSr )rr4r-rrrconnectionLosts  z"SSHForwardingClient.connectionLostN)rrrrr3rVrrrrrs rcCsl|\}}|\}}t|tr|d}t|tr|d}t|td|}t|td|}||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) isinstancestrencoderNSstructpack) destinationsourceconnHostconnPortorigHostorigPortr origrrrrs     rcCst|\}}t|tr|d}ttd|ddd}t|dd\}}t|tr4|d}ttd|ddd}||f||ffS)z#Unpack the data to a usable format.rWrXNrrgetNSrYbytesdecodeintr]unpack)r2rarestrbrcrdrrrrQs    rQcCs|\}}t|td|S)zv Pack the data for tcpip forwarding. @param peer: A tuple of the (host, port) . @type peer: L{tuple} rX)rr\r]r^)peerrrrrrpackGlobal_tcpip_forwardsrocCsFt|\}}t|tr|d}ttd|ddd}||fS)NrWrXrfrrg)r2rrmrrrrunpackGlobal_tcpip_forwards   rp)rMr]twisted.conch.sshrrtwisted.internetrrtwisted.internet.endpointsrrFactoryrr?rr9r=r>rTProtocolrrpackOpen_forwarded_tcpiprQunpackOpen_forwarded_tcpiprorprrrrs&c