web

package
v3.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 51 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 web is the `sshmgr serve` command's localhost web UI.

Server-rendered html/template pages over the same services the CLI and the TUI call, with a little vanilla JavaScript and Tabler's CSS, all embedded in the binary: the page makes no network request beyond this server. It never imports internal/cli, which holds the TUI and process-wide state a long-lived server cannot share (imports_test.go holds it to that).

Reading takes no lock. A change takes it only while it is checked and made (see services/change), never while a person reads its preview, so a running server does not hold up a CLI command.

Index

Constants

View Source
const (
	ExportFlat    = "ssh_config"
	ExportTermius = "termius-hosts.csv"
)

Exports a page can download. Each is rendered the way `sshmgr config export --flat` and `sshmgr termius export` render it, and none is written anywhere.

View Source
const ContentSecurityPolicy = "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; " +
	"connect-src 'self'; font-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'"

ContentSecurityPolicy is sent on every response. Everything is same-origin; nothing is inline. img-src allows data: because Tabler's CSS draws its checkboxes and selects with data: SVG URLs.

View Source
const DefaultPort = 7822

DefaultPort is where serve listens unless told otherwise.

View Source
const DefaultPortAttempts = 20

DefaultPortAttempts bounds how far past a taken default port Listen looks.

View Source
const SessionCookie = "sshmgr_session"

SessionCookie is the cookie a valid token is exchanged for.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is a name that matches nothing in the manifest, the snapshots or the history. Handlers turn it into a 404.

Functions

func AllowedHosts

func AllowedHosts(bindHost string, port int, extra []string) []string

AllowedHosts is the set of Host header values the server answers to.

For a loopback bind that is the three spellings of this machine at the bound port. Anything else is refused, which is the DNS-rebinding defence: a page on evil.example that rebinds its name to 127.0.0.1 still sends Host: evil.example. With a remote bind the bound host and any explicitly allowed names are added.

func IsAddrInUse

func IsAddrInUse(err error) bool

IsAddrInUse reports whether a listen error means the port is taken.

func IsLoopback

func IsLoopback(host string) bool

IsLoopback reports whether host only accepts connections from this machine: localhost, or an address in 127.0.0.0/8 or ::1.

func Listen

func Listen(listen ListenFunc, host string, port int, pinned bool, attempts int) (net.Listener, error)

Listen binds host:port.

When pinned is false and the port is in use, it tries the next port up, up to attempts ports in all, and returns the first that binds: the default port being taken by another process is ordinary, and the caller prints the URL actually bound. When pinned is true -- the user asked for this port -- a taken port is an error, because silently serving elsewhere would leave whatever they pointed at this port talking to something else. Any error other than "address in use" stops the search at once.

func NewToken

func NewToken() string

NewToken returns 32 random bytes, base64url-encoded.

Types

type AppEngine

type AppEngine struct {
	P paths.Paths
	// Retain is how many snapshots a change keeps, as the CLI's
	// $SSH_MANAGER_SNAPSHOT_RETAIN; zero means 10.
	Retain int
}

AppEngine reads one sshmgr home through the same services the CLI uses. It loads the manifest and inventory on every call: the CLI may change them at any moment, and a page must show what is there now.

func (AppEngine) Apply

func (e AppEngine) Apply(ch change.Change, hash string) (change.Plan, change.Result, error)

Apply makes ch if it would still do what was confirmed.

func (AppEngine) Audit

func (e AppEngine) Audit(now time.Time) (AuditView, error)

Audit is `sshmgr audit` as data: deployments, expiry, recent activity.

func (AppEngine) Bundles

func (e AppEngine) Bundles() ([]BundleView, error)

Bundles lists the bundles in dist/, newest first.

func (AppEngine) Doctor

func (e AppEngine) Doctor() (doctor.Report, error)

Doctor runs the same checks as `sshmgr doctor`, without --fix.

func (AppEngine) Export

func (e AppEngine) Export(name string, includeGit bool) (string, error)

Export renders one of the exports for download.

func (AppEngine) History

func (e AppEngine) History(f history.Filter) ([]history.Op, error)

History reads the operation history without writing to it: a page view must not create the database or run the first import.

func (AppEngine) Home

func (e AppEngine) Home() paths.Paths

Home is the paths this engine reads.

func (AppEngine) Host

func (e AppEngine) Host(alias string) (HostView, error)

Host is one host as `sshmgr show <alias>` reports it.

func (AppEngine) Hosts

func (e AppEngine) Hosts() ([]query.ProfileGroup, error)

Hosts lists every host, grouped by profile, as `sshmgr list` does.

func (AppEngine) Key

func (e AppEngine) Key(ref string) (KeyView, error)

Key is one key by its profile/key reference.

func (AppEngine) Keys

func (e AppEngine) Keys() ([]keysvc.Row, error)

Keys lists every key, as `sshmgr key list` does.

func (AppEngine) Op

func (e AppEngine) Op(id string) (OpView, error)

Op is one operation and the manifest diff across it.

func (AppEngine) Overview

func (e AppEngine) Overview(now time.Time) (Overview, error)

Overview reads the banner's rows through overview.Rows, the same function the TUI prints, so the two never disagree.

func (AppEngine) Preview

func (e AppEngine) Preview(ch change.Change) (change.Plan, error)

Preview shows what ch would do, without doing it.

func (AppEngine) Profiles

func (e AppEngine) Profiles() ([]ProfileView, error)

Profiles lists every profile with its counts.

func (AppEngine) SnapshotDiff

func (e AppEngine) SnapshotDiff(id, against string) (SnapshotDiff, error)

SnapshotDiff compares snapshot id with another snapshot, or with ~/.ssh as it is now when against is "". A snapshot holding key material is refused, not opened: showing a diff would put the key on the page.

func (AppEngine) Snapshots

func (e AppEngine) Snapshots() ([]SnapshotView, error)

Snapshots lists the snapshots, newest first.

func (AppEngine) Tokens

func (e AppEngine) Tokens() (TokensView, error)

Tokens is `authama token list` and `authama token audit` as data. The secrets never leave the service: Inventory is asked not to reveal them, and the view drops them again in case it ever does.

type AuditKey

type AuditKey struct {
	Path, Fingerprint, Status string
	Deployments               []inventory.Deployment
}

AuditKey is one inventory record and where it is deployed.

type AuditView

type AuditView struct {
	Keys   []AuditKey
	Expiry []expiry.Status
	Recent []history.Op
}

AuditView is the audit report, as data.

type BundleView

type BundleView struct {
	Name    string
	Written time.Time
	Size    int64
}

BundleView is one encrypted bundle in the config home's dist/.

type Engine

type Engine interface {
	Home() paths.Paths
	Overview(now time.Time) (Overview, error)
	Profiles() ([]ProfileView, error)
	Hosts() ([]query.ProfileGroup, error)
	Host(alias string) (HostView, error)
	Keys() ([]keysvc.Row, error)
	Key(ref string) (KeyView, error)
	Snapshots() ([]SnapshotView, error)
	Bundles() ([]BundleView, error)
	SnapshotDiff(id, against string) (SnapshotDiff, error)
	History(f history.Filter) ([]history.Op, error)
	Op(id string) (OpView, error)
	Doctor() (doctor.Report, error)
	Audit(now time.Time) (AuditView, error)
	Export(name string, includeGit bool) (string, error)
	Tokens() (TokensView, error)

	// Preview runs a change on a private copy of the home; Apply makes it
	// if a fresh preview under the lock still has the confirmed hash.
	Preview(ch change.Change) (change.Plan, error)
	Apply(ch change.Change, hash string) (change.Plan, change.Result, error)
}

Engine is everything the pages read. Every argument is a name - a profile, a host alias, a key, a snapshot id, an operation id - resolved here against what exists, never a filesystem path. AppEngine is the real one; tests pass their own.

type FileDiff

type FileDiff struct {
	Path  string
	State string // added | removed | changed
	Lines []history.DiffLine
}

FileDiff is one file's change.

type HostView

type HostView struct {
	Detail *query.HostDetail
	Block  string
	Pins   []knownhosts.Entry
	Token  string
	VM     bool // a VM host, which can have a Docker context
}

HostView is one host: the manifest entry and what it resolves to.

type KeyView

type KeyView struct {
	Detail keysvc.Detail
	Expiry *expiry.Status
	Ops    []history.Op
}

KeyView is one key: its row, its detail, and its expiry.

type ListenFunc

type ListenFunc func(network, address string) (net.Listener, error)

ListenFunc is net.Listen's signature, injectable so the port fallback can be tested without binding a socket.

type OpView

type OpView struct {
	Op       history.Op
	Diff     []history.DiffLine
	DiffNote string
}

OpView is one operation and its manifest diff.

type Options

type Options struct {
	// Engine reads the sshmgr home. Required.
	Engine Engine

	// Token is the launch token printed in the URL. Required.
	Token string

	// AllowedHosts are the Host header values answered (see AllowedHosts).
	AllowedHosts []string

	// AllowRemote records that the bind is not loopback; it lets a TLS
	// reverse proxy's X-Forwarded-Proto mark the session cookie Secure.
	AllowRemote bool

	// ReadOnly registers no route that changes anything, and hides the forms.
	ReadOnly bool

	// Version and BindAddr are shown in the footer.
	Version  string
	BindAddr string

	// Retain is how many snapshots a snapshot restore keeps when it snapshots
	// first, as SSH_MANAGER_SNAPSHOT_RETAIN sets for the CLI. 0 means 10.
	Retain int

	// Now is the clock. Nil means time.Now.
	Now func() time.Time
}

Options configures a Server.

type Overview

type Overview struct {
	Rows     []overview.Row
	Problems int
	Profiles int
	Hosts    int
	Keys     int
	Expiry   []expiry.Status
	Recent   []history.Op
	DevRoot  string
}

Overview is the first page: the banner's rows, and the counts behind them.

type ProfileView

type ProfileView struct {
	Name, Category, KeyScope, KeyStore string
	Hosts, Keys                        int
}

ProfileView is one profile in the profiles list.

type Server

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

Server is the web UI.

func New

func New(opts Options) (*Server, error)

New builds a Server. It parses every template once, so a template error is a startup error rather than a 500 on first visit.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the full middleware chain and routes.

func (*Server) Serve

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

Serve answers on ln until ctx is cancelled, then shuts down with a ten-second grace for requests in flight.

type SnapshotDiff

type SnapshotDiff struct {
	ID, Against string // Against "" means the current ~/.ssh
	Files       []FileDiff
	Refused     string // why the snapshot was not opened
}

SnapshotDiff compares a snapshot with the tree now, or with another snapshot.

type SnapshotView

type SnapshotView struct {
	ID         string
	Taken      time.Time
	Size       int64
	KeyBearing bool
}

SnapshotView is one snapshot in the list.

type TokensView

type TokensView struct {
	Items    []tokens.Installed
	Findings []tokensvc.Finding
	Failed   bool
	Config   string // the token config `token apply` would read, or "" for the built-in default
	Legacy   bool
}

TokensView is the token page: what is installed, secrets masked, and the checkup of the files that hold it.

Jump to

Keyboard shortcuts

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