server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: GPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureHostKey added in v0.1.0

func EnsureHostKey(path string) (created bool, err error)

EnsureHostKey makes path usable as the server's host key, generating a new ed25519 key there if the file does not exist yet. It reports whether it created one.

The host key is what an SSH client pins in known_hosts, so it has to be the same key on every start: a server that generated a fresh one each time would greet every returning client with the host-key-changed warning.

func HostKeyFingerprint added in v0.1.0

func HostKeyFingerprint(path string) (string, error)

HostKeyFingerprint returns the SHA256 fingerprint of the host key at path, in the form OpenSSH prints when it asks about an unknown host, so a user can compare the two without trusting the prompt.

func NewLogHandler added in v0.1.0

func NewLogHandler(w io.Writer, level slog.Leveler) slog.Handler

NewLogHandler returns the handler the server's log is written with: slog's text format, one key=value record per line, dropping records below level. Values are quoted and escaped where they need to be, so text a client chose — a user name, a subsystem, a forwarding destination — can neither break a record across lines nor pass for a field of its own.

func ParseLogLevel added in v0.1.0

func ParseLogLevel(name string) (slog.Level, error)

ParseLogLevel reads server.log_level. Empty means "info", which logs every connection, sign-in attempt and session; "debug" adds detail such as the command a session runs.

func ResolveShell added in v0.1.0

func ResolveShell(name string) (string, error)

ResolveShell finds a configured shell the way a command line would — a path as given, a bare name on PATH — and returns its path. Checking it before any session needs it is what turns a typo into a startup error rather than a failure served to every client.

Types

type AgentAuth

type AgentAuth struct {
	Agent sshagent.Agent
}

AgentAuth implements AuthKeySource using keys listed by an SSH agent.

func (*AgentAuth) Authorized

func (a *AgentAuth) Authorized(key ssh.PublicKey) bool

Authorized returns true if the given public key is one of the keys in the agent.

type AuthKeySource

type AuthKeySource interface {
	Authorized(key ssh.PublicKey) bool
}

AuthKeySource provides authorized-key checks for the SSH server. Implementations must use constant-time comparison when comparing keys.

type FileAuth

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

FileAuth implements AuthKeySource by reading authorized keys from a file (e.g. OpenSSH authorized_keys format). The file is read at construction. Changes to the file require a new FileAuth.

func NewFileAuth

func NewFileAuth(path string) (*FileAuth, error)

NewFileAuth reads the file at path and parses it as authorized_keys (one key per line, ssh.ParseAuthorizedKey). Returns an error if the file cannot be read; invalid lines are skipped.

func (*FileAuth) Authorized

func (f *FileAuth) Authorized(key ssh.PublicKey) bool

Authorized returns true if the given public key matches one of the keys in the file.

type PasswordSource added in v0.1.0

type PasswordSource interface {
	Verify(password []byte) bool
}

PasswordSource checks a password presented by an SSH client. How long a check takes must not depend on how close the password was.

type Server

type Server struct {
	ListenAddr string
	AuthKeys   AuthKeySource
	// HostKeyPath is the server's host key file. It is created if missing.
	// Empty means an ephemeral key for this process, which every client will see
	// as a host key change, so callers that outlive one run should always set it.
	HostKeyPath string
	// Shell is the shell sessions run, as a path or a name looked up on PATH. Empty
	// means the daemon's own $SHELL, then /bin/bash, then /bin/sh.
	Shell string
	// Passwords, if set, turns on password authentication alongside public keys,
	// every attempt going through a passwordGuard before it reaches Passwords. Nil
	// offers public keys alone.
	Passwords PasswordSource
	// Log, if set, receives a record of what the server does: its startup, every
	// connection and sign-in attempt, each session, and everything it refuses. Nil
	// logs nothing.
	Log *slog.Logger
	// Ready, if set, is called once the TCP listener is accepting
	// connections, before ListenAndServe blocks serving them.
	Ready func()
	// contains filtered or unexported fields
}

Server is a TCP SSH server that authenticates by public key (and, if Passwords is set, by password) and serves each session a shell or the command it asked for. It does not depend on config or CLI; all data is passed via struct fields.

func (*Server) Close added in v0.1.0

func (s *Server) Close() error

Close stops the server: the listener first, then every signed-in connection, whose sessions end the way they would on a disconnect. ListenAndServe then returns nil, once those sessions have had time to end. Closing a server that has not started listening yet keeps it from starting.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts the SSH server on s.ListenAddr. It does not return until the server exits. If HostKeyPath is set, that file is used, and a host key is generated there when the file does not exist yet; otherwise an ephemeral in-memory key is used for this process. A Shell that cannot be found is an error before anything listens. After Close it returns nil.

type SocketAuth added in v0.1.0

type SocketAuth struct {
	SocketPath string
}

SocketAuth implements AuthKeySource by asking the agent at SocketPath, dialling it fresh for each check.

A connection held for the life of the server would be the obvious alternative, but the agent outlives nothing: `sshush reload`, `stop`/`start`, or a crash all replace it, and a server holding the old connection would refuse every key from then on with no way back but a restart. Dialling per check also means the server can start before the agent does — it authorizes nobody until the agent is up, then works without being touched.

An authorization happens once per SSH connection, so the extra dial costs nothing worth measuring.

func (*SocketAuth) Authorized added in v0.1.0

func (s *SocketAuth) Authorized(key ssh.PublicKey) bool

Authorized returns true if the agent currently lists the given public key. It returns false, rather than an error, when the agent cannot be reached: an unreachable agent authorizes nobody.

type VaultPassphraseAuth added in v0.1.0

type VaultPassphraseAuth struct {
	VaultPath string
}

VaultPassphraseAuth implements PasswordSource by checking the password against the passphrase of the vault at VaultPath — the secret that already opens the vault everywhere else — without unlocking anything. The agent is neither asked nor changed: a locked agent stays locked, and signing in over SSH is not a way to unlock it.

The vault file is read for each check, as SocketAuth dials for each check, so a passphrase changed while the server runs is the one accepted from then on.

func (*VaultPassphraseAuth) Verify added in v0.1.0

func (v *VaultPassphraseAuth) Verify(password []byte) bool

Verify reports whether password is the vault's passphrase. A vault that is missing, unreadable or not initialized accepts nothing.

Jump to

Keyboard shortcuts

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