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 ¶
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 (*Client) Recv ¶
func (c *Client) Recv(ctx context.Context) (relayproto.RelayToClientMsg, error)
Recv receives the next relay-to-client message.
func (*Client) Send ¶
func (c *Client) Send(ctx context.Context, msg relayproto.ClientToRelayMsg) error
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
// AuthToken, if set, is sent as a Bearer token in the Authorization header.
AuthToken string
}
Options configures a relay client dial.