fleet

package
v0.4.92 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 34 Imported by: ? 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 fleet lets one box be the master of the owner's other boxes: each member keeps an outbound tunnel to the master, and the master relays an allow-listed set of each box's own API routes so one dashboard sees and acts on all of them.

Index

Constants

View Source
const MaxBody = 4 << 20

MaxBody bounds a relayed request or response body, in each direction.

View Source
const StateDir = "/var/lib/casaos/fleet"

StateDir holds master.json and member.json, under Root.

Variables

View Source
var (
	ErrOffline    = errors.New("box offline")
	ErrBusy       = errors.New("too many requests in flight for this box")
	ErrTimeout    = errors.New("the box did not answer in time")
	ErrNotAllowed = errors.New("this route is not relayed")
	ErrNoBox      = errors.New("no such box")
	ErrMasterOff  = errors.New("the master role is off")
)
View Source
var Default = New("/")

Default is this box's fleet, wired by main.

Functions

func Allowed

func Allowed(method, path string) (slow, ok bool)

Allowed says whether method+path (the path alone, no query) is in the table, and whether it is one of the slow routes. The path is compared as it is: no decoding, no trimming, no case folding, so the table cannot be reached around. A path carrying a character a URL reads as a delimiter or an escape (? # % \ or a control byte) is refused outright: an {id} that held one would turn a row of the table into another route once a request is built from it.

func CertFingerprint

func CertFingerprint(certFile string) (string, error)

CertFingerprint is the fingerprint of the first certificate in a PEM file.

func Fingerprint

func Fingerprint(cert *x509.Certificate) string

Fingerprint is the sha256 of a certificate as sent, hex: what a code pins.

func KeyHash

func KeyHash(key string) string

KeyHash is the sha256 of a key, hex: the only form of a key the master stores.

func NewKey

func NewKey() (key, keyHash string, err error)

NewKey makes a box's key (32 random bytes, hex) and the hash the master keeps.

func Timeout

func Timeout(slow bool) time.Duration

Timeout is how long the master waits for a box to answer one relayed request.

func ValidMasterURL

func ValidMasterURL(raw string) error

ValidMasterURL accepts https://host[:port], or plain http on this box's loopback (tests and the install check); never user info, a query or a fragment.

Types

type Box

type Box struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	URL       string    `json:"url"`
	KeyHash   string    `json:"key_sha256"`
	Self      bool      `json:"self"`
	CreatedAt time.Time `json:"created_at,omitzero"`
	LastSeen  time.Time `json:"last_seen,omitzero"`
	Hostname  string    `json:"hostname"`
	Version   string    `json:"version"`
}

Box is one enrolled box as the master keeps it: its key only hashed.

type BoxStatus

type BoxStatus struct {
	ID       string    `json:"id"`
	Name     string    `json:"name"`
	URL      string    `json:"url"`
	Self     bool      `json:"self"`
	Online   bool      `json:"online"`
	LastSeen time.Time `json:"last_seen,omitzero"`
	Hostname string    `json:"hostname"`
	Version  string    `json:"version"`
}

BoxStatus is one box as the dashboard sees it: never its key.

type Code

type Code struct {
	MasterURL  string `json:"u"`
	BoxID      string `json:"b"`
	Key        string `json:"k"`
	CertSHA256 string `json:"f"`
}

Code is what the owner copies from the master into a box's settings: where the master is, which box this is, the box's key, and the fingerprint of the master's certificate for when its chain does not verify. It is shown once.

func ParseCode

func ParseCode(s string) (Code, error)

ParseCode reads a pasted code back, refusing anything a box could not join with.

func (Code) String

func (c Code) String() string

String is the one line the owner copies.

type Fleet

type Fleet struct {
	Root        string                                 // "/" on a box; a temp dir in tests
	RuntimePath func() string                          // where internal.secret lives
	Now         func() time.Time                       // the clock of what is recorded
	Log         func(msg string, err error)            // one line when a file cannot be read
	Gateway     func() (GatewayInfo, error)            // where this box's gateway listens
	Hostname    func() (string, error)                 // this box's name, said in the hello
	Version     func() string                          // this box's ReCasaOS version, said in the hello
	Offline     func(boxID, name string, offline bool) // the alerts hub's hook: a box gone, or back
	// contains filtered or unexported fields
}

Fleet holds both roles of one box. Its func fields are what main wires and tests replace.

func New

func New(root string) *Fleet

New makes a Fleet rooted at root, with the box's defaults.

func (*Fleet) AddBox

func (f *Fleet) AddBox(name, link string, self bool) (Box, Code, error)

AddBox enrols a box and returns its code, the one time the key is handed over. self enrols this box itself: its code points at its own gateway, and its member.json is written. There is at most one self box.

func (*Fleet) DisableMaster

func (f *Fleet) DisableMaster() error

DisableMaster closes every tunnel and forgets the boxes. A self-enrolled master's own member.json pointed at this box's own gateway, which goes with the master, so it is cleared too; a member.json of another fleet is left.

func (*Fleet) EnableMaster

func (f *Fleet) EnableMaster(address string) error

EnableMaster turns the role on at address, keeping the boxes already enrolled. A box that is a member of another fleet cannot become a master: the two roles are exclusive (a self-enrolled master's own member.json names itself, which is not another fleet).

func (*Fleet) HandleTunnel

func (f *Fleet) HandleTunnel(w http.ResponseWriter, r *http.Request)

HandleTunnel is GET /v1/fleet/tunnel: a box's tunnel. 401 unless the Authorization header is "Fleet <id>:<key>" of an enrolled box; then the hello, then res frames until the connection goes. A member's req frame is ignored: only the master asks.

func (*Fleet) Join

func (f *Fleet) Join(code string) error

Join makes this box a member: the code is kept in member.json and the loop dials at once. A master does not join another fleet.

func (*Fleet) Leave

func (f *Fleet) Leave() error

Leave closes the tunnel and forgets the master.

func (*Fleet) MasterStatus

func (f *Fleet) MasterStatus() (MasterStatus, bool)

MasterStatus is the master's view: ok is false when the role is off.

func (*Fleet) MemberStatus

func (f *Fleet) MemberStatus() (MemberStatus, bool)

MemberStatus is the member's view: ok is false when the role is off.

func (*Fleet) Relay

func (f *Fleet) Relay(ctx context.Context, boxID, method, path, query, contentType string, body []byte) (Response, error)

Relay sends one request down the box's tunnel and returns what the box answered. The table is checked here, before anything reaches the tunnel.

func (*Fleet) RemoveBox

func (f *Fleet) RemoveBox(id string) error

RemoveBox revokes a box: its tunnel is closed and its key is worth nothing.

func (*Fleet) RenameBox

func (f *Fleet) RenameBox(id string, name, link *string) error

RenameBox changes the name and/or the dashboard link of a box; nil leaves one as it is.

func (*Fleet) RotateCode

func (f *Fleet) RotateCode(id string) (Code, error)

RotateCode gives a box a new key: the old one is worth nothing and its tunnel is closed; the new code is returned once.

func (*Fleet) Run

func (f *Fleet) Run(ctx context.Context)

Run is the member loop, started once by main: while member.json exists, keep a tunnel to the master and serve what it relays; otherwise wait for a code.

func (*Fleet) Status

func (f *Fleet) Status() Status

Status says which role this box plays and its state. The role is master when the master is on (a self-enrolled master also has a member half, its own tunnel), member when only member.json exists, else none.

type GatewayInfo

type GatewayInfo struct {
	Public      *url.URL // http(s)://127.0.0.1:<port>: where a member executes relayed requests
	HTTPS       *url.URL // https://127.0.0.1:<httpsport or 443>; nil when a configured certificate makes Public itself https, or HTTPS is off (httpsport=0)
	CertFile    string   // the certificate the gateway serves: tlscert, or the default self-signed one
	Unreachable string   // non-empty when the gateway is bound off loopback: the member cannot relay, and says so, but the master can still read the certificate to enrol boxes
}

GatewayInfo is where this box's gateway listens, as seen from the box itself.

func ReadGatewayINI

func ReadGatewayINI(path, dataPath string) (GatewayInfo, error)

ReadGatewayINI reads [gateway] port, address, tlscert, tlskey and httpsport from path; dataPath is where the default certificate lives (/var/lib/casaos). It errors only on config a box cannot use at all (no port, a bad httpsport). A gateway bound off loopback, and HTTPS turned off with httpsport=0, are normal states, not errors: the member's relay reports the first, and the certificate is still read so the master can enrol boxes.

type MasterState

type MasterState struct {
	Enabled bool   `json:"enabled"`
	Address string `json:"address"`
	Boxes   []Box  `json:"boxes"`
}

MasterState is master.json.

type MasterStatus

type MasterStatus struct {
	Address string      `json:"address"`
	Boxes   []BoxStatus `json:"boxes"`
}

MasterStatus is the master half of GET /v1/fleet.

type MemberState

type MemberState struct {
	MasterURL        string `json:"master_url"`
	BoxID            string `json:"box_id"`
	Key              string `json:"key"`
	MasterCertSHA256 string `json:"master_cert_sha256"`
}

MemberState is member.json: the code the owner pasted, kept.

type MemberStatus

type MemberStatus struct {
	MasterURL string    `json:"master_url"`
	Connected bool      `json:"connected"`
	Since     time.Time `json:"since,omitzero"`
	LastError string    `json:"last_error,omitempty"`
	LastTry   time.Time `json:"last_try,omitzero"`
}

MemberStatus is the member half of GET /v1/fleet.

type Response

type Response struct {
	Status      int
	ContentType string
	Body        []byte
}

Response is what a box answered to a relayed request, passed on as it is.

type Status

type Status struct {
	Role   string        `json:"role"` // none | master | member
	Master *MasterStatus `json:"master,omitempty"`
	Member *MemberStatus `json:"member,omitempty"`
}

Status is GET /v1/fleet: the role, and the half that is on.

Jump to

Keyboard shortcuts

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