relay

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 21 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 relay is how a phone reaches conch web without Tailscale: both connect out to a relay, which pairs a phone's WebSocket with one the gateway opens for it and copies messages between the two. What it copies is a Noise session the phone and the gateway made with a key the relay never sees (the gateway's, carried in the pairing QR code), so it forwards screens and keystrokes it cannot read and could not forge.

That holds for the bytes it copies, not for the page: the relay also serves the phone's app, the code that makes the session and keeps the device's token, so whoever runs a relay could serve an app that reads everything. The relay is to be trusted as the laptop is — one of your own — and the docs say so rather than leave the Noise session to imply otherwise.

A relay knows rooms, not people. A room's id is a hash of a secret only its gateway holds, so a phone — which knows the id — can ask for the gateway but cannot stand in for it. Nothing is stored: a relay that restarts loses its connections and the gateways make them again.

Index

Constants

View Source
const (
	GatewayPath = "/relay/gateway"
	AcceptPath  = "/relay/accept"
	PhonePath   = "/relay/phone"
	HealthPath  = "/relay/health"
	// SecretHeader carries a gateway's room secret. A header, not the
	// query, so it is in no access log along the way.
	SecretHeader = "X-Conch-Relay-Secret"
)

The relay's addresses. A gateway holds GatewayPath open and opens AcceptPath once for each phone; a phone opens PhonePath.

View Source
const (
	CodeNoGateway = 4404 // the room's gateway is not connected
	CodeBusy      = 4429 // the room has as many phones as it takes
	CodeReplaced  = 4409 // a newer connection from the same gateway took over
	CodeTimeout   = 4408 // the gateway didn't take the phone in time
	// Conch Cloud's relay adds two (PROTOCOL §3.3); conch's own never
	// sends them.
	CodeBadTicket = 4401 // no ticket, or one used, expired or for another room
	CodeRevoked   = 4403 // the device holding the socket was revoked
)

Close codes a phone is told, so it can say which part is missing rather than spin: the relay answered, but the gateway isn't there.

View Source
const (
	Prologue = "conch-relay-v1"
	// TunnelHost is the address the gateway's handler sees the tunnel's
	// calls at, and their Origin: its own, so its same-origin checks hold.
	TunnelHost = "conch-relay"
)

Protocol is the Noise protocol, and Prologue binds a session to conch's use of it: a handshake made for something else does not complete here.

View Source
const HandshakeVersion = 1

HandshakeVersion is the tunnel's version. A phone app speaking another is told so rather than half understood.

View Source
const TicketHeader = "X-Conch-Relay-Ticket"

TicketHeader carries a ticket to a relay that admits a room's clients only with one (Conch Cloud's, PROTOCOL §3.2): single use, short-lived, handed out by the control plane. A relay of conch's own ignores it.

Variables

View Source
var (
	ErrTicketRefused = errors.New("the relay refused the ticket")
	ErrRevoked       = errors.New("this device was signed out of the account")
)

ErrTicketRefused and ErrRevoked are a cloud relay closing a client's socket: its ticket was not good (used, expired, another room's), or the device it was given to has been signed out.

View Source
var ErrNoGateway = errors.New("the computer isn't connected to the relay")

ErrNoGateway is the relay saying the computer isn't connected to it.

Suite is Noise_NK_25519_AESGCM_SHA256: every part of it is in a browser's WebCrypto.

Functions

func EncodeKey

func EncodeKey(b []byte) string

EncodeKey and DecodePublic are a key as it is written down: base64url.

func Fingerprint

func Fingerprint(pub []byte) string

Fingerprint is a key as four words, for a person to compare on the phone and on the computer: the same words, the same key.

func HTTPURL

func HTTPURL(relay string) (string, error)

HTTPURL is the relay's address as a page is opened at: https for wss.

func KeyFromPrivate

func KeyFromPrivate(private string) (noise.DHKey, error)

KeyFromPrivate is a static key from its private half, as kept.

func NewKey

func NewKey() (noise.DHKey, error)

NewKey makes a gateway's static key, the one a phone learns from the pairing QR code.

func NewSecret

func NewSecret() (string, error)

NewSecret makes a gateway's room secret: 256 random bits.

func RoomID

func RoomID(secret string) string

RoomID is the room a secret opens: public, since a phone has to name it, and useless without the secret for anything but asking for the gateway.

func Through

func Through(r *http.Request) bool

Through reports whether r reached the gateway's handler through a relay's tunnel, rather than over the network. Nothing a client sends can make it true: it is set on the in-memory connection itself.

func Words

func Words() []string

Words is the list fingerprints are said in, for the test that the phone's copy agrees.

Types

type Frame

type Frame struct {
	T  string            `json:"t"`
	ID int64             `json:"id"`
	M  string            `json:"m,omitempty"`
	P  string            `json:"p,omitempty"`
	H  map[string]string `json:"h,omitempty"`
	B  string            `json:"b,omitempty"`
	S  int               `json:"s,omitempty"`
	D  string            `json:"d,omitempty"`
	C  int               `json:"c,omitempty"`
	R  string            `json:"r,omitempty"`
}

Frame is one message of the tunnel.

type Hello

type Hello struct {
	V int    `json:"v"`
	Z string `json:"z,omitempty"`
}

Hello is each side's handshake payload: the version, and on the gateway's side how its direction is compressed.

type Link struct {
	Relay   string // wss://relay.example.com
	Secret  string
	Key     noise.DHKey
	Handler http.Handler
	Logf    func(format string, args ...any)
	// OnStatus, when set, is told every change of Status.
	OnStatus func(Status)
	// contains filtered or unexported fields
}

Link is a gateway's connection to a relay: it keeps its room open and serves every phone that arrives with Handler — the gateway's own.

func (*Link) Room

func (l *Link) Room() string

Room is the room this link holds.

func (*Link) Run

func (l *Link) Run(ctx context.Context) error

Run keeps the link up until ctx ends, reconnecting with a backoff, and serves phones as they come.

func (*Link) Status

func (l *Link) Status() Status

Status is the link as it is now.

type Open

type Open struct {
	Open  string `json:"open,omitempty"`
	Ready bool   `json:"ready,omitempty"`
}

Open is what the relay tells a gateway: when a phone arrives, open AcceptPath with this id to take it. Ready comes first, once the room is the gateway's: until then a phone asking for it would be told nobody is there, so the gateway counts itself connected only from Ready.

type Server

type Server struct {
	UI   http.Handler
	Logf func(format string, args ...any)
	// contains filtered or unexported fields
}

Server is a relay. UI, when set, is served for everything that isn't the relay's own — the phone's app, so a phone can open the relay's address and pair.

func (*Server) Close

func (s *Server) Close()

Close tells every gateway the relay is going, so each reconnects (to this relay restarted, or another) rather than wait on a dead one.

func (*Server) ServeHTTP

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

ServeHTTP routes the relay's paths, and the app for the rest.

type Session

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

Session is a phone's end of the tunnel, in Go: what the phone's app does in tunnel.mjs. conch uses it to check a relay works end to end, and the tests to drive a gateway through one.

func Dial

func Dial(ctx context.Context, relay, room string, gwPub []byte) (*Session, error)

Dial opens a session with the gateway in room, whose key is gwPub (from the pairing link): the relay first, then the handshake through it.

func DialTicket

func DialTicket(ctx context.Context, relay, room, ticket string, gwPub []byte) (*Session, error)

DialTicket is Dial with a ticket for the relay, when it has one.

func (*Session) Close

func (s *Session) Close()

Close ends the session.

func (*Session) Do

func (s *Session) Do(ctx context.Context, method, path string, h map[string]string, body string) (Frame, error)

Do makes one call on the gateway and waits for its answer.

func (*Session) Done

func (s *Session) Done() <-chan struct{}

Done is closed when the session ends.

func (*Session) Err

func (s *Session) Err() error

Err is why the session ended, or nil while it lasts.

func (*Session) Socket

func (s *Session) Socket(ctx context.Context, path string, h map[string]string) (*Socket, error)

Socket opens a WebSocket at path on the gateway.

type Socket

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

Socket is a WebSocket on the gateway, carried by the session.

func (*Socket) Close

func (k *Socket) Close()

Close closes the socket.

func (*Socket) Recv

func (k *Socket) Recv(ctx context.Context) (string, error)

Recv waits for the socket's next message. Messages that came before the socket closed are still given, in order: a script's exit status is its last message, sent just before the close.

func (*Socket) Send

func (k *Socket) Send(ctx context.Context, text string) error

Send writes a message on the socket.

type Status

type Status struct {
	Connected bool      `json:"connected"`
	Error     string    `json:"error,omitempty"`
	Since     time.Time `json:"since"`
	Phones    int       `json:"phones"`
}

Status is how a gateway's link to its relay is, for `conch web` to say and the settings to show.

Jump to

Keyboard shortcuts

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