session

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package session provides ProtoMsg Session abstraction. A session is a persistent communication channel with it's own set of control packets for opening and closing channels and performing end-to-end health checks. This package provides three levels of abstractions. On top, the Router manages creation and deletion as well as routing of messages to Sessions based on the ProtoMsg 'sid' (SessionID) header. The Session abstraction manages persistent session context such as creating and deleting as well as routing messages to SessionHandlers based on the 'proto' (ProtoType) header. The Session also takes care of session control messages. The SessionHandler is the application specific handler interface that takes care of the application specific messages. The ServeProtoMsg function is called for every inbound message with the associated ProtoType for the registered handler. If the SessionHandler requires persisting resources, Close MUST free these resources when the session shuts down. NOTE: ProtoRoutes that are used to map ProtoTypes to SessionHandlers maps

ProtoTypes to Constructors, since the Router must be able to regenerate
new Sessions with independent sets of SessionHandlers.

Index

Constants

View Source
const (
	ACKSlidingWindowSend = 10
	ACKSlidingWindowRecv = 20
	FileTransferBufSize  = 4096
)
View Source
const (
	NormalMessage int = iota + 1
	ErrorMessage
)
View Source
const MaxTraceback = 32
View Source
const (
	NoExpirationTimeout = time.Second * 0
)

Variables

View Source
var (
	ErrNoSession   = errors.New("session: does not exist")
	ErrNoSessionID = errors.New("session: message does not have a session ID")
)
View Source
var (
	ErrSessionShellAlreadyRunning         = errors.New("shell is already running")
	ErrSessionShellNotRunning             = errors.New("shell is not running")
	ErrSessionShellTooManySessionsPerUser = errors.New("user has too many open sessions")
	ErrSessionNotFound                    = errors.New("session not found")
	ErrSessionTooManyShellsAlreadyRunning = errors.New("too many shells spawned")
)
View Source
var (
	MaxUserSessions = 1
)

Functions

func DeleteSessionById

func DeleteSessionById(id string) error

func GetSessionCount

func GetSessionCount() int

func GetSessionIds

func GetSessionIds() []string

func StopSessionById added in v1.0.2

func StopSessionById(sessionId string) error

func StopSessionByUserId

func StopSessionByUserId(userId string) (count uint, err error)

func TerminateAllSessions

func TerminateAllSessions() (shellCount int, sessionCount int, err error)

func TerminateExpiredSessions

func TerminateExpiredSessions() (
	shellCount int,
	sessionCount int,
	totalExpiredLeft int,
	err error,
)

Types

type Config

type Config struct {
	// IdleTimeout is the duration a session can remain inactive before
	// it shuts down.
	IdleTimeout time.Duration
}

Config is the static configuration for Sessions and Routers.

type Constructor

type Constructor func() SessionHandler

Constructor is a function for SessionHandler initializers. To create a Router, all ProtoType Routes must route to a SessionHandler Constructor (factory).

func FileTransfer

func FileTransfer(root string, limits config.Limits) Constructor

FileTransfer creates a new filetransfer constructor

func MenderClient

func MenderClient() Constructor

func PortForward

func PortForward() Constructor

type FileTransferHandler

type FileTransferHandler struct {
	// contains filtered or unexported fields
}

func (*FileTransferHandler) Close

func (h *FileTransferHandler) Close() error

func (*FileTransferHandler) DownloadHandler

func (h *FileTransferHandler) DownloadHandler(
	fd *os.File,
	msg *ws.ProtoMsg,
	w api.Sender,
) (err error)

func (*FileTransferHandler) Error

func (h *FileTransferHandler) Error(code int, msg *ws.ProtoMsg, w api.Sender, err error)

func (*FileTransferHandler) FileUploadHandler

func (h *FileTransferHandler) FileUploadHandler(
	msg *ws.ProtoMsg,
	params model.UploadRequest,
	dstPath string,
	w api.Sender,
) (err error)

func (*FileTransferHandler) InitFileDownload

func (h *FileTransferHandler) InitFileDownload(
	msg *ws.ProtoMsg,
	w api.Sender,
) (code int, err error)

func (*FileTransferHandler) InitFileUpload

func (h *FileTransferHandler) InitFileUpload(msg *ws.ProtoMsg, w api.Sender) (code int, err error)

func (*FileTransferHandler) ServeProtoMsg

func (h *FileTransferHandler) ServeProtoMsg(msg *ws.ProtoMsg, w api.Sender)

func (*FileTransferHandler) StatFile

func (h *FileTransferHandler) StatFile(msg *ws.ProtoMsg, w api.Sender)

type HandlerFunc

type HandlerFunc func(msg *ws.ProtoMsg, w api.Sender)

func (HandlerFunc) Close

func (h HandlerFunc) Close() error

func (HandlerFunc) ServeProtoMsg

func (h HandlerFunc) ServeProtoMsg(msg *ws.ProtoMsg, w api.Sender)

type PortForwardHandler

type PortForwardHandler struct {
	// contains filtered or unexported fields
}

func (*PortForwardHandler) Close

func (h *PortForwardHandler) Close() error

func (*PortForwardHandler) ServeProtoMsg

func (h *PortForwardHandler) ServeProtoMsg(msg *ws.ProtoMsg, w api.Sender)

type PortForwarder

type PortForwarder struct {
	SessionID    string
	ConnectionID string
	Sender       api.Sender
	// contains filtered or unexported fields
}

func (*PortForwarder) Close

func (f *PortForwarder) Close(sendStopMessage bool) error

func (*PortForwarder) Connect

func (f *PortForwarder) Connect(protocol string, host string, portNumber uint16) error

func (*PortForwarder) Read

func (f *PortForwarder) Read()

func (*PortForwarder) Write

func (f *PortForwarder) Write(body []byte) error

type ProtoRoutes

type ProtoRoutes map[ws.ProtoType]Constructor

type Router

type Router interface {
	RouteMessage(msg *ws.ProtoMsg, w api.Sender) error
}

func NewRouter

func NewRouter(routes ProtoRoutes, config Config) Router

type Session

type Session struct {
	Config
	ID     string
	Routes ProtoRoutes
	// contains filtered or unexported fields
}

func New

func New(
	sessionID string,
	msgChan chan *ws.ProtoMsg,
	w api.Sender,
	routes ProtoRoutes,
	config Config,
) *Session

func (*Session) Done

func (sess *Session) Done() <-chan struct{}

func (*Session) Error

func (sess *Session) Error(msg *ws.ProtoMsg, close bool, errMessage string)

func (*Session) HandleControl

func (sess *Session) HandleControl(msg *ws.ProtoMsg) (close bool)

func (*Session) ListenAndServe

func (sess *Session) ListenAndServe()

func (*Session) MsgChan

func (sess *Session) MsgChan() chan<- *ws.ProtoMsg

func (*Session) Ping

func (sess *Session) Ping() error

type SessionHandler

type SessionHandler interface {
	// ServeProtoMsg handles individual messages within the associated
	// class of ProtoTypes.
	ServeProtoMsg(msg *ws.ProtoMsg, w api.Sender)
	// Close frees allocated resources when the session closes. It SHOULD
	// return an error if the session closes unexpectedly.
	Close() error
}

SessionHandler defines the interface for application specific ProtoMsg handlers.

type SessionStatus

type SessionStatus int
const (
	SessionStatusActive SessionStatus = iota
	SessionStatusExpired
	SessionStatusIdle
	SessionStatusHanged
	SessionStatusEmpty
	SessionStatusNew
)

type TerminalSession

type TerminalSession struct {
	// contains filtered or unexported fields
}

func GetSessionById

func GetSessionById(id string) *TerminalSession

func GetSessionsByUserId

func GetSessionsByUserId(userId string) []*TerminalSession

func NewShellSession

func NewShellSession(
	sock api.Sender,
	sessionId string,
	userId string,
	expireAfter time.Duration,
	expireAfterIdle time.Duration,
) (s *TerminalSession, err error)

func (*TerminalSession) GetActiveAtFmt

func (s *TerminalSession) GetActiveAtFmt() string

func (*TerminalSession) GetExpiresAtFmt

func (s *TerminalSession) GetExpiresAtFmt() string

func (*TerminalSession) GetId

func (s *TerminalSession) GetId() string

func (*TerminalSession) GetShellCommandPath

func (s *TerminalSession) GetShellCommandPath() string

func (*TerminalSession) GetShellPid

func (s *TerminalSession) GetShellPid() int

func (*TerminalSession) GetStartedAtFmt

func (s *TerminalSession) GetStartedAtFmt() string

func (*TerminalSession) GetStatus

func (s *TerminalSession) GetStatus() SessionStatus

func (*TerminalSession) HealthcheckPong

func (s *TerminalSession) HealthcheckPong()

func (*TerminalSession) IsExpired

func (s *TerminalSession) IsExpired(setStatus bool) bool

func (*TerminalSession) ResizeShell

func (s *TerminalSession) ResizeShell(height, width uint16)

func (*TerminalSession) ShellCommand

func (s *TerminalSession) ShellCommand(m *ws.ProtoMsg) error

func (*TerminalSession) StartShell

func (s *TerminalSession) StartShell(
	sock api.Sender,
	sessionId string,
	terminal TerminalSettings,
) error

func (*TerminalSession) StopShell

func (s *TerminalSession) StopShell() (err error)

type TerminalSettings

type TerminalSettings struct {
	Uid            uint32
	Gid            uint32
	Shell          string
	HomeDir        string
	TerminalString string
	Height         uint16
	Width          uint16
	ShellArguments []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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