wsproxy

package
v0.0.0-...-11e1b04 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Proxy

func Proxy(w http.ResponseWriter, r *http.Request, upstreamURL string, opts ProxyOptions)

Proxy accepts a client WebSocket upgrade, dials the upstream URL, and pumps messages bidirectionally until either side closes. ProxyOptions fields are optional and use defaults when omitted.

func Pump

func Pump(ctx context.Context, client, upstream Conn, onClose func(cause PumpExitCause), logger *slog.Logger, transform MessageTransform)

Pump bidirectionally copies messages between client and upstream until either side errors or ctx is cancelled, then calls onClose with the cause. If transform is non-nil it is called for every message; the returned bytes are forwarded to the other side.

Types

type Conn

type Conn interface {
	Read(ctx context.Context) (websocket.MessageType, []byte, error)
	Write(ctx context.Context, typ websocket.MessageType, p []byte) error
	Close(statusCode websocket.StatusCode, reason string) error
}

Conn abstracts a WebSocket connection for testing and flexibility.

type MessageTransform

type MessageTransform func(direction string, mt websocket.MessageType, msg []byte) []byte

MessageTransform is called for every message flowing through the proxy. direction is "->" for client-to-upstream and "<-" for upstream-to-client. It returns the (possibly modified) message bytes to forward.

type ProxyOptions

type ProxyOptions struct {
	AcceptOptions *websocket.AcceptOptions
	DialOptions   *websocket.DialOptions
	Logger        *slog.Logger
	Transform     MessageTransform
}

ProxyOptions configures the proxy accept/dial behavior and optional message transformation. Zero values are valid and use sensible defaults.

type PumpExitCause

type PumpExitCause string

PumpExitCause names which side caused Pump to return. Callers use this to distinguish a clean client close from an upstream failure or context cancellation when deciding telemetry attribution and reconnect policy.

const (
	// PumpExitClient indicates the client-side read or upstream-side write
	// returned an error first (typically: client closed the WS).
	PumpExitClient PumpExitCause = "client"
	// PumpExitUpstream indicates the upstream-side read or client-side write
	// returned an error first (typically: upstream died or restarted).
	PumpExitUpstream PumpExitCause = "upstream"
	// PumpExitContext indicates the pump's context was cancelled before
	// either side errored (typically: server shutdown).
	PumpExitContext PumpExitCause = "context"
)

Jump to

Keyboard shortcuts

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