/** * WebSocket Tunnel Client * * TypeScript port of the Rust tunnel_client.rs. * Connects to the TunnelSession Durable Object via WebSocket, * receives HTTP requests, forwards them to a local server, and * sends responses back through the tunnel. * * Implements the same two-level keepalive strategy as the desktop app: * - Protocol pings every 30s (keep Cloudflare edge proxy alive) * - App-level JSON pings every 120s (wake DO from hibernation) * * Auto-reconnects with exponential backoff on disconnect. */ import { RequestEntry } from './request-log.js'; interface TunnelClientConfig { connectUrl: string; targetHost: string; targetPort: number; /** * Capture every request to a RequestEntry. Forwards to the local port when * targetPort > 0 (tunnel --log), otherwise responds 200 immediately (webhook). */ captureMode?: boolean; /** * Require HTTP Basic auth ("user:pass") on every request. Enforced here in * the client, before anything is forwarded or captured - the tunnel server * never sees the credentials. */ basicAuth?: string; /** * Inbound signature verification (webhook only): the server checks every * arriving request against the provider's scheme and stamps the verdict * (X-OtterKit-Verified header + capture badge). Declared on every connect; * unset clears any prior config on the session. */ verify?: { provider: string; secret: string; toleranceSec?: number; }; onConnected?: (publicUrl: string) => void; onDisconnected?: () => void; onError?: (error: string) => void; /** Server ended the session (auto-stop TTL, out of credits). No reconnect. */ onClosing?: (reason: string) => void; onRequest?: (method: string, path: string, status: number, durationMs: number) => void; onCapture?: (entry: RequestEntry) => void; /** Standby replay starting: how many requests were buffered/dropped while away. */ onStandbyReplay?: (buffered: number, dropped: number) => void; } export declare class TunnelClient { private config; private ws; private protocolPingTimer; private appPingTimer; private lastProtocolPingTime; private reconnectAttempts; private currentBackoff; private isShuttingDown; private isConnected; constructor(config: TunnelClientConfig); /** * Connect to the tunnel server */ connect(): Promise; /** * Gracefully disconnect */ disconnect(): Promise; private handleMessage; /** * Forward an HTTP request from the tunnel to the local server, * or capture it in capture mode. */ private handleRequest; /** * Validate Basic auth when configured. Constant-time compare so the * credentials can't be probed byte-by-byte via response timing. */ private checkAuth; /** * Forward a request to the local server and send the response back through the tunnel. * Shared by tunnel mode and capture mode. */ private forwardToLocal; /** Declare verification intent to the server (sent on every connect). */ private sendVerifyConfig; /** Import a server-answered capture (replay or live mirror) into the log. */ private handleBufferedReplay; /** * Capture mode: record the request, then forward (targetPort > 0) or return 200 (webhook). */ private handleCapture; /** * Two-level keepalive strategy: * 1. Protocol pings every 30s - keep Cloudflare edge proxy alive (CF ~100s idle timeout) * 2. App-level JSON pings every 120s - wake DO from hibernation */ private startKeepalive; private stopKeepalive; private send; /** * Handle disconnect with exponential backoff reconnection */ private handleDisconnect; } export {};