peers

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 16 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 peers keeps the other computers this machine reaches, and the channel to each (ADR-067).

Each computer is a Caprock daemon with access from anywhere on (ADR-058). This machine pairs with it the way a phone does — the target's owner shows a code there, this machine redeems it through the relay — and keeps the device token it is given in machines.json, mode 0600, beside remote.json and devices.json. From then on every request this machine makes for that computer goes through one end-to-end encrypted channel (remote.Client) and arrives there as that paired device, with the role its owner gave it.

The direction matters. Pairing gives this machine access to the target and nothing back: the target lists this machine among its devices and can revoke it at any time, and access is not transitive — a phone paired with this machine never reaches the target through it.

Nothing here starts, signals or types into a process (rule 7). It carries requests to the target's daemon, and the target decides, with its own gate, what a device in this role may do there.

Index

Constants

View Source
const (
	// StatusRevoked is a computer that answered GET /v1/pair/me with 401:
	// its owner removed this machine, or reset its pairing. Kept, not
	// deleted — the user decides to forget it — and asked again on the next
	// check, so a computer that was wrong about it comes back by itself.
	StatusRevoked = "revoked"
	// StatusPending is a computer that took this machine's code and waits
	// for its owner to allow this machine by name there (ADR-067). Until
	// then the token reaches nothing but GET /v1/pair/me.
	StatusPending = "pending"
)

Statuses beyond remote.ClientState's.

View Source
const File = "machines.json"

File holds the computers this machine reaches: their relay, id, pinned key and this machine's device token on each. Bearer tokens, so its own file at 0600 — never config.json, which is edited by hand and pasted into support threads.

Variables

View Source
var (
	// ErrUnknown is a computer this machine has not paired with.
	ErrUnknown = errors.New("no such computer")
	// ErrSelf is a pairing link for this machine itself.
	ErrSelf = errors.New("that link is for this computer")
	// ErrNoCode is a link without a code: the target's owner has to show
	// one first.
	ErrNoCode = errors.New("that link carries no code: show a new code on that computer")
	// ErrBadCode is the target refusing the code — wrong, expired, used, or
	// never issued; it does not say which, on purpose (internal/pairing).
	ErrBadCode = errors.New("that computer did not accept the code: show a new one there")
	// ErrNotAccepting is a target with no pairing open: access from
	// anywhere is off there.
	ErrNotAccepting = errors.New("that computer is not accepting pairings: turn on access from anywhere there")
	// ErrBadLink is a link that is not a pairing link; the error that
	// matches it says what is wrong with it.
	ErrBadLink = errors.New("that is not a pairing link")
)

Errors a caller turns into an answer.

Functions

func Write

func Write(dir string, ms []Machine) error

Write saves the list, 0600, atomically. On Windows the mode is not enforced; the file lives in the user's own profile, as devices.json does.

Types

type Machine

type Machine struct {
	// ID is the computer's relay id: its Ed25519 public key, base64url. Also
	// how the rest of Caprock names it.
	ID string `json:"id"`
	// Key is its X25519 static key, base64url, which every handshake pins.
	Key string `json:"key"`
	// Relay is where it waits, from its pairing link.
	Relay string `json:"relay"`
	// Name is what the user calls it; from the link, renamable here.
	Name string `json:"name"`
	// Token is this machine's device token on it. Never listed, never sent
	// anywhere but inside the channel to that computer.
	Token string `json:"token"`
	// DeviceID is how the computer lists this machine among its devices.
	DeviceID string `json:"device_id"`
	// AddedAt is unix milliseconds, this machine's clock.
	AddedAt int64 `json:"added_at"`
}

Machine is one computer this machine has paired with.

func Read

func Read(dir string) ([]Machine, error)

Read loads the list. A missing file is no computers; a corrupt one is an error the caller logs and treats as none — the safe reading of "I cannot tell which computers I may reach" costs a re-pair and risks nothing.

type Manager

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

Manager keeps the computers and a channel, opened on demand, to each.

func Open

func Open(opt Options) (*Manager, error)

Open loads machines.json. The Manager is usable even when that fails: the error says why the list is empty, for the log.

func (*Manager) Check

func (m *Manager) Check(ctx context.Context, id string) (Status, error)

Check asks computer id who this machine is there (GET /v1/pair/me) and records the answer: the role, or that the token is no longer known. Any other answer — offline, a 5xx — says nothing about the token and changes nothing but the channel's own state.

func (*Manager) Close

func (m *Manager) Close()

Close hangs up every channel.

func (*Manager) Do

func (m *Manager) Do(id string, req *http.Request) (*http.Response, error)

Do carries req to computer id as this machine's paired device there. Only the path and query travel. Whatever identifies the caller here — cookies, Origin, a device token of this machine's own — is removed, and this machine's token on that computer is set; the target's gate decides the rest.

func (*Manager) Forget

func (m *Manager) Forget(id string) error

Forget deletes a computer and its token here. The computer still lists this machine until its owner removes it there, unless Leave reached it first.

func (*Manager) Get

func (m *Manager) Get(id string) (Status, error)

Get is one computer.

func (*Manager) Leave added in v0.122.0

func (m *Manager) Leave(ctx context.Context, id string) error

Leave asks computer id to drop this machine (DELETE /v1/pair/me): Forget's courtesy to the computer, best effort. An error says only that it was not done — offline, already removed, or a computer from before ADR-067's PR2.

func (*Manager) List

func (m *Manager) List() []Status

List is every computer, in the order it was added.

func (*Manager) OpenWS

func (m *Manager) OpenWS(ctx context.Context, id, path string, protocols []string) (*remote.WSConn, error)

OpenWS opens a WebSocket at path on computer id, offering protocols plus this machine's device token there (as caprock.device.<token>, the way a phone's browser carries it). The socket's Subprotocol may be that token's; a caller passing it on must not echo it.

func (*Manager) Pair

func (m *Manager) Pair(ctx context.Context, link string) (Status, error)

Pair redeems link's code on the computer it names, through the relay, and keeps the device token it is given. The role is the one the target's owner chose when showing the code (ADR-034, ADR-067); nothing this machine sends names one. Pairing with a computer already on the list replaces its entry.

func (*Manager) Rename

func (m *Manager) Rename(id, name string) error

Rename changes what this machine calls a computer. The computer itself is not told.

type Options

type Options struct {
	// Dir is the data directory machines.json lives in.
	Dir string
	// Self is this machine's own relay id, refused as a target; "" when
	// unknown.
	Self string
	// Name is what the targets list this machine as.
	Name string
	Log  *slog.Logger
	// HTTPClient dials relays; nil is the default client.
	HTTPClient *http.Client
	// Idle overrides how long a channel with nothing open stays up.
	Idle time.Duration
}

Options configure a Manager.

type Status

type Status struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Relay   string `json:"relay"`
	AddedAt int64  `json:"added_at"`
	// Status is the channel's (idle, connecting, online, offline, refused)
	// or StatusRevoked.
	Status string `json:"status"`
	Error  string `json:"error,omitempty"`
	// Version is the Caprock the computer runs, from its last Welcome.
	Version string `json:"version,omitempty"`
	// Role is this machine's role there as the computer last said it:
	// viewer or controller; empty until asked.
	Role string `json:"role,omitempty"`
	// Kind is what the computer paired this machine as: "machine" (full
	// control, ADR-067), or empty for a phone's code — a computer from
	// before ADR-067's PR2, which pairs one as an ordinary device.
	Kind string `json:"kind,omitempty"`
	// LastSeen is when the computer last answered, unix milliseconds by
	// this machine's clock; 0 for not since this daemon started.
	LastSeen int64 `json:"last_seen,omitempty"`
	// Streams is how many requests and sockets are open to it.
	Streams int `json:"streams"`
}

Status is one computer as the dashboard and the CLI see it. No token.

Jump to

Keyboard shortcuts

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