o b&@sTdZddlmZddlmZddlmZddlmZeej GdddejZ dS) z The parent class for all the SSH Channels. Currently implemented channels are session, direct-tcp, and forwarded-tcp. Maintainer: Paul Swartz ) implementer) interfaces)Logger)logc@seZdZUdZeZdZeed<       d0ddZ de fdd Z defd d Z d d Z ddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdS)1 SSHChannela A class that represents a multiplexed channel over an SSH connection. The channel has a local window which is the maximum amount of data it will receive, and a remote which is the maximum amount of data the remote side will accept. There is also a maximum packet size for any individual data packet going each way. @ivar name: the name of the channel. @type name: L{bytes} @ivar localWindowSize: the maximum size of the local window in bytes. @type localWindowSize: L{int} @ivar localWindowLeft: how many bytes are left in the local window. @type localWindowLeft: L{int} @ivar localMaxPacket: the maximum size of packet we will accept in bytes. @type localMaxPacket: L{int} @ivar remoteWindowLeft: how many bytes are left in the remote window. @type remoteWindowLeft: L{int} @ivar remoteMaxPacket: the maximum size of a packet the remote side will accept in bytes. @type remoteMaxPacket: L{int} @ivar conn: the connection this channel is multiplexed through. @type conn: L{SSHConnection} @ivar data: any data to send to the other side when the channel is requested. @type data: L{bytes} @ivar avatar: an avatar for the logged-in user (if a server channel) @ivar localClosed: True if we aren't accepting more data. @type localClosed: L{bool} @ivar remoteClosed: True if the other side isn't accepting more data. @type remoteClosed: L{bool} NnamercCsn|pd|_|j|_|p d|_||_||_d|_||_||_||_d|_ d|_ g|_ d|_ d|_ d|_d|_dS)Niir)localWindowSizelocalWindowLeftlocalMaxPacketremoteWindowLeftremoteMaxPacket areWritingconndataavatar specificDatabufextBufclosing localClosed remoteClosedid)self localWindowr remoteWindowrrrrr;/usr/lib/python3/dist-packages/twisted/conch/ssh/channel.py__init__9s   zSSHChannel.__init__returncCs|dS)Nascii) __bytes__decoderrrr__str__TszSSHChannel.__str__cCs |j}|sd}d||j|jfS)zD Return a byte string representation of the channel sNones)rr r )rrrrrr"WszSSHChannel.__bytes__cCsL|jdur t|jp d}|jr|jd}nd}d|d|d|jS)Nunknownr!Nonez SSHChannel z (z) on )rstrrr#r logPrefix)rrrrrrr)es zSSHChannel.logPrefixcC|jddS)z Called when the channel is opened. specificData is any data that the other side sent us when opening the channel. @type specificData: L{bytes} z channel openN_loginfo)rrrrr channelOpenmszSSHChannel.channelOpencC|jjd|ddS)z Called when the open failed for some reason. reason.desc is a string descrption, reason.code the SSH error code. @type reason: L{error.ConchError} z(other side refused open reason: {reason})reasonN)r,error)rr0rrr openFailedvzSSHChannel.openFailedcCsv|j||_|js|jsd|_||jr!|j}d|_|||jr7|j}g|_|D] \}}|||q,dSdS)z Called when bytes are added to the remote window. By default it clears the data buffers. @type data: L{bytes} Tr N)r rr startWritingrwriter writeExtended)rrbtyperrraddWindowBytess    zSSHChannel.addWindowBytescCsB|ddd}t|d|d}|r||S|jjd|ddS) aJ Called when a request is sent to this channel. By default it delegates to self.request_. If this function returns true, the request succeeded, otherwise it failed. @type requestType: L{bytes} @type data: L{bytes} @rtype: L{bool} -_r!request_Nz#unhandled request for {requestType}) requestTyper)replacer#getattrr,r-)rr=rfoofrrrrequestReceiveds  zSSHChannel.requestReceivedcCr/)zL Called when we receive data. @type data: L{bytes} zgot data {data})rNr,debugrrrrr dataReceivedszSSHChannel.dataReceivedcCs|jjd||ddS)z Called when we receive extended data (usually standard error). @type dataType: L{int} @type data: L{str} z%got extended data {dataType} {data!r})dataTyperNrCrrGrrrr extReceiveds zSSHChannel.extReceivedcCr*)zD Called when the other side will send no more data. z remote eofNr+r$rrr eofReceivedszSSHChannel.eofReceivedcCs|jd|dS)zD Called when the other side has closed the channel. z remote closeN)r,r-loseConnectionr$rrr closeReceiveds  zSSHChannel.closeReceivedcCr*)z Called when the channel is closed. This means that both our side and the remote side have closed the channel. closedNr+r$rrrrMszSSHChannel.closedcCs|jr |j|7_dSt|}||jkr/|d|j||jd}|_d|_||j}|j}|jj}td||}|D] }||||||q>|j|8_|j r_|jsa| dSdSdS)z Write some data to the channel. If there is not enough remote window available, buffer until it is. Otherwise, split the data into packets of length remoteMaxPacket and send them. @type data: L{bytes} Nr) rlenr r stopWritingrrsendDatarangerrK)rrtoprmpr5roffsetrrrr5s(      zSSHChannel.writecCs|jr#|jdd|kr|jdd|7<dS|j||gdSt||jkrD|d|j|||jdgg}|_d|_|t||jkrn|j|||d|j||jd}|j|j8_t||jksK|r|j||||jt|8_|j r| dSdS)a Send extended data to this channel. If there is not enough remote window available, buffer until there is. Otherwise, split the data into packets of length remoteMaxPacket and send them. @type dataType: L{int} @type data: L{bytes} rrN) rappendrNr rrOrrsendExtendedDatarrKrHrrrr6s.   zSSHChannel.writeExtendedcCs|d|dS)z Part of the Transport interface. Write a list of strings to the channel. @type data: C{list} of L{str} r N)r5joinrErrr writeSequence r3zSSHChannel.writeSequencecCs*d|_|js|js|j|dSdSdS)zr Close the channel if there is no buferred data. Otherwise, note the request and return. rN)rrrr sendCloser$rrrrKs zSSHChannel.loseConnectioncC |jjS)z See: L{ITransport.getPeer} @return: The remote address of this connection. @rtype: L{SSHTransportAddress}. )r transportgetPeerr$rrrr^ zSSHChannel.getPeercCr\)z See: L{ITransport.getHost} @return: An address describing this side of the connection. @rtype: L{SSHTransportAddress}. )rr]getHostr$rrrr`%r_zSSHChannel.getHostcCdS)z Called when the remote buffer is full, as a hint to stop writing. This can be ignored, but it can be helpful. Nrr$rrrrO.zSSHChannel.stopWritingcCra)ze Called when the remote buffer has more room, as a hint to continue writing. Nrr$rrrr44rbzSSHChannel.startWriting)rrrrNNN)__name__ __module__ __qualname____doc__rr,rbytes__annotations__rr(r%r"r)r.r2r9rBrFrIrJrLrMr5r6rZrKr^r`rOr4rrrrrs@           rN) rfzope.interfacertwisted.internetrtwisted.loggerrtwisted.pythonr ITransportrrrrrs