Skip to content

Commit

Permalink
add more logging to pty
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 15, 2024
1 parent 17b7743 commit 0c6d2b4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
11 changes: 6 additions & 5 deletions apps/pty-proxy/src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const onConnect = async (ws: WebSocket, request: IncomingMessage) => {
if (agent != null) {
logger.info("Agent connected");
if (agent.resource?.id == null) {
logger.error("Agent resource id is null");
ws.close();
throw new Error("Agent resource id is null");
logger.error("Agent resource ID is null");
ws.close(1008, "Agent resource ID is null");
throw new Error("Agent resource ID is null");
}

agents.set(agent.resource.id, agent);
Expand All @@ -32,8 +32,9 @@ const onConnect = async (ws: WebSocket, request: IncomingMessage) => {
return;
}

logger.warn("Connection rejected - neither agent nor user");
ws.close();
const msg = "Neither agent nor user";
logger.warn(msg);
ws.close(1008, msg);
};

const wss = new WebSocketServer({ noServer: true });
Expand Down
1 change: 0 additions & 1 deletion apps/pty-proxy/src/controller/user-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class UserSocket {

logger.info("Found agent for session create", {
targetId: data.targetId,
agentName: agent.resource?.name,
});

createSessionSocket(data.sessionId);
Expand Down
4 changes: 3 additions & 1 deletion apps/pty-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import ms from "ms";

import { logger } from "@ctrlplane/logger";

import { env } from "./config.js";
import { addSocket } from "./routing.js";
import { app } from "./server.js";

const server = addSocket(app).listen(env.PORT, () => {
console.log(`Server is running on port ${env.PORT}`);
logger.info(`Server is running on port ${env.PORT}`);
});

const onCloseSignal = () => {
Expand Down
23 changes: 22 additions & 1 deletion apps/pty-proxy/src/routing.ts
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;
};
2 changes: 1 addition & 1 deletion apps/pty-proxy/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ app.use(express.json());
app.use(cookieParser());

// Health check endpoint
app.get("/api/proxy/health", (_req, res) => {
app.get("/api/v1/resources/proxy/health", (_req, res) => {
res.status(200).json({ status: "ok" });
});

Expand Down

0 comments on commit 0c6d2b4

Please sign in to comment.