Ϫf :dZddlZddlmZmZddlmZmZddlm Z m Z GddejZ Gdd ejZGd d eZGd d eZGddejZdZGddej&ZdZeZdZeZdZdZy)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)HostnameEndpointconnectProtocolceZdZdZdZy)SSHListenForwardingFactoryc.||_||_||_yN)connhostportklass)self connectionrrs >/usr/lib/python3/dist-packages/twisted/conch/ssh/forwarding.py__init__z#SSHListenForwardingFactory.__init__s    c|j|j}t|}||_|j|j f}t |j|}|jj|||S)N)r ) rr SSHForwardingClientclienthostportpackOpen_direct_tcpipr openChannel)raddrrr addrTuplechannelOpenDatas r buildProtocolz(SSHListenForwardingFactory.buildProtocolsb**$))*,$W-YY * / yI g7 rN)__name__ __module__ __qualname__rrrrr r s  rr c*eZdZdZdZdZdZdZy)SSHListenForwardingChannelc |jjd|jt|jj dkDr*|jj dd}|j |d|j_y)Nzopened forwarding channel {id}idr)_loginfor(lenrbufwrite)r specificDatabs r channelOpenz&SSHListenForwardingChannel.channelOpen$sZ 7DGGD t{{ ! # #A JJqM rc$|jyr )closedrreasons r openFailedz%SSHListenForwardingChannel.openFailed+s  rcN|jjj|yr )r transportr.rdatas r dataReceivedz'SSHListenForwardingChannel.dataReceived.s ##D)rcL|jjjyr )rr8loseConnectionrs r eofReceivedz&SSHListenForwardingChannel.eofReceived1s ,,.rct|drN|jjd|j|jj j |`yy)Nrz%closing local forwarding channel {id}r')hasattrr*r+r(rr8r=r>s rr3z!SSHListenForwardingChannel.closed4sE 4 " IINNBtwwN O KK ! ! 0 0 2  #rN)r r!r"r1r6r;r?r3r#rrr%r%#s*/rr%ceZdZdZy) SSHListenClientForwardingChannels direct-tcpipNr r!r"namer#rrrCrC;s DrrCceZdZdZy) SSHListenServerForwardingChannelsforwarded-tcpipNrDr#rrrGrG?s DrrGc8eZdZdZeZdZdZdZdZ dZ dZ 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} cttjj|g|i|||_d|_d|_y)Nr)r SSHChannelrrr clientBuf)rrargskws rrz$SSHConnectForwardingChannel.__init__]s5##D64626   rcZ|jjd|jd|jdt|j|jd|jd}t |t |}|j|j|j||_ y), See: L{channel.SSHChannel} zconnecting to {host}:{port}rr)rrN) r*r+rr_reactorrr addCallbacks _setClient_close_channelOpenDeferred)rr/epds rr1z'SSHConnectForwardingChannel.channelOpencs  ) a0@t}}UVGW  dmmT]]1-=t}}Q?O P B 3D 9 : t 4$%!rc||_|jjd|jd|jd|jr6|jj j |jd|_|jjddr(|j |jjddd|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} zconnected to {host}:{port}rr)rQNr)rr*r+rrLr8r.r-)rrs rrTz&SSHConnectForwardingChannel._setClientos   (t}}Q/?dmmTUFV   >> KK ! ! ' ' 7!DN ;;??12  JJt{{qr* + rc|jjd|jd|jd||jy)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))rrr5N)r*errorrr=r4s rrUz"SSHConnectForwardingChannel._closesE  :q!q!  rc|jr&|jjj|y|xj|z c_y)rPN)rr8r.rLr9s rr;z(SSHConnectForwardingChannel.dataReceiveds1 ;; KK ! ! ' ' - NNd "Nrc|jrt|jjd|j|jjr|j |jj j |`yy)rPz%closed remote forwarding channel {id}r'N)rr*r+r(rr=r8r>s rr3z"SSHConnectForwardingChannel.closeds] ;; IINNBtwwN O{{""##% KK ! ! 0 0 2 rN) r r!r"__doc__rrRrr1rTrUr;r3r#rrrIrICs,.H && # rrIc<t|\}}t||||S)N) remoteWindowremoteMaxPacketavatar)unpackOpen_direct_tcpiprI)r`rar:rbremoteHPorigHPs ropenConnectForwardingClientrfs*.t4Hf &!'  rceZdZdZdZdZy)rc ||_d|_y)N)rr-)rrs rrzSSHForwardingClient.__init__s rc~|jr|xj|z c_y|jj|yr )r-rr.r9s rr;z SSHForwardingClient.dataReceiveds) 88 HH H LL  t $rc`|jr"|jjd|_yyr )rr=r4s rconnectionLostz"SSHForwardingClient.connectionLosts% << LL ' ' )DL rN)r r!r"rr;rlr#rrrrs%  rrcT|\}}|\}}t|tr|jd}t|tr|jd}tj|t j d|z}tj|t j d|z}||zS)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 origs rrrs'Xx!Xx(C ??7+(C ??7+ 99X T8!< rs-.H !1!1  !3!30'A'A`'"4"4`F (++ "*1 655r