server

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 33 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package server implements the sshkey-chat SSH server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadBackupStatsForCLI added in v0.2.0

func ReadBackupStatsForCLI(dataDir string) (failures, successes int64, lastSuccess, lastFailure time.Time, lastErrMsg string)

ReadBackupStatsForCLI is exposed so cmd/sshkey-ctl can read the scheduler's persisted stats from a separate process. Returns zero-valued stats if the file is absent or unreadable (operator just sees zeros, not an error).

func SetBackupStartupGracePeriod added in v0.2.0

func SetBackupStartupGracePeriod(d time.Duration) time.Duration

SetBackupStartupGracePeriod overrides the default 60s startup grace period. Test-only — production code never calls this. Returns the previous value so tests can restore it via t.Cleanup.

Types

type Client

type Client struct {
	UserID   string // nanoid (usr_ prefix) — immutable identity
	DeviceID string
	// Encoder is the thread-safe write surface for this client's
	// SSH Channel 1. All writes to the client — whether from a
	// per-handler goroutine (direct response) or from the per-client
	// writer goroutine (fanOut broadcast) — go through this wrapper,
	// which serializes concurrent Encode calls via an internal mutex
	// so they never interleave on the wire.
	Encoder         *safeEncoder
	Decoder         *protocol.Decoder
	Channel         ssh.Channel // NDJSON control-plane channel (1st "session" channel)
	DownloadChannel ssh.Channel // shared download channel (2nd "session" channel); nil only if the client failed to open it within the grace period — downloads fail closed with not_found
	Conn            *ssh.ServerConn

	// LastActivity is the unix-seconds timestamp of the most recent
	// successful NDJSON decode on Channel 1. Updated by messageLoop
	// on every frame; read by runIdleWatchdog (Phase 17b Step 5a)
	// to detect NDJSON-level idleness and close the channel if the
	// configured idle_timeout_seconds is exceeded.
	//
	// Zero value means "not yet stamped" — watchdog interprets 0 as
	// "fresh client, no activity yet" and grants one full timeout
	// window before closing.
	LastActivity atomic.Int64
	// contains filtered or unexported fields
}

Client represents a connected client session.

Three SSH session channels are opened per connection, in fixed order:

  1. Control / NDJSON (this struct's `Channel`). Long-lived control plane — protocol verbs including `download` requests.
  2. Download channel (`DownloadChannel`). Server writes raw file bytes here in response to `download` verbs. One in-flight download per session; client serializes.
  3. Upload channel (handled elsewhere). Client writes raw bytes.

Authorization (`authorizeDownload`), `file_contexts` binding, cascade cleanup, hash verification, and counter signals are all enforced on Channel 1's dispatch path before bytes move on Channel 2.

func (*Client) TryEnqueue added in v0.2.0

func (c *Client) TryEnqueue(msg any) (queued bool, drops int32)

TryEnqueue attempts a non-blocking send of msg onto the outbound queue. Returns (true, 0) on success with consecutiveDrops reset; returns (false, drops) on full queue with the drops counter post-increment. Callers use the drops return to decide whether to trigger a consecutive-drop disconnect.

Phase 17b Step 5b. Nil sendCh (test-mode Client) returns (false, 0) — no queueing, no drop tracking; direct-Encode sites see the call as a no-op in tests that don't exercise the queue.

type Server

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

Server is the sshkey-chat SSH server.

func New

func New(cfg *config.Config, logger *slog.Logger, dataDir ...string) (*Server, error)

New creates a new server with the given config and data directory.

func (*Server) BackupFailuresTotal added in v0.2.0

func (s *Server) BackupFailuresTotal() int64

BackupFailuresTotal is a test-only accessor that reads the in-process counter via the persisted file. atomic.Int64 isn't used because the stats are read/written under no concurrent contention — only the scheduler goroutine writes, and at most every interval.

Production code uses ReadBackupStatsForCLI; this is just a brief alias for tests that only care about the counter.

func (*Server) BackupSuccessesTotal added in v0.2.0

func (s *Server) BackupSuccessesTotal() int64

BackupSuccessesTotal mirrors BackupFailuresTotal for the success counter. Test-only.

func (*Server) Close

func (s *Server) Close() error

func (*Server) CounterIncForTesting added in v0.2.0

func (s *Server) CounterIncForTesting(signal, deviceID string)

CounterIncForTesting increments a counter entry directly. This is test-only plumbing used by integration tests that need to drive the auto-revoke pipeline across all signals without crafting one protocol-level trigger frame per signal.

func (*Server) CounterSnapshotForTesting added in v0.2.0

func (s *Server) CounterSnapshotForTesting() map[string]map[string]int64

CounterSnapshotForTesting returns the current per-signal counter map. Exposed for integration-test assertions that need to verify launch-gate "no misbehavior signals" contracts against a running server instance.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts the SSH listener and accepts connections.

func (*Server) ProcessAutoRevokeForTesting added in v0.2.0

func (s *Server) ProcessAutoRevokeForTesting()

ProcessAutoRevokeForTesting runs a single synchronous auto-revoke evaluation tick. Exposed so integration tests don't need to sleep for poll intervals.

func (*Server) ProcessPendingDeviceRevocationsForTesting added in v0.2.0

func (s *Server) ProcessPendingDeviceRevocationsForTesting()

ProcessPendingDeviceRevocationsForTesting drains pending revocations once. Integration tests use this to deterministically kick sessions immediately.

func (*Server) Reload

func (s *Server) Reload()

Reload reloads config files. Can be called from SIGHUP handler.

func (*Server) Store added in v0.2.0

func (s *Server) Store() *store.Store

Close shuts down the server gracefully. Store returns the underlying Store. Used by tests that need to seed or inspect persistent state directly without going through the protocol surface.

Jump to

Keyboard shortcuts

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