client

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 26 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 client talks to a running daemon and starts one when needed.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotRunning = errors.New("daemon is not running")

ErrNotRunning means no daemon answered and none was started.

View Source
var ErrSandboxed = errors.New("not starting the daemon from inside a sandbox")

ErrSandboxed means no daemon answered and none was started because the caller runs in an agent's sandbox: a daemon started there would inherit the sandbox's file and network limits, and every server it starts too.

View Source
var ErrUnverified = errors.New("cannot prove it is this account's daemon")

ErrUnverified means that what answers on the daemon's port could not prove it holds this account's token, so nothing signed is sent to it: another program holds the port, or a daemon from before verified requests.

Functions

func DaemonRunning

func DaemonRunning() bool

DaemonRunning reports whether a daemon holds this state directory's lock. The test takes a shared lock for a moment; a daemon starting meanwhile retries its own.

func DashboardURL

func DashboardURL(port int) string

DashboardURL returns the dashboard's address for the daemon on port; a browser signs in there through `open`.

func LivePort

func LivePort() int

LivePort returns the port recorded by a running daemon, or 0.

func Sandboxed

func Sandboxed() bool

Sandboxed reports whether this process appears to run in an agent's sandbox.

Types

type APIError

type APIError struct {
	Status int
	Msg    string
	Code   string
	Body   json.RawMessage
}

APIError is an error response from the daemon.

func (*APIError) Error

func (e *APIError) Error() string

type Client

type Client struct {
	Port int
	Base string
	// contains filtered or unexported fields
}

Client is an HTTP client bound to the daemon's port.

func New

func New(port int) *Client

New returns a client for the daemon on port. It dials IPv4 loopback by address, never a name that could resolve to another listener.

func NewAt

func NewAt(host string, port int) *Client

NewAt returns a client for the daemon on port at the loopback address host, such as "::1" for a check of the daemon's IPv6 listener.

func (*Client) ContainerAction

func (c *Client) ContainerAction(ctx context.Context, name, action string, build, pull bool) (docker.JobResult, error)

ContainerAction uses the daemon's engine and compose context. Recreate waits until its job ends and returns a bounded output tail, including on failure.

func (*Client) DialWebSocket

func (c *Client) DialWebSocket(ctx context.Context, p string) (*websocket.Conn, error)

DialWebSocket opens a signed WebSocket to an API path, routed as Path does.

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, path string, body, out any) error

Do sends a signed request with an optional JSON body and decodes a JSON response into out. The daemon must have proved itself first, and its reply must carry a proof as well; a request refused for an old epoch is sent again once after a new handshake.

func (*Client) Ensure

func (c *Client) Ensure(ctx context.Context) (Health, error)

Ensure makes sure a daemon answers and proves itself, starting one in the background if none answers. When what answers cannot prove itself, it returns its claimed identity and an error wrapping ErrUnverified. When another program, or another account's daemon, holds the port and no daemon of this account runs, it starts one anyway: a daemon without a configured port then listens on another port, which the client follows. It starts none while this product of this account holds the port (a daemon with another state directory), and says which one.

func (*Client) On

func (c *Client) On(ctx context.Context, name string) (*Client, *Connection, error)

On returns a client whose API requests go to the connected computer called name (its name, DNS label or DNS name), relayed by this daemon, and the connection. It fails when this daemon is not connected to that computer.

func (*Client) Path

func (c *Client) Path(p string) string

Path returns where the daemon serves an API path: the path itself, or the same path on the connected computer for a client made by On.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) (Health, error)

Ping returns the identity of this computer's daemon (also for a client made by On), or an error if none answers. It repeats the handshake, so Verified tells whether what answers proved itself.

func (*Client) Running added in v0.3.0

func (c *Client) Running(ctx context.Context) (Health, error)

Running makes sure a daemon that already runs answers and proves itself, and never starts one. It follows the port a running daemon recorded. It returns an error wrapping ErrNotRunning when nothing listens.

func (*Client) Token

func (c *Client) Token() string

Token returns the daemon's API token, or "" if the daemon never ran.

func (*Client) Unverified

func (c *Client) Unverified() error

Unverified is the error for a daemon that did not prove itself.

func (*Client) WebSocketURL

func (c *Client) WebSocketURL(p string) string

WebSocketURL returns the ws:// URL of an API path, routed as Path does.

type Connection

type Connection struct {
	Peer struct {
		Name    string `json:"name"`
		Label   string `json:"label"`
		DNSName string `json:"dnsName"`
	} `json:"peer"`
	Status    string `json:"status"`
	Error     string `json:"error,omitempty"`
	LocalPort int    `json:"localPort"`
	Mirrors   []struct {
		Port  int    `json:"port"`
		Local int    `json:"local"`
		State string `json:"state"`
	} `json:"mirrors"`
}

Connection is this daemon's connection to another computer, as GET /api/remote reports it.

func (*Connection) Is

func (cn *Connection) Is(name string) bool

Is reports whether name is the connected computer's name, DNS label or DNS name.

func (*Connection) LocalURL

func (cn *Connection) LocalURL(server, raw string) string

LocalURL returns how this computer opens a URL of the connected computer's server: at the local port its port is forwarded to, else through this daemon's proxy at <server>.<computer>.localhost.

The other computer supplies the URL, so only its scheme (http or https), port, path and query are used; the host is always this computer's, and a server name that is not a valid host label gets no URL.

type Health

type Health struct {
	App       string    `json:"app"`
	Version   string    `json:"version"`
	PID       int       `json:"pid"`
	Port      int       `json:"port"`
	StartedAt time.Time `json:"startedAt"`
	// Verified is true when it proved that it holds this account's token;
	// only then does the client send it signed requests.
	Verified bool `json:"-"`
}

Health is the daemon's identity.

type Holder

type Holder struct {
	PID     int
	Command string
	// Own is true when lsof or ss name the listening process, it belongs to
	// this account and its command is this product's binary. A pid claimed
	// only by what answers on the port, or a process of this account that
	// forwards the port (such as an ssh tunnel to another computer's
	// daemon), is not taken for this product.
	Own bool
	// StateDir is the state directory of such a daemon, when its lock file
	// can be seen among its open files.
	StateDir string
}

Holder is what listens on a port that this account's daemon wants.

func PortHolder

func PortHolder(ctx context.Context, port int) Holder

PortHolder finds out what listens on port on loopback. Only lsof or ss are trusted to name the process (another account's are usually invisible to them), and its command name to tell whether it is this product; what answers on the port cannot prove either.

func (Holder) Ours

func (h Holder) Ours() bool

Ours reports whether the holder is this product running for this account, such as a daemon with another state directory. Its port is never taken over: moving away would leave two daemons serving one config.

func (Holder) Refusal

func (h Holder) Refusal(port int) string

Refusal explains why a daemon does not start on port while h holds it.

Jump to

Keyboard shortcuts

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