Documentation
¶
Overview ¶
Package config reads and writes the hub's data dir: homed.yaml, the instance id, the instance lock and the paths of the other files homed keeps there.
homed.yaml holds only what homed needs before its database opens: listen, setup_listen and the tunnel (how homed reaches the internet through Cloudflare). Plugins, people, the home name and the language live in homie.db.
listen: 127.0.0.1:8080 # loopback only; cloudflared, run by homed, forwards to it setup_listen: 0.0.0.0:8090 # the first-run page; default: the home network while no tunnel is configured tunnel: mode: named # named (your domain), quick (a trycloudflare.com link, for trying) or off hostname: home.example.com # named only; written by `homed cloudflare connect` cloudflared: /usr/local/bin/cloudflared # optional; default: PATH, then next to homed
homed.yaml is edited in place: Update rewrites only the top-level sections that changed, so comments and the order of untouched sections survive a `homed cloudflare connect`.
MigrateV01 turns a v0.1 data dir into this one: v0.1's relay section goes, and so do the phase-1 sections of a v0.1 that never ran (its homed.yaml is kept as homed.yaml.v0.1), the relay token file is deleted and a tunnel section is added.
Index ¶
- Constants
- Variables
- func CheckHostname(host string) error
- func DataDirWarning(dir string) string
- func DefaultDataDir() (string, error)
- func InstanceID(dir string) (string, error)
- func LockInstance(dir string) (func(), error)
- func LoopbackPort(addr string) (int, error)
- func ResolveDataDir(flagValue string) (string, error)
- func Update(dir string, fn func(*Config) error) error
- func WriteFileAtomic(path string, data []byte, perm os.FileMode) error
- type Config
- type MigrateResult
- type Tunnel
Constants ¶
const ( FileName = "homed.yaml" InstanceIDFile = "instance-id" // SocketFile is the admin socket (the gatekeeper's adminsock): the homed CLI's way into the running hub. SocketFile = "homed.sock" // DBFile is the database: the gatekeeper's tables and the hub's (plugins, settings, activity). DBFile = "homie.db" // OldDBFile is v0.1's database; homed imports it once and renames it to OldDBFile + ".v0.1". OldDBFile = "homed.db" // TunnelDir holds the Cloudflare tunnel: config.yml, credentials.json and tunnel.json of a named tunnel, and the // private HOME of the cloudflared child. TunnelDir = "tunnel" // LogsDir holds the plugins' logs and cloudflared.log. LogsDir = "logs" // InstanceLockFile is flocked by the running homed for its whole life (LockInstance). InstanceLockFile = ".homed.lock" // V01Backup receives v0.1's homed.yaml as it was before MigrateV01 rewrote it. V01Backup = FileName + ".v0.1" )
Files in the data dir.
const ( // TunnelOff: no tunnel; the hub is not reachable from the internet (the first-run page connects one). TunnelOff = "off" // TunnelNamed: the owner's own domain through a named Cloudflare Tunnel (`homed cloudflare connect <host>`). TunnelNamed = "named" // TunnelQuick: a Cloudflare quick tunnel, a random trycloudflare.com link that changes at every restart; for trying. TunnelQuick = "quick" )
Tunnel modes.
const DefaultListen = "127.0.0.1:8080"
DefaultListen is the hub's address when homed.yaml sets none.
Variables ¶
var ErrAlreadyRunning = errors.New("homed is already running on this data dir")
ErrAlreadyRunning means another homed holds the instance lock of the data dir.
Functions ¶
func CheckHostname ¶
CheckHostname reports whether host is a full lower-case host name, e.g. home.example.com.
func DataDirWarning ¶
DataDirWarning returns a warning when the data dir is readable or writable by group or others, "" otherwise. Its secret files are 0600 either way, but others could list them or, with write access, swap them.
func DefaultDataDir ¶
DefaultDataDir is ~/Library/Application Support/homie on macOS and $XDG_DATA_HOME/homie (or ~/.local/share/homie) elsewhere.
func InstanceID ¶
InstanceID returns the hub's persistent random id, creating it on first use. Every answer of the public listener carries it (X-Homie-Instance), so the tunnel's self-check can tell that https://<host> reaches this hub.
func LockInstance ¶
LockInstance takes the data dir's instance lock (<dir>/.homed.lock, flock, non-blocking) and returns its release func. The daemon holds it for its whole life: the kernel drops it when the process ends, crash included, so there is no stale-lock cleanup and no window in which two daemons both see "nobody runs here".
func LoopbackPort ¶
LoopbackPort checks that addr is 127.0.0.1:<port> or localhost:<port> and returns the port. cloudflared forwards to 127.0.0.1:<port> and the Host guard knows only that address, so other loopback addresses (127.0.0.2, [::1]) would leave the hub unreachable through the tunnel; nothing else should reach the hub directly.
func ResolveDataDir ¶
ResolveDataDir applies the flag value, then HOMED_DATA_DIR, then the platform default. A dir it has to create is made 0700. An existing dir keeps its mode, so a mistaken --data-dir (a home dir, /var/lib) is never chmodded; the one exception is the platform default dir, which is homed's own and is tightened back to 0700. DataDirWarning reports an existing dir that is open to others.
Types ¶
type Config ¶
type Config struct {
Listen string `yaml:"listen,omitempty"`
// SetupListen is the first-run page's address; empty means firstrun.DefaultListen (0.0.0.0:8090 while no tunnel
// is configured, 127.0.0.1:8090 with one).
SetupListen string `yaml:"setup_listen,omitempty"`
Tunnel Tunnel `yaml:"tunnel,omitempty"`
// Relay is v0.1's relay section, read only so that an old file still loads: homed ignores it (with a warning,
// HasRelay) and MigrateV01 removes it.
Relay any `yaml:"relay,omitempty"`
// HomeName, Plugins and People are v0.1's phase-1 sections. v0.1 moved them into its database at its first start,
// so only a v0.1 data dir that never ran still has them. They are read only so that such a file still loads: homed
// ignores them (HasPhase1) and MigrateV01 removes them, keeping them in homed.yaml.v0.1.
HomeName any `yaml:"home_name,omitempty"`
Plugins any `yaml:"plugins,omitempty"`
People any `yaml:"people,omitempty"`
}
Config is homed.yaml.
func Load ¶
Load reads and validates homed.yaml in dir. A missing file is an empty config. Unknown keys are errors, so a typo does not silently switch something off; v0.1's relay and phase-1 sections are the exceptions (HasRelay, HasPhase1).
func (*Config) HasPhase1 ¶
HasPhase1 reports whether homed.yaml still has v0.1's phase-1 sections (home_name, plugins, people), which homed ignores.
func (*Config) HasRelay ¶
HasRelay reports whether homed.yaml still has v0.1's relay section, which homed ignores.
func (*Config) ListenAddr ¶
ListenAddr returns Listen or DefaultListen.
type MigrateResult ¶
type MigrateResult struct {
// Backup is where v0.1's homed.yaml was copied; "" when this call made no copy.
Backup string
// RelayRemoved is true when the relay section was removed from homed.yaml.
RelayRemoved bool
// Phase1Removed is true when v0.1's phase-1 sections (home_name, plugins, people) were removed from homed.yaml;
// they are in the backup.
Phase1Removed bool
// TokenDeleted is true when the relay token file was deleted.
TokenDeleted bool
}
MigrateResult says what MigrateV01 did.
func MigrateV01 ¶
func MigrateV01(dir string) (MigrateResult, error)
MigrateV01 turns a v0.1 data dir into a v0.2 one; homed calls it at every start, before Load, and it is a no-op once done. When homed.yaml has v0.1's relay section, or its phase-1 sections (a v0.1 data dir that never ran):
- homed.yaml as it is goes to homed.yaml.v0.1 (0600; an existing copy is kept, never overwritten);
- homed.yaml is rewritten without them and with a tunnel section (mode off), and, when it had a relay and set no setup_listen, with setup_listen 127.0.0.1:8090: the home had a relay, so its first-run page stays off the home network;
- the relay token file is deleted (also when homed.yaml had no relay left).
A crash between the steps leaves a state the next call finishes.
type Tunnel ¶
type Tunnel struct {
// Mode is TunnelNamed, TunnelQuick or TunnelOff; empty is off.
Mode string `yaml:"mode,omitempty"`
// Hostname is the named tunnel's public host, e.g. home.example.com: the passkeys' relying party and the OAuth
// issuer. Required in named mode.
Hostname string `yaml:"hostname,omitempty"`
// Cloudflared is the cloudflared binary: an absolute path or a name looked up in PATH. Empty: "cloudflared" in
// PATH, then next to the homed binary.
Cloudflared string `yaml:"cloudflared,omitempty"`
}
Tunnel is how homed reaches the internet: cloudflared, run by homed as a child process.
func (Tunnel) Configured ¶
Configured reports whether a tunnel is set up (named or quick). While it is not, the first-run page may listen on the home network.