Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(cloud): fit TURN usage tags within provider limits (#18)

leepokai006b59f67d6e
diff --git a/cloud/src/api.ts b/cloud/src/api.ts
index 2ec7623a..6e322d15 100644
--- a/cloud/src/api.ts
+++ b/cloud/src/api.ts
@@ -2,11 +2,13 @@
import { Webhook } from "svix";
import {
+ b64url,
COMPUTER_ID,
computerIdFor,
fromB64url,
NONCE_TTL_MS,
randomId,
+ sha256,
utf8,
verifyClerk,
verifyEd25519,
@@ -660,13 +662,16 @@ export async function hostScreenIce(c: Ctx): Promise<Response> {
const limit = await c.env.TURN_LIMITER.limit({ key: comp.owner_user_id! });
if (!limit.success) throw new ApiError("rateLimited", "Too many screen connections. Try again in a minute.");
const ttl = 3600;
+ // Cloudflare limits customIdentifier to 64 characters. Keep the computer ID
+ // and a stable 128-bit device fingerprint (45 characters total).
+ const deviceFingerprint = b64url((await sha256(fromB64url(dk, 32)!)).subarray(0, 16));
try {
const res = await fetch(
`https://rtc.live.cloudflare.com/v1/turn/keys/${encodeURIComponent(c.env.TURN_KEY_ID)}/credentials/generate-ice-servers`,
{
method: "POST",
headers: { Authorization: `Bearer ${c.env.TURN_KEY_API_TOKEN}`, "Content-Type": "application/json" },
- body: JSON.stringify({ ttl, customIdentifier: `${comp.id}:${dk}` }),
+ body: JSON.stringify({ ttl, customIdentifier: `${comp.id}:${deviceFingerprint}` }),
signal: AbortSignal.timeout(8000),
},
);
diff --git a/cloud/test/screen.test.ts b/cloud/test/screen.test.ts
index faa89f6f..4950c6b8 100644
--- a/cloud/test/screen.test.ts
+++ b/cloud/test/screen.test.ts
@@ -31,7 +31,12 @@ afterEach(() => vi.restoreAllMocks());
describe("screen relay credentials", () => {
it("issues expiring credentials for a claimed host and tags device usage", async () => {
const { host } = await claimedHost();
- const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(Response.json({ iceServers: servers }));
+ const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (_url, init) => {
+ const payload = JSON.parse(init!.body as string);
+ // Match the live provider, which rejects the original 66-character tag.
+ if (payload.customIdentifier.length > 64) return new Response("invalid argument", { status: 400 });
+ return Response.json({ iceServers: servers });
+ });
const start = Date.now();
const res = await request(host);
expect(res.status).toBe(200);
@@ -42,8 +47,16 @@ describe("screen relay credentials", () => {
expect(data.expiresAt).toBeLessThanOrEqual(Date.now() + 3_600_000);
const [url, init] = fetch.mock.calls[0]!;
expect(url).toBe("https://rtc.live.cloudflare.com/v1/turn/keys/test-key/credentials/generate-ice-servers");
- expect(JSON.parse(init!.body as string)).toEqual({ ttl: 3600, customIdentifier: `${host.cid}:${deviceKey}` });
+ expect(JSON.parse(init!.body as string)).toEqual({ ttl: 3600, customIdentifier: `${host.cid}:${ref.computerId(ref.unb64url(deviceKey))}` });
expect(JSON.stringify(data)).not.toContain("server-secret");
+ const repeated = await request(host);
+ expect(repeated.status).toBe(200);
+ expect(JSON.parse(fetch.mock.calls[1]![1]!.body as string).customIdentifier)
+ .toBe(JSON.parse(init!.body as string).customIdentifier);
+ const otherDevice = ref.b64url(ref.signKey().pub);
+ expect((await request(host, {}, { deviceKey: otherDevice })).status).toBe(200);
+ expect(JSON.parse(fetch.mock.calls[2]![1]!.body as string).customIdentifier)
+ .not.toBe(JSON.parse(init!.body as string).customIdentifier);
});
it("rejects anonymous registration, blocked computers and deleted accounts before contacting TURN", async () => {
diff --git a/docs/features/remote-screen.md b/docs/features/remote-screen.md
index ad35bf1f..cecd32f3 100644
--- a/docs/features/remote-screen.md
+++ b/docs/features/remote-screen.md
@@ -6,7 +6,7 @@ Remote screen lets an authorized phone view and control a computer. Signaling tr
1. The phone calls `screenPrepare {relay}`. `relay` is true when its active host channel uses Cloudflare; direct LAN/Tailscale connections do not contact a STUN/TURN provider.
2. The host checks the `screen` scope and that screen access is enabled. For relay connections it signs `POST /v1/host/screen-ice {deviceKey}`. The Worker requires a registered, active computer belonging to an active account. The host attests the device's local authorization, so account grants and local QR pairing both work on a claimed computer.
-3. The Worker issues Cloudflare Realtime credentials with a one-hour TTL, tags usage with `computerId:deviceKey`, and returns `Cache-Control: no-store`. Permanent TURN keys stay in Worker secrets.
+3. The Worker issues Cloudflare Realtime credentials with a one-hour TTL, tags usage with `computerId:deviceFingerprint` (the fingerprint is the base64url encoding of the first 16 bytes of SHA-256 of the device public key; the resulting 45-character tag fits Cloudflare’s 64-character limit), and returns `Cache-Control: no-store`. Permanent TURN keys stay in Worker secrets.
4. The host reserves a random session ID bound to the calling device and keeps the exact ICE configuration in memory. The phone and helper use that configuration. Clients cannot supply arbitrary ICE servers to the host.
5. `screenOffer {session, sdp, display?}` carries the completed offer to the helper. Both peers gather for up to ten seconds. ICE prefers direct candidates and uses TURN when necessary, with UDP, TCP and TLS on port 443 available.
6. The phone reconnects with fresh credentials five minutes before expiry. The host checks active sessions every five seconds and closes expired sessions, revoked devices and expired account leases. A prepared session must start within one minute.