Documentation
¶
Overview ¶
Package wstunnel adapts a single WebSocket into a multiplexed byte stream (yamux over WebSocket), so one outbound, NAT-friendly connection can carry many independent streams. It is the shared wire protocol for the Miabi control plane and the agents/runners that dial into it: the control plane OPENS streams (Client), the agent/runner ACCEPTS them (Server). Both ends MUST use this package so the yamux framing and keepalive settings match exactly — a mismatch silently breaks the tunnel.
Index ¶
- Constants
- func Client(ws *websocket.Conn) (*yamux.Session, error)
- func Config() *yamux.Config
- func Dial(ctx context.Context, opts ClientOptions) (*yamux.Session, error)
- func NewConn(ws *websocket.Conn) net.Conn
- func Serve(ctx context.Context, opts ClientOptions, ...) error
- func Server(ws *websocket.Conn) (*yamux.Session, error)
- func URL(base, path string) string
- type ClientOptions
Constants ¶
const ( KeepAliveInterval = 20 * time.Second ConnectionWriteTimeout = 15 * time.Second )
Keepalive/timeout defaults for the yamux session. Exported so callers can build a custom Config from the same baseline if needed.
Variables ¶
This section is empty.
Functions ¶
func Client ¶
Client opens the stream-opening side of the session over ws (the control plane): it OPENS streams (e.g. one per Docker request or job lease).
func Config ¶
Config returns the shared yamux config: keepalive enabled and logging muted. Both ends use the same values, so the framing and liveness detection agree.
func Dial ¶
Dial opens one WebSocket to opts.URL and returns the accepting (Server-side) yamux session. The caller owns closing the session (which closes the ws).
func Serve ¶
func Serve(ctx context.Context, opts ClientOptions, handle func(ctx context.Context, sess *yamux.Session) error) error
Serve runs the accepting side until ctx is cancelled, reconnecting with exponential backoff. For each live session it calls handle(ctx, sess), which should block for the session's lifetime (e.g. accepting and servicing streams) and return when it ends. A clean session (handle returns nil) resets the backoff. handle owns the session; Serve closes it before retrying.
func Server ¶
Server opens the stream-accepting side of the session over ws (an agent or runner): it ACCEPTS streams opened by the control plane.
func URL ¶
URL converts an http(s) control-plane base URL and a connect path into the ws(s) endpoint a client dials. It normalizes the scheme (http→ws, https→wss) and joins the path with exactly one slash, so callers pass their own connect path (e.g. "/api/v1/agent/connect" or "/api/v1/runner/connect").
Types ¶
type ClientOptions ¶
type ClientOptions struct {
// URL is the fully-resolved ws(s) endpoint (see URL()).
URL string
// Header carries auth and metadata on the WebSocket handshake (e.g.
// "Authorization: Bearer <token>", "X-Runner-Version").
Header http.Header
// Dialer overrides the WebSocket dialer (nil uses a copy of the default).
Dialer *websocket.Dialer
// Insecure skips TLS verification (dev only). Ignored when Dialer is set.
Insecure bool
// MinBackoff / MaxBackoff bound the reconnect delay (defaults 1s / 30s).
MinBackoff, MaxBackoff time.Duration
// OnConnect fires after each successful session is established; OnError fires
// with the cause each time a session ends or a dial fails. Both are optional
// (nil = no-op), keeping this package free of any logging dependency.
OnConnect func()
OnError func(error)
}
ClientOptions configures the accepting (agent/runner) side of the tunnel.