-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
32 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,49 @@ | ||
import { createServer } from "node:http"; | ||
import type { Express } from "express"; | ||
|
||
import { logger } from "@ctrlplane/logger"; | ||
|
||
import { controllerOnUpgrade } from "./controller/index.js"; | ||
import { sessionOnUpgrade } from "./sessions/index.js"; | ||
|
||
export const addSocket = (expressApp: Express) => { | ||
const server = createServer(expressApp); | ||
logger.info("Created HTTP server for WebSocket upgrades"); | ||
|
||
server.on("upgrade", (request, socket, head) => { | ||
logger.debug("Received WebSocket upgrade request", { | ||
url: request.url, | ||
headers: request.headers, | ||
}); | ||
|
||
if (request.url == null) { | ||
logger.warn("WebSocket upgrade rejected - no URL provided"); | ||
socket.destroy(); | ||
return; | ||
} | ||
|
||
const { pathname } = new URL(request.url, "ws://base.ws"); | ||
logger.debug("Processing WebSocket upgrade path", { pathname }); | ||
|
||
if (pathname.startsWith("/api/v1/resources/proxy/session")) { | ||
logger.info("Upgrading WebSocket connection for session proxy", { | ||
pathname, | ||
}); | ||
sessionOnUpgrade(request, socket, head); | ||
return; | ||
} | ||
|
||
if (pathname.startsWith("/api/v1/resources/proxy/controller")) { | ||
logger.info("Upgrading WebSocket connection for controller", { | ||
pathname, | ||
}); | ||
controllerOnUpgrade(request, socket, head); | ||
return; | ||
} | ||
socket.destroy(); | ||
|
||
logger.warn("WebSocket upgrade rejected - invalid path", { pathname }); | ||
socket.destroy(new Error("Incorrect path for WebSocket upgrade")); | ||
}); | ||
|
||
return server; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters