shelob

package module
v0.0.0-...-bcfee49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2019 License: Apache-2.0 Imports: 15 Imported by: 1

README

sshh Build Status Coverage Status

An SSH Handler library which makes it easy to implement SSH servers

Documentation

Index

Constants

View Source
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 Handle

func Handle(h SessionHandler)

Handle sets the default handler.

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

func WithServerConn(ctx context.Context, sshConn *ssh.ServerConn) context.Context

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

type ConnectionClosedEvent struct {
	LocalAddr  net.Addr
	RemoteAddr net.Addr
}

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

type ConnectionOpenedEvent struct {
	LocalAddr  net.Addr
	RemoteAddr net.Addr
}

ConnectionOpenedEvent is emitted when a connection is successfully established.

type Event

type Event interface {
}

Event represents an event on the server.

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

type HandshakeFailedEvent struct {
	LocalAddr  net.Addr
	RemoteAddr net.Addr
	Error      error
}

HandshakeFailedEvent is emitted when the SSH handshake failed.

type HandshakeSuccessfulEvent

type HandshakeSuccessfulEvent struct {
	LocalAddr  net.Addr
	RemoteAddr net.Addr
}

HandshakeSuccessfulEvent is emitted when the SSH handshake was successful.

type ListenerClosedEvent

type ListenerClosedEvent struct {
}

ListenerClosedEvent is emitted when the listener is closed.

type ListenerOpenedEvent

type ListenerOpenedEvent struct {
	Addr *net.TCPAddr
}

ListenerOpenedEvent is emitted when the listener is opened.

type MaxClientConnectionsEvent

type MaxClientConnectionsEvent struct {
	LocalAddr  net.Addr
	RemoteAddr net.Addr
}

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

type OptionFunc func(*Config) error

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 Pty

type Pty struct {
	Term   string
	Window Window
}

Pty represents a PTY request and configuration.

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

type Server struct {
	Addr *net.TCPAddr
	// contains filtered or unexported fields
}

Server handles all the incoming connections as well as handler dispatch.

func New

func New(ctx context.Context, conf *Config) (*Server, error)

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

func (s *Server) ListenAndServe() error

ListenAndServe starts accepting client connections.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the server and kills all goroutines. This method is blocking.

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

type SessionHandler func(ctx context.Context, s Session) int

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.

type Window

type Window struct {
	Width  int
	Height int
}

Window represents the size of a PTY window.

Directories

Path Synopsis
examples
shell command
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL