hub

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 30 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 hub is notesd's hub role (ADR-0005, ADR-0016, docs/specs/sync-protocol.md): the HTTP endpoints devices sync through (pairing, the /sync WebSocket, blobs), the token check that guards them, the log domain devices call, and the hub domain for the hub's own local clients. Pushes become writes through the vaults' actors (vault.Vault.Push), so the hub merges with the same write path as every local writer.

Index

Constants

View Source
const DeviceOrigin = "notesd://device"

DeviceOrigin is the only Origin a request to the hub may carry: a device sends it on its WebSocket (golang.org/x/net/websocket requires one), and no browser page can have it (docs/specs/sync-protocol.md, Endpoints).

View Source
const NonceSize = 32

NonceSize is the bytes of a relocation nonce; it travels as 43 base64url characters, unpadded.

Variables

This section is empty.

Functions

func CheckURL

func CheckURL(raw string) (*url.URL, error)

CheckURL accepts a hub URL whose scheme is https, or http on loopback (ADR-0016), and returns it without a trailing '/' and with its host in lower case, so URLs that SameURL calls the same are equal strings.

func Proof

func Proof(key []byte, hubID, url, nonce string) string

Proof is a relocation proof: HMAC-SHA256, keyed with the 32 bytes of the SHA-256 of the device's token, of "frostnotes relocate\n" + hub ID + "\n" + url + "\n" + nonce, in unpadded base64url. The hub computes it from the hash it keeps; the device from its own token (TokenKey).

func SameURL

func SameURL(a, b string) bool

SameURL reports whether two hub URLs are the same: scheme and host compared without case, the path exactly, a trailing '/' ignored.

func TokenHash

func TokenHash(token string) string

TokenHash is the SHA-256, hex, the hub keeps of a device token.

func TokenKey

func TokenKey(token string) []byte

TokenKey is a relocation proof's key for a device token: its SHA-256, the bytes TokenHash spells in hex.

func WithPeer

func WithPeer(ctx context.Context, p Peer) context.Context

WithPeer returns ctx naming the peer a request came from.

Types

type Config

type Config struct {
	DB     *store.DB
	Blobs  *blobs.Store
	Vaults *vault.Manager
	RPC    *rpcserver.Server
	Log    *slog.Logger     // [discard]
	Now    func() time.Time // [time.Now]
	// Listen is the address notesd --hub serves, for hub.status; "" when
	// the hub serves none.
	Listen string
	// MaxBlob is the largest blob a device may upload [blobxfer.DefaultMaxSize].
	MaxBlob int64
	// Crash, when set, is asked after a push is applied and recorded and
	// before its answer ("pushed"); true drops the device's connection
	// instead of answering, as a crash or a lost link would. The simulation
	// sets it (ADR-0020).
	Crash func(point string) bool
}

Config is what the hub needs. Zero fields take the defaults in brackets.

type Hub

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

Hub serves devices. Its router, set with SetRouter, serves their WebSocket connections as caller device.

func New

func New(cfg Config) *Hub

New returns a hub over cfg.

func (*Hub) Handler

func (h *Hub) Handler() http.Handler

Handler serves the hub's endpoints (docs/specs/sync-protocol.md, Endpoints).

func (*Hub) Log

func (h *Hub) Log() api.LogService

Log returns the log domain, for paired devices.

func (*Hub) Serve

func (h *Hub) Serve(ctx context.Context, ln net.Listener) error

Serve serves Handler on ln until ctx ends.

func (*Hub) Service

func (h *Hub) Service() api.HubService

Service returns the hub domain, for the hub's own local clients.

func (*Hub) SetRouter

func (h *Hub) SetRouter(r *api.Router)

SetRouter sets the router device connections dispatch through; it is built from services that include the hub's own.

type Peer

type Peer struct {
	Name string
	Key  string
}

Peer is the node a request came from as a listener in front of the hub saw it (lodge's Lodge-* headers): Name is shown beside a pending pairing, to tell pairings apart, and Key, its node ID, bounds how many pairings one node may have waiting. Neither admits anything (docs/specs/lodge-app.md#device).

type Relocation

type Relocation struct {
	Hub   string `json:"hub"`
	URL   string `json:"url"`
	Proof string `json:"proof"`
}

Relocation is POST /sync/hub's answer.

type RelocationRequest

type RelocationRequest struct {
	Device int64  `json:"device"`
	Nonce  string `json:"nonce"`
}

RelocationRequest is POST /sync/hub's body.

Jump to

Keyboard shortcuts

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