firstrun

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 24 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 firstrun serves the first-run page (docs/ADMIN-API.md §7): the local admin before the tunnel. The owner enters a PIN homed shows on the home box, connects the home to the internet through Cloudflare (their own domain, or a quick tunnel "for trying"), waits for the tunnel's self-check, and gets the one-time setup link https://<host>/setup#t=<token> to open on the phone or the Mac, where the first passkey is made.

The listener (setup_listen: 0.0.0.0:8090 while no tunnel is configured, 127.0.0.1:8090 once one is) speaks plain HTTP, so the page is built to be short-lived and hard to guess:

  • it closes 30 minutes after it opened, and for good after 20 wrong PINs; `homed admin firstrun` on the home box (or a restart) opens it again with a new PIN, and `homed admin setup-link` never needs it;
  • the PIN has 8 digits and changes every 10 minutes and after every issued link. It is printed only to a terminal (Options.Terminal); under launchd or systemd the log says how to get it instead;
  • one client gets 5 wrong PINs per 10 minutes and then waits, without locking out the other devices;
  • the homie_firstrun cookie (HttpOnly, SameSite=Strict, 30 minutes) is random and kept only as a hash, in memory;
  • links issued here live 3 minutes (the admin socket's live 15); every link and tunnel change is logged and audited with the client's address, and a connected domain is never replaced from here;
  • only Host names a LAN can have pass (IP literals, localhost, single-label and .local-style names), which stops DNS rebinding; mutating requests need X-Homie: 1 and a same-origin Origin and Sec-Fetch-Site;
  • once the owner has a passkey of the current public URL every path answers 410 setup_done.

In quick mode the public URL changes whenever cloudflared restarts, and the owner's passkeys of the old URL no longer count: homed opens the page again by itself, and its link step becomes "Reconnect", a new setup link for the same owner.

cmd/homed wires it: New(Options{…}) and Serve(ctx, ln); a Server may be served again on another listener (the page moves from the home network to loopback once a tunnel is configured) and keeps its PIN, cookies and limits.

Index

Constants

View Source
const (
	// ListenLAN is the default while no tunnel is configured: the page connects one, perhaps from another device on
	// the home network (a Raspberry Pi has no browser of its own).
	ListenLAN = "0.0.0.0:8090"
	// ListenLocal is the default once a tunnel is configured: the page only issues the link, which the home box itself
	// opens or shows as a QR code.
	ListenLocal = "127.0.0.1:8090"
)

setup_listen defaults (DefaultListen).

View Source
const (
	// OpenFor is how long the page stays open; then it closes until `homed admin firstrun` or a restart.
	OpenFor = 30 * time.Minute
	// LinkTTL is the life of a setup link issued here, shorter than the admin socket's (auth.SetupLinkTTL): the page
	// and the link travel over plain http on the home network.
	LinkTTL = 3 * time.Minute
	// PINEvery is how often the PIN changes; it changes after every issued link too.
	PINEvery = 10 * time.Minute
)
View Source
const CookieName = "homie_firstrun"

CookieName is the first-run cookie. It is not __Host- and not Secure: the page is plain http on the LAN.

Variables

This section is empty.

Functions

func DefaultListen

func DefaultListen(tunnelConfigured bool) string

DefaultListen is setup_listen when neither the command line nor homed.yaml sets it.

func FormatPIN

func FormatPIN(pin string) string

FormatPIN shows a PIN in two groups of four digits ("1234 5678"); the page ignores the space.

func PageURLs

func PageURLs(addr net.Addr) []string

PageURLs lists the page's addresses for the terminal and `homed admin firstrun`: every LAN IPv4 address and localhost when the listener is on all interfaces.

Types

type Home

type Home interface {
	// OwnerReady reports whether the superadmin has a passkey of the current public URL: then the page is done. False
	// while there is no public URL.
	OwnerReady(ctx context.Context) (bool, error)
	// OwnerExists reports whether the home has its owner at all (a quick tunnel's new URL: the link reconnects them).
	OwnerExists(ctx context.Context) (bool, error)
	// SetupLink makes a setup link of the current public URL that lives at most ttl (auth.IssueSetupLink of the
	// current stack); an error wrapping core.ErrConflict means the owner can sign in already.
	SetupLink(ctx context.Context, ttl time.Duration) (url string, err error)
	// HomeName is the home's name, or the one that pre-fills the setup; "" when there is none.
	HomeName(ctx context.Context) string
	// Audit appends an entry to the admin audit (the gatekeeper's).
	Audit(ctx context.Context, e core.AuditEntry)
}

Home is the rest of homed the page needs; cmd/homed implements it over the current gatekeeper stack.

type Options

type Options struct {
	Tunnel Tunnel
	Home   Home
	// UI serves every path outside /api/: the SPA bundle with its /firstrun route and assets. Nil answers 404.
	UI http.Handler
	// Terminal is where the PIN is printed: the terminal homed runs in. Nil means homed has none (launchd, systemd,
	// a log file): the PIN is never printed, and the log says to use `homed admin firstrun` or `homed admin setup-link`.
	Terminal io.Writer
	// OpenFor is how long the page stays open; 0 means OpenFor (30 minutes).
	OpenFor time.Duration
	// Log receives the page's events; the PIN, the cookies and the links never reach it.
	Log *slog.Logger
	// Now is the clock; nil means time.Now.
	Now func() time.Time
}

Options configures the first-run server. Tunnel and Home are required.

type Server

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

Server is one opening of the first-run page. Create it with New; a reopening after it closed is a new Server.

func New

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

New returns the server with a fresh PIN; Serve announces it.

func (*Server) Closed

func (s *Server) Closed() bool

Closed reports whether the page closed by itself (its time, or too many wrong PINs); a closed page is not served again.

func (*Server) ClosesAt

func (s *Server) ClosesAt() time.Time

ClosesAt returns when the page closes by itself.

func (*Server) Handler

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

Handler serves the page: /api/firstrun/* and, for every other path, the UI.

func (*Server) PIN

func (s *Server) PIN() (pin string, until time.Time)

PIN returns the current PIN and when it changes next, for `homed admin firstrun` over the admin socket.

func (*Server) Serve

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

Serve announces the page (the PIN on the terminal, or a hint in the log) and serves ln until ctx is done, the page's time is over or too many wrong PINs closed it. It changes the PIN every PINEvery and closes ln. It may be called again with another listener once it returned (the page moves to loopback): the PIN, the cookies and the time stay.

type Tunnel

type Tunnel interface {
	// Status reports the tunnel.
	Status() tunnel.Status
	// Job returns the running or last connect.
	Job() (tunnel.ConnectJob, bool)
	// Connect starts connecting the owner's domain.
	Connect(hostname string, overwriteDNS bool) (tunnel.ConnectJob, error)
	// Quick starts a quick tunnel on a hub that has none.
	Quick() error
}

Tunnel is homed's tunnel as the page needs it (internal/tunnel.Manager).

Jump to

Keyboard shortcuts

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