Add more logging (#32)

* Add more logging

* Refactor logging in WebRTC signaling to remove "WS" prefix for consistency
This commit is contained in:
Adam Shiervani 2025-04-09 11:33:27 +02:00 committed by GitHub
parent 27755874af
commit dc5aed27e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 8 deletions

View File

@ -75,7 +75,7 @@ async function handleDeviceSocketRequest(
// Inflight means that the device has connected, a client has connected to that device via HTTP, and they're now doing the signaling dance // Inflight means that the device has connected, a client has connected to that device via HTTP, and they're now doing the signaling dance
if (inFlight.has(device.id)) { if (inFlight.has(device.id)) {
console.log( console.log(
`[Device WS] Device ${device.id} already has an inflight client connection.`, `[Device] Device ${device.id} already has an inflight client connection.`,
); );
return socket.destroy(); return socket.destroy();
} }
@ -83,7 +83,7 @@ async function handleDeviceSocketRequest(
// Handle existing connections for this device // Handle existing connections for this device
if (activeConnections.has(device.id)) { if (activeConnections.has(device.id)) {
console.log( console.log(
`[Device WS] Device ${device.id} already connected. Terminating existing connection.`, `[Device] Device ${device.id} already connected. Terminating existing connection.`,
); );
activeConnections.get(device.id)?.[0]?.terminate(); activeConnections.get(device.id)?.[0]?.terminate();
activeConnections.delete(device.id); activeConnections.delete(device.id);
@ -107,26 +107,26 @@ async function authenticateDeviceRequest(req: IncomingMessage) {
const secretToken = authHeader?.split(" ")?.[1]; const secretToken = authHeader?.split(" ")?.[1];
if (!secretToken) { if (!secretToken) {
console.log("[Device WS] No authorization header provided."); console.log("[Device] No authorization header provided.");
return null; return null;
} }
try { try {
const device = await prisma.device.findFirst({ where: { secretToken } }); const device = await prisma.device.findFirst({ where: { secretToken } });
if (!device) { if (!device) {
console.log("[Device WS] Invalid secret token provided."); console.log("[Device] Invalid secret token provided.");
return null; return null;
} }
const id = req.headers["x-device-id"] as string; const id = req.headers["x-device-id"] as string;
if (!id || id !== device.id) { if (!id || id !== device.id) {
console.log("[Device WS] Invalid device ID or ID/token mismatch."); console.log("[Device] Invalid device ID or ID/token mismatch.");
return null; return null;
} }
return device; return device;
} catch (error) { } catch (error) {
console.error("[Device WS] Error authenticating device:", error); console.error("[Device] Error authenticating device:", error);
return null; return null;
} }
} }
@ -143,7 +143,7 @@ function setupDeviceWebSocket(deviceWs: WebSocket, device: Device, req: Incoming
// Store the connection // Store the connection
activeConnections.set(id, [deviceWs, `${ip}`, deviceVersion || null]); activeConnections.set(id, [deviceWs, `${ip}`, deviceVersion || null]);
console.log( console.log(
`[Device WS] New connection for device ${id}, with version ${deviceVersion || "unknown"}`, `[Device] New connection for device ${id}, with version ${deviceVersion || "unknown"}`,
); );
// Setup ping/pong for connection health checks // Setup ping/pong for connection health checks
@ -157,7 +157,7 @@ function setupDeviceWebSocket(deviceWs: WebSocket, device: Device, req: Incoming
const checkAliveInterval = setInterval(function checkAlive() { const checkAliveInterval = setInterval(function checkAlive() {
// @ts-ignore // @ts-ignore
if (deviceWs.isAlive === false) { if (deviceWs.isAlive === false) {
console.log("[Device WS] WS is not alive. Terminating connection."); console.log(`[Device] ${id} is not alive. Terminating connection.`);
return deviceWs.terminate(); return deviceWs.terminate();
} }
// @ts-ignore // @ts-ignore

View File

@ -71,6 +71,7 @@ export const CreateSession = async (req: express.Request, res: express.Response)
); );
}); });
console.log("[CreateSession] got response from device", id);
return res.json(JSON.parse(resp.data)); return res.json(JSON.parse(resp.data));
} catch (e) { } catch (e) {
console.log(`Error sending data to kvm with ${id}`, e); console.log(`Error sending data to kvm with ${id}`, e);