Documentation
¶
Overview ¶
Package remote connects a running Flockdeck to a relay, so the window can be opened from another device.
The desktop never listens for anyone. It dials out to the relay, holds one WebSocket open, and carries a stream multiplexer over it; the relay opens a stream for each connection a paired browser makes, and each stream is served here by the very handlers the local window uses. Nothing on this machine is reachable from the network that was not before, and no port is opened.
What the relay is, and what it may see, is described in the relay's own design notes: traffic is TLS on both legs and the relay decrypts it to route it. It is trusted, and the help says so rather than implying otherwise.
Index ¶
- Constants
- Variables
- func CheckName(name string) (string, error)
- func CheckRelay(raw string) (string, error)
- func Clear() error
- func Disable(ctx context.Context, version string, force bool) (had bool, untold error, err error)
- func IsRevoked(err error) bool
- func Probe(ctx context.Context, relay string) error
- func QRSVG(text string) (string, error)
- func QRTerminal(text string) (string, error)
- func RelayURL(named string) (string, error)
- func RevokedReason(err error) string
- func SameRelay(a, b string) bool
- func Seal(p256dh, auth string, plain []byte) (string, error)
- type APIError
- type AlreadyEnabledError
- type Client
- func (c *Client) Devices(ctx context.Context) (*Roster, error)
- func (c *Client) Pair(ctx context.Context, kind string) (*Pairing, error)
- func (c *Client) Push(ctx context.Context, n Notification) (int, error)
- func (c *Client) PushDevices(ctx context.Context) ([]PushDevice, error)
- func (c *Client) RenameDevice(ctx context.Context, deviceID, name string) error
- func (c *Client) RenameHost(ctx context.Context, name string) error
- func (c *Client) Revoke(ctx context.Context, deviceID string) error
- func (c *Client) SetE2EKey(ctx context.Context, publicKey string) error
- func (c *Client) Unregister(ctx context.Context) error
- type Config
- func Enable(ctx context.Context, version string, req EnableRequest) (cfg *Config, replaced bool, err error)
- func Load() (*Config, error)
- func Move(ctx context.Context, version string, req EnableRequest, switched func()) (cfg *Config, untold error, err error)
- func Rename(ctx context.Context, version, name string) (*Config, error)
- type Connector
- type Device
- type EnableRequest
- type Host
- type Manager
- func (m *Manager) Client() (*Client, error)
- func (m *Manager) Close()
- func (m *Manager) Disable(ctx context.Context, force bool) (untold error, err error)
- func (m *Manager) E2ECapable(ctx context.Context, deviceID string) bool
- func (m *Manager) E2EKeyStatus() (at time.Time, err error)
- func (m *Manager) E2ERespond(ctx context.Context, deviceID string, hello []byte) (*e2e.Session, []byte, error)
- func (m *Manager) Enable(ctx context.Context, req EnableRequest) (replaced bool, err error)
- func (m *Manager) EnsureE2EKey(ctx context.Context) error
- func (m *Manager) Move(ctx context.Context, req EnableRequest) (untold error, err error)
- func (m *Manager) Reconnect() error
- func (m *Manager) Reload() error
- func (m *Manager) Rename(ctx context.Context, name string) error
- func (m *Manager) Status() (Status, bool)
- type Notification
- type Pairing
- type Plan
- type PushDevice
- type PushMessage
- type RegisterRequest
- type Registration
- type RelayUntoldError
- type Roster
- type State
- type Status
Constants ¶
const ( KindDevice = "device" KindHost = "host" )
Pairing kinds.
const ( // CloseRevoked: the host's token is no longer accepted. CloseRevoked websocket.StatusCode = 4001 // CloseReplaced: another connection has come in for the same host, and a // host has only one. Most often that is a second Flockdeck on this machine, // started with -solo; fighting it for the slot would have the two of them // knock each other off the relay every few seconds for as long as both run. CloseReplaced websocket.StatusCode = 4002 // CloseLapsed: the account's trial or subscription on the relay has run // out. Unlike the other two it is not the end: the relay lets this host // back in once the account is paid for, so it is tried again, slowly. CloseLapsed websocket.StatusCode = 4003 )
The relay closes the tunnel with one of these when retrying would be wrong.
const DefaultRelay = "https://remote.flockdeck.ai"
DefaultRelay is the relay a machine is enrolled with when nobody names one.
It was https://relay.flockdeck.ai in v0.2.0, and a machine enrolled then keeps that address in its remote.json. That is fine: the relay still answers desktops on its old name, and sends a browser that arrives there to this one.
const File = "remote.json"
File is where the enrolment is kept, in the state directory.
const RelayEnv = "FLOCKDECK_RELAY"
RelayEnv overrides DefaultRelay, for pointing Flockdeck at a development or staging relay.
const Subprotocol = "flockdeck-relay.v1"
Subprotocol is what the tunnel is spoken as. A relay that does not know it is not one this build can talk to.
Variables ¶
var ErrNoE2EKey = errors.New("remote: no end-to-end key is registered for this host and device")
ErrNoE2EKey is what E2ERespond fails with, and what makes E2ECapable answer false, when either this host or the device asking for a terminal has no long-term end-to-end key registered with the relay: an older client that predates this feature, or a key rotation still in flight. The caller's answer is to serve the terminal unencrypted rather than refuse it -- see internal/server/pty.go's use of both.
var ErrNotEnabled = errors.New("remote access is not enabled on this machine; turn it on from Remote access… in the command palette, or with `flockdeck remote enable`")
ErrNotEnabled is what anything that needs an enrolment is told when there is none.
var ErrSameRelay = errors.New("this machine is already on that relay, so there is nowhere to move it")
ErrSameRelay is a move to the relay this machine is already on.
Functions ¶
func CheckName ¶ added in v0.2.10
CheckName is a new name for this machine or a device as the relay will keep it, or why it cannot be one: nothing left once it is cleaned, or one of the relay's codes, which would be shown on every device as what it is called.
func CheckRelay ¶
CheckRelay insists on a relay address that can be trusted with the token.
That means HTTPS. The host token and everything a paired device types cross this connection, and the relay being trusted is no help if the hop to it is not. Plain HTTP is allowed only to this machine itself, which is where a relay being developed or tested runs, and where there is no network for the traffic to cross.
func Clear ¶
func Clear() error
Clear forgets the enrolment. It is not an error for there to be none.
func Disable ¶ added in v0.2.3
Disable takes this machine off its relay and forgets the enrolment, and reports whether there was one.
A relay that cannot be told stops it with a *RelayUntoldError, and an enrolment that cannot be read with Load's error, unless force says to forget the enrolment here regardless. Then why the relay could not be told comes back as untold, for the caller to pass on.
func Probe ¶ added in v0.2.3
Probe asks the relay at relay whether it is there, without a token. Moving a machine is leaving one relay and then enrolling with another, and a mistyped or unreachable address is best found out before the first step, while the machine still has a relay to be on.
func QRSVG ¶
QRSVG draws text as a QR code, as an SVG document.
It is dark modules on a white square whatever the window's theme is. A code drawn in the theme's colours would be light on dark in this application, and while some readers cope with an inverted code, a phone's own camera is not reliably one of them.
func QRTerminal ¶
QRTerminal draws text as a QR code in text, two modules to a line.
Each character covers two rows with the half-block characters, which keeps the code square in a terminal whose cells are about twice as tall as they are wide. Light modules are the ones drawn, so on the dark background most terminals have, the code comes out the right way round: dark modules on a light field, quiet zone included.
func RelayURL ¶
RelayURL decides which relay is meant: the one named, with -relay or in the window, else the one in the environment, else the default. It is checked and normalised on the way out, so that everything downstream can join paths on to it without thinking.
func RevokedReason ¶ added in v0.3.5
RevokedReason explains, in one plain sentence, what err (which must satisfy IsRevoked) means: this machine is no longer paired, and the two most likely reasons why. When the relay said more than the boilerplate refusal above -- because it still had the tunnel open, and so knew, or the token was refused with a reason of its own -- that is passed on too.
func SameRelay ¶ added in v0.2.3
SameRelay reports whether two relay addresses name the same relay, however each was typed: the hosted relay by either of its names is one relay, and telling somebody enrolled under the old name to move to the new one would have them unpair every device to arrive where they already are.
func Seal ¶ added in v0.3.5
Seal encrypts a message to a browser's push subscription -- its P-256 public key and its 16-byte auth secret, base64url as the browser gives them -- and gives back the aes128gcm body, base64url, which only that browser can open. Every body is the same length, whatever the message.
Types ¶
type APIError ¶
APIError is the relay saying no, with its reason.
func (*APIError) Revoked ¶
Revoked reports whether the relay no longer accepts this host's token — because the host was taken off the relay, by something holding that token or by another member of its account, a paired device or another of its machines, or the relay no longer has it. There is nothing to retry: the enrolment is spent.
Only a refusal in the relay's own words counts. A 403 is also what a proxy or a firewall in front of the relay answers with, on a page of its own, and taking that for a revocation would stop the tunnel for good and have `remote enable` enrol the machine again over a host that is still live.
type AlreadyEnabledError ¶ added in v0.2.3
AlreadyEnabledError refuses to enrol over an enrolment the relay may still hold. Enrolling a second time would leave the first host on the relay with nobody holding its token, listed on every device as a machine that is never online. Err is why the relay could not be asked whether it still holds it; nil means it answered that it does.
func (*AlreadyEnabledError) Error ¶ added in v0.2.3
func (e *AlreadyEnabledError) Error() string
func (*AlreadyEnabledError) Unwrap ¶ added in v0.2.3
func (e *AlreadyEnabledError) Unwrap() error
type Client ¶
Client makes the relay's REST calls on behalf of an enrolled host.
func (*Client) Push ¶ added in v0.3.5
Push sends a notification to every device of the account that asked for them, each copy sealed here for its own device, and says how many a push service took. A relay that sends none, or an account whose plan does not include them, says so in its own words: whether pushes are sent is the relay's to decide.
A device that replaced its keys between the asking and the sending is not sent a message it could not open; the relay says so, and is asked once more for the keys of the devices that changed.
func (*Client) PushDevices ¶ added in v0.3.5
func (c *Client) PushDevices(ctx context.Context) ([]PushDevice, error)
PushDevices asks the relay which of the account's devices asked to be sent pushes, and their keys.
func (*Client) RenameDevice ¶ added in v0.2.10
RenameDevice gives one of the account's paired devices a new name.
func (*Client) RenameHost ¶ added in v0.2.10
RenameHost gives this machine a new name, which is what every paired device and the account's other machines list it as. Rename, which also saves it in the enrolment, is what the command line and the window use.
func (*Client) Revoke ¶
Revoke unpairs a device. Its session ends at once, including any window it has open.
type Config ¶
type Config struct {
Relay string `json:"relay"`
HostID string `json:"hostId"`
AccountID string `json:"accountId"`
Token string `json:"token"`
Name string `json:"name,omitempty"`
}
Config is this machine's enrolment with a relay.
Token is the credential the relay knows this host by. It is a secret with the same reach as the window itself — whoever holds it can mint pairing codes, and a paired device can type into every agent — so it lives in a 0600 file in the state directory beside the local server's own token, is never printed, and never travels anywhere but to the relay that issued it.
func Enable ¶ added in v0.2.3
func Enable(ctx context.Context, version string, req EnableRequest) (cfg *Config, replaced bool, err error)
Enable enrols this machine with a relay and saves the enrolment.
An enrolment already here is an *AlreadyEnabledError, except when its relay has forgotten it: that is exactly when enrolling again is right, and replaced says it happened.
func Load ¶
Load returns the enrolment, or nil when this machine has none.
A file that is there and cannot be understood is an error rather than "not enabled": answering that would tell the user remote access is off while a relay still holds a live host for this machine, and invite them to enrol it a second time.
It is read the way the store reads its own files, waiting out a save that has the file open. On Windows a save replacing it, or the virus scanner reading what the save wrote, refuses every other open for that moment, and read straight the enrolment came back as an error: remote access reported broken, and not reconnected, for a file that was perfectly sound.
func Move ¶ added in v0.3.26
func Move(ctx context.Context, version string, req EnableRequest, switched func()) (cfg *Config, untold error, err error)
Move enrols this machine with another relay and then takes it off the one it is on: what a company moving its desktops off the shared relay, or off an old relay of its own, does.
The order is the point. The machine is enrolled with the new relay first, and the new relay is asked, with the new credential, to answer for it; only then is the enrolment here replaced and the old relay told. A new relay that refuses, needs an invitation not given, or is not there leaves the machine where it was, still reachable by every device paired with it.
Every device has to pair again afterwards, with the new relay: a device's session is a cookie on the relay's own origin, which no other relay can read. The caller says so before the move, since it cannot be undone by moving back.
switched, if given, is called once the new enrolment is saved and before the old relay is told, for a running instance to move its tunnel across then, rather than see it cut by the old relay first.
untold is why the old relay could not be told. The move is done then, and the old relay goes on listing this machine, offline, until a device paired there removes it.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector holds one host's tunnel to its relay open, and serves whatever arrives through it.
func NewConnector ¶
func NewConnector(cfg Config, version string, serve func(net.Listener) error, changed func()) *Connector
NewConnector prepares a tunnel for an enrolment; Start opens it.
func (*Connector) RetryNow ¶ added in v0.2.3
func (c *Connector) RetryNow()
RetryNow cuts short the wait before the next attempt, when one is being waited out, so that somebody who has put right whatever was wrong need not sit through up to a minute of backoff to see it.
func (*Connector) Start ¶
func (c *Connector) Start()
Start opens the tunnel and keeps it open until Stop, or until the relay says there is no point.
type Device ¶
type Device struct {
ID string `json:"id"`
Name string `json:"name"`
Created time.Time `json:"created"`
LastSeen time.Time `json:"lastSeen"`
// PublicKey is this device's long-term key for end-to-end encrypting the
// terminals it opens (internal/e2e), base64url -- what this host hands to
// RespondHostHandshake for this device's hello. Empty until the browser
// has registered one with POST /api/v1/device/e2e-key, which an older
// client that predates end-to-end encryption never will: a terminal
// opened by one of those is served unencrypted rather than refused.
PublicKey string `json:"publicKey,omitempty"`
}
Device is a browser paired with the account.
type EnableRequest ¶ added in v0.2.3
EnableRequest is what enrolling takes. Every field may be left empty: the relay is then the one RelayURL settles on, and the name the host name.
type Host ¶
type Host struct {
ID string `json:"id"`
Name string `json:"name"`
Online bool `json:"online"`
LastSeen time.Time `json:"lastSeen"`
Self bool `json:"self"`
URL string `json:"url,omitempty"`
// PublicKey is this desktop's long-term key for end-to-end encrypting its
// terminals (internal/e2e), base64url. Empty until it has registered one
// with POST /api/v1/host/e2e-key -- which EnsureE2EKey does, on this
// machine's own entry, as soon as the tunnel is up.
PublicKey string `json:"publicKey,omitempty"`
}
Host is a desktop enrolled in the account; Self marks this one. URL is where a paired device opens it, when the relay says.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is remote access as a running instance has it: the enrolment on disk, and the tunnel that goes with it.
The enrolment is written by `flockdeck remote`, from another process that then tells the instance to look again, or by Enable and Disable here on the window's behalf. Reload is that looking: it brings the tunnel in line with whatever the file now says — opened, reopened to a different relay, or closed.
func NewManager ¶
NewManager makes a manager with nothing running. serve answers each tunnel's connections; changed is told whenever what the window should show moves.
func (*Manager) Close ¶
func (m *Manager) Close()
Close closes the tunnel, and every remote window with it.
func (*Manager) Disable ¶ added in v0.2.3
Disable takes this machine off its relay and closes the tunnel: what `flockdeck remote disable` does, for the window. untold is why the relay could not be told, when force had the enrolment forgotten regardless.
func (*Manager) E2ECapable ¶ added in v0.3.26
E2ECapable reports whether a terminal opened for deviceID right now could be end-to-end encrypted: this machine has a long-term key, and the relay's roster says deviceID has registered one too. It answers false, never an error, for anything that stops it finding out -- the relay unreachable, no such device, an unenrolled machine -- since every one of those is a plain "serve this terminal unencrypted", not a fault to report.
func (*Manager) E2EKeyStatus ¶ added in v0.3.26
E2EKeyStatus reports the outcome of the last attempt to register this machine's end-to-end key with the relay -- ok if it has never been tried, since that only means no enrolment has asked for it yet. It is what a diagnostics view queries; nothing here decides anything by it.
func (*Manager) E2ERespond ¶ added in v0.3.26
func (m *Manager) E2ERespond(ctx context.Context, deviceID string, hello []byte) (*e2e.Session, []byte, error)
E2ERespond runs this host's whole side of a fresh terminal handshake (internal/e2e.RespondHostHandshake): hello is the browser's ephemeral public key, the terminal socket's first frame. It returns the session and the response to send back as the socket's second frame.
Callers are expected to have checked E2ECapable first, so that hello is only ever read from a socket both sides are expected to encrypt; called otherwise, a missing key on either side comes back as ErrNoE2EKey rather than attempting a handshake that cannot complete.
func (*Manager) Enable ¶ added in v0.2.3
Enable enrols this machine with a relay and brings the tunnel up to it: what `flockdeck remote enable` does, for the window.
func (*Manager) EnsureE2EKey ¶ added in v0.3.26
EnsureE2EKey makes sure this machine has a long-term end-to-end identity and that the relay has its public half on file, generating and registering one if not. Both halves are idempotent, so it is safe to call often: Reload does, every time the enrolment is read, which includes every application start and every explicit reconnect.
A machine enrolled before this feature existed has no identity on its first call after upgrading, and gets one then; a machine that already has one, whose relay has simply forgotten it (a restore from backup, a migration), is registered again rather than left looking unencrypted to every device that asks.
func (*Manager) Move ¶ added in v0.3.26
Move enrols this machine with another relay and leaves the one it is on, once the new one answers: what `flockdeck remote move` does, for the window. The tunnel is moved across as soon as the new enrolment is saved, before the old relay is told, so the window shows it connecting to the new relay rather than cut off by the old. untold is why the old relay could not be told, when the move went ahead regardless.
func (*Manager) Reconnect ¶ added in v0.2.3
Reconnect tries the relay again now, for the window's "try again": a tunnel waiting out a failure skips the rest of the wait, and one that has given up, revoked or stepped aside for another instance, is started again, as Reload does. A connected tunnel is left alone.
func (*Manager) Reload ¶
Reload reads the enrolment again and makes the tunnel match it.
A tunnel that already matches is left alone, so reloading costs a working connection nothing, and so does a new name, which the relay keeps rather than the tunnel: renaming this machine must not drop every remote window. One that has given up — revoked, or displaced by another instance — is started again, because being asked to reload is somebody saying that whatever stopped it has been dealt with.
A file that cannot be read is reported and the tunnel left as it was: there is no telling what the file meant, and dropping every remote window over a half-written file is the worse guess.
type Notification ¶ added in v0.3.5
type Notification struct {
Title string `json:"title"`
Body string `json:"body,omitempty"`
URL string `json:"url"`
Tag string `json:"tag"`
}
Notification is what a push says, as the client's service worker reads it: its title and body, the page a tap on it opens, and its tag, which a newer notification with the same one replaces on the device.
type Pairing ¶
type Pairing struct {
Code string `json:"code"`
URL string `json:"url"`
ExpiresAt time.Time `json:"expiresAt"`
}
Pairing is a one-time code, and for a device the link that redeems it. ExpiresAt is by this machine's clock, however far the relay's is from it.
type Plan ¶ added in v0.3.26
type Plan struct {
// Plan is trial, subscribed, or lapsed once either has run out.
Plan string `json:"plan"`
// Name is what the relay calls it.
Name string `json:"name"`
Active bool `json:"active"`
// Ends is when the trial or subscription runs out, or ran out.
Ends time.Time `json:"ends,omitzero"`
// Was is, for a lapsed account, the plan that ran out.
Was string `json:"was,omitempty"`
// DeleteAt is when a lapsed account is deleted unless it subscribes.
DeleteAt time.Time `json:"deleteAt,omitzero"`
// Message is, for a lapsed account, the relay's own words about it.
Message string `json:"message,omitempty"`
}
Plan is an account's plan as its relay reports it. Flockdeck shows it and decides nothing by it: whether remote access works is the relay's to say, and it says so by refusing the tunnel.
type PushDevice ¶ added in v0.3.5
type PushDevice struct {
DeviceID string `json:"deviceId"`
P256dh string `json:"p256dh"`
Auth string `json:"auth"`
}
PushDevice is a device of the account that asked to be sent pushes, as the relay describes it: the keys a message to it is encrypted to, and nothing about where it is reached.
type PushMessage ¶ added in v0.3.5
type PushMessage struct {
DeviceID string `json:"deviceId"`
P256dh string `json:"p256dh"`
Payload string `json:"payload"`
}
PushMessage is one device's notification, sealed for it alone, and the key it was sealed to.
type RegisterRequest ¶
type RegisterRequest struct {
Name string `json:"name"`
Join string `json:"join,omitempty"`
Invite string `json:"invite,omitempty"`
}
RegisterRequest enrols a machine. Join puts it in the account a host-kind pairing code belongs to rather than a new one; Invite is what a relay that does not take registrations from just anyone asks for.
type Registration ¶
type Registration struct {
HostID string `json:"hostId"`
AccountID string `json:"accountId"`
Token string `json:"token"`
}
Registration is what the relay hands back for a new host.
func Register ¶
func Register(ctx context.Context, relay, version string, req RegisterRequest) (*Registration, error)
Register enrols this machine with a relay. It is made without a token, because it is the call that gets one; Probe, the only other made without, asks no more than whether a relay is there.
type RelayUntoldError ¶ added in v0.2.3
type RelayUntoldError struct{ Err error }
RelayUntoldError is taking this machine off a relay that could not be told, without being asked to forget the enrolment regardless. Forgetting it here would leave the machine listed on the relay, offline, until somebody removes it from a paired device.
func (*RelayUntoldError) Error ¶ added in v0.2.3
func (e *RelayUntoldError) Error() string
func (*RelayUntoldError) Unwrap ¶ added in v0.2.3
func (e *RelayUntoldError) Unwrap() error
type Roster ¶
type Roster struct {
Devices []Device `json:"devices"`
Hosts []Host `json:"hosts"`
// Plan is the account's plan, from a relay that asks for a subscription;
// any other relay sends none.
Plan *Plan `json:"plan,omitempty"`
}
Roster is everything the account has paired and enrolled.
type State ¶
type State string
State is where the connection to the relay stands.
const ( StateOff State = "off" StateConnecting State = "connecting" StateConnected State = "connected" StateRevoked State = "revoked" StateReplaced State = "replaced" StateError State = "error" // StateLapsed is the relay refusing remote access because the account's // trial or subscription has run out. Detail is the relay's own sentence: // Flockdeck decides nothing by a plan, and only shows what the relay says. StateLapsed State = "lapsed" )
type Status ¶
type Status struct {
State State `json:"state"`
Detail string `json:"detail,omitempty"`
Since time.Time `json:"since"`
// RetryAt is when the next attempt is due, while one is being waited for.
RetryAt time.Time `json:"retryAt,omitzero"`
Relay string `json:"relay"`
HostID string `json:"hostId"`
Name string `json:"name,omitempty"`
}
Status is what the window is told about the tunnel. The command line asks the relay instead, since the tunnel belongs to a process it is not.