client

package
v0.0.0-...-fe767e9 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 16 Imported by: 1 module, 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 reaches an agent through a relay, or directly when it can, over an end-to-end encrypted session.

The short version:

c, _ := client.New("https://relay.example.com/#k=…", nil)
resp, _ := c.HTTPClient().Get("http://agent/api/status")

Client picks the fastest path (direct candidates race the relay), redials when the connection drops, and upgrades from the relay to a direct path in the background when one becomes reachable.

Index

Constants

This section is empty.

Variables

View Source
var ErrAgentOffline = errors.New("client: agent is offline, or the code is wrong")

ErrAgentOffline is returned when the relay has no agent for this code. A wrong code looks the same as an offline agent: the relay only sees hashes.

Functions

func StreamConn

func StreamConn(st *mux.Stream, path string) net.Conn

StreamConn wraps a stream as a net.Conn (for SSH, database drivers, or anything else that wants a socket).

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client keeps one live encrypted session to an agent.

func New

func New(connString string, opts *Options) (*Client, error)

New parses a connection string and returns a Client. It does not dial until the first use.

func (*Client) Close

func (c *Client) Close() error

Close ends the session and stops background work.

func (*Client) Conn

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

Conn returns the live session, dialing (or redialing) if needed.

func (*Client) DialTCP

func (c *Client) DialTCP(ctx context.Context, target string) (net.Conn, error)

DialTCP opens a raw byte stream to one of the agent's named TCP targets ("" is the default target).

func (*Client) DialWebSocket

func (c *Client) DialWebSocket(ctx context.Context, path string, protocols ...string) (*wskind.Conn, string, error)

DialWebSocket opens a WebSocket to path on the agent.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns an http.Client whose requests travel over the session. The URL's scheme and host are ignored; only path and query are sent. Each request redials if the session is gone.

func (*Client) Open

func (c *Client) Open(ctx context.Context, kind string, meta any) (*mux.Stream, error)

Open opens a stream of any kind on the current session.

func (*Client) Path

func (c *Client) Path() string

Path reports the active path ("relay", "direct ws://…") or "" when idle.

func (*Client) Transport

func (c *Client) Transport() http.RoundTripper

Transport is the http.RoundTripper behind HTTPClient.

type Conn

type Conn struct {
	Session *mux.Session
	Path    string
	Hello   *e2e.AgentHello
}

Conn is one established session.

func (*Conn) Close

func (c *Conn) Close()

Close closes the session.

type Mode

type Mode int

Mode pins the path selection.

const (
	// Auto races direct candidates against the relay and prefers direct.
	Auto Mode = iota
	// RelayOnly never tries direct candidates.
	RelayOnly
	// DirectOnly never uses the relay.
	DirectOnly
)

type Options

type Options struct {
	Name   string   // sent to the agent inside the encrypted hello
	Mode   Mode     // path selection, Auto by default
	Direct []string // extra direct candidates (ws:// or wss:// URLs)
	// RelayDelay is how long direct candidates get a head start (default 250ms).
	RelayDelay time.Duration
	// DirectTimeout bounds one direct attempt (default 2s).
	DirectTimeout time.Duration
	// UpgradeInterval is how often a relayed client probes direct
	// candidates (default 30s; negative disables).
	UpgradeInterval time.Duration
	HTTPClient      *http.Client
	Logger          *slog.Logger
	// OnPath is called whenever the active path changes.
	OnPath func(path string)
	// Accept handles streams the agent opens toward this client. Optional.
	Accept func(ctx context.Context, st *mux.Stream)
}

Options configure a Client. All fields are optional.

Jump to

Keyboard shortcuts

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