remote

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT 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 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

View Source
const (
	KindDevice = "device"
	KindHost   = "host"
)

Pairing kinds.

View Source
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.

View Source
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.

View Source
const File = "remote.json"

File is where the enrolment is kept, in the state directory.

View Source
const RelayEnv = "FLOCKDECK_RELAY"

RelayEnv overrides DefaultRelay, for somebody running a relay of their own or developing against one.

View Source
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

View Source
var ErrNotEnabled = errors.New("remote access is not enabled on this machine; run `flockdeck remote enable` in a terminal")

ErrNotEnabled is what anything that needs an enrolment is told when there is none.

Functions

func CheckRelay

func CheckRelay(raw string) (string, error)

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 IsRevoked

func IsRevoked(err error) bool

IsRevoked reports whether err is the relay refusing this host's token.

func QRSVG

func QRSVG(text string) (string, error)

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

func QRTerminal(text string) (string, error)

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

func RelayURL(named string) (string, error)

RelayURL decides which relay is meant: the one named on the command line, 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.

Types

type APIError

type APIError struct {
	Status  int
	Message string
}

APIError is the relay saying no, with its reason.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Revoked

func (e *APIError) Revoked() bool

Revoked reports whether the relay no longer accepts this host's token — because the host was removed, from here or from a device, or the account was. There is nothing to retry: the enrolment is spent.

type Client

type Client struct {
	Relay   string
	Token   string
	Version string
	HTTP    *http.Client
}

Client makes the relay's REST calls on behalf of an enrolled host.

func NewClient

func NewClient(c *Config, version string) *Client

NewClient is a client for an enrolment.

func (*Client) Devices

func (c *Client) Devices(ctx context.Context) (*Roster, error)

Devices lists the account's devices and hosts.

func (*Client) Pair

func (c *Client) Pair(ctx context.Context, kind string) (*Pairing, error)

Pair asks for a one-time pairing code of the given kind.

func (*Client) Revoke

func (c *Client) Revoke(ctx context.Context, deviceID string) error

Revoke unpairs a device. Its session ends at once, including any window it has open.

func (*Client) Unregister

func (c *Client) Unregister(ctx context.Context) error

Unregister removes this host from the relay, which spends its token.

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 Load

func Load() (*Config, error)

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.

func (*Config) Save

func (c *Config) Save() error

Save writes the enrolment, readable by this user only.

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) Start

func (c *Connector) Start()

Start opens the tunnel and keeps it open until Stop, or until the relay says there is no point.

func (*Connector) Status

func (c *Connector) Status() Status

Status reports where the tunnel stands.

func (*Connector) Stop

func (c *Connector) Stop()

Stop closes the tunnel, and every remote window with it, and waits for that to have happened.

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 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"`
}

Host is a desktop enrolled in the account; Self marks this one.

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, and the instance is told to look again. 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

func NewManager(version string, serve func(net.Listener) error, changed func()) *Manager

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) Client

func (m *Manager) Client() (*Client, error)

Client is a client for the relay this machine is enrolled with.

func (*Manager) Close

func (m *Manager) Close()

Close closes the tunnel, and every remote window with it.

func (*Manager) Reload

func (m *Manager) Reload() error

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. 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.

func (*Manager) Status

func (m *Manager) Status() (Status, bool)

Status reports the tunnel's state, and whether this machine is enrolled at all.

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.

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 the one call made without a token, because it is the one that gets one.

type Roster

type Roster struct {
	Devices []Device `json:"devices"`
	Hosts   []Host   `json:"hosts"`
}

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"
)

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 and the command line are told about the tunnel.

Jump to

Keyboard shortcuts

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