Package org.apache.sshd.common.session
Interface Session
-
- All Superinterfaces:
AttributeRepository
,AttributeStore
,java.lang.AutoCloseable
,java.nio.channels.Channel
,ChannelListenerManager
,ChannelStreamWriterResolver
,ChannelStreamWriterResolverManager
,java.io.Closeable
,Closeable
,ConnectionEndpointsIndicator
,FactoryManagerHolder
,KexExtensionHandlerManager
,KexFactoryManager
,MutableUserHolder
,PortForwardingEventListenerManager
,PortForwardingInformationProvider
,PropertyResolver
,ReservedSessionMessagesManager
,SessionContext
,SessionDisconnectHandlerManager
,SessionHeartbeatController
,SessionListenerManager
,SignatureFactoriesHolder
,SignatureFactoriesManager
,UnknownChannelReferenceHandlerManager
,UsernameHolder
- All Known Subinterfaces:
ClientSession
,ServerSession
- All Known Implementing Classes:
AbstractClientSession
,AbstractServerSession
,AbstractSession
,ClientSessionImpl
,ServerSessionImpl
,SessionHelper
public interface Session extends SessionContext, MutableUserHolder, KexFactoryManager, SessionListenerManager, ReservedSessionMessagesManager, SessionDisconnectHandlerManager, ChannelListenerManager, ChannelStreamWriterResolverManager, PortForwardingEventListenerManager, UnknownChannelReferenceHandlerManager, FactoryManagerHolder, PortForwardingInformationProvider
Represents an SSH session. Note: the associated username for the session may benull
/empty if the session is not yet authenticated
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.sshd.common.AttributeRepository
AttributeRepository.AttributeKey<T>
-
Nested classes/interfaces inherited from interface org.apache.sshd.common.session.SessionHeartbeatController
SessionHeartbeatController.HeartbeatType
-
-
Field Summary
-
Fields inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolver
NONE
-
Fields inherited from interface org.apache.sshd.common.PropertyResolver
EMPTY
-
Fields inherited from interface org.apache.sshd.common.session.SessionContext
DEFAULT_SSH_VERSION_PREFIX, FALLBACK_SSH_VERSION_PREFIX, MAX_VERSION_LINE_LENGTH
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Buffer
createBuffer(byte cmd)
Create a new buffer for the specified SSH packet and reserve the needed space (5 bytes) for the packet header.Buffer
createBuffer(byte cmd, int estimatedSize)
Create a new buffer for the specified SSH packet and reserve the needed space (5 bytes) for the packet header.void
disconnect(int reason, java.lang.String msg)
Send a disconnect packet with the given reason and message.void
exceptionCaught(java.lang.Throwable t)
Handle any exceptions that occurred on this session.java.time.Duration
getAuthTimeout()
java.time.Instant
getAuthTimeoutStart()
java.time.Duration
getIdleTimeout()
java.time.Instant
getIdleTimeoutStart()
IoSession
getIoSession()
KeyExchange
getKex()
default java.net.SocketAddress
getLocalAddress()
default java.net.SocketAddress
getRemoteAddress()
<T extends Service>
TgetService(java.lang.Class<T> clazz)
Get the service of the specified type.TimeoutIndicator
getTimeoutStatus()
Check if timeout has occurred.Buffer
prepareBuffer(byte cmd, Buffer buffer)
Prepare a new "clean" buffer while reserving the needed space (5 bytes) for the packet header.KeyExchangeFuture
reExchangeKeys()
Initiate a new key exchange.Buffer
request(java.lang.String request, Buffer buffer, long maxWaitMillis)
Send a global request and wait for the response.default Buffer
request(java.lang.String request, Buffer buffer, long timeout, java.util.concurrent.TimeUnit unit)
Send a global request and wait for the response.default Buffer
request(java.lang.String request, Buffer buffer, java.time.Duration timeout)
Send a global request and wait for the response.java.time.Instant
resetAuthTimeout()
Re-start the authentication timeout timerjava.time.Instant
resetIdleTimeout()
Re-start idle timeout timerdefault <T> T
resolveAttribute(AttributeRepository.AttributeKey<T> key)
Attempts to resolve the associated value by going up the store's hierarchy (if any)static <T> T
resolveAttribute(Session session, AttributeRepository.AttributeKey<T> key)
Attempts to use the session's attribute, if not found then tries the factory managerIoWriteFuture
sendDebugMessage(boolean display, java.lang.Object msg, java.lang.String lang)
Sends anSSH_MSG_DEBUG
to the peer sessionIoWriteFuture
sendIgnoreMessage(byte... data)
Sends anSSH_MSG_IGNORE
to the peer sessionvoid
setAuthenticated()
void
startService(java.lang.String name, Buffer buffer)
IoWriteFuture
writePacket(Buffer buffer)
Encode and send the given buffer.default IoWriteFuture
writePacket(Buffer buffer, long maxWaitMillis)
Encode and send the given buffer with the specified timeout.IoWriteFuture
writePacket(Buffer buffer, long timeout, java.util.concurrent.TimeUnit unit)
Encode and send the given buffer with the specified timeout.default IoWriteFuture
writePacket(Buffer buffer, java.time.Duration timeout)
Encode and send the given buffer with the specified timeout.-
Methods inherited from interface org.apache.sshd.common.AttributeRepository
attributeKeys, getAttribute, getAttributesCount
-
Methods inherited from interface org.apache.sshd.common.AttributeStore
clearAttributes, computeAttributeIfAbsent, removeAttribute, setAttribute
-
Methods inherited from interface org.apache.sshd.common.channel.ChannelListenerManager
addChannelListener, getChannelListenerProxy, removeChannelListener
-
Methods inherited from interface org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager
getChannelStreamWriterResolver, resolveChannelStreamWriter, resolveChannelStreamWriterResolver, setChannelStreamWriterResolver
-
Methods inherited from interface org.apache.sshd.common.Closeable
addCloseFutureListener, close, close, isClosed, isClosing, isOpen, removeCloseFutureListener
-
Methods inherited from interface org.apache.sshd.common.FactoryManagerHolder
getFactoryManager
-
Methods inherited from interface org.apache.sshd.common.kex.extension.KexExtensionHandlerManager
getKexExtensionHandler, setKexExtensionHandler
-
Methods inherited from interface org.apache.sshd.common.kex.KexFactoryManager
getCipherFactories, getCipherFactoriesNameList, getCipherFactoriesNames, getCompressionFactories, getCompressionFactoriesNameList, getCompressionFactoriesNames, getKeyExchangeFactories, getMacFactories, getMacFactoriesNameList, getMacFactoriesNames, setCipherFactories, setCipherFactoriesNameList, setCipherFactoriesNames, setCipherFactoriesNames, setCompressionFactories, setCompressionFactoriesNameList, setCompressionFactoriesNames, setCompressionFactoriesNames, setKeyExchangeFactories, setMacFactories, setMacFactoriesNameList, setMacFactoriesNames, setMacFactoriesNames
-
Methods inherited from interface org.apache.sshd.common.auth.MutableUserHolder
setUsername
-
Methods inherited from interface org.apache.sshd.common.forward.PortForwardingEventListenerManager
addPortForwardingEventListener, getPortForwardingEventListenerProxy, removePortForwardingEventListener
-
Methods inherited from interface org.apache.sshd.common.forward.PortForwardingInformationProvider
getBoundLocalPortForwards, getBoundRemotePortForward, getLocalForwardsBindings, getRemoteForwardsBindings, getStartedLocalPortForwards, getStartedRemotePortForwards, isLocalPortForwardingStartedForPort, isRemotePortForwardingStartedForPort
-
Methods inherited from interface org.apache.sshd.common.PropertyResolver
getBoolean, getBooleanProperty, getCharset, getInteger, getIntProperty, getLong, getLongProperty, getObject, getParentPropertyResolver, getProperties, getString, getStringProperty, isEmpty
-
Methods inherited from interface org.apache.sshd.common.session.ReservedSessionMessagesManager
getReservedSessionMessagesHandler, setReservedSessionMessagesHandler
-
Methods inherited from interface org.apache.sshd.common.session.SessionContext
getCipherInformation, getClientKexProposals, getClientVersion, getCompressionInformation, getKexNegotiationResult, getKexState, getMacInformation, getNegotiatedKexParameter, getServerKexProposals, getServerVersion, getSessionId, isAuthenticated, isServerSession
-
Methods inherited from interface org.apache.sshd.common.session.SessionDisconnectHandlerManager
getSessionDisconnectHandler, setSessionDisconnectHandler
-
Methods inherited from interface org.apache.sshd.common.session.SessionHeartbeatController
disableSessionHeartbeat, getSessionHeartbeatInterval, getSessionHeartbeatType, setSessionHeartbeat, setSessionHeartbeat
-
Methods inherited from interface org.apache.sshd.common.session.SessionListenerManager
addSessionListener, getSessionListenerProxy, removeSessionListener
-
Methods inherited from interface org.apache.sshd.common.signature.SignatureFactoriesHolder
getSignatureFactories, getSignatureFactoriesNameList, getSignatureFactoriesNames
-
Methods inherited from interface org.apache.sshd.common.signature.SignatureFactoriesManager
setSignatureFactories, setSignatureFactoriesNameList, setSignatureFactoriesNames, setSignatureFactoriesNames
-
Methods inherited from interface org.apache.sshd.common.session.UnknownChannelReferenceHandlerManager
getUnknownChannelReferenceHandler, resolveUnknownChannelReferenceHandler, setUnknownChannelReferenceHandler
-
Methods inherited from interface org.apache.sshd.common.auth.UsernameHolder
getUsername
-
-
-
-
Method Detail
-
createBuffer
default Buffer createBuffer(byte cmd)
Create a new buffer for the specified SSH packet and reserve the needed space (5 bytes) for the packet header.- Parameters:
cmd
- the SSH command- Returns:
- a new buffer (of unknown size) ready for write
- See Also:
createBuffer(byte, int)
-
createBuffer
Buffer createBuffer(byte cmd, int estimatedSize)
Create a new buffer for the specified SSH packet and reserve the needed space (5 bytes) for the packet header.- Parameters:
cmd
- The SSH command to initialize the buffer withestimatedSize
- Estimated number of bytes the buffer will hold, 0 if unknown.- Returns:
- a new buffer ready for write
- See Also:
prepareBuffer(byte, Buffer)
-
prepareBuffer
Buffer prepareBuffer(byte cmd, Buffer buffer)
Prepare a new "clean" buffer while reserving the needed space (5 bytes) for the packet header.- Parameters:
cmd
- The SSH command to initialize the buffer withbuffer
- TheBuffer
instance to initialize- Returns:
- The initialized buffer
-
sendDebugMessage
IoWriteFuture sendDebugMessage(boolean display, java.lang.Object msg, java.lang.String lang) throws java.io.IOException
Sends anSSH_MSG_DEBUG
to the peer session- Parameters:
display
-true
if OK to display the message at the peer as-ismsg
- The message object whosetoString()
value to be used - ifnull
then the "null" string is sentlang
- 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:
java.io.IOException
- if an error occurred when encoding or sending the packet- See Also:
- RFC 4253 - section 11.3
-
sendIgnoreMessage
IoWriteFuture sendIgnoreMessage(byte... data) throws java.io.IOException
Sends anSSH_MSG_IGNORE
to the peer session- Parameters:
data
- The message data- Returns:
- An
IoWriteFuture
that can be used to check when the packet has actually been sent - Throws:
java.io.IOException
- if an error occurred when encoding or sending the packet- See Also:
- RFC 4253 - section 11.2
-
writePacket
IoWriteFuture writePacket(Buffer buffer) throws java.io.IOException
Encode and send the given buffer. The buffer has to have 5 bytes free at the beginning to allow the encoding to take place. Also, the write position of the buffer has to be set to the position of the last byte to write.- Parameters:
buffer
- the buffer to encode and send- Returns:
- An
IoWriteFuture
that can be used to check when the packet has actually been sent - Throws:
java.io.IOException
- if an error occurred when encoding sending the packet
-
writePacket
default IoWriteFuture writePacket(Buffer buffer, java.time.Duration timeout) throws java.io.IOException
Encode and send the given buffer with the specified timeout. If the buffer could not be written before the timeout elapses, the returnedIoWriteFuture
will be set with aTimeoutException
exception to indicate a timeout.- Parameters:
buffer
- the buffer to encode and spendtimeout
- the (nevernull
) timeout value - itsmilliseconds
value will be used- Returns:
- a future that can be used to check when the packet has actually been sent
- Throws:
java.io.IOException
- if an error occurred when encoding or sending the packet- See Also:
writePacket(Buffer, long)
-
writePacket
default IoWriteFuture writePacket(Buffer buffer, long maxWaitMillis) throws java.io.IOException
Encode and send the given buffer with the specified timeout. If the buffer could not be written before the timeout elapses, the returnedIoWriteFuture
will be set with aTimeoutException
exception to indicate a timeout.- Parameters:
buffer
- the buffer to encode and spendmaxWaitMillis
- the timeout in milliseconds- Returns:
- a future that can be used to check when the packet has actually been sent
- Throws:
java.io.IOException
- if an error occurred when encoding or sending the packet
-
writePacket
IoWriteFuture writePacket(Buffer buffer, long timeout, java.util.concurrent.TimeUnit unit) throws java.io.IOException
Encode and send the given buffer with the specified timeout. If the buffer could not be written before the timeout elapses, the returnedIoWriteFuture
will be set with aTimeoutException
exception to indicate a timeout.- Parameters:
buffer
- the buffer to encode and spendtimeout
- the timeoutunit
- the time unit of the timeout parameter- Returns:
- a future that can be used to check when the packet has actually been sent
- Throws:
java.io.IOException
- if an error occurred when encoding or sending the packet
-
request
default Buffer request(java.lang.String request, Buffer buffer, long timeout, java.util.concurrent.TimeUnit unit) throws java.io.IOException
Send a global request and wait for the response. This must only be used when sending aSSH_MSG_GLOBAL_REQUEST
with a result expected, else it will time out- Parameters:
request
- the request name - used mainly for logging and debuggingbuffer
- the buffer containing the global requesttimeout
- The number of time units to wait - must be positiveunit
- TheTimeUnit
to wait for the response- Returns:
- the return buffer if the request was successful,
null
otherwise. - Throws:
java.io.IOException
- if an error occurred when encoding or sending the packetjava.net.SocketTimeoutException
- If no response received within specified timeout
-
request
default Buffer request(java.lang.String request, Buffer buffer, java.time.Duration timeout) throws java.io.IOException
Send a global request and wait for the response. This must only be used when sending aSSH_MSG_GLOBAL_REQUEST
with a result expected, else it will time out- Parameters:
request
- the request name - used mainly for logging and debuggingbuffer
- the buffer containing the global requesttimeout
- The (nevernull
) timeout to wait - its milliseconds value is used- Returns:
- the return buffer if the request was successful,
null
otherwise. - Throws:
java.io.IOException
- if an error occurred when encoding or sending the packetjava.net.SocketTimeoutException
- If no response received within specified timeout
-
request
Buffer request(java.lang.String request, Buffer buffer, long maxWaitMillis) throws java.io.IOException
Send a global request and wait for the response. This must only be used when sending aSSH_MSG_GLOBAL_REQUEST
with a result expected, else it will time out- Parameters:
request
- the request name - used mainly for logging and debuggingbuffer
- the buffer containing the global requestmaxWaitMillis
- Max. time to wait for response (millis) - must be positive- Returns:
- the return buffer if the request was successful,
null
otherwise. - Throws:
java.io.IOException
- if an error occurred when encoding or sending the packetjava.net.SocketTimeoutException
- If no response received within specified timeout
-
exceptionCaught
void exceptionCaught(java.lang.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 anSshException
with a positive error code- Parameters:
t
- the exception to process
-
reExchangeKeys
KeyExchangeFuture reExchangeKeys() throws java.io.IOException
Initiate a new key exchange.- Returns:
- A
KeyExchangeFuture
for awaiting the completion of the exchange - Throws:
java.io.IOException
- If failed to request keys re-negotiation
-
getService
<T extends Service> T getService(java.lang.Class<T> clazz)
Get the service of the specified type. If the service is not of the specified class, an IllegalStateException will be thrown.- Type Parameters:
T
- The genericService
type- Parameters:
clazz
- The service class- Returns:
- The service instance
- Throws:
java.lang.IllegalStateException
- If failed to find a matching service
-
getLocalAddress
default java.net.SocketAddress getLocalAddress()
- Specified by:
getLocalAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of local machine which is associated with this session.
-
getRemoteAddress
default java.net.SocketAddress getRemoteAddress()
- Specified by:
getRemoteAddress
in interfaceConnectionEndpointsIndicator
- Returns:
- the socket address of remote peer.
-
getTimeoutStatus
TimeoutIndicator getTimeoutStatus()
Check if timeout has occurred.- Returns:
- the timeout status - never
null
-
getIdleTimeout
java.time.Duration getIdleTimeout()
- Returns:
- Timeout value in milliseconds for communication
-
getIdleTimeoutStart
java.time.Instant getIdleTimeoutStart()
- Returns:
- The timestamp value (milliseconds since EPOCH) when timer was started
-
resetIdleTimeout
java.time.Instant resetIdleTimeout()
Re-start idle timeout timer- Returns:
- The timestamp value (milliseconds since EPOCH) when timer was started
- See Also:
getIdleTimeoutStart()
-
getAuthTimeout
java.time.Duration getAuthTimeout()
- Returns:
- Timeout value in milliseconds for authentication stage
-
getAuthTimeoutStart
java.time.Instant getAuthTimeoutStart()
- Returns:
- The timestamp value (milliseconds since EPOCH) when timer was started
-
resetAuthTimeout
java.time.Instant resetAuthTimeout()
Re-start the authentication timeout timer- Returns:
- The timestamp value (milliseconds since EPOCH) when timer was started
- See Also:
getAuthTimeoutStart()
-
setAuthenticated
void setAuthenticated() throws java.io.IOException
- Throws:
java.io.IOException
-
getKex
KeyExchange getKex()
- Returns:
- The current
KeyExchange
in progress -null
if KEX not started or successfully completed
-
disconnect
void disconnect(int reason, java.lang.String msg) throws java.io.IOException
Send a disconnect packet with the given reason and message. Once the packet has been sent, the session will be closed asynchronously.- Parameters:
reason
- the reason code for this disconnectmsg
- the text message- Throws:
java.io.IOException
- if an error occurred sending the packet
-
startService
void startService(java.lang.String name, Buffer buffer) throws java.lang.Exception
- Parameters:
name
- Service namebuffer
- Extra information provided when the service start request was received- Throws:
java.lang.Exception
- If failed to start it
-
resolveAttribute
default <T> T resolveAttribute(AttributeRepository.AttributeKey<T> key)
Description copied from interface:AttributeRepository
Attempts to resolve the associated value by going up the store's hierarchy (if any)- Specified by:
resolveAttribute
in interfaceAttributeRepository
- Type Parameters:
T
- The generic attribute type- Parameters:
key
- The key of the attribute; must not benull
.- Returns:
null
if there is no value associated with the specified key either in this repository or any of its ancestors (if any available)
-
resolveAttribute
static <T> T resolveAttribute(Session session, AttributeRepository.AttributeKey<T> key)
Attempts to use the session's attribute, if not found then tries the factory manager- Type Parameters:
T
- The generic attribute type- Parameters:
session
- TheSession
- ignored ifnull
key
- The attribute key - nevernull
- Returns:
- Associated value -
null
if not found - See Also:
FactoryManagerHolder.getFactoryManager()
,FactoryManager.resolveAttribute(FactoryManager, AttributeRepository.AttributeKey)
-
-