Documentation
¶
Overview ¶
Package previewhost is muster-mirror's half of previews: serving a loopback web server an agent started on this host to its owner, through the preview gateway (docs/design/previews.md). This package holds what the host decides ON ITS OWN, whatever the gateway asks:
- config.go: previews.json, the allowlist and caps, and why a bad one turns previews off (never fatal);
- ports.go: which ports may be exposed, and the ports that never may (muster-mirror's own -mcp-listen, the opencode server it talks to);
- procnet.go: this host's listening sockets, from /proc/net/tcp{,6}, with the uid that owns each (the foreign_listener check);
- listeners.go: the listener reports, attributing an allowlisted listener to the tmux window whose pane runs it.
It imports the standard library only. The process table it walks comes in as a function (ProcTable): muster-mirror passes mirrorhost's, the same seam the Claude Code locator reads, so this package does not link mirrorhost and mirrorhost may later link it.
Nothing here dials, listens or serves: the tunnel to the gateway is a later change. With -preview set, muster-mirror loads and validates this configuration and logs what it would offer.
Index ¶
Constants ¶
const ( DefaultMaxOpen = 8 MaxMaxOpen = 32 DefaultMaxConns = 64 MaxMaxConns = 64 )
Caps on previews.json's numbers. MaxConns is the gateway-wide cap: the gateway sizes each tunnel's HTTP/2 connection window from it before the host announces its own maxConns, so a host may not ask for more.
const ( CodePortNotAllowed = "port_not_allowed" CodeForeignListener = "foreign_listener" CodeNotListening = "not_listening" )
Refusal codes, as the CONNECT answer and the agent tools return them.
const FileName = "previews.json"
FileName is previews.json's name in muster-mirror's config directory.
const MaxProcessName = 15
MaxProcessName is the longest process name a report carries: the kernel's own comm length (TASK_COMM_LEN 16, less its NUL).
const MinPort = 1024
MinPort is the lowest port previews.json may list: a privileged port is a system service's, never an agent's dev server.
Variables ¶
This section is empty.
Functions ¶
func CheckListener ¶
CheckListener is the foreign_listener check, on a ReadListeners read: nil when port has at least one listener where a loopback dial reaches it (Socket.Local) and EVERY such listener is uid's; a *Refusal with CodeForeignListener when any is another user's — the dial could reach that one, so one is enough — and ALSO when the read had rows it could not parse (FAIL CLOSED: an unread row may be another user's listener on this port); CodeNotListening when there is none.
func DefaultFile ¶
func DefaultFile() string
DefaultFile is where muster-mirror reads previews.json when -preview-file does not say: muster-mirror's directory under os.UserConfigDir() ($XDG_CONFIG_HOME, else ~/.config, on Linux), beside the MCP secret. "" when there is no user config directory.
Types ¶
type AlwaysRefused ¶
AlwaysRefused is the ports the host never exposes, whatever previews.json lists, each with why: muster-mirror's own MCP endpoint (it would hand a viewer muster's tools) and the opencode server it talks to (an opencode session's shell). A function, read at every check, because the opencode server's address comes from a token file that is re-read while the host runs.
type Config ¶
type Config struct {
// Ports is the allowlist. Never empty in a Config Parse returned.
Ports PortSet
// Offer: listener reports on (the default) or off.
Offer bool
// MaxOpen is how many previews may be open at once on this host.
MaxOpen int
// MaxConns is how many upstream connections (tunnel streams) at once.
MaxConns int
}
Config is a loaded, valid previews.json.
func Load ¶
Load reads and validates previews.json at path. The error is the reason previews are off, written for the log and the offer stream: a missing, unreadable, empty or invalid file. A caller disables previews on it; it is never a reason to stop.
func Parse ¶
Parse validates previews.json's bytes. Unknown fields are refused, and every key must be spelt exactly (see the exact-key pass below).
func (Config) Allows ¶
func (c Config) Allows(port int, refused AlwaysRefused) error
Allows is the host's port check, before any listener is looked at: nil when port may be exposed, else a *Refusal with CodePortNotAllowed. The always-refused ports are checked FIRST, so listing one in previews.json changes nothing.
type Listeners ¶
Listeners is one read of /proc/net/tcp{,6}: its LISTEN sockets, and how many rows could not be parsed. A bad row could be any socket — another user's listener included — so a check that must not fail open (CheckListener) refuses while Bad > 0; the offer scan, which only ever leaves things out, reads Sockets alone.
func ReadListeners ¶
ReadListeners reads <root>/net/tcp and <root>/net/tcp6 and returns their LISTEN sockets and bad-row count. root is "/proc" on this host or a test's fixture tree. A missing tcp6 (a kernel without IPv6) is not an error; a missing tcp is, and so is a file with bad rows and no socket read.
type PortRange ¶
type PortRange struct{ Lo, Hi int }
PortRange is an inclusive range of ports; Lo == Hi for one port.
type PortSet ¶
type PortSet struct {
// contains filtered or unexported fields
}
PortSet is previews.json's allowlist: ranges, sorted by Lo.
func ParsePorts ¶
ParsePorts reads previews.json's "ports": each entry one port ("3000") or an inclusive range ("8000-8099"), digits only, within MinPort-65535. Any entry that is not one is an error naming it: one bad entry disables previews, it is never skipped (a skipped entry would leave an allowlist smaller than the one written, silently).
type ProcTable ¶
ProcTable reads this host's process table: every process's parent and name. muster-mirror passes mirrorhost's (mirrorhost.ProcTable), the source the Claude Code locator walks.
type Refusal ¶
Refusal is why the host will not expose a port. Code is the wire's ("port_not_allowed", "foreign_listener", "not_listening"); Why is for a log.
type Report ¶
type Report struct {
Port int `json:"port"`
Process string `json:"process"` // the listening process's name, at most MaxProcessName bytes
Window string `json:"window"` // the tmux window id ("@12")
FirstSeen time.Time `json:"first_seen"`
}
Report is one listener offered to the owner: an allowlisted port this user listens on from a process in a mirrored tmux window. These four fields are ALL it carries (TestAReportCarriesExactlyItsFourKeys): never the argv, working directory, environment or pid.
type Scanner ¶
type Scanner struct {
// Proc is the proc root for net/tcp{,6} and <pid>/fd ("" = "/proc").
Proc string
// UID is this user's: only its listeners are reported.
UID int
// Procs reads the process table (required).
Procs ProcTable
// Now is the clock (nil = time.Now).
Now func() time.Time
// contains filtered or unexported fields
}
Scanner finds the listener reports. Each Scan is one poll; the scanner keeps first_seen across polls. Safe for concurrent use.
func (*Scanner) Scan ¶
Scan is one poll. panes maps each mirrored pane's root process (its #{pane_pid}) to its tmux window id. A listener is reported when its port is allowed (Config.Allows, so never an always-refused one), it listens where a loopback dial reaches it, it is this user's, and its process is a pane's root or descends from one; a listener in no pane has no session to show it in and is not reported. Sorted by window, then port. With offers off (Config.Offer false) it reads nothing and reports nothing.
type Socket ¶
type Socket struct {
Addr netip.Addr // the bound address (an IPv4-mapped IPv6 address is unmapped)
Port int
UID int // the socket's owner
Inode uint64 // what /proc/<pid>/fd/* links to, as socket:[<inode>]
}
Socket is one listening TCP socket from /proc/net/tcp or tcp6.
func ParseProcNet ¶
ParseProcNet reads a /proc/net/tcp (v6 false) or /proc/net/tcp6 (v6 true) file and returns its LISTEN sockets. The header and any line that is not a socket row are skipped; bad counts them, so a caller (and a test) can tell a file in an unknown shape from one with no listeners.
A row is
sl local_address rem_address st tx:rx tr:when retrnsmt uid timeout inode …
with the local address as hex: the IPv4 address as one 32-bit word, IPv6 as four, each written in the kernel's (this host's) byte order, then ':' and the port in network order. Hence binary.NativeEndian: the file is always read on the host that wrote it.