Skip FCM wake (tunnel service start) and cert renewal when no
integrations are enabled. This avoids wasting the 50/week ACME cert
quota and unnecessary foreground service starts when the device has
nothing to serve.
Checks are in FcmReceiver (primary gate) and TunnelForegroundService
onStartCommand (defensive fallback).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The relay accepts {"type":"fcm_token","token":"..."} as a text WebSocket
message to register a device's FCM token. This adds:
- sendText() to WebSocketHandle for text frame support
- sendFcmToken() to TunnelClient interface and TunnelClientImpl
- FCM token sending in TunnelForegroundService after connect and reconnect
- Tests verifying token JSON is sent and no-op behavior before connect
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The idle timer previously started on any CONNECTED transition, including
initial connect --- disconnecting the tunnel before any client had a chance
to arrive. Now it only starts after a stream has been ACTIVE and returns
to CONNECTED (i.e. all clients left).
Also adds exponential-backoff reconnect (1s-30s, give up after 5min) for
unexpected disconnects, 15-second OkHttp WebSocket ping interval for dead
connection detection, and restores the 5-minute idle timeout from the
testing value of 30 minutes.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- HttpTransport: add handleNotification() for fire-and-forget JSON-RPC
messages (no response expected). Without this, notifications/initialized
hung forever waiting for a response that never came.
- McpRouting: detect JSON-RPC notifications (no "id" field) and return
202 Accepted immediately instead of dispatching through handleRequest.
Also add 5s timeout on receiveText() for /register to prevent hangs.
- TlsInputStream: same multi-record fix as TlsAcceptor handshake —
check netIn.position() before reading from stream. HTTP headers and
body arriving in the same mux DATA frame caused the body read to block.
- TunnelClientImpl: catch ClosedSendChannelException in onBinaryMessage
to prevent crash when WebSocket frames arrive after disconnect.
- TunnelForegroundService: handle double-wake by disconnecting stale
connection before reconnecting.
- Increase idle timeout to 30 minutes for testing (was 5 minutes, which
caused the connection to drop right when Claude tried tool calls).
Verified: Claude connected via OAuth, called echo and device_info tools
successfully through the full relay → device → MCP pipeline.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When the relay sends an OPEN frame (AI client connected), the device now:
1. Performs TLS server-side accept using the ACME cert/key
2. Bridges the plaintext I/O to the local Ktor MCP HTTP server
3. Routes MCP JSON-RPC requests to the registered integration tools
Key changes:
- SessionHandler interface in :work for the foreground service to dispatch streams
- McpSessionBridge in :app implements TLS accept + TCP bridge to MCP
- TlsAcceptor.fromPem() factory for PEM-based cert/key loading
- McpSession.resolvePort() to get the ephemeral port after start
- Koin wiring for McpSession singleton and SessionHandler
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Three root causes prevented the wake flow test from passing:
1. TunnelForegroundService and FcmReceiver were not declared in the work
module's AndroidManifest.xml, so Android couldn't find or start them.
2. ADB broadcasts to FirebaseInstanceIdReceiver require the C2DM sender
permission, which the ADB shell (uid 2000) lacks. Added a debug-only
TestWakeReceiver that starts TunnelForegroundService without requiring
any sender permission.
3. The relay URL was hardcoded to wss:// but the test relay runs without
TLS. Added configurable relay.scheme build property (defaults to wss,
tests pass ws). The relay now injects a test DeviceIdentity when running
without TLS so /ws accepts connections. Added cleartext network security
config for debug builds.
Also fixed: TunnelForegroundService now catches connection errors instead
of crashing, and installApk() auto-uninstalls on signature mismatch.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The service had lateinit var fields that were never set, causing a crash
on FCM wake. Convert to Koin by inject() delegation and register
TunnelClient, WakelockManager, IdleTimeoutManager, and relayUrl in AppModule.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Wire SelfCertVerifier and CtLogMonitor into a WorkManager periodic task
that runs every 4 hours. Results are persisted to SharedPreferences for
UI consumption, and alerts/warnings trigger appropriate notifications.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Security: the device no longer reads relay host/port from the FCM message
payload, preventing a spoofed message from redirecting connections. The relay
URL is baked into the APK via BuildConfig at compile time.
- FCM payload now only contains {"type": "wake"} or {"type": "renew"}
- Relay URL configurable via Gradle properties: -Prelay.host=x -Prelay.port=y
- Defaults to relay.rousecontext.com:443 if no property set
- TunnelForegroundService.relayUrl injected by :app module
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add the Android service lifecycle layer that bridges FCM wakeups to
TunnelClient, manages wakelocks based on tunnel state, enforces idle
timeouts, and schedules certificate renewal via WorkManager.
Components:
- FcmDispatch/FcmReceiver: route FCM messages to service start or cert renewal
- WakelockManager: acquire during CONNECTING/ACTIVE, release otherwise
- IdleTimeoutManager: disconnect after idle period, respects battery exemption
- CertRenewalWorker: WorkManager worker, renews certs expiring within 14 days
- TunnelForegroundService: lifecycle service wiring it all together
21 unit tests covering all four test scenarios from the task spec.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>