sshcode

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 16 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 sshcode is the SSH server of the sign-in codes (SPEC §7.16, D52). A user who registered a public key runs ssh and the server answers with a one-time code, then closes the connection. It offers nothing else: no password, no shell, no forwarding.

The server talks to the rest of Clowder through two small interfaces, SignIn and Limiter, which internal/app satisfies. It is the only package that runs an SSH server, and it never imports internal/store.

Index

Constants

View Source
const (
	// DefaultMaxConns is the most connections one process serves at once.
	DefaultMaxConns = 64
	// DefaultMaxConnsPerAddr is the most connections one client address may
	// hold at once (step 24.1): without it one address could keep every place
	// of DefaultMaxConns for the length of the handshake deadline and lock
	// everybody else out of the server. A person needs one connection at a
	// time.
	DefaultMaxConnsPerAddr = 8
	// DefaultHandshakeTimeout is how long a client has for the handshake,
	// the authentication included.
	DefaultHandshakeTimeout = 10 * time.Second
	// DefaultConnTimeout is the total time a connection lives.
	DefaultConnTimeout = 30 * time.Second
)

The limits of SPEC §7.16. A Config field that is zero takes the default.

Variables

This section is empty.

Functions

func LoadHostKey

func LoadHostKey(path string) (ssh.Signer, error)

LoadHostKey returns the host key kept at path: the PEM of an OpenSSH private key. When the file does not exist it makes an Ed25519 key and stores it there, so that the next start, and the other processes that share the directory, use the same key.

The key is made exclusively. It is written to a temporary file in the same directory with mode 0600 and then linked to path, and the temporary file is removed. A reader therefore never sees a half-written key, and of two processes that start together one link wins and the other reads the winner's key.

It fails with tagHostKeyReadFailed when the file cannot be read, tagHostKeyInvalid when it is not a private key, and the tags of the making when the key cannot be made or stored (a missing directory among them).

Types

type Config

type Config struct {
	// HostKey is the host key of the server (see LoadHostKey).
	HostKey ssh.Signer
	// MaxConns is the most connections at once; more are closed when they
	// are accepted. Zero is DefaultMaxConns.
	MaxConns int
	// MaxConnsPerAddr is the most connections one client address may hold at
	// once; more are closed when they are accepted. Zero is
	// DefaultMaxConnsPerAddr.
	MaxConnsPerAddr int
	// HandshakeTimeout is the time for the handshake. Zero is
	// DefaultHandshakeTimeout.
	HandshakeTimeout time.Duration
	// ConnTimeout is the total time of a connection. Zero is
	// DefaultConnTimeout.
	ConnTimeout time.Duration
}

Config holds the settings of the server.

type Limiter

type Limiter interface {
	// AllowAddr reports whether the address may connect.
	AllowAddr(ctx context.Context, addr string) bool
	// FailAddr records a failed login of the address.
	FailAddr(ctx context.Context, addr string)
}

Limiter counts the failed logins of client addresses. *app.LoginLimiter satisfies it; it is the limiter of the web login too, so that the limit of SPEC §7.1 covers both.

type Server

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

Server is the SSH server of the sign-in codes. Make it with New and run it with Serve. It is safe for concurrent use.

func New

func New(cfg Config, signIn SignIn, limiter Limiter, logger *slog.Logger) *Server

New makes a server. A nil logger discards the log.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, ln net.Listener) error

Serve accepts connections on ln until ctx is done. When ctx is done it closes ln and every open connection, waits for every goroutine it started and returns nil. It returns an error when ln fails for another reason, after it has closed the connections and waited the same way. The caller does not close ln.

type SignIn

type SignIn interface {
	// KeyAllowed reports whether the key with the fingerprint belongs to the
	// enabled user with the username. It says nothing about why not.
	KeyAllowed(ctx context.Context, username, fingerprint string) bool
	// IssueCode makes a code for the user and the key, for the client at addr.
	IssueCode(ctx context.Context, username, fingerprint, addr string) (app.IssuedSSHCode, error)
}

SignIn is what the server asks of the services of internal/app. *app.SignIn satisfies it.

Jump to

Keyboard shortcuts

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