wsproxy

package
v0.0.0-...-6026c52 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 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(), logger *slog.Logger, transform MessageTransform)

Pump bidirectionally copies messages between client and upstream until either side errors or ctx is cancelled, then calls onClose. 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.

Jump to

Keyboard shortcuts

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