File size: 14,560 Bytes
6111b2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | /**
* Live Dashboard WebSocket Server
*
* Separate process (runs alongside Next.js on port 20129).
* Forwards EventBus events to subscribed dashboard clients.
*
* Protocol:
* Client β Server: { type: "subscribe", channels: ["requests", "combo"] }
* Server β Client: { type: "event", channel: "requests", event: "request.started", data: {...} }
* Client β Server: { type: "ping" }
* Server β Client: { type: "pong" }
* Server β Client: { type: "welcome", version, sessionId, channels, backlog }
* Server β Client: { type: "error", code, message }
*/
import { WebSocketServer, WebSocket } from "ws";
import { createServer } from "http";
import { randomUUID } from "crypto";
// ββ Types βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
import type { WsClientMessage, WsServerMessage, WsAuthResult } from "./types";
import { emit, on, onAny, getEventHistory, type HistoryEntry } from "@/lib/events/eventBus";
import type { DashboardEventName, DashboardEventMap, DashboardChannel } from "@/lib/events/types";
import { CHANNEL_EVENTS, getChannelForEvent } from "@/lib/events/types";
// ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const DEFAULT_PORT = 20129;
// Loopback by default. Opt-in to LAN exposure via LIVE_WS_HOST=0.0.0.0 β the
// caller is then responsible for fronting it with a TLS terminator + origin
// allow-list. Mirrors the route guard "local-only by default" posture.
const DEFAULT_HOST = "127.0.0.1";
const HEARTBEAT_INTERVAL_MS = 15_000;
const HEARTBEAT_TIMEOUT_MS = 35_000;
const MAX_CLIENTS = 500;
const MAX_EVENTS_PER_SECOND = 100;
/**
* Origins allowed to open a WebSocket. Defaults to the loopback dashboard
* origins; admins can extend via LIVE_WS_ALLOWED_ORIGINS (comma-separated).
*
* WS does not honour CORS β a malicious page on origin X can otherwise open
* a WebSocket to our server and ride the user's API key (if it lives in a
* cookie or is reachable through the page). Browsers DO send the Origin
* header on the WS upgrade, so checking it server-side is the standard
* mitigation. Non-browser clients (CLI, MCP) omit Origin, which we accept.
*/
function buildAllowedOrigins(): Set<string> {
const base = [`http://127.0.0.1:20128`, `http://localhost:20128`, `http://[::1]:20128`];
const extra = (process.env.LIVE_WS_ALLOWED_ORIGINS || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
return new Set([...base, ...extra]);
}
const ALLOWED_ORIGINS = buildAllowedOrigins();
function isOriginAllowed(origin: string | undefined): boolean {
// Non-browser client (curl, native ws, MCP) β Origin header is omitted by
// spec. Allow only when the upstream listener is bound to loopback; if the
// operator opted into LAN exposure we require an explicit Origin.
if (!origin) {
const host = process.env.LIVE_WS_HOST || DEFAULT_HOST;
return host === "127.0.0.1" || host === "::1" || host === "localhost";
}
return ALLOWED_ORIGINS.has(origin);
}
// ββ Client State ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
interface ClientState {
ws: WebSocket;
sessionId: string;
subscribedChannels: Set<DashboardChannel>;
lastActivity: number;
/** Per-second rate limit counter */
eventCounter: number;
eventCounterReset: number;
/** Current IP for rate limiting */
remoteAddress: string;
}
const clients = new Map<string, ClientState>();
let eventHistoryBacklog: HistoryEntry[] = [];
const BACKLOG_MAX = 500;
// ββ Auth ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function authorizeConnection(request: import("http").IncomingMessage): Promise<WsAuthResult> {
const sessionId = randomUUID().slice(0, 8);
// Token MUST come from the Authorization header (or X-Live-WS-Token).
// Query-string tokens leak into access logs, browser history, and Referer
// headers β a single screenshot of the URL bar exposes the API key.
const token = extractBearerToken(request) || extractAltTokenHeader(request);
if (!token) {
return { authorized: false, sessionId, error: "Missing token" };
}
try {
// Validate API key via the existing auth system
const { extractApiKey, isValidApiKey } = await import("../services/auth");
const apiKey = extractApiKey({ headers: { authorization: `Bearer ${token}` } } as any);
if (!apiKey || !isValidApiKey(apiKey)) {
return { authorized: false, sessionId, error: "Invalid API key" };
}
return { authorized: true, sessionId };
} catch {
return { authorized: false, sessionId, error: "Auth system unavailable" };
}
}
function extractAltTokenHeader(request: import("http").IncomingMessage): string | null {
const raw = request.headers["x-live-ws-token"];
if (Array.isArray(raw)) return raw[0] || null;
return typeof raw === "string" ? raw : null;
}
function extractBearerToken(request: import("http").IncomingMessage): string | null {
const auth = request.headers["authorization"];
if (!auth || typeof auth !== "string") return null;
const match = auth.match(/^Bearer\s+(.+)$/i);
return match?.[1] || null;
}
// ββ Protocol Handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function handleMessage(clientId: string, raw: string): void {
const client = clients.get(clientId);
if (!client) return;
// Rate limiting
const now = Date.now();
if (now - client.eventCounterReset > 1000) {
client.eventCounter = 0;
client.eventCounterReset = now;
}
client.eventCounter++;
if (client.eventCounter > MAX_EVENTS_PER_SECOND) {
sendTo(client.ws, { type: "error", code: "RATE_LIMITED", message: "Too many messages" });
return;
}
let msg: WsClientMessage;
try {
msg = JSON.parse(raw);
} catch {
sendTo(client.ws, { type: "error", code: "PARSE_ERROR", message: "Invalid JSON" });
return;
}
client.lastActivity = now;
switch (msg.type) {
case "subscribe": {
client.subscribedChannels = new Set(msg.channels);
// Send buffered events that match subscribed channels
const relevantHistory = eventHistoryBacklog.filter((h) => {
const ch = getChannelForEvent(h.event as DashboardEventName);
return ch && msg.channels.includes(ch);
});
sendTo(client.ws, {
type: "welcome",
version: "1.0.0",
sessionId: client.sessionId,
serverTime: now,
channels: msg.channels,
backlog: relevantHistory.length,
data: relevantHistory.map((h) => ({
event: h.event,
channel: getChannelForEvent(h.event as DashboardEventName),
data: h.payload,
timestamp: h.timestamp,
})),
} as any);
break;
}
case "ping":
sendTo(client.ws, { type: "pong" } as WsServerMessage);
break;
}
}
// ββ Send ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function sendTo(ws: WebSocket, msg: WsServerMessage | Record<string, unknown>): void {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(msg));
}
}
// ββ Event Bus β WebSocket Bridge ββββββββββββββββββββββββββββββββββββββββββ
function subscribeToEventBus(): () => void {
return onAny((event: DashboardEventName, payload: unknown) => {
const channel = getChannelForEvent(event);
if (!channel) return;
// Store in backlog
eventHistoryBacklog.push({ event, payload, timestamp: Date.now() });
if (eventHistoryBacklog.length > BACKLOG_MAX) {
eventHistoryBacklog.shift();
}
// Forward to subscribed clients
const msg: WsEventMessage = {
type: "event",
channel,
event,
data: payload,
};
for (const [clientId, client] of clients) {
if (client.ws.readyState !== WebSocket.OPEN) {
clients.delete(clientId);
continue;
}
if (client.subscribedChannels.has(channel)) {
sendTo(client.ws, msg);
}
}
});
}
// ββ Heartbeat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function startHeartbeat(server: WebSocketServer): void {
const interval = setInterval(() => {
const now = Date.now();
for (const [clientId, client] of clients) {
if (client.ws.readyState !== WebSocket.OPEN) {
clients.delete(clientId);
continue;
}
// Check heartbeat timeout
if (now - client.lastActivity > HEARTBEAT_TIMEOUT_MS) {
client.ws.terminate();
clients.delete(clientId);
continue;
}
// Send ping
sendTo(client.ws, { type: "pong" } as WsServerMessage);
}
}, HEARTBEAT_INTERVAL_MS);
server.on("close", () => clearInterval(interval));
}
// ββ Server Start ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Start the live dashboard WebSocket server.
*
* Bound to 127.0.0.1 by default. Set LIVE_WS_HOST=0.0.0.0 to expose on the
* LAN β the caller is then responsible for fronting it with TLS + an Origin
* allow-list via LIVE_WS_ALLOWED_ORIGINS.
*/
export async function startLiveDashboardServer(
port = DEFAULT_PORT,
host = DEFAULT_HOST
): Promise<import("http").Server> {
const server = createServer();
const wss = new WebSocketServer({ server });
// Subscribe to EventBus
const unsubscribe = subscribeToEventBus();
wss.on("connection", async (ws, request) => {
// Origin check β browsers always send Origin on the WS upgrade; reject
// unknown origins to stop drive-by cross-origin WebSocket from a victim
// page. Non-browser clients (CLI / MCP) omit Origin and are accepted
// only when bound to loopback (see isOriginAllowed).
const origin = request.headers["origin"];
const originStr = Array.isArray(origin) ? origin[0] : origin;
if (!isOriginAllowed(originStr)) {
sendTo(ws, { type: "error", code: "FORBIDDEN_ORIGIN", message: "Origin not allowed" });
ws.close(4003, "Forbidden origin");
return;
}
// Enforce max clients
if (clients.size >= MAX_CLIENTS) {
sendTo(ws, { type: "error", code: "SERVER_FULL", message: "Max clients reached" });
ws.close(1013, "Server full");
return;
}
// Authorize
const auth = await authorizeConnection(request);
if (!auth.authorized) {
sendTo(ws, { type: "error", code: "UNAUTHORIZED", message: auth.error || "Unauthorized" });
ws.close(4001, "Unauthorized");
return;
}
const clientId = auth.sessionId;
const client: ClientState = {
ws,
sessionId: clientId,
subscribedChannels: new Set(),
lastActivity: Date.now(),
eventCounter: 0,
eventCounterReset: Date.now(),
remoteAddress: request.socket?.remoteAddress || "unknown",
};
clients.set(clientId, client);
// Constant format string + %s args β keeps clientId / remoteAddress out
// of the format slot so a malicious value cannot forge log lines via
// injected format specifiers (CWE-134).
console.log(
"[LiveWS] Client connected: %s (%s) [%d total]",
clientId,
client.remoteAddress,
clients.size
);
// Handle messages
ws.on("message", (data) => {
handleMessage(clientId, data.toString());
});
// Handle close
ws.on("close", () => {
clients.delete(clientId);
console.log("[LiveWS] Client disconnected: %s [%d remaining]", clientId, clients.size);
});
// Handle errors
ws.on("error", (err) => {
console.error("[LiveWS] Client error %s: %s", clientId, err.message);
clients.delete(clientId);
});
});
// Heartbeat
startHeartbeat(wss);
// Cleanup on close
wss.on("close", () => {
unsubscribe();
clients.clear();
});
return new Promise((resolve) => {
server.listen(port, host, () => {
console.log("[LiveWS] Dashboard WebSocket server listening on %s:%d", host, port);
resolve(server);
});
});
}
// ββ Auto-start on import (opt-in) ββββββββββββββββββββββββββββββββββββββββ
//
// Default: OFF. The live dashboard WebSocket is an opt-in feature β operators
// who want it must set OMNIROUTE_ENABLE_LIVE_WS=1 (or "true"). This avoids
// silently opening a network listener on every Next.js boot.
//
// Build/test environments never auto-start regardless of the flag.
function isBuildOrTest(): boolean {
return (
process.env.NEXT_PHASE === "phase-production-build" ||
process.env.NODE_ENV === "test" ||
process.env.VITEST !== undefined ||
process.argv.some((arg) => arg.includes("test"))
);
}
function isLiveWsEnabled(): boolean {
const v = process.env.OMNIROUTE_ENABLE_LIVE_WS;
return v === "1" || v === "true";
}
if (!isBuildOrTest() && isLiveWsEnabled()) {
const port = parseInt(process.env.LIVE_WS_PORT || String(DEFAULT_PORT), 10);
const host = process.env.LIVE_WS_HOST || DEFAULT_HOST;
startLiveDashboardServer(port, host).catch((err) => {
console.error("[LiveWS] Failed to start: %s", err instanceof Error ? err.message : String(err));
});
}
|