peers

package
v0.201.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 20 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 (
	JoinNone    = "none"    // no key was made, or it was withdrawn
	JoinWaiting = "waiting" // open, nobody has used it yet
	JoinJoining = "joining" // a computer proved the secret and is linking
	JoinJoined  = "joined"  // linked: Machine says which computer
	JoinExpired = "expired" // JoinTTL passed unused
	JoinFailed  = "failed"  // burned by wrong secrets, or the link failed

)

The states of a join key (JoinState.State).

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 (
	SetupChromeExtension = "chrome_extension"
	SetupSiteSignIns     = "site_sign_ins"
)

The setup steps only the computer's own screen can do, which the laptop keeps as marked done (Machine.SetupDone).

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.

View Source
const JoinTTL = 15 * time.Minute

JoinTTL is how long a join key works: long enough to install Caprock on the new computer first.

View Source
const JoinTries = 5

JoinTries is how many wrong secrets burn a key.

Variables

View Source
var (
	// ErrJoinGone is a key whose rendezvous is not there: expired, used,
	// withdrawn, or the issuing computer offline.
	ErrJoinGone = errors.New("that key's computer is not waiting for it: the key expired (15 minutes), was used, or was withdrawn, or that computer is off. Make a new key there")
	// ErrJoinRefused is a key the rendezvous refused.
	ErrJoinRefused = errors.New("that computer did not accept the key")
)

Errors a join tells apart.

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.

View Source
var ErrNoIdentity = errors.New("this computer has no relay key yet: open Settings → Use on your phone once, or run `caprock remote on` and off")

ErrNoIdentity is a machine with no relay key to pre-approve.

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 JoinDesk added in v0.201.0

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

JoinDesk holds the open join key, if any.

func NewJoinDesk added in v0.201.0

func NewJoinDesk(opt JoinOptions) *JoinDesk

NewJoinDesk makes the desk; nothing is dialled until New.

func (*JoinDesk) Close added in v0.201.0

func (j *JoinDesk) Close()

Close is Withdraw, for the daemon's shutdown.

func (*JoinDesk) New added in v0.201.0

func (j *JoinDesk) New() (NewJoinKey, error)

New makes a key, replacing any open one, and starts waiting for it.

func (*JoinDesk) State added in v0.201.0

func (j *JoinDesk) State() JoinState

State is how the open key stands.

func (*JoinDesk) Withdraw added in v0.201.0

func (j *JoinDesk) Withdraw()

Withdraw closes the open key: it stops working at once.

type JoinOptions added in v0.201.0

type JoinOptions struct {
	// Relay is where the rendezvous waits: this machine's own relay, the
	// one access from anywhere uses (Settings → Use on your phone).
	Relay func() string
	// Controller is this machine's relay key, remote.HostID of the key its
	// channels prove (Options.Identity); "" refuses to make a key, since the
	// new computer could pre-approve nothing.
	Controller func() string
	Name       string
	Version    string
	Log        *slog.Logger
	// Pair links the computer a link names: Manager.Pair.
	Pair func(ctx context.Context, link string) (Status, error)
	// Base bounds every rendezvous: the daemon's life.
	Base context.Context
	// TTL overrides JoinTTL, for tests.
	TTL time.Duration
}

JoinOptions configure the join desk.

type JoinState added in v0.201.0

type JoinState struct {
	State string `json:"state"`
	// ExpiresAt is unix milliseconds, this machine's clock; 0 with no key.
	ExpiresAt int64 `json:"expires_at,omitempty"`
	// Relay is where the rendezvous waits, and RelayStatus how its
	// connection there stands (connecting, online, offline).
	Relay       string `json:"relay,omitempty"`
	RelayStatus string `json:"relay_status,omitempty"`
	// Joiner is the name the new computer gave, once it proved the secret.
	Joiner string `json:"joiner,omitempty"`
	// Machine is the computer that joined, as GET /v1/machines lists it.
	Machine *Status `json:"machine,omitempty"`
	Error   string  `json:"error,omitempty"`
}

JoinState is GET /v1/machines/join-key: never the key.

type JoinWithOptions added in v0.201.0

type JoinWithOptions struct {
	// Name is what this computer calls itself to the issuer.
	Name string
	// Link mints this computer's pairing link with a code that pre-approves
	// controller (pairing.Store.NewCodeForKey) and returns it, access from
	// anywhere on and connected to its relay. Cancel withdraws that code
	// when the join fails after it.
	Link   func(ctx context.Context, controller string) (string, error)
	Cancel func()
	// HTTPClient dials the relay; nil is the default.
	HTTPClient *http.Client
	Log        *slog.Logger
}

JoinWithOptions are what joining needs of the daemon it runs in.

type Joined added in v0.201.0

type Joined struct {
	// Controller is the issuing computer's relay key, and ControllerName
	// what it calls itself: the one computer now allowed here.
	Controller     string `json:"controller"`
	ControllerName string `json:"controller_name"`
	// Status is this computer as the issuer lists it after pairing
	// (online, normally).
	Status string `json:"status"`
}

Joined says what a join did.

func Join added in v0.201.0

func Join(ctx context.Context, raw string, opt JoinWithOptions) (Joined, error)

Join uses a join key: proves it to the issuer's rendezvous, mints this computer's link for the issuer alone, and hands it over; the issuer pairs with it, and is allowed here the moment it does.

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"`
	// SetupDone are the setup steps that need the computer's own screen and
	// that the user marked done there from here (*Done there*, issue
	// #1088): SetupChromeExtension, SetupSiteSignIns. Nothing checks them.
	SetupDone []string `json:"setup_done,omitempty"`
}

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.

func (*Manager) SetSetupDone added in v0.198.0

func (m *Manager) SetSetupDone(id string, done []string) error

SetSetupDone keeps which of the steps that need the computer's own screen the user marked done there (issue #1088). Only the known steps are kept, each once, in a fixed order.

type NewJoinKey added in v0.201.0

type NewJoinKey struct {
	JoinState
	Key string `json:"key"`
}

NewJoinKey is POST /v1/machines/join-key's answer: the key, once.

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
	// Identity is this machine's relay key, proved on every channel so each
	// computer holds this machine's token there to it (remote.KindProve,
	// ADR-067 Phase 4); nil proves nothing.
	Identity ed25519.PrivateKey
}

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"`
	// SetupDone are the steps marked done there by hand (Machine.SetupDone).
	SetupDone []string `json:"setup_done,omitempty"`
	// Traffic is what this machine's channel to it has cost the relay since
	// this daemon started or paired it (OQ-14): the relay messages and their
	// bytes each way, the relay's own pings included.
	Traffic Traffic `json:"traffic"`
}

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

type Traffic added in v0.174.1

type Traffic struct {
	MsgsOut  uint64 `json:"msgs_out"`
	MsgsIn   uint64 `json:"msgs_in"`
	BytesOut uint64 `json:"bytes_out"`
	BytesIn  uint64 `json:"bytes_in"`
}

Traffic counts relay messages and bytes, each way.

Jump to

Keyboard shortcuts

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