# Space Relay wire protocol, machine-readable. # Derived from the code (src/SpaceRelay/RelayServer.cs, RelayStore.cs, src/SpaceRelay.Client/Frames.cs, Messages.cs, # RelayClient.cs, PeerDialer.cs, GuestPass.cs), not from the prose of chapter 11. # Kept honest by tests/SpaceRelay.Tests/RelayProtocolSchemaTests.cs: every message type (Msg), every error code # (RelayErrors), the limits and the worked examples' byte lengths are checked against the real code. # Subset: 2-space indent, maps, lists, scalars, block scalars; no anchors. info: title: "Space Relay protocol" protocolVersion: 1 manual: "11-relay.md#relay-protocol" peerProtocol: "10-api-peer.md" defaultAddress: "relay.spacebackup.net:47900" legacyAddress: "spacerigs.io:47900" port: 47900 sources: - "src/SpaceRelay/RelayServer.cs" - "src/SpaceRelay/RelayStore.cs" - "src/SpaceRelay.Client/Frames.cs" - "src/SpaceRelay.Client/Messages.cs" - "src/SpaceRelay.Client/RelayClient.cs" - "src/SpaceRelay.Client/PeerDialer.cs" - "src/SpaceRelay.Client/GuestPass.cs" tls: manual: "11-relay.md#protocol-tls" protocols: - "TLS 1.2" - "TLS 1.3" clientCertificate: "none" serverCertificate: "the production relay: a publicly trusted certificate for relay.spacebackup.net (validate chain and name); elsewhere a self-signed one, pinned by SHA-256 of its DER (hex pairs joined by ':')" sni: "TargetHost = the host part of the relay address" pinRule: "with a pinned fingerprint only the fingerprint counts; without one, the chain must be trusted and valid for the host" connectTimeoutSeconds: 10 handshakeAndFirstFrameSeconds: 15 tcpKeepalive: "60 s idle, then every 15 s, 4 probes" connections: control: "first frame hello: one per device, kept open" data: "first frame attach: one per pipe; raw bytes after attached" firstFrameErrors: - message: "expected hello or attach" code: "bad-request" frames: manual: "11-relay.md#protocol-framing" layout: - "4 bytes: body length n, signed int32 big-endian" - "n bytes: UTF-8 JSON object with a string field t (the message type)" maxBody: 65536 rules: - "n <= 0 or n > 65536: 'bad frame length {n}', connection closed" - "a body that is not a JSON object with a string t: 'bad frame: not a typed message', connection closed" - "after attached a data connection carries raw bytes, no more frames" - "unknown fields are ignored" errors: - message: "bad frame length {n}" - message: "bad frame: not a typed message" - message: "frame of {n} bytes is over the {max} limit" group: manual: "11-relay.md#joining-a-group" derivation: - "master = PBKDF2-HMAC-SHA256(UTF-8 company key text trimmed, salt UTF-8 'SpaceBackup company key v1', 200000 iterations, 32 bytes)" - "relayKey = HKDF-Expand(SHA-256, master, 32 bytes, info UTF-8 'relay')" - "group = lower-case hex(HMAC-SHA256(relayKey, UTF-8 'space-relay-group-v1')), 64 hex digits" code: "first 40 bits of SHA-256(ASCII lower-case group id) as 8 Crockford base-32 digits (0123456789ABCDEFGHJKMNPQRSTVWXYZ), XXXX-XXXX" codeInput: "any case; O read as 0, I and L as 1; '-' and spaces ignored" accounts: openGroup: "hello without token: billed to the group (usage key 'group:' + group id); registered on its first hello" token: "hello with token: billed to the token (stored as lower-case hex SHA-256 of the trimmed token); tokens are 'sr_' + base64url of 32 random bytes" guest: "hello with guest: billed to the group its pass names" separation: "a group meets only inside its account: groupKey = account key + '/' + group" messages: hello: manual: "11-relay.md#protocol-messages" direction: "C->R" connection: "control, first frame" fields: t: "\"hello\"" group: "64 hex digits, any case (lower-cased by the relay)" instanceId: "1-64 ASCII characters 0x21-0x7E (no spaces)" machine: "string, at most 64 chars" version: "string, kept to 32 chars; required without a token" protocol: "int, 1 (sent by the client, not checked by the relay)" lanAddrs: "[ip:port], at most 16, each at most 64 chars and a valid IPEndPoint; others dropped" publicPort: "int, sent by versions before requests #128 only; ignored and passed on to nobody" blob: "b64, optional, at most 48 KiB decoded; larger or bad base64 is dropped" token: "string, optional, at most 256 chars" upload: "int 1, optional: this PC takes uploads through the HTTPS door (docs/relay-upload.md)" answers: - "welcome, then peers (and a notice when over 80 % / 100 %)" - "error (fatal), then closed" errors: - message: "hello needs group (64 hex digits), instanceId and machine" code: "bad-request" - message: "hello needs the app version" code: "bad-request" - message: "invalid relay token" code: "auth" - message: "relay token revoked" code: "revoked" - message: "this relay needs a relay token" code: "auth" - message: "this group is blocked by the relay" code: "blocked" - message: "too many new relay groups from {ip} today" code: "busy" - message: "at most {max} {PCs} per relay {group}" code: "busy" - message: "that instance id is in use" code: "bad-request" example: | {"t":"hello","token":null,"group":"<64 hex>","instanceId":"5f0c2a9e8b7d4c31a6e2f90d14b7c3a8","machine":"ANNA-PC","version":"0.14.2","protocol":1,"lanAddrs":["192.168.1.34:47800"],"blob":"U0JBMd..."} helloGuest: manual: "11-relay.md#protocol-guests" direction: "C->R" connection: "control, first frame" type: "hello" fields: t: "\"hello\"" guest: "the guest pass (gp1.)" instanceId: "string" machine: "string" version: "string" protocol: "int" errors: - message: "invalid guest pass" code: "auth" - message: "guest pass expired" code: "auth" - message: "no such relay group" code: "auth" welcome: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"welcome\"" publicIp: "the address the relay sees (IPv4-mapped IPv6 mapped to IPv4)" keepalive: "int seconds (settings keepaliveSeconds, default 30); the client uses min(it, its own) when 5..600" protocol: "int, 1" usedBytes: "int, this month in + out of the account" quotaBytes: "int or null" groupCode: "open groups and guests: XXXX-XXXX" activateUrl: "open groups: https://spacebackup.net/account/relay/activate?group={code}" activationNeeded: "open groups: bool" upload: "int 1 when the relay's HTTPS upload door (PUT /v1/upload) is on; absent otherwise" logs: "int 1 when the relay keeps devices' logs (PUT /v1/logs, docs/relay-logs.md); absent otherwise" peers: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"peers\"" peers: "[Peer]: the other non-guest devices of the group (a guest: only the PCs its pass names, without blob)" peer-joined: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"peer-joined\"" peer: "Peer" peer-updated: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"peer-updated\"" peer: "Peer" notes: - "at most one per device every 2 s; a burst sends only the latest state" peer-left: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"peer-left\"" instanceId: "string" update: manual: "11-relay.md#protocol-messages" direction: "C->R" fields: t: "\"update\"" lanAddrs: "[ip:port]" publicPort: "int, older versions only; ignored" blob: "b64, optional (absent: the old blob is kept)" notes: - "ignored for a guest; never answered" relay-connect: manual: "11-relay.md#protocol-pipes" direction: "C->R" fields: t: "\"relay-connect\"" to: "the other device's instanceId" ref: "string chosen by the client" answers: - "relay-session {ref, sessionId}, and relay-incoming to the other device" - "error {ref, code, message}" errors: - message: "relay token revoked" code: "revoked" - message: "this group is blocked by the relay" code: "blocked" - message: "that PC is not connected to the relay" code: "offline" - message: "guest pass expired" code: "auth" - message: "the relay is busy (too many connections being set up)" code: "busy" - message: "relay quota reached" code: "quota" - message: "Space Relay activation needed" code: "activation" relay-session: manual: "11-relay.md#protocol-pipes" direction: "R->C" fields: t: "\"relay-session\"" ref: "the request's ref" sessionId: "32 lower-case hex digits (16 random bytes)" relay-incoming: manual: "11-relay.md#protocol-pipes" direction: "R->C" fields: t: "\"relay-incoming\"" sessionId: "string" from: "the asking device's instanceId (an upload: the phone's X-SB-From, may be empty)" kind: "absent: a pipe; \"upload\": an upload through the HTTPS door (only to a PC whose hello said upload: 1)" attach: manual: "11-relay.md#protocol-pipes" direction: "C->R" connection: "data, first frame" fields: t: "\"attach\"" token: "string or null; required (and must match) for a token account's pipe" sessionId: "string" instanceId: "the attaching device's instanceId (one of the session's two)" errors: - message: "invalid relay token" code: "auth" - message: "no such relay session (expired?)" code: "offline" - message: "not a party of this relay session" code: "auth" - message: "already attached" code: "bad-request" - message: "{machine} did not join the relay session in {seconds} s" code: "timeout" attached: manual: "11-relay.md#protocol-pipes" direction: "R->C" fields: t: "\"attached\"" notes: - "sent to both sides once both attached; raw bytes from then on" upload: manual: "11-relay.md#protocol-uploads" direction: "R->C" connection: "an upload's data connection, after attached" fields: t: "\"upload\"" envelope: "the X-SB-Envelope header as it came (base64 of the SBU1 packet the relay cannot open)" length: "int, the body's Content-Length" from: "the phone's instance id (X-SB-From) or empty" answers: - "upload-go, then exactly length body bytes, then upload-done" - "upload-done (a refusal; no body byte moves)" upload-go: manual: "11-relay.md#protocol-uploads" direction: "C->R" connection: "an upload's data connection" fields: t: "\"upload-go\"" upload-done: manual: "11-relay.md#protocol-uploads" direction: "C->R" connection: "an upload's data connection" fields: t: "\"upload-done\"" status: "ok | already | closed | full | bad | denied | busy | offline | error (HTTP 200 200 409 507 400 403 503 503 500)" error: "string, optional" ping: manual: "11-relay.md#protocol-messages" direction: "C->R" fields: t: "\"ping\"" ref: "string" pong: manual: "11-relay.md#protocol-messages" direction: "R->C" fields: t: "\"pong\"" ref: "the ping's ref" usedBytes: "int" quotaBytes: "int or null" groupCode: "open groups" activateUrl: "open groups" activationNeeded: "open groups: bool" notice: manual: "11-relay.md#protocol-limits" direction: "R->C" fields: t: "\"notice\"" code: "quota-warning | quota | activation" message: "string" groupCode: "with activation" activateUrl: "with activation" notes: - "once a month at 80 % (fixed limits only) and at 100 %" error: manual: "11-relay.md#protocol-errors" direction: "R->C" fields: t: "\"error\"" code: "one of errors" message: "string" ref: "the request's ref, when it answers one" fatal: "false on a non-fatal error without ref" groupCode: "with activation" activateUrl: "with activation" rules: - "with ref: the answer to that request; the connection stays" - "without ref, fatal:false: information; the connection stays" - "without ref and without fatal: the relay closes the connection after it" peer: manual: "11-relay.md#protocol-messages" fields: instanceId: "string" machine: "string" version: "string" publicIp: "string" lanAddrs: "[ip:port]" online: "bool (true)" blob: "b64 of the device's sealed LAN announcement ('SBA1' | nonce 12 | AES-256-GCM ciphertext | tag 16); never sent to guests" upload: "int 1, only when the PC takes uploads through the HTTPS door" pipes: manual: "11-relay.md#protocol-pipes" steps: - "A: relay-connect {to: B, ref}" - "R: relay-session {ref, sessionId} to A; relay-incoming {sessionId, from: A} to B" - "A and B: each a new TLS connection, attach {token?, sessionId, instanceId}" - "R: attached to both; the two connections are spliced" - "inside: the Space Backup peer protocol, A is the client (handshake first)" - "either side closes: the relay closes the other (no half-close)" attachTimeoutSeconds: 30 chunkBytes: 65536 usageCheckBytes: 4194304 clientRelayConnectTimeoutSeconds: 20 clientAttachReplyTimeoutSeconds: 30 routeOrder: - "LAN: same public IP, or a lanAddr on one of this device's own IPv4 networks -> those lanAddrs (1.5 s each)" - "never publicIp: a device at another site is reached only through the relay (requests #128)" - "relayed: relay-connect + attach" badRouteMinutes: 10 limits: manual: "11-relay.md#protocol-limits" maxFrame: 65536 maxBlob: 49152 maxLanAddrs: 16 maxLanAddrChars: 64 maxTokenChars: 256 maxIdChars: 64 maxMachineChars: 64 versionChars: 32 handshakeTimeoutSeconds: 15 attachTimeoutSeconds: 30 maxPendingPerAccount: 200 maxSessions: 10000 maxQueuedFrames: 32 frameWriteSeconds: 20 updateEverySeconds: 2 keepaliveSeconds: 30 idleKeepalives: 3 maxPcsPerGroup: 200 maxConnectionsPerToken: 200 maxGuestsPerGroup: 50 newGroupsPerIpPerDay: 20 rateMbit: 300 freeGbPerMonth: 5 chunkBytes: 65536 usageCheckBytes: 4194304 guestPassMaxChars: 8192 errors: manual: "11-relay.md#protocol-errors" auth: when: "not allowed in, or not a party" messages: - "invalid relay token" - "this relay needs a relay token" - "not a party of this relay session" - "invalid guest pass" - "guest pass expired" - "no such relay group" blocked: when: "the group is blocked by the operator" messages: - "this group is blocked by the relay" quota: when: "a token account reached its monthly quota" messages: - "relay quota reached" quota-warning: when: "notice only: 80 % of a fixed limit" messages: - "{pct} % of the {limit} used this month ({used} of {quota} GB)" busy: when: "a limit was reached" messages: - "too many new relay groups from " - "the relay is busy (too many connections being set up)" bad-request: when: "malformed or unexpected message" messages: - "hello needs group (64 hex digits), instanceId and machine" - "hello needs the app version" - "expected hello or attach" - "unknown message " - "that instance id is in use" - "already attached" offline: when: "the other device is not connected, or the session is gone" messages: - "that PC is not connected to the relay" - "no such relay session (expired?)" timeout: when: "the other side did not attach in time (also raised by the client: the relay did not set up the connection in time)" messages: - "{machine} did not join the relay session in {seconds} s" revoked: when: "the token was revoked (running connections closed within about 5 s)" messages: - "relay token revoked" replaced: when: "the same instance id connected again" messages: - "the same PC connected again" shutdown: when: "the relay is restarting" messages: - "the relay is restarting" activation: when: "an open group used its free allowance (and credit): with groupCode, activateUrl" messages: - "Space Relay activation needed" - ": this month's free relay traffic and the prepaid relay credit are used" guests: manual: "11-relay.md#protocol-guests" pass: format: "'gp1.' + base64url without padding of (nonce 12 | AES-256-GCM ciphertext | tag 16)" key: "HKDF-SHA256(ikm UTF-8 shared relay secret, 32 bytes, no salt, info 'space-relay-guest-pass-v1')" aad: "UTF-8 'gp1'" contents: "JSON {g: group (64), c: company, d: guest device, x: expiry unix seconds, m: [PC names], v: 1, ExpiresAt: x as an ISO 8601 date-time (a computed property the serializer writes too; ignored when opened)}; expired when x <= now" maxChars: 8192 validWhen: "opens, v 1, group 64 chars, device non-empty, machines present; not expired; the group is registered" rules: - "the guest sees only the PCs its pass names, without their blobs" - "the company's devices are never told about a guest; its update is ignored" - "relay-connect only to those PCs; nobody reaches a guest" - "at most 50 guests per group" - "at expiry: error {code: auth, message: 'guest pass expired'}, the control connection closes (pipes run on)" - "an instance id in use by the other kind (guest vs PC) is refused: 'that instance id is in use'" keepalive: manual: "11-relay.md#protocol-messages" rules: - "the client pings every keepalive seconds" - "the relay closes a control connection silent for 3 * max(30, keepalive) seconds" - "the client reconnects after 3 keepalives without any frame" - "reconnect back-off: 1 s doubling to 60 s, plus jitter up to half (max 5 s); 5 min after auth or revoked" examples: manual: "11-relay.md#protocol-framing" note: "byte lengths recomputed by RelayProtocolSchemaTests with the real Frames.Encode and GuestPass.Seal" ping: json: | {"t":"ping","ref":"17"} bodyBytes: 23 wireBytes: 27 attached: json: | {"t":"attached"} bodyBytes: 16 wireBytes: 20 relayConnect: json: | {"t":"relay-connect","to":"a07d4c1e9b2f4e6d8c3a5b7f9e1d2c4a","ref":"3"} bodyBytes: 71 wireBytes: 75 guestPass: contents: | {"g":"3f9a1c07b2e44d6c8e1a5b7f9c2d4e6a8b0c1d2e3f405162738495a6b7c8d9e0","c":"acme","d":"Bobs-iPhone","x":1792310400,"m":["ANNA-PC"],"v":1,"ExpiresAt":"2026-10-18T08:00:00+00:00"} contentsBytes: 178 passChars: 279 group: passphrase: "SCHEMA-TEST-KEY-2026" groupId: "558357e89e3072365ae8944e87b91c3e372cbe7ecd1cc02564b3a991159eacaa" groupCode: "T8YS-CEJQ"