lodgeapp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 22 Imported by: 2 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 lodgeapp serves an app daemon's side of the lodge app contract (https://github.com/frostyard/lodge/blob/main/docs/specs/app-contract.md): HTTP/1.1 on the app's Unix socket, which admits only lodge's uid, with the Lodge-* headers parsed into a Caller and each path admitting one caller kind.

env, err := lodgeapp.LoadEnv() // LODGE_ROOT, LODGE_UID, or user lodge
ln, err := env.Listen("notes") // socket activation, or /run/lodge/apps/notes.sock
mcp := mcpsession.New(mcpHandler, mcpsession.Options{})
defer mcp.Close()
srv := &lodgeapp.Server{
	Health: func(context.Context) lodgeapp.Health {
		return lodgeapp.Health{OK: true, Version: version, Protocol: api.Protocol}
	},
	API: func(ctx context.Context, nc net.Conn, c lodgeapp.Caller) {
		rpc.ServeConn(ctx, nc, router, api.CallerSession)
	},
	MCP: mcp,
}
err = srv.Serve(ctx, ln)

The /api WebSocket reaches the app as a net.Conn that carries one JSON-RPC message per line, so an app plugs in the server it already has for its private socket. Package kittest plays lodge's side in tests.

Index

Constants

View Source
const (
	HeaderCaller      = "Lodge-Caller"
	HeaderLogin       = "Lodge-Login"
	HeaderDisplayName = "Lodge-Display-Name"
	HeaderNodeID      = "Lodge-Node-ID"
	HeaderNodeName    = "Lodge-Node-Name"
	HeaderSession     = "Lodge-Session"
	HeaderToken       = "Lodge-Token"
	HeaderScope       = "Lodge-Scope"
)

The headers lodge sets on every proxied request, after deleting any Lodge-* header the client sent (app-contract.md, Caller headers).

View Source
const (
	// AppsDir holds the app sockets, <id>.sock.
	AppsDir = "/run/lodge/apps"
	// InferenceSocket is lodge's inference socket, for app daemons.
	InferenceSocket = "/run/lodge/inference.sock"
	// LodgeUser and LodgeGroup are lodge's system user and its group,
	// which owns app sockets.
	LodgeUser  = "lodge"
	LodgeGroup = "lodge"
)

The install layout's paths (app-contract.md, Install layout), under Env.Root.

View Source
const AgentSocket = "/run/lodge/agent.sock"

AgentSocket is lodge's agent socket, where an agent on lodge's host reaches other apps' /mcp/<id> (app-contract.md, The agent socket).

View Source
const MaxMessage = wsline.MaxMessage

MaxMessage is the largest /api frame: 16 MiB, one JSON-RPC message.

Variables

View Source
var ErrUnsupported = errors.New("lodgeapp: lodge app sockets are Linux-only")

ErrUnsupported is Listen's and Serve's error off Linux: lodge and its apps run on Linux, and the peer check needs SO_PEERCRED, so an app socket elsewhere could only refuse every connection.

Functions

func AgentURL added in v0.2.0

func AgentURL(id string) string

AgentURL is app id's MCP endpoint on the agent socket, for AgentClient. The host is a placeholder: the client dials the socket whatever it is.

func DecodeHeader

func DecodeHeader(s string) (string, error)

DecodeHeader reverses EncodeHeader. It refuses a byte that should have been encoded, a malformed escape, and a result that is not UTF-8.

func EncodeHeader

func EncodeHeader(s string) string

EncodeHeader percent-encodes s (RFC 3986) for Lodge-Login and Lodge-Display-Name: every byte outside A–Z a–z 0–9 - . _ ~ @ + becomes %XX, with uppercase hex digits.

func WithCaller

func WithCaller(ctx context.Context, c Caller) context.Context

WithCaller returns ctx carrying c, as the server gives each request. It is for tests of handlers that read CallerFrom.

Types

type Caller

type Caller struct {
	Kind Kind
	// Login is the node owner's tailnet login, decoded:
	// alice@example.com, alice@github.
	Login string
	// DisplayName is the owner's display name, decoded; it may be empty.
	DisplayName string
	// NodeID is the node's Tailscale StableNodeID.
	NodeID string
	// NodeName is the node's short host name, [a-z0-9-].
	NodeName string
	// Session is the browser session's ID, 16 lowercase hex digits, on the
	// /api upgrade.
	Session string
	// Token is the MCP token's name, [A-Za-z0-9@._-]{1,64}.
	Token string
	// Scope is the token's scope, whose grammar is the app's own.
	Scope string
}

Caller is who lodge says sent a request. Which fields are set depends on Kind:

  • Session and Content: Login, NodeID and NodeName, and DisplayName when Tailscale has one. Session is set on the /api upgrade only.
  • Agent: Token and Scope.
  • Device: NodeID and NodeName when WhoIs named the peer, and Login when the node is untagged. They are for display only: an app MUST NOT authorize a device route on them (Rule 12).

func CallerFrom

func CallerFrom(ctx context.Context) (Caller, bool)

CallerFrom returns the caller of a request the server admitted. Every request but /health has one.

func ParseCaller

func ParseCaller(h http.Header) (Caller, error)

ParseCaller reads the Lodge-* headers of a request into a Caller, and checks each value's form. It returns an error when Lodge-Caller is missing or unknown, a header the kind requires is missing, or a value is malformed or repeated.

func (Caller) Name

func (c Caller) Name() string

Name is what an app attributes a caller's writes to: a session's or content caller's node name, an agent's token name, and for a device the node name lodge saw, or "device".

func (Caller) SetHeaders

func (c Caller) SetHeaders(h http.Header)

SetHeaders writes c into h as lodge does: it deletes every Lodge-* header in h, then sets c's non-empty fields, encoding Login and DisplayName. lodge's gateway and kittest both use it, so the two sides agree.

type Env

type Env struct {
	// Root moves every path of the install layout under it, for
	// development and tests (LODGE_ROOT). It is "" in production.
	Root string
	// LodgeUID is the only peer uid the app socket admits: user lodge's
	// in production, and under Root the uid in LODGE_UID, or the app's
	// own when that is unset.
	LodgeUID int
}

Env is where an app's socket lives and whose connections it admits.

func LoadEnv

func LoadEnv() (Env, error)

LoadEnv reads LODGE_ROOT and LODGE_UID. Without LODGE_ROOT it looks up user lodge, and LODGE_UID is ignored.

func (Env) AgentClient added in v0.2.0

func (e Env) AgentClient(token string) *http.Client

AgentClient returns an HTTP client of lodge's agent socket under e, which sends token as its bearer on every request. lodge admits the connection only from a user its configuration names as an agent, and the token only when it was issued for that user (lodgectl token issue … --agent USER). An agent uses one client per app it calls, at AgentURL(id), as the MCP client's HTTP client. It needs no Tailscale.

func (Env) Listen

func (e Env) Listen(id string) (*Listener, error)

Listen returns app id's socket: the one systemd passed (socket activation, LISTEN_FDS), or else a new one at e.SocketPath(id).

A socket it creates gets the install layout's mode, 0660, and in production group lodge, before anything can be accepted. An app's user is not in group lodge, so only root can do that; a production daemon should take its socket from a systemd socket unit instead (SocketUser=<user>, SocketGroup=lodge, SocketMode=0660). Under e.Root the socket keeps the developer's own group.

func (Env) Path

func (e Env) Path(p string) string

Path returns a path of the install layout, such as InferenceSocket, under Root.

func (Env) SocketPath

func (e Env) SocketPath(id string) string

SocketPath returns app id's socket, /run/lodge/apps/<id>.sock under Root.

type Health

type Health struct {
	OK bool `json:"ok"`
	// Version is the app's version.
	Version string `json:"version,omitzero"`
	// Protocol is the major of the app's own API, such as notes' RPC major.
	Protocol int `json:"protocol,omitzero"`
	// Reason says why the app is not OK.
	Reason string `json:"reason,omitzero"`
	// Badge is the count of things the user has not seen, such as unread
	// messages. 0 is no badge. It is the app's count, never a caller's:
	// /health carries no identity, and lodge shows the one count to every
	// session (ADR-0020).
	Badge int `json:"badge,omitzero"`
}

Health is GET /health's answer: {"ok": true, "version": "…", "protocol": N} when the app is up, or {"ok": false, "reason": "…"} while it is starting or degraded. lodge reads it with the same type.

type Kind

type Kind string

Kind is a caller kind: the one value of Lodge-Caller that a path admits (app-contract.md, What an app serves on its socket).

const (
	// Session is a browser in the shell: /api and /x/.
	Session Kind = "session"
	// Agent is an MCP client with a token: /mcp.
	Agent Kind = "agent"
	// Device is a request the app authenticates itself: /device/.
	Device Kind = "device"
	// Content is a browser on the content node: /content/.
	Content Kind = "content"
)

The caller kinds of contract 1.

type Listener

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

Listener is an app socket. Accept admits only connections whose peer uid (SO_PEERCRED) is lodge's, and closes the rest before reading anything from them (Rule 2).

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept waits for the next connection from lodge's uid.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the socket's address.

func (*Listener) Close

func (l *Listener) Close() error

Close stops listening, and removes the socket if Listen created it.

type Server

type Server struct {
	// Health reports the app's state for GET /health. It is required.
	Health func(ctx context.Context) Health

	// API serves one browser session's GET /api WebSocket, for caller
	// session, until it returns. nc carries one JSON-RPC message per
	// line, as an app's Unix-socket server reads and writes them, so an
	// app hands it to the same server: frostnotes'
	// rpcserver.ServeConn(ctx, nc, router, caller). When the browser
	// leaves, lodge closes the socket and reads from nc fail; when ctx
	// ends, nc closes. API is required.
	API func(ctx context.Context, nc net.Conn, c Caller)

	// X serves GET and HEAD /x/<path>, the app's own reads for its UI,
	// for caller session.
	X http.Handler
	// Content serves GET and HEAD /content/<path>, untrusted bytes, for
	// caller content. Apps whose manifest sets content serve it.
	Content http.Handler
	// MCP serves POST, GET and DELETE /mcp, MCP Streamable HTTP, for
	// caller agent. Wrap it in mcpsession.
	MCP http.Handler
	// Device serves any method on /device/<path>, for caller device. The
	// app authenticates these requests itself.
	Device http.Handler

	// Logger receives refused connections and failed requests. Nil
	// discards them.
	Logger *slog.Logger
}

Server serves an app's socket: every route of app-contract.md, each admitting its one caller kind. A request without Lodge-Caller, or with a kind its path does not admit, answers 403, except GET /health, which needs no caller. A route whose handler is nil answers 404.

Handlers see the request as lodge sent it, path included, with its Caller in the context (CallerFrom).

func (*Server) Serve

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

Serve serves the socket until ctx ends, then closes every connection, waits for the API sessions to return, and returns nil. Off Linux it returns ErrUnsupported at once.

Jump to

Keyboard shortcuts

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