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
- Variables
- func Write(dir string, ms []Machine) error
- type Machine
- type Manager
- func (m *Manager) Check(ctx context.Context, id string) (Status, error)
- func (m *Manager) Close()
- func (m *Manager) Do(id string, req *http.Request) (*http.Response, error)
- func (m *Manager) Forget(id string) error
- func (m *Manager) Get(id string) (Status, error)
- func (m *Manager) Leave(ctx context.Context, id string) error
- func (m *Manager) List() []Status
- func (m *Manager) OpenWS(ctx context.Context, id, path string, protocols []string) (*remote.WSConn, error)
- func (m *Manager) Pair(ctx context.Context, link string) (Status, error)
- func (m *Manager) Rename(id, name string) error
- type Options
- type Status
Constants ¶
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.
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 ¶
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 ¶
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.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager keeps the computers and a channel, opened on demand, to each.
func Open ¶
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 ¶
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) Do ¶
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 ¶
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) Leave ¶ added in v0.122.0
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) 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 ¶
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.
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.