wsdgram

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wsdgram carries UDP datagrams over a WebSocket, so that a peer in a network that lets only HTTPS out (CI runners, cloud containers, corporate proxies) can still reach a rendezvous server and its relay.

The client side is a net.PacketConn: the rendezvous and relay code, which is QUIC over a UDP socket, runs on it unchanged. End-to-end encryption, the PAKE and server pinning are exactly as on UDP; the WebSocket and whatever TLS terminates it in front of the server (a Cloudflare Tunnel, say) only ever see QUIC ciphertext.

The server end (see internal/signal) forwards each datagram to one of the rendezvous server's own UDP ports from a UDP socket of its own per WebSocket connection, and never anywhere else: it is not a UDP proxy.

Wire format

One binary WebSocket message per datagram:

type(1)  port(2, big endian)  payload

Type 0x01 is a datagram. From client to server, port is the server UDP port it is for; from server to client, the port it came from. The address part of every datagram is the server itself, so the client side presents the server under one placeholder address, ServerIP.

Type 0x02 is the server's hello, its first message: port is 0 and the payload holds the rendezvous port and the NAT-probe port (2 bytes each, 0 if there is none), so a client needs to know nothing but the URL.

The WebSocket subprotocol is Subprotocol. The client sends the Origin OriginValue; the server refuses any other origin, which is what keeps a web page from using visitors' browsers to talk to the bridge.

Index

Constants

View Source
const (
	// Subprotocol is the WebSocket subprotocol both ends require.
	Subprotocol = "remohop-dgram.1"
	// DefaultPath is the URL path the server serves by default.
	DefaultPath = "/remohop/v1/udp"
	// OriginValue is the Origin header remohop clients send. Browsers always
	// send the origin of the page, so a server that insists on this one
	// cannot be driven from a web page.
	OriginValue = "remohop:remohop"

	// MaxDatagram bounds a datagram's payload; QUIC packets are at most
	// about 1500 bytes and the relay drops anything beyond 2048.
	MaxDatagram = 2048
	// MaxMessage bounds a whole WebSocket message.
	MaxMessage = headerLen + MaxDatagram
)

Variables

View Source
var ErrMalformed = errors.New("wsdgram: malformed message")

ErrMalformed is returned for a message that is not one of ours.

View Source
var ServerIP = netip.AddrFrom4([4]byte{192, 0, 2, 1})

ServerIP is the placeholder address under which the client side presents the server: datagrams to any port at it go over the WebSocket, and datagrams from the server arrive from it. It is TEST-NET-1 (RFC 5737), which is never routed, so it cannot be mistaken for a real peer, and a peer that is told about it by an older server sends nothing anywhere.

Functions

func Advertised

func Advertised(s string) (string, error)

Advertised checks a bridge URL that a rendezvous server announces to its peers (or that its operator asks it to announce) and returns it in canonical form. A peer will connect to such a URL on the server's word, from inside whatever network it is in, so the word is given little room: TLS only, no query, and nothing but DefaultPath, so that the request it leads to is the same harmless one whichever host it names. A bridge on another path has to be configured on the peers by hand.

func AppendData

func AppendData(b []byte, port uint16, payload []byte) []byte

AppendData appends a datagram message to b.

func ForHost

func ForHost(host string) string

ForHost is where the bridge of the rendezvous server named host is by convention: on that same host, TLS on port 443, at DefaultPath. It is "" for an IP address, which has no certificate to speak of.

func Hello

func Hello(mainPort, altPort uint16) []byte

Hello encodes the server's hello.

func IsBridge

func IsBridge(a net.Addr) bool

IsBridge reports whether a is the local address of a Conn.

func ParseData

func ParseData(m []byte) (port uint16, payload []byte, err error)

ParseData splits a datagram message. Messages of any other type, and oversized ones, are ErrMalformed.

func ParseURL

func ParseURL(s string) (*url.URL, error)

ParseURL checks a server endpoint URL, adding DefaultPath when it has no path. It refuses user information in the URL, which would end up in logs.

Types

type Addr

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

Addr is the local address of a Conn: the endpoint, not a UDP address, so code that lists a socket's own UDP addresses finds none.

func (Addr) Network

func (a Addr) Network() string

Network is "websocket".

func (Addr) String

func (a Addr) String() string

type Conn

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

Conn is a net.PacketConn whose only peer is the server behind a WebSocket endpoint, addressed as ServerIP and the server's UDP port.

The WebSocket is opened on first use (or by Connect) and opened again after it drops; datagrams written while it is down are queued briefly or dropped, as a UDP socket would lose them. Reads block until a datagram arrives, the read deadline passes or the Conn is closed.

func New

func New(opts Options) (*Conn, error)

New prepares a Conn; it does not connect yet.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the WebSocket and ends pending reads.

func (*Conn) Connect

func (c *Conn) Connect(ctx context.Context) error

Connect opens the WebSocket now, if it is not open, and waits for the server's hello. It reports why connecting failed, which datagram writes cannot.

func (*Conn) Connected

func (c *Conn) Connected() bool

Connected reports whether the WebSocket is open right now.

func (*Conn) Disconnect

func (c *Conn) Disconnect()

Disconnect closes the WebSocket if it is open, and makes a connection attempt under way come to nothing. The Conn stays usable: the next Connect, or the next datagram written, opens it again. It is for a caller that connected the bridge in case it would be needed and then did not need it.

func (*Conn) Err

func (c *Conn) Err() error

Err is why the last attempt to open the WebSocket failed, or nil.

func (*Conn) LocalAddr

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

LocalAddr is an Addr naming the endpoint.

func (*Conn) ReadFrom

func (c *Conn) ReadFrom(b []byte) (int, net.Addr, error)

ReadFrom returns the next datagram from the server.

func (*Conn) ServerAddr

func (c *Conn) ServerAddr() netip.AddrPort

ServerAddr is the rendezvous server's main address as reached through this Conn; valid once Connect has succeeded.

func (*Conn) SetDeadline

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

SetDeadline sets the read deadline; writes never block for long.

func (*Conn) SetReadDeadline

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

SetReadDeadline makes pending and future reads fail after t.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(time.Time) error

SetWriteDeadline is accepted and ignored: writes are bounded by an internal timeout.

func (*Conn) URL

func (c *Conn) URL() string

URL is the endpoint, without query parameters.

func (*Conn) WriteTo

func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error)

WriteTo sends a datagram to the server port named by addr, which must be at ServerIP. Like UDP it does not report loss: while the WebSocket is being (re)opened a few datagrams are queued and the rest dropped.

type Options

type Options struct {
	// URL is the server's endpoint, ws:// or wss://. A URL without a path
	// gets DefaultPath.
	URL string
	// Header holds extra request headers, such as Cloudflare Access
	// service-token headers. They are sent to the server (and through a
	// proxy only inside the TLS tunnel for wss://), never logged.
	Header http.Header
	// TLSConfig, if set, is used for wss:// (tests pass their own roots).
	TLSConfig *tls.Config
	// Proxy picks the HTTP proxy for a request URL (https:// for wss://,
	// http:// for ws://); nil means the environment: HTTPS_PROXY,
	// HTTP_PROXY and NO_PROXY, in either case. A proxy is used with
	// CONNECT.
	Proxy func(*url.URL) (*url.URL, error)
	// DialContext opens TCP connections; nil means a net.Dialer.
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
	// HandshakeTimeout bounds connecting, proxy, TLS and WebSocket
	// handshakes included. Default 10 s.
	HandshakeTimeout time.Duration
	// Logger receives connection events at debug level; nil discards them.
	Logger *slog.Logger
}

Options configures a client Conn.

type StatusError

type StatusError struct {
	Code int
	Line string
}

StatusError is a refusal of the WebSocket handshake by the server or something in front of it.

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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