Documentation
¶
Index ¶
- Constants
- func Handle(h SessionHandler)
- func ListenAndServe(addr string, opts ...OptionFunc) error
- func SSHServerConn(ctx context.Context) (*ssh.ServerConn, bool)
- func WithServerConn(ctx context.Context, sshConn *ssh.ServerConn) context.Context
- type ChannelEvent
- type ChannelHandler
- type Config
- type ConnectionClosedEvent
- type ConnectionFailedEvent
- type ConnectionOpenedEvent
- type Event
- type EventHandler
- type HandshakeFailedEvent
- type HandshakeSuccessfulEvent
- type ListenerClosedEvent
- type ListenerOpenedEvent
- type MaxClientConnectionsEvent
- type MaxConnectionsEvent
- type OptionFunc
- func WithAuthLogCallback(cb func(conn ssh.ConnMetadata, method string, err error)) OptionFunc
- func WithChannelHandler(chType string, handler ChannelHandler) OptionFunc
- func WithConnectionCallback(fn func(net.Conn) net.Conn) OptionFunc
- func WithEventHandler(handler EventHandler) OptionFunc
- func WithHostKey(signer ssh.Signer) OptionFunc
- func WithHostKeyFile(filepath string) OptionFunc
- func WithMaxClientConnections(conns int) OptionFunc
- func WithMaxConnectionDuration(timeout time.Duration) OptionFunc
- func WithMaxConnections(conns int) OptionFunc
- func WithMaxDeadline(deadline time.Duration) OptionFunc
- func WithNoClientAuth() OptionFunc
- func WithPasswordAuth(user, password string) OptionFunc
- func WithPublicKeyAuth(pubkey ssh.PublicKey) OptionFunc
- func WithRequestHandler(reqType string, handler RequestHandler) OptionFunc
- func WithServerConfig(c *ssh.ServerConfig) OptionFunc
- func WithSignalCh(ch chan os.Signal) OptionFunc
- type Pty
- type PublicKeyCallback
- type RequestEvent
- type RequestHandler
- type Server
- type ServerStartedEvent
- type ServerStoppedEvent
- type Session
- type SessionHandler
- type UnknownChannelEvent
- type UnknownRequestEvent
- type Window
Constants ¶
const ( SIGABRT = "ABRT" SIGALRM = "ALRM" SIGFPE = "FPE" SIGHUP = "HUP" SIGILL = "ILL" SIGINT = "INT" SIGKILL = "KILL" SIGPIPE = "PIPE" SIGQUIT = "QUIT" SIGSEGV = "SEGV" SIGTERM = "TERM" SIGUSR1 = "USR1" SIGUSR2 = "USR2" )
POSIX signals as listed in RFC 4254 Section 6.10.
Variables ¶
This section is empty.
Functions ¶
func ListenAndServe ¶
func ListenAndServe(addr string, opts ...OptionFunc) error
ListenAndServe starts the server with the options.
func SSHServerConn ¶
func SSHServerConn(ctx context.Context) (*ssh.ServerConn, bool)
SSHServerConn returns a ssh.ServerConn from a context
func WithServerConn ¶
WithServerConn adds a ssh.ServerConn to a context.
Types ¶
type ChannelEvent ¶
type ChannelEvent struct {
Conn *ssh.ServerConn
ChannelType string
}
ChannelEvent is emitted when a channel is recieved on a connection.
type ChannelHandler ¶
type ChannelHandler interface {
HandleChannel(ctx context.Context, ch ssh.NewChannel)
}
ChannelHandler handles channels inside a connection.
func NewSessionChannelHandler ¶
func NewSessionChannelHandler(handler SessionHandler, allowPty bool, allowAgentFwd bool) ChannelHandler
NewSessionChannelHandler creates a new ChannelHandler for session channels.
type Config ¶
type Config struct {
// Addr specifies the bind address the SSH server will listen on.
Addr string
// MaxConnections is the maximum connections allowed by the server.
MaxConnections int
// MaxClientConnections is the maximum connections from 1 IP address.
MaxClientConnections int
// MaxDeadline is the maximum time the listener will block
// between connections. As a consequence, this duration
// also sets the max length of time the SSH server will
// be unresponsive before shutting down.
MaxDeadline time.Duration
// MaxConnectionDuration is the maximum length of time a connection can stay open.
MaxConnectionDuration time.Duration
// RequestHandlers is a map of RequestHandlers which handle certain global ssh.Requests.
RequestHandlers map[string]RequestHandler
// ChannelHandlers is a map of ChannelHandlers which handle SSH channels based on type.
ChannelHandlers map[string]ChannelHandler
// ConnectionCallback allows for modification of the incoming network connection and/or connection wrapping
ConnectionCallback func(net.Conn) net.Conn
// EventHandler handles events for logging, etc. Must be non-blocking.
EventHandler EventHandler
// PrivateKey is added to the SSH config as a host key.
PrivateKey ssh.Signer
// SignalChan recieves os.Signal for stopping the server with signals.
SignalChan chan os.Signal
// ServerConfig configures the underlying SSH server. It allows for full control of the authenication mechanisms.
ServerConfig *ssh.ServerConfig
}
Config is used to setup the Server, including the server config and the Handlers.
type ConnectionClosedEvent ¶
ConnectionClosedEvent is emitted when the connection is closed.
type ConnectionFailedEvent ¶
type ConnectionFailedEvent struct {
Error error
}
ConnectionFailedEvent is emitted when theres a connection failure.
type ConnectionOpenedEvent ¶
ConnectionOpenedEvent is emitted when a connection is successfully established.
type EventHandler ¶
type EventHandler func(Event)
EventHandler allows for handling of server event notification. Must be non-blocking.
func LoggingEventHandler ¶
func LoggingEventHandler(logger *log.Logger) EventHandler
LoggingEventHandler logs all the events to the standard logging interface.
type HandshakeFailedEvent ¶
HandshakeFailedEvent is emitted when the SSH handshake failed.
type HandshakeSuccessfulEvent ¶
HandshakeSuccessfulEvent is emitted when the SSH handshake was successful.
type ListenerClosedEvent ¶
type ListenerClosedEvent struct {
}
ListenerClosedEvent is emitted when the listener is closed.
type ListenerOpenedEvent ¶
ListenerOpenedEvent is emitted when the listener is opened.
type MaxClientConnectionsEvent ¶
MaxClientConnectionsEvent is emitted when a client reaches the maximum client connection limit.
type MaxConnectionsEvent ¶
type MaxConnectionsEvent struct {
}
MaxConnectionsEvent is emitted when the maximum connection limit is reached.
type OptionFunc ¶
OptionFunc modifies the default config options.
func WithAuthLogCallback ¶
func WithAuthLogCallback(cb func(conn ssh.ConnMetadata, method string, err error)) OptionFunc
func WithChannelHandler ¶
func WithChannelHandler(chType string, handler ChannelHandler) OptionFunc
func WithConnectionCallback ¶
func WithConnectionCallback(fn func(net.Conn) net.Conn) OptionFunc
func WithEventHandler ¶
func WithEventHandler(handler EventHandler) OptionFunc
func WithHostKey ¶
func WithHostKey(signer ssh.Signer) OptionFunc
func WithHostKeyFile ¶
func WithHostKeyFile(filepath string) OptionFunc
func WithMaxClientConnections ¶
func WithMaxClientConnections(conns int) OptionFunc
func WithMaxConnectionDuration ¶
func WithMaxConnectionDuration(timeout time.Duration) OptionFunc
func WithMaxConnections ¶
func WithMaxConnections(conns int) OptionFunc
func WithMaxDeadline ¶
func WithMaxDeadline(deadline time.Duration) OptionFunc
func WithNoClientAuth ¶
func WithNoClientAuth() OptionFunc
func WithPasswordAuth ¶
func WithPasswordAuth(user, password string) OptionFunc
func WithPublicKeyAuth ¶
func WithPublicKeyAuth(pubkey ssh.PublicKey) OptionFunc
func WithRequestHandler ¶
func WithRequestHandler(reqType string, handler RequestHandler) OptionFunc
func WithServerConfig ¶
func WithServerConfig(c *ssh.ServerConfig) OptionFunc
func WithSignalCh ¶
func WithSignalCh(ch chan os.Signal) OptionFunc
type PublicKeyCallback ¶
type PublicKeyCallback func(meta ssh.ConnMetadata, key ssh.PublicKey) (perm *ssh.Permissions, err error)
PublicKeyCallback represents the function type for Public Key auth in crypto/ssh.
type RequestEvent ¶
type RequestEvent struct {
Conn *ssh.ServerConn
RequestType string
}
RequestEvent is emitted when a gloabl request is recieved on a connection.
type RequestHandler ¶
type RequestHandler interface {
HandleRequest(ctx context.Context, req *ssh.Request) (ok bool, payload []byte)
}
RequestHandler handles global requests on a connection.
type Server ¶
Server handles all the incoming connections as well as handler dispatch.
func New ¶
New creates a new server with the given config. If the Bind address is invalid an error will be returned when started. If there is an error starting the TCP server, the error will be returned.
func (*Server) ListenAndServe ¶
ListenAndServe starts accepting client connections.
type ServerStartedEvent ¶
type ServerStartedEvent struct{}
ServerStartedEvent is emitted when the server.Start() is called but before the net.Listener is created. If there is an error creating the net.Listener, an error is returned from the ListenAndServe method.
type ServerStoppedEvent ¶
type ServerStoppedEvent struct{}
ServerStoppedEvent is emitted when the server stops but before all the connections have been closed.
type Session ¶
type Session interface {
ssh.Channel
// User returns the username used when establishing the SSH connection.
User() string
// RemoteAddr returns the net.Addr of the client side of the connection.
RemoteAddr() net.Addr
// LocalAddr returns the net.Addr of the server side of the connection.
LocalAddr() net.Addr
// Environ returns a copy of strings representing the environment set by the
// user for this session, in the form "key=value".
Environ() []string
// Exit sends an exit status and then closes the session.
Exit(code int) error
// Command returns a shell parsed slice of arguments that were provided by the
// user. Shell parsing splits the command string according to POSIX shell rules,
// which considers quoting not just whitespace.
Command() []string
// PublicKey returns the PublicKey used to authenticate. If a public key was not
// used it will return nil.
PublicKey() ssh.PublicKey
// Permissions returns a copy of the Permissions object that was available for
// setup in the auth handlers.
Permissions() *ssh.Permissions
// Pty returns PTY information, a channel of window size changes, and a boolean
// of whether or not a PTY was accepted for this session.
Pty() (Pty, <-chan Window, bool)
WriteString(s string) (n int, err error)
// Signals registers a channel to receive signals sent from the client. The
// channel must handle signal sends or it will block the SSH request loop.
// Registering nil will unregister the channel from signal sends. During the
// time no channel is registered signals are buffered up to a reasonable amount.
// If there are buffered signals when a channel is registered, they will be
// sent in order on the channel immediately after registering.
Signals(c chan<- os.Signal)
}
Session represents a user's SSH session.
type SessionHandler ¶
SessionHandler handles session channels.
var DefaultHandler SessionHandler = func(_ context.Context, s Session) int {
return 0
}
DefaultHandler is the default session handler.
type UnknownChannelEvent ¶
type UnknownChannelEvent struct {
Conn *ssh.ServerConn
ChannelType string
}
UnknownChannelEvent is emitted when the channel type does not have a handler.
type UnknownRequestEvent ¶
type UnknownRequestEvent struct {
Conn *ssh.ServerConn
RequestType string
}
UnknownRequestEvent is emitted when the global request does not have a handler.