Documentation
¶
Overview ¶
Package session owns PTY terminal sessions: lifecycle, limits, bounded output replay, and fan-out to viewers.
Index ¶
- Constants
- Variables
- type CommandPolicy
- type Config
- type CreateRequest
- type Event
- type EventKind
- type Exit
- type Info
- type Lifecycle
- type Manager
- func (m *Manager) Close(ctx context.Context) error
- func (m *Manager) Create(ctx context.Context, req CreateRequest) (Info, error)
- func (m *Manager) Get(ctx context.Context, id string) (Info, error)
- func (m *Manager) List(ctx context.Context) ([]Info, error)
- func (m *Manager) Resize(id string, rows, cols int) error
- func (m *Manager) Subscribe(ctx context.Context, id string, opts SubscribeOptions) (*Subscription, error)
- func (m *Manager) Terminate(ctx context.Context, id, remoteAddr string) (Info, error)
- func (m *Manager) Write(ctx context.Context, id string, data []byte) error
- func (m *Manager) WriteAuthorized(ctx context.Context, id string, data []byte, allow func() bool) error
- type Recorder
- type Recorders
- type ReplayGapError
- type Repository
- type SubscribeOptions
- type Subscription
- type Timer
Constants ¶
const ( MaxRows = 1000 MaxCols = 1000 MaxArgs = 256 MaxCommandBytes = 32 << 10 )
Size and command limits.
const EventOverhead = 48
EventOverhead is the fixed per-event cost charged against replay and client queue budgets, so floods of tiny chunks stay bounded.
Variables ¶
var ( ErrNotFound = errors.New("session: not found") ErrSessionLimit = errors.New("session: active session limit reached") ErrViewerLimit = errors.New("session: viewer limit reached") ErrInvalidState = errors.New("session: invalid state") ErrInvalidArgument = errors.New("session: invalid argument") ErrReplayGap = errors.New("session: replay unavailable") ErrSlowConsumer = errors.New("session: slow consumer") ErrProcessFailed = errors.New("session: process failed") ErrClosed = errors.New("session: manager closed") ErrSubscriptionClosed = errors.New("session: subscription closed") ErrInputRevoked = errors.New("session: input no longer authorized") ErrCommandDenied = errors.New("session: command not permitted") )
Functions ¶
This section is empty.
Types ¶
type CommandPolicy ¶
type CommandPolicy struct {
// contains filtered or unexported fields
}
CommandPolicy restricts which executables sessions may run. A command is matched both as given and as resolved through PATH and symlinks, so an alias of a denied binary is denied too. Deny wins over Allow; an empty allow list permits every command that is not denied.
func AllowAllCommands ¶
func AllowAllCommands() *CommandPolicy
AllowAllCommands returns the policy a Manager uses when none is configured.
func NewCommandPolicy ¶
func NewCommandPolicy(allow, deny []string) (*CommandPolicy, error)
NewCommandPolicy builds a policy from absolute executable paths.
func (*CommandPolicy) Permits ¶
func (p *CommandPolicy) Permits(path string) bool
Permits reports whether path may be run.
type Config ¶
type Config struct {
Store Repository
Starter pty.Starter
DefaultCommand pty.Command
// CommandPolicy restricts the executables sessions may run. Nil means
// AllowAllCommands.
CommandPolicy *CommandPolicy
MaxSessions int
MaxViewers int
IdleTimeout time.Duration
// KillGrace is how long a terminated process group has between SIGTERM
// and SIGKILL.
KillGrace time.Duration
// DrainTimeout bounds how long output is read after the process exits.
DrainTimeout time.Duration
// ForceReapTimeout bounds how long Close waits for sessions to be
// reaped after it SIGKILLs them.
ForceReapTimeout time.Duration
ReplayBytes int
ClientQueueBytes int
// OnEnd, if set, is called once per session after its final state is
// persisted and its viewers have been told it ended.
OnEnd func(id string)
// Recorders, if set, records sessions not created with NoRecording.
Recorders Recorders
Logger *slog.Logger
Now func() time.Time
AfterFunc func(time.Duration, func()) Timer
}
Config wires a Manager. Zero numeric fields receive defaults; a zero IdleTimeout disables idle termination.
type CreateRequest ¶
type CreateRequest struct {
Command pty.Command
Rows, Cols int
RemoteAddr string
// NoRecording opts the session out of recording.
NoRecording bool
}
CreateRequest describes a new session. A zero Command uses the default command; zero Rows or Cols use 24x80.
type Event ¶
Event is one item delivered to a subscriber. Data must not be modified. Resize events carry Rows and Cols, have no Seq, and are not replayed: a new subscriber learns the size from Info.
type EventKind ¶
type EventKind int
EventKind distinguishes output, size changes, and the final exit event.
type Exit ¶
type Exit struct {
State store.TerminalState
Code *int
Signal string
}
Exit describes how a session ended. Code is nil when the process was killed by Signal or failed.
type Lifecycle ¶
type Lifecycle struct {
State store.TerminalState
ExitCode *int
Signal string
Reason string
}
Lifecycle is a recorded state change. Reason is a fixed code such as "admin", "idle", "shutdown", "start_failed", or "wait_failed"; it never carries addresses, commands, or error text.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns every live session.
func NewManager ¶
NewManager validates cfg and marks sessions orphaned by a previous server process as failed.
func (*Manager) Close ¶
Close terminates every session and waits until all are reaped. Sessions still running when ctx is done have their process groups SIGKILLed, and Close waits up to Config.ForceReapTimeout more for them to be reaped; it returns an error only if some are not. Later calls to Create fail with ErrClosed. It is idempotent.
func (*Manager) Create ¶
Create starts a session and returns it running, or returns ErrProcessFailed after recording the failed session.
func (*Manager) Subscribe ¶
func (m *Manager) Subscribe(ctx context.Context, id string, opts SubscribeOptions) (*Subscription, error)
Subscribe attaches a viewer to a running session.
func (*Manager) Terminate ¶
Terminate ends a session and waits until its process is reaped or ctx is done. Terminating an ended session returns its final metadata.
func (*Manager) Write ¶
Write forwards terminal input through the session's single input writer. It returns ctx.Err() if the PTY does not accept the input before ctx is done; input already handed to the writer may still be delivered later.
func (*Manager) WriteAuthorized ¶
func (m *Manager) WriteAuthorized(ctx context.Context, id string, data []byte, allow func() bool) error
WriteAuthorized is Write, except allow is called immediately before the input reaches the PTY and ErrInputRevoked is returned if it reports false. Input queued behind a slow write therefore cannot outlive a revocation.
type Recorder ¶
type Recorder interface {
// Output records PTY output. The session never modifies data after the
// call, so the recorder may retain it but must not modify it.
Output(data []byte)
Resize(rows, cols int)
Lifecycle(Lifecycle)
// End records the final lifecycle event. No calls follow it.
End(Lifecycle)
}
Recorder receives one session's terminal output, size, and lifecycle events in order. It never receives terminal input. Its methods are called while the session's lock is held, so they must return without blocking.
type Recorders ¶
type Recorders interface {
// Begin is called once the session is persisted and before its process
// starts, so no output precedes it. A nil Recorder leaves the session
// unrecorded.
Begin(info Info) Recorder
}
Recorders begins session recordings.
type ReplayGapError ¶
type ReplayGapError struct {
AfterSeq, FirstSeq, LastSeq uint64
}
ReplayGapError reports that output after AfterSeq is no longer buffered. FirstSeq is the oldest buffered sequence and LastSeq the newest.
func (*ReplayGapError) Error ¶
func (e *ReplayGapError) Error() string
func (*ReplayGapError) Is ¶
func (e *ReplayGapError) Is(target error) bool
type Repository ¶
type Repository interface {
CreateTerminalSession(context.Context, store.TerminalSession) error
UpdateTerminalSession(context.Context, store.TerminalSession) error
TerminalSession(context.Context, string) (store.TerminalSession, error)
TerminalSessions(context.Context) ([]store.TerminalSession, error)
FailInterruptedTerminalSessions(context.Context, time.Time) (int64, error)
AppendAuditEvent(context.Context, store.AuditEvent) error
}
Repository persists session metadata and audit events.
type SubscribeOptions ¶
SubscribeOptions selects replayed output. Without Resume every buffered event is replayed; with Resume only events after AfterSeq are, or ErrReplayGap is returned if some are no longer buffered.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription is one viewer's bounded event queue. Enqueueing never blocks: a viewer whose queue exceeds its byte budget is disconnected with ErrSlowConsumer. Next must be called from one goroutine at a time.
func (*Subscription) Close ¶
func (s *Subscription) Close()
Close detaches the viewer. The session keeps running. It is idempotent.
func (*Subscription) Info ¶
func (s *Subscription) Info() Info
Info is the session metadata at the time of subscription.
func (*Subscription) LastSeq ¶
func (s *Subscription) LastSeq() uint64
LastSeq is the newest output sequence at the time of subscription.