tunnel

package
v0.34.10 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StreamTypeHTTP     byte = 0x01 // HTTP proxy request (server → agent)
	StreamTypeTerminal byte = 0x02 // Terminal bidirectional stream (server → agent)
	StreamTypeControl  byte = 0x03 // Control message: agent info, etc. (agent → server)
)

Stream types identify the purpose of each yamux stream.

Variables

This section is empty.

Functions

func ClientMux added in v0.27.0

func ClientMux(conn net.Conn) (*yamux.Session, error)

ClientMux creates a yamux client session over conn. The local agent acts as the yamux client: it opens streams towards the server (control messages) and accepts streams from the server (HTTP proxy, terminal).

func MarshalStreamMeta added in v0.27.0

func MarshalStreamMeta(v interface{}) ([]byte, error)

MarshalStreamMeta marshals metadata to JSON for WriteStreamHeader.

func MuxConfig added in v0.27.0

func MuxConfig() *yamux.Config

MuxConfig returns the yamux configuration for the tunnel.

func ReadStreamHeader added in v0.27.0

func ReadStreamHeader(r io.Reader) (streamType byte, metadata []byte, err error)

ReadStreamHeader reads the stream header and returns the type and metadata.

func ServerMux added in v0.27.0

func ServerMux(conn net.Conn) (*yamux.Session, error)

ServerMux creates a yamux server session over conn. The agentserver side acts as the yamux server: it accepts streams opened by the agent (control messages) and opens streams towards the agent (HTTP proxy, terminal).

func UnmarshalStreamMeta added in v0.27.0

func UnmarshalStreamMeta(data []byte, v interface{}) error

UnmarshalStreamMeta unmarshals metadata JSON from ReadStreamHeader.

func WriteStreamHeader added in v0.27.0

func WriteStreamHeader(w io.Writer, streamType byte, metadata []byte) error

WriteStreamHeader writes the stream header: [1 byte type][4 bytes metadata len][metadata].

Types

type HTTPResponseMeta added in v0.27.0

type HTTPResponseMeta struct {
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers"`
}

HTTPResponseMeta is the response header written by the agent on an HTTP stream.

type HTTPStreamMeta added in v0.27.0

type HTTPStreamMeta struct {
	Method  string            `json:"method"`
	Path    string            `json:"path"`
	Headers map[string]string `json:"headers"`
	BodyLen int               `json:"body_len"`
}

HTTPStreamMeta is the metadata for an HTTP proxy stream (server → agent). BodyLen indicates the number of request body bytes that follow the stream header.

type Registry

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

Registry tracks active WebSocket tunnels keyed by sandbox ID.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new tunnel registry.

func (*Registry) Get

func (r *Registry) Get(sandboxID string) (*Tunnel, bool)

Get returns the active tunnel for a sandbox.

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, sandboxID string, ws *websocket.Conn) *Tunnel

Register accepts a WebSocket connection, wraps it in WSConn + yamux, and registers the resulting Tunnel for the given sandbox.

func (*Registry) Unregister

func (r *Registry) Unregister(sandboxID string, t *Tunnel) bool

Unregister removes the tunnel only if it matches the provided instance. Returns true if the tunnel was actually removed.

type Tunnel

type Tunnel struct {
	SandboxID string

	// OnAgentInfo is called when the agent sends a control message with agent info.
	OnAgentInfo func(data json.RawMessage)
	// contains filtered or unexported fields
}

Tunnel represents an active multiplexed tunnel to a local agent. It wraps a WebSocket connection with yamux for stream multiplexing.

func (*Tunnel) Close

func (t *Tunnel) Close()

Close shuts down the tunnel and underlying connections.

func (*Tunnel) Done

func (t *Tunnel) Done() <-chan struct{}

Done returns a channel that is closed when the tunnel shuts down.

func (*Tunnel) OpenHTTPStream added in v0.27.0

func (t *Tunnel) OpenHTTPStream(ctx context.Context, meta HTTPStreamMeta, reqBody []byte) (HTTPResponseMeta, io.ReadCloser, error)

OpenHTTPStream opens a new yamux stream for proxying an HTTP request. The caller must close the returned body reader when done.

Protocol:

  1. Server writes: stream header (StreamTypeHTTP + HTTPStreamMeta with BodyLen)
  2. Server writes: request body bytes (exactly BodyLen bytes)
  3. Agent reads BodyLen bytes, processes request, then writes response.
  4. Agent writes: stream header (StreamTypeHTTP + HTTPResponseMeta)
  5. Agent writes: response body until stream close.

func (*Tunnel) OpenTerminalStream added in v0.27.0

func (t *Tunnel) OpenTerminalStream() (net.Conn, error)

OpenTerminalStream opens a new yamux stream for bidirectional terminal I/O. The returned net.Conn carries raw terminal data in both directions.

type WSConn added in v0.27.0

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

WSConn wraps a WebSocket connection as a net.Conn interface. Inspired by xray-core transport/internet/websocket/connection.go.

Write sends each call as a single BinaryMessage. Read transparently iterates over WebSocket messages, caching the current message reader between calls (xray-core reader-caching pattern).

func NewWSConn added in v0.27.0

func NewWSConn(ctx context.Context, ws *websocket.Conn) *WSConn

NewWSConn wraps a websocket.Conn into a net.Conn.

func (*WSConn) Close added in v0.27.0

func (c *WSConn) Close() error

Close sends a WebSocket close frame and releases resources.

func (*WSConn) LocalAddr added in v0.27.0

func (c *WSConn) LocalAddr() net.Addr

func (*WSConn) Read added in v0.27.0

func (c *WSConn) Read(b []byte) (int, error)

Read reads from the WebSocket message stream. Messages are transparently concatenated: when one message ends (EOF), the next message is fetched automatically.

func (*WSConn) RemoteAddr added in v0.27.0

func (c *WSConn) RemoteAddr() net.Addr

func (*WSConn) SetDeadline added in v0.27.0

func (c *WSConn) SetDeadline(t time.Time) error

func (*WSConn) SetReadDeadline added in v0.27.0

func (c *WSConn) SetReadDeadline(t time.Time) error

func (*WSConn) SetWriteDeadline added in v0.27.0

func (c *WSConn) SetWriteDeadline(t time.Time) error

func (*WSConn) Write added in v0.27.0

func (c *WSConn) Write(b []byte) (int, error)

Write sends b as a single WebSocket BinaryMessage.

Jump to

Keyboard shortcuts

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