Class SessionHelper

java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.common.util.closeable.IoBaseCloseable
org.apache.sshd.common.util.closeable.AbstractCloseable
org.apache.sshd.common.util.closeable.AbstractInnerCloseable
org.apache.sshd.common.kex.AbstractKexFactoryManager
org.apache.sshd.common.session.helpers.SessionHelper
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, org.apache.sshd.common.AttributeRepository, org.apache.sshd.common.AttributeStore, org.apache.sshd.common.auth.MutableUserHolder, org.apache.sshd.common.auth.UsernameHolder, ChannelListenerManager, ChannelStreamWriterResolver, ChannelStreamWriterResolverManager, org.apache.sshd.common.Closeable, FactoryManagerHolder, PortForwardingEventListenerManager, PortForwardingInformationProvider, KexExtensionHandlerManager, KexFactoryManager, org.apache.sshd.common.PropertyResolver, ReservedSessionMessagesManager, Session, org.apache.sshd.common.session.SessionContext, SessionDisconnectHandlerManager, org.apache.sshd.common.session.SessionHeartbeatController, SessionListenerManager, UnknownChannelReferenceHandlerManager, org.apache.sshd.common.signature.SignatureFactoriesHolder, org.apache.sshd.common.signature.SignatureFactoriesManager, org.apache.sshd.common.util.net.ConnectionEndpointsIndicator
Direct Known Subclasses:
AbstractSession

public abstract class SessionHelper extends AbstractKexFactoryManager implements Session
Contains split code in order to make AbstractSession class smaller
  • Field Details

    • authStart

      protected Instant authStart
    • idleStart

      protected Instant idleStart
    • initialKexProposal

      protected Map<org.apache.sshd.common.kex.KexProposalOption, String> initialKexProposal
      Stores the initial KEX proposal after it has been run through the hooks allowing client code to modify it; see getKexProposal(). The same proposal is then used for the initial KEX and for any re-KEX.
  • Constructor Details

    • SessionHelper

      protected SessionHelper(boolean serverSession, FactoryManager factoryManager, org.apache.sshd.common.io.IoSession ioSession)
      Create a new session.
      Parameters:
      serverSession - true if this is a server session, false if client one
      factoryManager - the factory manager
      ioSession - the underlying I/O session
  • Method Details

    • getIoSession

      public org.apache.sshd.common.io.IoSession getIoSession()
      Specified by:
      getIoSession in interface Session
      Returns:
      The IoSession associated to this session
    • isServerSession

      public boolean isServerSession()
      Specified by:
      isServerSession in interface org.apache.sshd.common.session.SessionContext
    • getFactoryManager

      public FactoryManager getFactoryManager()
      Specified by:
      getFactoryManager in interface FactoryManagerHolder
      Returns:
      The currently associated FactoryManager
    • getParentPropertyResolver

      public org.apache.sshd.common.PropertyResolver getParentPropertyResolver()
      Specified by:
      getParentPropertyResolver in interface org.apache.sshd.common.PropertyResolver
    • getProperties

      public Map<String,Object> getProperties()
      Specified by:
      getProperties in interface org.apache.sshd.common.PropertyResolver
    • getAttributesCount

      public int getAttributesCount()
      Specified by:
      getAttributesCount in interface org.apache.sshd.common.AttributeRepository
    • getAttribute

      public <T> T getAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key)
      Specified by:
      getAttribute in interface org.apache.sshd.common.AttributeRepository
    • attributeKeys

      public Collection<org.apache.sshd.common.AttributeRepository.AttributeKey<?>> attributeKeys()
      Specified by:
      attributeKeys in interface org.apache.sshd.common.AttributeRepository
    • computeAttributeIfAbsent

      public <T> T computeAttributeIfAbsent(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key, Function<? super org.apache.sshd.common.AttributeRepository.AttributeKey<T>, ? extends T> resolver)
      Specified by:
      computeAttributeIfAbsent in interface org.apache.sshd.common.AttributeStore
    • setAttribute

      public <T> T setAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key, T value)
      Specified by:
      setAttribute in interface org.apache.sshd.common.AttributeStore
    • removeAttribute

      public <T> T removeAttribute(org.apache.sshd.common.AttributeRepository.AttributeKey<T> key)
      Specified by:
      removeAttribute in interface org.apache.sshd.common.AttributeStore
    • clearAttributes

      public void clearAttributes()
      Specified by:
      clearAttributes in interface org.apache.sshd.common.AttributeStore
    • getUsername

      public String getUsername()
      Specified by:
      getUsername in interface org.apache.sshd.common.auth.UsernameHolder
    • setUsername

      public void setUsername(String username)
      Specified by:
      setUsername in interface org.apache.sshd.common.auth.MutableUserHolder
    • isAuthenticated

      public boolean isAuthenticated()
      Specified by:
      isAuthenticated in interface org.apache.sshd.common.session.SessionContext
    • setAuthenticated

      public void setAuthenticated() throws IOException
      Specified by:
      setAuthenticated in interface Session
      Throws:
      IOException
    • checkForTimeouts

      protected org.apache.sshd.common.session.helpers.TimeoutIndicator checkForTimeouts() throws IOException
      Checks whether the session has timed out (both authentication and idle timeouts are checked). If the session has timed out, a DISCONNECT message will be sent.
      Returns:
      An indication whether timeout has been detected
      Throws:
      IOException - If failed to check
      See Also:
    • getAuthTimeoutStart

      public Instant getAuthTimeoutStart()
      Specified by:
      getAuthTimeoutStart in interface Session
      Returns:
      The timestamp value (milliseconds since EPOCH) when timer was started
    • resetAuthTimeout

      public Instant resetAuthTimeout()
      Description copied from interface: Session
      Re-start the authentication timeout timer
      Specified by:
      resetAuthTimeout in interface Session
      Returns:
      The timestamp value (milliseconds since EPOCH) when timer was started
      See Also:
    • checkAuthenticationTimeout

      protected org.apache.sshd.common.session.helpers.TimeoutIndicator checkAuthenticationTimeout(Instant now, Duration authTimeout)
      Checks if authentication timeout expired
      Parameters:
      now - The current time in millis
      authTimeout - The configured timeout - if non-positive then no timeout
      Returns:
      A TimeoutIndicator specifying the timeout status and disconnect reason message if timeout expired, null or NoTimeout if no timeout occurred
      See Also:
    • getIdleTimeoutStart

      public Instant getIdleTimeoutStart()
      Specified by:
      getIdleTimeoutStart in interface Session
      Returns:
      The timestamp value (milliseconds since EPOCH) when timer was started
    • checkIdleTimeout

      protected org.apache.sshd.common.session.helpers.TimeoutIndicator checkIdleTimeout(Instant now, Duration idleTimeout)
      Checks if idle timeout expired
      Parameters:
      now - The current time in millis
      idleTimeout - The configured timeout - if non-positive then no timeout
      Returns:
      A TimeoutIndicator specifying the timeout status and disconnect reason message if timeout expired, null or NoTimeout if no timeout occurred
      See Also:
    • resetIdleTimeout

      public Instant resetIdleTimeout()
      Description copied from interface: Session
      Re-start idle timeout timer
      Specified by:
      resetIdleTimeout in interface Session
      Returns:
      The timestamp value (milliseconds since EPOCH) when timer was started
      See Also:
    • getTimeoutStatus

      public org.apache.sshd.common.session.helpers.TimeoutIndicator getTimeoutStatus()
      Description copied from interface: Session
      Check if timeout has occurred.
      Specified by:
      getTimeoutStatus in interface Session
      Returns:
      the timeout status - never null
    • getReservedSessionMessagesHandler

      public ReservedSessionMessagesHandler getReservedSessionMessagesHandler()
      Specified by:
      getReservedSessionMessagesHandler in interface ReservedSessionMessagesManager
      Returns:
      The currently registered ReservedSessionMessagesHandler - may be null
    • setReservedSessionMessagesHandler

      public void setReservedSessionMessagesHandler(ReservedSessionMessagesHandler handler)
      Specified by:
      setReservedSessionMessagesHandler in interface ReservedSessionMessagesManager
      Parameters:
      handler - The ReservedSessionMessagesHandler to use - may be null
    • getSessionDisconnectHandler

      public SessionDisconnectHandler getSessionDisconnectHandler()
      Specified by:
      getSessionDisconnectHandler in interface SessionDisconnectHandlerManager
    • setSessionDisconnectHandler

      public void setSessionDisconnectHandler(SessionDisconnectHandler sessionDisconnectHandler)
      Specified by:
      setSessionDisconnectHandler in interface SessionDisconnectHandlerManager
    • handleIgnore

      protected void handleIgnore(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • doInvokeIgnoreMessageHandler

      protected void doInvokeIgnoreMessageHandler(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Invoked by handleDebug(Buffer) after validating that the buffer structure seems well-formed and also resetting the idle timeout. By default, retrieves the ReservedSessionMessagesHandler and invokes its handleIgnoreMessage method.
      Parameters:
      buffer - The input Buffer
      Throws:
      Exception - if failed to handle the message
    • sendNotImplemented

      protected org.apache.sshd.common.io.IoWriteFuture sendNotImplemented(long seqNoValue) throws IOException
      Sends a SSH_MSG_UNIMPLEMENTED message
      Parameters:
      seqNoValue - The referenced sequence number
      Returns:
      An IoWriteFuture that can be used to wait for packet write completion
      Throws:
      IOException - if an error occurred sending the packet
    • handleUnimplemented

      protected void handleUnimplemented(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • doInvokeUnimplementedMessageHandler

      protected boolean doInvokeUnimplementedMessageHandler(int cmd, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Parameters:
      cmd - The unimplemented command
      buffer - The input Buffer
      Returns:
      Result of invoking handleUnimplementedMessage
      Throws:
      Exception - if failed to handle the message
    • sendDebugMessage

      public org.apache.sshd.common.io.IoWriteFuture sendDebugMessage(boolean display, Object msg, String lang) throws IOException
      Description copied from interface: Session
      Sends an SSH_MSG_DEBUG to the peer session
      Specified by:
      sendDebugMessage in interface Session
      Parameters:
      display - true if OK to display the message at the peer as-is
      msg - The message object whose toString() value to be used - if null then the "null" string is sent
      lang - The language - null/empty if some pre-agreed default is used
      Returns:
      An IoWriteFuture that can be used to check when the packet has actually been sent
      Throws:
      IOException - if an error occurred when encoding or sending the packet
      See Also:
    • handleDebug

      protected void handleDebug(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • doInvokeDebugMessageHandler

      protected void doInvokeDebugMessageHandler(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Invoked by handleDebug(Buffer) after validating that the buffer structure seems well-formed and also resetting the idle timeout. By default, retrieves the ReservedSessionMessagesHandler and invokes its handleDebugMessage method.
      Parameters:
      buffer - The input Buffer
      Throws:
      Exception - if failed to handle the message
    • resolveReservedSessionMessagesHandler

      protected ReservedSessionMessagesHandler resolveReservedSessionMessagesHandler()
    • getUnknownChannelReferenceHandler

      public UnknownChannelReferenceHandler getUnknownChannelReferenceHandler()
      Specified by:
      getUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Returns:
      The UnknownChannelReferenceHandlerManager to use - if null then any reference to unknown channel causes an SshChannelNotFoundException
    • setUnknownChannelReferenceHandler

      public void setUnknownChannelReferenceHandler(UnknownChannelReferenceHandler unknownChannelReferenceHandler)
      Specified by:
      setUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Parameters:
      unknownChannelReferenceHandler - The UnknownChannelReferenceHandlerManager to use - if null then any reference to unknown channel causes an SshChannelNotFoundException
    • resolveUnknownChannelReferenceHandler

      public UnknownChannelReferenceHandler resolveUnknownChannelReferenceHandler()
      Description copied from interface: UnknownChannelReferenceHandlerManager
      Check if current manager has a specific handler set for it - if not, try and resolve one from the "parent" container (if any)
      Specified by:
      resolveUnknownChannelReferenceHandler in interface UnknownChannelReferenceHandlerManager
      Returns:
      The resolved handler instance
    • getChannelStreamWriterResolver

      public ChannelStreamWriterResolver getChannelStreamWriterResolver()
      Specified by:
      getChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • setChannelStreamWriterResolver

      public void setChannelStreamWriterResolver(ChannelStreamWriterResolver resolver)
      Specified by:
      setChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • resolveChannelStreamWriterResolver

      public ChannelStreamWriterResolver resolveChannelStreamWriterResolver()
      Specified by:
      resolveChannelStreamWriterResolver in interface ChannelStreamWriterResolverManager
    • sendIgnoreMessage

      public org.apache.sshd.common.io.IoWriteFuture sendIgnoreMessage(byte... data) throws IOException
      Description copied from interface: Session
      Sends an SSH_MSG_IGNORE to the peer session
      Specified by:
      sendIgnoreMessage in interface Session
      Parameters:
      data - The message data
      Returns:
      An IoWriteFuture that can be used to check when the packet has actually been sent
      Throws:
      IOException - if an error occurred when encoding or sending the packet
      See Also:
    • signalSessionCreated

      protected void signalSessionCreated(org.apache.sshd.common.io.IoSession ioSession) throws Exception
      Throws:
      Exception
    • signalSessionStarting

      protected void signalSessionStarting() throws Exception
      Throws:
      Exception
    • signalSendIdentification

      protected void signalSendIdentification(String version, List<String> extraLines) throws Exception
      Throws:
      Exception
    • signalReadPeerIdentificationLine

      protected void signalReadPeerIdentificationLine(String version, List<String> extraLines) throws Exception
      Throws:
      Exception
    • signalPeerIdentificationReceived

      protected void signalPeerIdentificationReceived(String version, List<String> extraLines) throws Exception
      Throws:
      Exception
    • signalNegotiationOptionsCreated

      protected void signalNegotiationOptionsCreated(Map<org.apache.sshd.common.kex.KexProposalOption, String> proposal)
    • signalNegotiationStart

      protected void signalNegotiationStart(Map<org.apache.sshd.common.kex.KexProposalOption, String> c2sOptions, Map<org.apache.sshd.common.kex.KexProposalOption, String> s2cOptions)
    • signalNegotiationEnd

      protected void signalNegotiationEnd(Map<org.apache.sshd.common.kex.KexProposalOption, String> c2sOptions, Map<org.apache.sshd.common.kex.KexProposalOption, String> s2cOptions, Map<org.apache.sshd.common.kex.KexProposalOption, String> negotiatedGuess, Throwable reason)
    • signalSessionEvent

      protected void signalSessionEvent(SessionListener.Event event) throws Exception
      Sends a session event to all currently registered session listeners
      Parameters:
      event - The event to send
      Throws:
      Exception - If any of the registered listeners threw an exception.
    • signalDisconnect

      protected void signalDisconnect(int code, String msg, String lang, boolean initiator)
    • signalExceptionCaught

      protected void signalExceptionCaught(Throwable t)
    • signalSessionClosed

      protected void signalSessionClosed()
    • invokeSessionSignaller

      protected void invokeSessionSignaller(Consumer<SessionListener> listener) throws Throwable
      Throws:
      Throwable
    • resolvePeerAddress

      protected SocketAddress resolvePeerAddress(SocketAddress knownAddress)
      Parameters:
      knownAddress - Any externally set peer address - e.g., due to some proxy mechanism meta-data
      Returns:
      The external address if not null otherwise, the IoSession peer address
    • resolveIdentificationString

      protected String resolveIdentificationString(String configPropName)
      Resolves the identification to send to the peer session by consulting the associated FactoryManager. If a value is set, then it is appended to the standard SessionContext.DEFAULT_SSH_VERSION_PREFIX. Otherwise a default value is returned consisting of the prefix and the core artifact name + version in uppercase - e.g.,' "SSH-2.0-APACHE-SSHD-1.2.3.4"
      Parameters:
      configPropName - The property used to query the factory manager
      Returns:
      The resolved identification value
    • doReadIdentification

      protected List<String> doReadIdentification(org.apache.sshd.common.util.buffer.Buffer buffer, boolean server) throws Exception
      Read the remote identification from this buffer. If more data is needed, the buffer will be reset to its original state and a null value will be returned. Else the identification string will be returned and the data read will be consumed from the buffer.
      Parameters:
      buffer - the buffer containing the identification string
      server - true if it is called by the server session, false if by the client session
      Returns:
      A List of all received remote identification lines until the version line was read or null if more data is needed. The identification line is the last one in the list
      Throws:
      Exception - if malformed identification found
    • resolveSessionKexProposal

      protected String resolveSessionKexProposal(String hostKeyTypes) throws IOException
      Throws:
      IOException
    • resolveAvailableSignaturesProposal

      protected String resolveAvailableSignaturesProposal() throws IOException, GeneralSecurityException
      Computes the list of available host key signature algorithms supported.
      Returns:
      A comma-separated list of all the signature protocols to be included in the proposal - null/empty if no proposal
      Throws:
      IOException - If failed to read/parse the keys data
      GeneralSecurityException - If failed to generate the keys
      See Also:
    • resolveAvailableSignaturesProposal

      protected abstract String resolveAvailableSignaturesProposal(FactoryManager manager) throws IOException, GeneralSecurityException
      Computes the list of available host key signature algorithms supported.
      Parameters:
      manager - The FactoryManager
      Returns:
      A comma-separated list of all the signature protocols to be included in the proposal - null/empty if no proposal
      Throws:
      IOException - If failed to read/parse the keys data
      GeneralSecurityException - If failed to generate the keys
    • getKexProposal

      protected Map<org.apache.sshd.common.kex.KexProposalOption, String> getKexProposal() throws Exception
      Retrieves this side's initial proposal for KEX negotiation. If no proposal exists yet, one is created and passed though the KexExtensionHandler and the SessionListener for customization, otherwise the previously created proposal for the session is returned.
      Returns:
      the proposal Map
      Throws:
      Exception - when no proposal can be created
    • createProposal

      protected Map<org.apache.sshd.common.kex.KexProposalOption, String> createProposal(String hostKeyTypes) throws IOException
      Create our proposal for SSH negotiation
      Parameters:
      hostKeyTypes - The comma-separated list of supported host key types
      Returns:
      The proposal Map
      Throws:
      IOException - If internal problem - e.g., KEX extensions negotiation issue
    • mergeProposals

      protected Map<org.apache.sshd.common.kex.KexProposalOption, String> mergeProposals(Map<org.apache.sshd.common.kex.KexProposalOption, String> current, Map<org.apache.sshd.common.kex.KexProposalOption, String> proposal)
    • disconnect

      public void disconnect(int reason, String msg) throws IOException
      Description copied from interface: Session
      Send a disconnect packet with the given reason and message. Once the packet has been sent, the session will be closed asynchronously.
      Specified by:
      disconnect in interface Session
      Parameters:
      reason - the reason code for this disconnect
      msg - the text message
      Throws:
      IOException - if an error occurred sending the packet
    • handleDisconnect

      protected void handleDisconnect(org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • handleDisconnect

      protected void handleDisconnect(int code, String msg, String lang, org.apache.sshd.common.util.buffer.Buffer buffer) throws Exception
      Throws:
      Exception
    • exceptionCaught

      public void exceptionCaught(Throwable t)
      Handle any exceptions that occurred on this session. The session will be closed and a disconnect packet will be sent before if the given exception is an SshException.
      Specified by:
      exceptionCaught in interface Session
      Parameters:
      t - the exception to process
    • getConnectionService

      protected abstract ConnectionService getConnectionService()
    • getForwarder

      protected Forwarder getForwarder()
    • getLocalForwardsBindings

      public List<Map.Entry<org.apache.sshd.common.util.net.SshdSocketAddress, org.apache.sshd.common.util.net.SshdSocketAddress>> getLocalForwardsBindings()
      Specified by:
      getLocalForwardsBindings in interface PortForwardingInformationProvider
      Returns:
      A snapshot of the currently bound forwarded local ports as "pairs" of local/remote SshdSocketAddress-es
    • isLocalPortForwardingStartedForPort

      public boolean isLocalPortForwardingStartedForPort(int port)
      Description copied from interface: PortForwardingInformationProvider
      Test if local port forwarding is started
      Specified by:
      isLocalPortForwardingStartedForPort in interface PortForwardingInformationProvider
      Parameters:
      port - The local port
      Returns:
      true if local port forwarding is started
      See Also:
    • getStartedLocalPortForwards

      public List<org.apache.sshd.common.util.net.SshdSocketAddress> getStartedLocalPortForwards()
      Specified by:
      getStartedLocalPortForwards in interface PortForwardingInformationProvider
      Returns:
      A List snapshot of the currently started local port forward bindings
    • getBoundLocalPortForwards

      public List<org.apache.sshd.common.util.net.SshdSocketAddress> getBoundLocalPortForwards(int port)
      Specified by:
      getBoundLocalPortForwards in interface PortForwardingInformationProvider
      Parameters:
      port - The port number
      Returns:
      The local bound SshdSocketAddress-es for the port
      See Also:
    • getRemoteForwardsBindings

      public List<Map.Entry<Integer, org.apache.sshd.common.util.net.SshdSocketAddress>> getRemoteForwardsBindings()
      Specified by:
      getRemoteForwardsBindings in interface PortForwardingInformationProvider
      Returns:
      A snapshot of the currently bound forwarded remote ports as "pairs" of port + bound SshdSocketAddress
    • isRemotePortForwardingStartedForPort

      public boolean isRemotePortForwardingStartedForPort(int port)
      Description copied from interface: PortForwardingInformationProvider
      Test if remote port forwarding is started
      Specified by:
      isRemotePortForwardingStartedForPort in interface PortForwardingInformationProvider
      Parameters:
      port - The remote port
      Returns:
      true if remote port forwarding is started
      See Also:
    • getStartedRemotePortForwards

      public NavigableSet<Integer> getStartedRemotePortForwards()
      Specified by:
      getStartedRemotePortForwards in interface PortForwardingInformationProvider
      Returns:
      A NavigableSet snapshot of the currently started remote port forwards
    • getBoundRemotePortForward

      public org.apache.sshd.common.util.net.SshdSocketAddress getBoundRemotePortForward(int port)
      Specified by:
      getBoundRemotePortForward in interface PortForwardingInformationProvider
      Parameters:
      port - The port number
      Returns:
      The remote bound SshdSocketAddress for the port - null if none bound
      See Also:
    • getAuthTimeout

      public Duration getAuthTimeout()
      Specified by:
      getAuthTimeout in interface Session
      Returns:
      Timeout value in milliseconds for authentication stage
    • getIdleTimeout

      public Duration getIdleTimeout()
      Specified by:
      getIdleTimeout in interface Session
      Returns:
      Timeout value in milliseconds for communication
    • toString

      public String toString()
      Overrides:
      toString in class Object