relayclient

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 19 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 relayclient implements the client side of an iroh relay connection.

A client dials a relay server over a secure WebSocket (standard WebPKI TLS, wire-compatible with iroh), negotiates the relay protocol version, completes the authentication handshake, and then exchanges relay frames (relayproto.ClientToRelayMsg / relayproto.RelayToClientMsg).

It is a port of the client side of iroh-relay/src/client.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrBadVersionHeader = errors.New("relayclient: relay returned an unsupported protocol version")
	ErrHandshake        = errors.New("relayclient: handshake failed")
)

Errors returned when connecting to a relay.

Functions

This section is empty.

Types

type Client

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

Client is a connected relay client. It is not safe for concurrent use by multiple senders or multiple receivers; use one goroutine for Send and one for Recv.

Example

Example shows the typical client lifecycle: connect to a relay, exchange a datagram with a peer, then close the connection.

ts := exampleRelay()
defer ts.Close()

u, _ := netaddr.ParseRelayURL(ts.URL)
sk, _ := key.GenerateSecretKey()

ctx := context.Background()
c, err := Connect(ctx, u, Options{SecretKey: sk})
if err != nil {
	fmt.Println("connect:", err)
	return
}
defer c.Close()

peer, _ := key.GenerateSecretKey()
if err := c.Send(ctx, relayproto.ClientToRelayMsg{
	Type:          relayproto.FrameClientToRelayDatagram,
	DstEndpointID: peer.Public().EndpointID(),
	Datagrams:     relayproto.DatagramsFromBytes([]byte("ping")),
}); err != nil {
	fmt.Println("send:", err)
	return
}

msg, err := c.Recv(ctx)
if err != nil {
	fmt.Println("recv:", err)
	return
}
fmt.Printf("version=%d echoed=%q\n", c.Version(), msg.Datagrams.Contents)
Output:
version=2 echoed="ping"

func Connect

func Connect(ctx context.Context, u netaddr.RelayURL, opts Options) (*Client, error)

Connect dials the relay at u and completes the protocol handshake.

func (*Client) Close

func (c *Client) Close() error

Close closes the relay connection.

func (*Client) Recv

Recv receives the next relay-to-client message.

func (*Client) Send

Send sends a client-to-relay message.

func (*Client) Version

func (c *Client) Version() relayproto.ProtocolVersion

Version returns the negotiated relay protocol version.

type Options

type Options struct {
	// SecretKey is the client's secret key, used for the authentication
	// handshake. Required.
	SecretKey key.SecretKey
	// TLSConfig overrides the TLS configuration used for the WSS connection.
	// If nil, the default WebPKI verification is used.
	TLSConfig *tls.Config
	// HTTPClient overrides the HTTP client used for the WebSocket dial.
	HTTPClient *http.Client
	// Proxy selects an HTTP proxy for the relay connection. Nil disables
	// proxying.
	Proxy func(*url.URL) (*url.URL, error)
	// AuthToken, if set, is sent as a Bearer token in the Authorization header.
	AuthToken string
}

Options configures a relay client dial.

Jump to

Keyboard shortcuts

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