Help us shape the future of Go! Take the 2026 Go Developer Survey

relay

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package relay forwards already-encrypted packets between nodes that cannot reach each other directly. Nodes hold a WebSocket to the coordinator and address each other by WireGuard public key; the relay never sees plaintext.

Frames are binary WebSocket messages:

client -> server: 0x01 | destination key (32) | payload
server -> client: 0x02 | source key (32)      | payload

Index

Constants

View Source
const (

	// MaxPayload bounds a single packet; WireGuard packets are far smaller.
	MaxPayload = 64 << 10
)
View Source
const Path = "/relay/v1"

Path is where the relay is mounted on the coordinator.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator func(ctx context.Context, token string) (wgkey.Key, error)

Authenticator maps a bearer token to the WireGuard key of the node it belongs to.

type Client

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

Client keeps a connection to the relay open, reconnecting as needed.

func NewClient

func NewClient(baseURL, token string, log *slog.Logger) (*Client, error)

NewClient returns a client for the relay on the coordinator at baseURL. Received packets are delivered on Recv().

func (*Client) Connected

func (c *Client) Connected() bool

Connected reports whether the client currently holds a connection.

func (*Client) Recv

func (c *Client) Recv() <-chan Packet

Recv returns the channel received packets are delivered on.

func (*Client) Run

func (c *Client) Run(ctx context.Context)

Run maintains the connection until ctx is cancelled.

func (*Client) Send

func (c *Client) Send(ctx context.Context, dst wgkey.Key, payload []byte) error

Send forwards payload to the node with key dst. It fails fast when not connected rather than queueing: callers resend (WireGuard retransmits).

type Packet

type Packet struct {
	From    wgkey.Key
	Payload []byte
}

Packet is a payload received from another node through the relay.

type Server

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

Server is the relay endpoint.

func NewServer

func NewServer(auth Authenticator, bytesPerSec, burst int, log *slog.Logger) *Server

NewServer returns a relay that lets each node send bytesPerSec on average (with bursts up to burst bytes). Zero bytesPerSec means unlimited.

func (*Server) Connected

func (s *Server) Connected(key wgkey.Key) bool

Connected reports whether a node with key holds a relay connection.

func (*Server) Connections

func (s *Server) Connections() int

Connections returns the number of connected nodes.

func (*Server) Prune

func (s *Server) Prune(keep func(wgkey.Key) bool)

Prune disconnects every node for which keep returns false, e.g. nodes that were deleted: their token no longer works, but an open connection would otherwise last until it next reconnects.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades the request to a relay connection.

func (*Server) Stats

func (s *Server) Stats() *Stats

Stats returns the relay's counters.

type Stats

type Stats struct {
	Bytes            atomic.Uint64 // payload bytes forwarded
	DroppedRateLimit atomic.Uint64
	DroppedNoPeer    atomic.Uint64 // destination not connected
	DroppedSlowPeer  atomic.Uint64 // destination's queue full
}

Stats counts relay traffic, for the coordinator's metrics.

Jump to

Keyboard shortcuts

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