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
- Variables
- func Advertised(s string) (string, error)
- func AppendData(b []byte, port uint16, payload []byte) []byte
- func ForHost(host string) string
- func Hello(mainPort, altPort uint16) []byte
- func IsBridge(a net.Addr) bool
- func ParseData(m []byte) (port uint16, payload []byte, err error)
- func ParseURL(s string) (*url.URL, error)
- type Addr
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) Connect(ctx context.Context) error
- func (c *Conn) Connected() bool
- func (c *Conn) Disconnect()
- func (c *Conn) Err() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) ReadFrom(b []byte) (int, net.Addr, error)
- func (c *Conn) ServerAddr() netip.AddrPort
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(time.Time) error
- func (c *Conn) URL() string
- func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error)
- type Options
- type StatusError
Constants ¶
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 ¶
var ErrMalformed = errors.New("wsdgram: malformed message")
ErrMalformed is returned for a message that is not one of ours.
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 ¶
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 ¶
AppendData appends a datagram message to b.
func ForHost ¶
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.
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.
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 (*Conn) Connect ¶
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) 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) ServerAddr ¶
ServerAddr is the rendezvous server's main address as reached through this Conn; valid once Connect has succeeded.
func (*Conn) SetDeadline ¶
SetDeadline sets the read deadline; writes never block for long.
func (*Conn) SetReadDeadline ¶
SetReadDeadline makes pending and future reads fail after t.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline is accepted and ignored: writes are bounded by an internal timeout.
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 ¶
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