Class SessionChannelNG

java.lang.Object
com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
com.sshtools.server.SessionChannelNG
All Implemented Interfaces:
com.sshtools.common.nio.IdleStateListener, com.sshtools.common.ssh.Channel, com.sshtools.common.ssh.SessionChannel, com.sshtools.common.ssh.SessionChannelServer
Direct Known Subclasses:
UnsupportedSession, VirtualShellNG

public abstract class SessionChannelNG extends com.sshtools.synergy.ssh.ChannelNG<SshServerContext> implements com.sshtools.common.nio.IdleStateListener, com.sshtools.common.ssh.SessionChannelServer

This class provides an abstract session, it handles all the requests defined in the SSH Connection protocol for a session channel and passes the request to the concrete implementation through its abstract methods.

When creating a session implementation your #processStdinData(byte[]) will receive data from the client and process as session input. To send stdout data for your session you should use the sendStdoutData(byte[]) method and any sdterr output should be send using sendStderrData(byte[]) method.

As the server uses an asynchronous framework expensive blocking operations SHOULD NOT be performed within your session as this will cause a deadlock on the server.

The basic process of establishing a session is this

  • 1. The session is opened

  • 2. The client possibly sends requests for a pseudo terminal or setting of environment variables

  • 3. The client requests to either start a shell or execute a command. Once this has been requested the interactive session starts and data can be sent or received.

  • 4. Data is sent/received until the command has completed. Once completed the session should close the channel and optionally sent the exit status of the command using #sendExitStatus(int).

  • 5. At anytime throughout the open session the client may send a signal. See the SSH Connection Protocol specification for more information on this advanced topic.

  • Nested Class Summary

    Nested classes/interfaces inherited from class com.sshtools.synergy.ssh.ChannelNG

    com.sshtools.synergy.ssh.ChannelNG.ChannelInputStream
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected com.sshtools.common.command.ExecutableCommand
     
    protected Map<String,String>
     
    static final int
     
    protected com.sshtools.common.ssh.Subsystem
     

    Fields inherited from class com.sshtools.synergy.ssh.ChannelNG

    cache, con, connection, localWindow, remoteWindow
  • Constructor Summary

    Constructors
    Constructor
    Description
    SessionChannelNG(com.sshtools.common.ssh.SshConnection con)
     
    SessionChannelNG(com.sshtools.common.ssh.SshConnection con, boolean autoConsume)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract boolean
    allocatePseudoTerminal(String term, int cols, int rows, int width, int height, byte[] modes)
    If the client requests a pseudo terminal for the session this method will be invoked before the shell, exec or subsystem is started.
    protected abstract void
    changeWindowDimensions(int cols, int rows, int width, int height)
    When the window (terminal) size changes on the client side, it MAY send notification in which case this method will be invoked to notify the session that a change has occurred.
    protected final byte[]
     
    void
     
    void
     
    protected boolean
    Invoked when the user wants to execute a command
     
    com.sshtools.common.util.UnsignedInteger32
     
    com.sshtools.common.util.UnsignedInteger32
     
    com.sshtools.common.ssh.Subsystem
     
    boolean
     
    boolean
     
    boolean
     
    boolean
     
    protected void
     
    protected void
    Called when the channel is closing.
    protected final void
    Called when data arrives on the channel.
    protected void
    Free the session and its resources.
    protected void
    Called once the channel has been opened.
    protected void
    Called when the channel is confirmed as open
    protected void
    onChannelRequest(String type, boolean wantreply, byte[] requestdata)
    Process session requests and invoke the relevant abstract methods of this class to handle the requests.
    protected void
    onExtendedData(ByteBuffer data, int type)
    Called when extended data arrives on the channel - for a session channel this would not normally be called.
    protected void
    The remote side has reported EOF so no more data will be received.
    void
     
    protected final byte[]
    openChannel(byte[] data)
     
    protected abstract void
    A signal can be delivered to the process by the client.
    protected boolean
    Implement this method to support agent forwarding.
    void
    sendStderrData(byte[] data)
    Send stderr data to the remote client.
    void
    sendStderrData(byte[] data, int off, int len)
    Sends stderr data to the remote client.
    void
    sendStdoutData(byte[] data)
    Sends stdout data to the remote client
    void
    sendStdoutData(byte[] data, int off, int len)
    Sends stdout data to the remote client.
    abstract boolean
    If the client requests that an environment variable be set this method will be invoked.
    void
    setSingleSession(boolean singleSession)
     
    protected abstract boolean
    Invoked when the user wants to start a shell.
    protected boolean
     

    Methods inherited from class com.sshtools.synergy.ssh.ChannelNG

    addEventListener, canClose, checkWindowSpace, clearIdleState, close, close, close, createCache, disposeCache, evaluateWindowSpace, getChannelType, getCloseFuture, getConnection, getConnectionProtocol, getContext, getInputStream, getLastActivity, getLocalId, getLocalPacket, getLocalWindow, getMaxiumRemotePacketSize, getMaxiumRemoteWindowSize, getOpenFuture, getOutputStream, getRemoteId, getRemotePacket, getRemoteWindow, getSessionIdentifier, getTimeout, haltIncomingData, isAutoConsume, isClosed, isClosing, isConnected, isLocalEOF, isOpen, isRemoteEOF, log, log, log, log, logMessage, logMessage, onChannelError, onChannelOpenFailure, onLocalEOF, onRemoteClose, onWindowAdjust, processChannelRequestResponse, registerExtendedDataType, resetIdleState, resumeIncomingData, sendChannelDataAndBlock, sendChannelDataAndBlock, sendChannelDataAndBlock, sendChannelDataAndBlock, sendChannelDataAndBlock, sendChannelDataAndBlock, sendChannelRequest, sendChannelRequest, sendData, sendEOF, sendExtendedData, sendExtendedData, sendRequestResponse, sendWindowAdjust, sendWindowAdjust, setTimeout

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.sshtools.common.ssh.Channel

    addEventListener, close, getChannelType, getContext, getLocalPacket, getLocalWindow, getRemoteWindow, isClosed, isLocalEOF, isRemoteEOF, sendChannelRequest, sendChannelRequest, sendData, sendWindowAdjust

    Methods inherited from interface com.sshtools.common.ssh.SessionChannel

    getConnection, getInputStream, getOutputStream, haltIncomingData, resumeIncomingData
  • Field Details

    • SSH_EXTENDED_DATA_STDERR

      public static final int SSH_EXTENDED_DATA_STDERR
      See Also:
    • subsystem

      protected com.sshtools.common.ssh.Subsystem subsystem
    • command

      protected com.sshtools.common.command.ExecutableCommand command
    • environment

      protected Map<String,String> environment
  • Constructor Details

    • SessionChannelNG

      public SessionChannelNG(com.sshtools.common.ssh.SshConnection con)
    • SessionChannelNG

      public SessionChannelNG(com.sshtools.common.ssh.SshConnection con, boolean autoConsume)
  • Method Details

    • isSingleSession

      public boolean isSingleSession()
    • setSingleSession

      public void setSingleSession(boolean singleSession)
    • onChannelClosed

      protected void onChannelClosed()
      Specified by:
      onChannelClosed in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • enableRawMode

      public void enableRawMode()
      Specified by:
      enableRawMode in interface com.sshtools.common.ssh.SessionChannelServer
    • disableRawMode

      public void disableRawMode()
      Specified by:
      disableRawMode in interface com.sshtools.common.ssh.SessionChannelServer
    • getSubsystem

      public com.sshtools.common.ssh.Subsystem getSubsystem()
    • createChannel

      protected final byte[] createChannel() throws IOException
      Specified by:
      createChannel in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
      Throws:
      IOException
    • getErrorStream

      public OutputStream getErrorStream()
      Specified by:
      getErrorStream in interface com.sshtools.common.ssh.SessionChannelServer
    • isAgentForwardingRequested

      public boolean isAgentForwardingRequested()
    • requestAgentForwarding

      protected boolean requestAgentForwarding(String requestType)
      Implement this method to support agent forwarding.
      Returns:
    • allocatePseudoTerminal

      protected abstract boolean allocatePseudoTerminal(String term, int cols, int rows, int width, int height, byte[] modes)
      If the client requests a pseudo terminal for the session this method will be invoked before the shell, exec or subsystem is started.
      Parameters:
      term -
      cols -
      rows -
      width -
      height -
      modes -
      Returns:
      boolean
    • changeWindowDimensions

      protected abstract void changeWindowDimensions(int cols, int rows, int width, int height)
      When the window (terminal) size changes on the client side, it MAY send notification in which case this method will be invoked to notify the session that a change has occurred.
      Parameters:
      cols -
      rows -
      width -
      height -
    • processSignal

      protected abstract void processSignal(String signal)
      A signal can be delivered to the process by the client. If a signal is received this method will be invoked so that the session may evaluate and take the required action.
      Parameters:
      signal -
    • setEnvironmentVariable

      public abstract boolean setEnvironmentVariable(String name, String value)
      If the client requests that an environment variable be set this method will be invoked.
      Specified by:
      setEnvironmentVariable in interface com.sshtools.common.ssh.SessionChannelServer
      Parameters:
      name -
      value -
      Returns:
      true if the variable has been set, otherwise false
    • startShell

      protected abstract boolean startShell()
      Invoked when the user wants to start a shell.
      Returns:
      true if the shell has been started, otherwise false
    • executeCommand

      protected boolean executeCommand(String[] args)
      Invoked when the user wants to execute a command
      Parameters:
      cmd -
      Returns:
      true if the cmd has been executed, otherwise false
    • onChannelOpen

      protected void onChannelOpen()
      Called once the channel has been opened.
      Specified by:
      onChannelOpen in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • idle

      public boolean idle()
      Specified by:
      idle in interface com.sshtools.common.nio.IdleStateListener
    • onChannelRequest

      protected void onChannelRequest(String type, boolean wantreply, byte[] requestdata)
      Process session requests and invoke the relevant abstract methods of this class to handle the requests. If you overide this method make sure that you call the super method.
      Specified by:
      onChannelRequest in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
      Parameters:
      type - String
      wantreply - boolean
      requestdata - byte[]
    • onChannelOpenConfirmation

      protected void onChannelOpenConfirmation()
      Called when the channel is confirmed as open
      Specified by:
      onChannelOpenConfirmation in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • onRemoteEOF

      protected void onRemoteEOF()
      The remote side has reported EOF so no more data will be received. This will force the channel to close. If this behaviour is not required you can override this method
      Specified by:
      onRemoteEOF in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • onChannelFree

      protected void onChannelFree()
      Free the session and its resources. If you override this method make sure that you call the super method to ensure that the resources of the abstract class are freed.
      Specified by:
      onChannelFree in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • onChannelClosing

      protected void onChannelClosing()
      Called when the channel is closing. If you override this method make sure that you call the super method.
      Specified by:
      onChannelClosing in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • onChannelData

      protected final void onChannelData(ByteBuffer data)
      Called when data arrives on the channel.
      Overrides:
      onChannelData in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
      Parameters:
      data - byte[]
    • onExtendedData

      protected void onExtendedData(ByteBuffer data, int type)
      Called when extended data arrives on the channel - for a session channel this would not normally be called.
      Overrides:
      onExtendedData in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
      Parameters:
      data - byte[]
      type - int
    • sendStdoutData

      public void sendStdoutData(byte[] data, int off, int len) throws IOException
      Sends stdout data to the remote client.
      Parameters:
      data -
      off -
      len -
      Throws:
      IOException
    • sendStdoutData

      public void sendStdoutData(byte[] data) throws IOException
      Sends stdout data to the remote client
      Parameters:
      data -
      Throws:
      IOException
    • sendStderrData

      public void sendStderrData(byte[] data, int off, int len) throws IOException
      Sends stderr data to the remote client.
      Parameters:
      data -
      off -
      len -
      Throws:
      IOException
    • sendStderrData

      public void sendStderrData(byte[] data) throws IOException
      Send stderr data to the remote client.
      Parameters:
      data -
      Throws:
      IOException
    • openChannel

      protected final byte[] openChannel(byte[] data) throws com.sshtools.common.nio.WriteOperationRequest, com.sshtools.common.ssh.ChannelOpenException
      Specified by:
      openChannel in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
      Throws:
      com.sshtools.common.nio.WriteOperationRequest
      com.sshtools.common.ssh.ChannelOpenException
    • isIncomingDataHalted

      public boolean isIncomingDataHalted()
      Overrides:
      isIncomingDataHalted in class com.sshtools.synergy.ssh.ChannelNG<SshServerContext>
    • getMaximumWindowSpace

      public com.sshtools.common.util.UnsignedInteger32 getMaximumWindowSpace()
      Specified by:
      getMaximumWindowSpace in interface com.sshtools.common.ssh.SessionChannel
    • getMinimumWindowSpace

      public com.sshtools.common.util.UnsignedInteger32 getMinimumWindowSpace()
      Specified by:
      getMinimumWindowSpace in interface com.sshtools.common.ssh.SessionChannel
    • onSessionOpen

      public void onSessionOpen()
      Specified by:
      onSessionOpen in interface com.sshtools.common.ssh.SessionChannel
    • startSubsystem

      protected boolean startSubsystem(String name)