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
- func RandToken(n int) string
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Error
- type Server
- func (s *Server) CSRF() string
- func (s *Server) Close()
- func (s *Server) Events(pattern string, state func() any, subscribe func() (<-chan struct{}, func()))
- func (s *Server) Get(pattern string, h http.HandlerFunc)
- func (s *Server) Hello(extra func() map[string]any)
- func (s *Server) Post(pattern string, csrf bool, ...)
- func (s *Server) Presence() (viewers int, seen bool, gone time.Time)
- func (s *Server) PresenceChanged() <-chan struct{}
- func (s *Server) Serve() error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetFrameSrc(origin string)
- func (s *Server) URL() string
Constants ¶
const HeaderCSRF = "X-RemoHop-CSRF"
HeaderCSRF carries the CSRF token on every state-changing request.
Variables ¶
This section is empty.
Functions ¶
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.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the local UI server.
func New ¶
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 ¶
CSRF is the token state-changing requests must carry (handed to the page by /api/hello).
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 ¶
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 ¶
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) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP checks the Host header and the token, then routes.
func (*Server) SetFrameSrc ¶
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.