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) Unregister(ctx context.Context) error
- type Config
- 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) Enable(ctx context.Context, req EnableRequest) (replaced bool, 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 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 )
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 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.
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.
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.
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"`
}
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"`
}
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) 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) 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 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 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.