mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-07 15:52:52 +03:00
fix: auto-start WS server in-process and change default port to 20132 (#6072)
* feat: change default LIVE_WS_PORT from 20129 to 20132 Update the default WebSocket port for the live dashboard server from 20129 to 20132 across all configuration files, documentation, code comments, and tests. Also consolidate OMNIROUTE_DISABLE_LIVE_WS and OMNIROUTE_ENABLE_LIVE_WS into a single OMNIROUTE_ENABLE_LIVE_WS flag. Wire the live WebSocket server to start in-process via instrumentation-node.ts. * feat: clarify NEXT_PUBLIC_LIVE_WS_PUBLIC_URL path usage and derive upgrade path from URL Update .env.example and ENVIRONMENT.md to document that the pathname portion of NEXT_PUBLIC_LIVE_WS_PUBLIC_URL (e.g. /live-ws) is used as the WebSocket upgrade path by the dev proxy, handshake response, and client connection logic. Extract deriveLiveWsPath() into shared/utils/wsPath.ts and wire it through: - src/app/api/v1/ws/route.ts — handshake response path field - src/hooks/useLiveDashboard.ts — build * fix: use the standard URL API to safely parse and update the effectiveWsUrl * build(docker): expose live WebSocket server port and configure CORS origins Add LIVE_WS_PORT (20132), LIVE_WS_HOST (0.0.0.0), and LIVE_WS_ALLOWED_ORIGINS environment variables to all Docker Compose profiles and expose the WebSocket port mapping. Prevent infinite self-loop in standalone-server-ws.mjs by skipping proxy when the server itself is running on the LiveWS port. * docs(env): fix comment formatting for HOST and HOSTNAME variables --------- Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
a42f993645
commit
45310f202d
@@ -19,9 +19,7 @@ const { wrapRequestListenerWithHeadResponseGuard } = headResponseGuard;
|
||||
// TLS). Absent or misconfigured → null → identical plain-HTTP behavior as before.
|
||||
const tlsOptions = resolveTlsOptions(process.env);
|
||||
if (tlsOptions) {
|
||||
console.log(
|
||||
`[omniroute][tls] HTTPS enabled — terminating TLS with cert=${tlsOptions.certPath}`
|
||||
);
|
||||
console.log(`[omniroute][tls] HTTPS enabled — terminating TLS with cert=${tlsOptions.certPath}`);
|
||||
}
|
||||
|
||||
process.env.OMNIROUTE_WS_BRIDGE_SECRET ||= randomUUID();
|
||||
@@ -51,8 +49,23 @@ function getProxy(server) {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
function deriveLiveWsPath() {
|
||||
const publicUrl = process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL;
|
||||
if (!publicUrl) return "/live-ws";
|
||||
if (!publicUrl.startsWith("ws://") && !publicUrl.startsWith("wss://")) return "/live-ws";
|
||||
try {
|
||||
const parsed = new URL(publicUrl);
|
||||
const pathname = parsed.pathname;
|
||||
return pathname && pathname !== "/" ? pathname : "/live-ws";
|
||||
} catch {
|
||||
return "/live-ws";
|
||||
}
|
||||
}
|
||||
|
||||
const LIVE_WS_PATH = deriveLiveWsPath();
|
||||
|
||||
function proxyLiveWs(req, socket, head) {
|
||||
const targetPort = parseInt(process.env.LIVE_WS_PORT || "20129", 10);
|
||||
const targetPort = parseInt(process.env.LIVE_WS_PORT || "20132", 10);
|
||||
const targetSocket = net.connect(targetPort, "127.0.0.1", () => {
|
||||
let rawRequest = `${req.method} ${req.url} HTTP/${req.httpVersion}\r\n`;
|
||||
for (const [key, val] of Object.entries(req.headers)) {
|
||||
@@ -76,8 +89,16 @@ function proxyLiveWs(req, socket, head) {
|
||||
function wrapUpgradeListener(server, listener) {
|
||||
return async function responsesWsAwareUpgrade(req, socket, head) {
|
||||
try {
|
||||
// If this server IS the LiveWS server (port 20132), the ws library's
|
||||
// own upgrade handler should process the request directly — proxying
|
||||
// /live-ws back to 127.0.0.1:20132 would create an infinite self-loop.
|
||||
const liveWsPort = parseInt(process.env.LIVE_WS_PORT || "20132", 10);
|
||||
if (getPort(server) === liveWsPort) {
|
||||
return listener.call(this, req, socket, head);
|
||||
}
|
||||
|
||||
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
||||
if (url.pathname === "/live-ws" || url.pathname.startsWith("/live-ws")) {
|
||||
if (url.pathname === LIVE_WS_PATH || url.pathname.startsWith(LIVE_WS_PATH + "/")) {
|
||||
proxyLiveWs(req, socket, head);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user