localui

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 17 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 localui serves a program's own user interface as a web page on 127.0.0.1, for the desktop app (internal/desktop), whose native window or browser window shows it.

Security model

It is the model of quick support's page (internal/quick) and of internal/web's loopback mode. The server listens on 127.0.0.1 only; every URL starts with a 128-bit random token, which is the login; the Host header must name the loopback address (DNS rebinding); a request that says where it comes from (Origin, Sec-Fetch-Site) must come from the page itself; and everything that changes state is a JSON POST carrying a second secret (the CSRF token) that the page obtains over that same checked channel. Responses forbid framing, caching and third-party resources (Content-Security-Policy). The page is never reachable from another machine.

Index

Constants

View Source
const HeaderCSRF = "X-RemoHop-CSRF"

HeaderCSRF carries the CSRF token on every state-changing request.

Variables

This section is empty.

Functions

func RandToken

func RandToken(n int) string

RandToken returns n random bytes, base64url-encoded.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON writes v with status.

Types

type Error

type Error struct {
	Status  int    `json:"-"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

Error is a refusal the page explains in its own language: Code names it, Message is the English text.

func Errorf

func Errorf(code, msg string) *Error

Errorf makes a refusal with code (HTTP 409).

func (*Error) Error

func (e *Error) Error() string

type Server

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

Server is the local UI server.

func New

func New(assets fs.FS, log *slog.Logger) (*Server, error)

New listens on a free loopback port and serves the files of assets: index.html at the root, the others under assets/.

func (*Server) CSRF

func (s *Server) CSRF() string

CSRF is the token state-changing requests must carry (handed to the page by /api/hello).

func (*Server) Close

func (s *Server) Close()

Close stops the server and ends the event streams.

func (*Server) Events

func (s *Server) Events(pattern string, state func() any, subscribe func() (<-chan struct{}, func()))

Events streams state() as server-sent events: at the start, whenever changed receives, and every half second if it differs (countdowns move). An open stream counts as a page on screen (see Presence).

func (*Server) Get

func (s *Server) Get(pattern string, h http.HandlerFunc)

Get registers a same-origin GET handler.

func (*Server) Hello

func (s *Server) Hello(extra func() map[string]any)

Hello registers /api/hello, the one POST without the CSRF token: it answers with extra()'s result plus the token as "csrf". Call it once.

func (*Server) Post

func (s *Server) Post(pattern string, csrf bool, h func(r *http.Request, body json.RawMessage) (any, error))

Post registers a same-origin JSON POST handler; with csrf it must carry the CSRF token. The body (at most 16 KiB) is passed raw. A nil result is {"ok": true}; an error is HTTP 409 with its code ("failed" unless it is an *Error).

func (*Server) Presence

func (s *Server) Presence() (viewers int, seen bool, gone time.Time)

Presence says whether a page shows the UI: how many event streams are open, whether one ever was, and when the last one closed.

func (*Server) PresenceChanged

func (s *Server) PresenceChanged() <-chan struct{}

PresenceChanged receives when Presence may have changed.

func (*Server) Serve

func (s *Server) Serve() error

Serve runs until Close.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP checks the Host header and the token, then routes.

func (*Server) SetFrameSrc

func (s *Server) SetFrameSrc(origin string)

SetFrameSrc lets the page show origin (one http://127.0.0.1:PORT origin: the desktop app's gateway) in frames. Empty allows no frames.

func (*Server) URL

func (s *Server) URL() string

URL is the page's address, with the token.

Jump to

Keyboard shortcuts

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