Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.2 KB
Raw Blame
#!/usr/bin/env npx tsx
// Standalone E2EE WebSocket mock exposes desktop RPCs with fake data for mobile development without Orca.
import { WebSocketServer, type WebSocket } from 'ws'
import { deriveSharedKey, e2eeDecrypt, e2eeEncrypt, type E2EEState } from './mock-server-encryption'
import { loadOrCreateMockServerKeyPair } from './mock-server-key-pair'
import {
  error,
  handleRequest,
  mockScenarioSummary,
  type RpcRequest
} from './mock-server-rpc-handlers'

const PORT = Number(process.env.PORT) || 6768
const AUTH_TOKEN = 'mock-device-token'

// Print server public key for pairing QR; MOCK_SERVER_KEY_FILE preserves it across restarts for pinned devices.
const serverKeyPair = loadOrCreateMockServerKeyPair(process.env.MOCK_SERVER_KEY_FILE)
const serverPublicKeyB64 = Buffer.from(serverKeyPair.publicKey).toString('base64')

const wss = new WebSocketServer({ port: PORT })

// First frame is plaintext e2ee_hello; encrypt subsequent RPCs with derived shared key.
const connectionState = new Map<WebSocket, E2EEState>()

wss.on('connection', (ws) => {
  console.log('[mock] Client connected — waiting for e2ee_hello')

  ws.on('message', (data) => {
    const msg = typeof data === 'string' ? data : data.toString('utf-8')
    const e2ee = connectionState.get(ws)

    if (!e2ee) {
      let hello: { type?: string; publicKeyB64?: string }
      try {
        hello = JSON.parse(msg)
      } catch {
        ws.send(JSON.stringify({ type: 'e2ee_error', message: 'Invalid JSON' }))
        ws.close()
        return
      }

      if (hello.type !== 'e2ee_hello' || !hello.publicKeyB64) {
        ws.send(JSON.stringify({ type: 'e2ee_error', message: 'Expected e2ee_hello' }))
        ws.close()
        return
      }

      const clientPublicKey = Uint8Array.from(Buffer.from(hello.publicKeyB64, 'base64'))
      if (clientPublicKey.length !== 32) {
        ws.send(JSON.stringify({ type: 'e2ee_error', message: 'Invalid public key' }))
        ws.close()
        return
      }

      const sharedKey = deriveSharedKey(serverKeyPair.secretKey, clientPublicKey)
      connectionState.set(ws, { sharedKey, deviceToken: null, authenticated: false })

      ws.send(JSON.stringify({ type: 'e2ee_ready' }))
      console.log('[mock] E2EE key exchange complete — waiting for encrypted auth')
      return
    }

    const plaintext = e2eeDecrypt(msg, e2ee.sharedKey)
    if (plaintext === null) {
      console.log('[mock] Decryption failed — dropping message')
      return
    }

    let request: RpcRequest
    try {
      request = JSON.parse(plaintext) as RpcRequest
    } catch {
      const encrypted = e2eeEncrypt(
        JSON.stringify(error('unknown', 'bad_request', 'Invalid JSON')),
        e2ee.sharedKey
      )
      ws.send(encrypted)
      return
    }

    if (!e2ee.authenticated) {
      const auth = request as unknown as { type?: string; deviceToken?: string }
      if (auth.type !== 'e2ee_auth' || auth.deviceToken !== AUTH_TOKEN) {
        ws.send(
          e2eeEncrypt(
            JSON.stringify({ type: 'e2ee_error', error: { code: 'unauthorized' } }),
            e2ee.sharedKey
          )
        )
        ws.close()
        return
      }
      e2ee.deviceToken = auth.deviceToken
      e2ee.authenticated = true
      ws.send(e2eeEncrypt(JSON.stringify({ type: 'e2ee_authenticated' }), e2ee.sharedKey))
      console.log('[mock] E2EE authentication complete')
      return
    }

    console.log(`[mock] ${request.method} (id: ${request.id})`)
    handleRequest(
      request,
      (response) => {
        if (ws.readyState === ws.OPEN) {
          ws.send(e2eeEncrypt(JSON.stringify(response), e2ee.sharedKey))
        }
      },
      ws
    )
  })

  ws.on('close', () => {
    connectionState.delete(ws)
    console.log('[mock] Client disconnected')
  })

  ws.on('error', () => {
    connectionState.delete(ws)
    ws.close()
  })
})

console.log(`[mock] Orca mock server listening on ws://localhost:${PORT}`)
console.log(`[mock] Auth token: ${AUTH_TOKEN}`)
console.log(`[mock] Server public key (base64): ${serverPublicKeyB64}`)
console.log(
  `[mock] Scenario: ${mockScenarioSummary.repoCount} repos, ${mockScenarioSummary.worktreeCount} worktrees, ${mockScenarioSummary.rpcDelayMs}ms default RPC delay`
)
console.log(`[mock] E2EE enabled — clients must send e2ee_hello before RPC`)