Lets a client signed in to a JuCode account (a mobile app, the web app's
/remote page, a desktop) reach the
jucode daemon of a computer signed in to the same account, from anywhere.
Three parties:
- host: the daemon. Keeps one outbound WebSocket to the relay while its computer is signed in to JuCode.
- relay: part of the JuCode backend (
<jucode_api_url>/v1/relay/*, same binary and deploy as the rest of the API). Checks both ends' OAuth tokens, keeps the account's list of computers, and forwards opaque bytes between a client and its host. Holds no stream keys and sees no content. - client: opens one WebSocket per connection to the relay, naming the host.
The account decides who may reach a computer; end-to-end encryption decides
who can read the stream: every client stream carries a Noise
Noise_IK_25519_ChaChaPoly_SHA256 session between the client and the
daemon, and the relay only ever sees Noise handshake and transport messages.
Version 2 replaces v1 outright: v1 hosts and clients (no token, the
standalone jucode-relay under /relay/v1) no longer connect.
All base64 in this document is base64url without padding.
The daemon keeps two keypairs in <daemon state dir>/relay-identity.json
(mode 0600), created on first use:
| Key | Algorithm | Use |
|---|---|---|
| identity key | Ed25519 | proves the host id to the relay |
| static key | X25519 | the Noise responder static key (s in IK) |
Host id = first 16 bytes of SHA-256(ed25519 public key), base64url →
22 characters. It names the computer at the relay, in the account's computer
list and in pairing links.
Account owner of a computer: the user whose access token the daemon presented on its latest host connection. Signing the daemon in to another account moves the computer there (§5).
Client device: an OAuth authorization of a client app (jucode-mobile,
jucode-web, jucode-cli) plus that install's own X25519 static keypair,
which never leaves the install. The daemon knows a paired device by its
static public key.
<jucode_app_url>/remote#pair=<host_id>.<host_static_pub>.<code>
- The origin is the web app (
jucode_app_url), whatever relay URL the daemon uses; without it set the daemon gives no link. Clients always connect to their own configured relay; the link only names the computer and a code. host_static_pub: the daemon's X25519 public key (32 bytes). The client checks it against thestatic_pubthe account's computer list gives forhost_idand refuses a mismatch.code: a one-time pairing code from the daemon'spair_linkop (8 characters, 5 minutes, single use).- The host must be in the client's account list (same account only).
- The fragment is never sent to any server.
Base: jucode_api_url with a wss (or ws) scheme plus /v1/relay, e.g.
wss://jucode.example/v1/relay.
Authentication on both sockets. The upgrade request carries a valid
OAuth device access token. Native clients and the daemon send
Authorization: Bearer <token>; browsers, which cannot set headers on a
WebSocket, offer the subprotocols jucode-relay and
jucode-bearer.<token>, and the relay answers with jucode-relay. A
missing or invalid token fails the upgrade with HTTP 401. Personal
access tokens and web session tokens are refused.
host routes the connection (the load balancer hashes on it). After the
upgrade:
- relay → host, text:
{"t":"challenge","nonce":"<32 random bytes>"} - host → relay, text:
{"t":"auth","pub":"<ed25519 pub>","sig":"<sig over \"jucode-relay-v1:\" || nonce bytes>", "v":"<daemon version>","static":"<x25519 static pub>","name":"<hostname>","platform":"macos|windows|linux"} - The relay verifies the signature and that
HostID(pub)equals thehostparameter (else close 4400), registers the computer under the token's user (name only until the user renamed it), and replies{"t":"ready","host":"<host_id>","user":"<user id>"}. A newer connection for the same host replaces the older one (close 4409). A bad signature: close 4401.
Then all messages are binary frames:
byte 0 kind: 1 = open, 2 = data, 3 = close
bytes 1..4 stream id, u32 big-endian (allocated by the relay, never 0)
bytes 5.. payload (open and data)
- relay → host
open sid payload: a client connected. The payload is a JSON object naming the caller, from its token:{"user":"<user id>","authz":"<authorization id>","client":"jucode-mobile","name":"<authorization name>"}. data sid payload: both directions; payload is one client message.close sid: both directions; the other side of the stream closes. The host sends it to drop a client (bad handshake, refused, revoked device).
Keepalive: the relay pings the host every 30 s. The host treats 75 s without any frame as a dead connection.
Reconnecting: the host reconnects when its socket drops, with backoff 1 s,
2 s, 4 s … capped at 60 s, plus up to 1 s of jitter, back to 1 s once a
connection lasted 30 s. Close 4503 (server restarting, sent when a
backend instance drains) means reconnect after 1 s, without growing the
backoff. HTTP 401 on the upgrade: the host refreshes its token once and
retries; refused again, it stays off until its saved login changes.
- Unknown host, or a host owned by another user → the relay accepts the
upgrade and closes with 4403 (
not your computer), so browsers can read the code. - Host not connected → accept, then close 4404 (
host offline). - Otherwise the relay allocates a stream id, sends
openwith the caller payload to the host, and then forwards: each binary client message ⇄ onedataframe. Text frames from the client are a protocol error (close 4400). - Either side closing ends the stream (
closeto the host / WS close 4410host closedto the client). Host disconnect closes all its client streams with 4404; a draining instance closes them with 4503 (retry soon).
| Limit | Value |
|---|---|
| max message (client or data payload) | 1 MiB |
| streams per host | 32 (further connects close 4429) |
| new client connections per IP | 20 per minute (close 4429) |
| host auth attempts per IP | 10 per minute |
| challenge answered | 10 s |
| idle client stream (no traffic either way) | 10 min |
Terminal output is flow-controlled end to end by the daemon (daemon-protocol.md, Terminals).
The computer list is ordinary backend API (OAuth access token, under
/v1/oauth): GET /v1/oauth/computers and
PATCH/DELETE /v1/oauth/computers/:host_id.
Noise_IK_25519_ChaChaPoly_SHA256, prologue = ASCII jucode-relay-v1.
The client is the initiator and knows the daemon static key from the
account's computer list (or the pairing link).
-
msg 1 (client → daemon,
-> e, es, s, ss), payload = JSON{"name":"<device name>","pair":"<code>"?,"request":true?}.paironly on the first connection after scanning a link;requestonly when the user asked to connect to a computer this install is not paired with. The daemon waits 10 s for msg 1. -
The daemon authorizes the client static key
rs, in order:- the
openpayload'suseris not the account the computer is registered under → refused,wrong account; rsbelongs to a paired, unrevoked device → allowed as that device;pairis a valid unexpired code → paired now, allowed;requestis true → a connect request: with no desktop (local client) connected, refused at once withno_desktop; with 3 requests already open,busy; otherwise the desktop is asked and the daemon waits up to 120 s: allowed → paired now;denied;expired; the client closing first cancels it;- otherwise → refused,
not paired.
A device paired in step 3 or 4 is recorded keyed by
hex(SHA-256(rs)), with theopenpayload'sauthz,clientanduser; its name is msg 1'sname, else the payload'sname. - the
-
msg 2 (daemon → client,
<- e, ee, se), payload{"ok":true,"device":"<device id>","name":"<device name>"}, or{"ok":false,"error":"<reason>"}followed byclose. In request mode msg 2 may come up to 120 s after msg 1. -
Transport: after msg 2 both sides split into cipher states. Each WS message is one Noise transport message. Noise messages are ≤ 65535 bytes, so daemon protocol frames (one JSON document each, as on the local WebSocket) are chunked:
plaintext = flag (1 byte: 0 = last chunk, 1 = more follows) || chunkchunks of up to 65000 bytes; the receiver concatenates until flag 0 and parses the result as one daemon protocol frame (UTF-8 JSON).
After the handshake the stream behaves like a local client with the device
restrictions (docs/daemon-protocol.md): hello first, the same ops and
events. Revoking a device closes its relay streams.
--relay <wss url>(default<jucode_api_url>/v1/relayoverwss://) overrides the relay for development;--no-relaydisables it.- The host connection is made only while the computer is signed in to JuCode
(the token comes from the JuCode login and is refreshed as needed) and the
relaysetting istrue(off by default; turned on withrelay_set). Signed out, nothing connects, and a connected host leaves the relay. ready.useris kept asrelay_userinsettings.json. When it differs from the stored one, every paired device is revoked before any stream is served.- Local-client ops:
relay_status({enabled, connected, signed_in, host, url, user}),relay_set {enabled},pair_link,connect_requests,connect_decide,device_list,device_revoke(daemon-protocol.md, Remote access).
crates/daemon ships examples/noise_peer.rs: a responder that reads Noise
messages as hex lines on stdin and writes replies as hex lines on stdout, with
a fixed static key given on the command line. The JS side has a Node test that
runs its initiator against it (handshake, one chunked frame each way). Keep
both implementations in step with this document.