tailscale.comtailscale.com/net/wsconn Index | Files

package wsconn

import "tailscale.com/net/wsconn"

Package wsconn contains an adapter type that turns a websocket connection into a net.Conn. It a temporary fork of the netconn.go file from the github.com/coder/websocket package while we wait for https://github.com/nhooyr/websocket/pull/350 to be merged.

Index

Functions

func NetConn

func NetConn(ctx context.Context, c *websocket.Conn, msgType websocket.MessageType, remoteAddr string) net.Conn

NetConn converts a *websocket.Conn into a net.Conn.

It's for tunneling arbitrary protocols over WebSockets. Few users of the library will need this but it's tricky to implement correctly and so provided in the library. See https://github.com/nhooyr/websocket/issues/100.

Every Write to the net.Conn will correspond to a message write of the given type on *websocket.Conn.

The passed ctx bounds the lifetime of the net.Conn. If cancelled, all reads and writes on the net.Conn will be cancelled.

If a message is read that is not of the correct type, the connection will be closed with StatusUnsupportedData and an error will be returned.

Close will close the *websocket.Conn with StatusNormalClosure.

When a deadline is hit, the connection will be closed. This is different from most net.Conn implementations where only the reading/writing goroutines are interrupted but the connection is kept alive.

The Addr methods will return a mock net.Addr that returns "websocket" for Network and "websocket/unknown-addr" for String.

A received StatusNormalClosure or StatusGoingAway close frame will be translated to io.EOF when reading.

The given remoteAddr will be the value of the returned conn's RemoteAddr().String(). For best compatibility with consumers of conns, the string should be an ip:port if available, but in the absence of that it can be any string that describes the remote endpoint, or the empty string to makes RemoteAddr() return a place holder value.

Source Files

wsconn.go

Version
v1.84.0 (latest)
Published
May 21, 2025
Platform
linux/amd64
Imports
10 packages
Last checked
1 day ago

Tools for package owners.