Documentation
¶
Overview ¶
Package wire is the preview tunnel's protocol, shared by the gateway (cmd/muster-preview), the host (muster-mirror -preview) and, in phase 2, an agent pod's gate, so the three cannot disagree about it. See docs/design/previews.md ("The host tunnel").
🔴 IT IMPORTS ONLY THE STANDARD LIBRARY. The pod's supervisor (cmd/ccd) will link it, and cmd/ccd must not link a websocket client (internal/modulegate/leafdeps_test.go pins both). The transport — the WebSocket carrying HTTP/2 — lives in the sibling package internal/preview/tunnel; nothing here may import it.
The shape, in one place:
- The host dials the gateway's HostPath with VersionHeader, HostNameHeader and "Authorization: Bearer mpt_…". A version the gateway does not speak is refused with VersionRangeHeader naming the range it does.
- Over that WebSocket the host is the HTTP/2 SERVER and the gateway the client. The gateway's first stream is GET OfferPath: a long-lived response on which the host writes newline-delimited Offer messages (offer.go). The gateway answers the host's requests with POST OpenedPath and POST ClosedPath.
- Each upstream TCP connection is one CONNECT stream whose authority is 127.0.0.1:<port>. The host uses ONLY the port (ConnectPort), and either answers 200 and copies bytes or refuses with a Reason (WriteRefusal).
Index ¶
- Constants
- Variables
- func AcceptVersion(h string) (int, bool)
- func ConnectAuthority(port int) string
- func ConnectPort(r *http.Request) (int, error)
- func VersionRange() string
- func WriteRefusal(w http.ResponseWriter, r Reason)
- type Answer
- type Config
- type Decoder
- type Encoder
- type Listener
- type Offer
- type Reason
- type Request
Constants ¶
const ( TypeConfig = "config" // the host's allowlist and caps (Config); first, and again on change TypeWindows = "windows" // the tmux window ids the host mirrors (Windows), on change TypeListeners = "listeners" // the full set of offered listeners (Listeners), on change TypeOpen = "open" // an agent's preview_open (Request) TypeClose = "close" // an agent's preview_close (Request) )
Offer message types.
const ( Version = 1 MinVersion = 1 MaxVersion = 1 )
Version is the preview protocol version this side speaks, and [MinVersion, MaxVersion] the range it accepts. One version exists.
const ( // VersionHeader carries the version the host speaks on its tunnel dial. VersionHeader = "X-Muster-Preview-Version" // VersionRangeHeader is set on a refusal of an unspoken version: // "<min>-<max>" (VersionRange). Its presence is what tells a host the // refusal was about the version, not its token. VersionRangeHeader = "X-Muster-Preview-Version-Range" // HostNameHeader names the host on its tunnel dial; the gateway binds it // to the host token's registered name. HostNameHeader = "X-Muster-Preview-Host" // GatewayBootHeader is on the gateway's GET OfferPath: an id that changes // each time the gateway process starts. A host that sees a new one // re-announces the opens the previous gateway had acknowledged (that // gateway's previews were in memory and are gone). GatewayBootHeader = "X-Muster-Preview-Gateway-Boot" // ReasonHeader carries a CONNECT refusal's Reason. ReasonHeader = "X-Muster-Preview-Reason" // TokenPrefix begins every host token (stored hashed by the gateway). TokenPrefix = "mpt_" HostPath = "/v1/host" // the host's tunnel WebSocket, on the gateway OfferPath = "/v1/offer" // gateway → host, GET, long-lived NDJSON response OpenedPath = "/v1/opened" // gateway → host, POST, an Answer ClosedPath = "/v1/closed" // gateway → host, POST, an Answer )
Header names and paths of the protocol.
const MaxConns = 64
MaxConns is the cap on a host's maxConns, and its default: the number of UPSTREAM connections (CONNECT streams) a host's tunnel carries at once. The control streams are on top of it (internal/preview/tunnel.ControlStreams). Phase 1 caps it at 64, below the design's 256, so the gateway's connection window — sized from this cap in internal/preview/tunnel — stays (64+4+1) × 4 MiB. Raising it raises that window and the gateway memory paused readers can hold; the invariant test in internal/preview/tunnel pins the numbers together.
const MaxLine = 1 << 20
MaxLine bounds one offer-stream line, in bytes.
const PingInterval = 30 * time.Second
PingInterval is how often each end of a tunnel sends a WebSocket ping. It must stay well under the shortest idle timeout on any path a tunnel takes (the public edge and the overlay mesh's relays both cut quiet connections).
Variables ¶
var ErrLineTooLong = errors.New("wire: offer line over MaxLine")
ErrLineTooLong is Decoder.Next's error for a line over MaxLine.
var ErrNotConnect = errors.New("wire: not a CONNECT request")
ErrNotConnect is ConnectPort's error for a request that is not a CONNECT.
Functions ¶
func AcceptVersion ¶
AcceptVersion reports whether a VersionHeader value names a version this side speaks, and which. Only a plain decimal integer is a version.
func ConnectAuthority ¶
ConnectAuthority is the authority the gateway sends for port.
func ConnectPort ¶
ConnectPort is the port a CONNECT asks for. 🔴 IT RETURNS ONLY THE PORT: the authority's host part is ignored, never resolved and never dialed, because the host dials loopback and nothing else whatever the gateway sends. The port must be plain decimal, 1–65535.
func VersionRange ¶
func VersionRange() string
VersionRange is the value of VersionRangeHeader for this side.
func WriteRefusal ¶
func WriteRefusal(w http.ResponseWriter, r Reason)
WriteRefusal answers a CONNECT with r's status and ReasonHeader. It panics on a value that is not a Reason: a refusal with no status is a bug in the caller, not something to send.
Types ¶
type Answer ¶
type Answer struct {
RequestID string `json:"request_id,omitempty"`
Preview string `json:"preview,omitempty"`
Window string `json:"window,omitempty"`
Port int `json:"port,omitempty"`
Error string `json:"error,omitempty"` // a tool error code, e.g. "max_open"; empty on success
}
Answer is the body of POST OpenedPath and POST ClosedPath. RequestID is empty for an open the owner made by clicking an offer (the gateway tells the host unsolicited, so the agent's preview_list includes it).
type Config ¶
type Config struct {
Ports []string `json:"ports"`
Offer bool `json:"offer"`
MaxOpen int `json:"maxOpen"`
MaxConns int `json:"maxConns"`
Disabled string `json:"disabled,omitempty"`
}
Config is the host's previews.json as the gateway may know it. Disabled is non-empty, and says why, when previews are off (a missing, empty or invalid file); the other fields are then zero.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads offer lines.
func NewDecoder ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes offer lines. It does not flush: an http.ResponseWriter caller flushes after each Encode so the gateway sees the line at once.
func NewEncoder ¶
type Listener ¶
type Listener struct {
Port int `json:"port"`
Process string `json:"process"`
Window string `json:"window"`
FirstSeen string `json:"first_seen"`
}
Listener is one offered loopback listener. 🔴 THESE FOUR KEYS AND NO OTHER: not the argv, the working directory, the environment or the pid (the design's privacy rule; a ledger test pins the key set). Process is the listener's comm, at most 15 bytes; Window is a tmux window id ("@3"); FirstSeen is RFC 3339.
type Offer ¶
type Offer struct {
Type string `json:"type"`
Config *Config `json:"config,omitempty"`
Windows []string `json:"windows,omitempty"`
Listeners []Listener `json:"listeners,omitempty"`
Request *Request `json:"request,omitempty"`
}
Offer is one line of the offer stream. Exactly the field its Type names is set; Windows and Listeners are the WHOLE current set (an empty one clears).
type Reason ¶
type Reason string
Reason is why a host refused a CONNECT. Each has exactly one status.
const ( ReasonPortNotAllowed Reason = "port_not_allowed" // 403: the port is not in previews.json ReasonForeignListener Reason = "foreign_listener" // 403: the listener is another user's ReasonRefused Reason = "refused" // 502: nothing is listening (both loopbacks) // ReasonMaxConns (429): all of the host's maxConns upstream slots stayed // in use. The GATEWAY emits it, when a Dial has waited its bound for a // free slot (internal/preview/tunnel, ClientOptions.DialWait); the host // sends it only as a backstop, for a CONNECT that arrives past its // maxConns anyway, and the gateway retries that within the same wait. ReasonMaxConns Reason = "max_conns" )
type Request ¶
type Request struct {
ID string `json:"id"`
Window string `json:"window"`
Session string `json:"session,omitempty"`
Port int `json:"port,omitempty"` // open
Label string `json:"label,omitempty"` // open, at most 60 bytes
Path string `json:"path,omitempty"` // open, starts with "/"
// Preview names the preview a close ends. On an open it is ADVISORY and
// unused: a gateway mints a new id for every open it records, a
// re-announced one included (ids are the gateway's to mint, never the
// host's to choose), so a host must take the id from the Answer and
// accept that it changed.
Preview string `json:"preview,omitempty"`
}
Request is an agent tool's open or close, carried to the gateway. ID is the host's request id, echoed in the gateway's Answer. Session is display-only; the gateway matches a preview on (owner, host, Window).