Skip to content

relay: WS upgrade for /v1/client — accept phone connection, look up server-id, register #5

@ilmoniemi

Description

@ilmoniemi

User Story

As a phone connecting to the relay, I want a /v1/client endpoint that validates my headers, looks up the server-id I'm targeting, and registers me as a phone connection on that binary's slot — or closes with 4404 if no binary holds the server-id — so that the phone-side of the routing topology is established per the protocol spec.

Context

Implements the phone-side handshake from pyrycode/pyrycode/docs/protocol-mobile.md § Phone → relay → binary. The relay does NOT validate the device token — it forwards everything to the binary, which validates. The relay's job is purely routing: "is there a binary holding this server-id? if yes, connect; if no, close 4404."

Acceptance Criteria

  • New file internal/relay/client_endpoint.go exporting:
    • func ClientHandler(r *Registry, logger *slog.Logger) http.Handler — returns an http.Handler for /v1/client.
  • On request:
  • cmd/pyrycode-relay/main.go updated: register the handler at /v1/client on the existing mux. Existing routes (/healthz, /v1/server) keep working.
  • Tests in internal/relay/client_endpoint_test.go covering:
    • Successful upgrade when a binary holds the server-id → registry.PhonesFor(serverID) contains the new connection.
    • Missing each required header → 400 (table-driven, one row per missing/empty header).
    • No binary holds the server-id → WS close 4404.
    • Closing the phone WS unregisters it from the registry.
    • Multiple phones on the same server-id register and unregister independently.
  • gosec and govulncheck clean.

Technical Notes

Size Estimate

S — ~100 LOC + ~120 LOC tests.

Depends on

Metadata

Metadata

Assignees

No one assigned

    Labels

    security-sensitiveTouches auth, crypto, or internet-exposed input pathssize:sSmall ticket: <100 lines production code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions