Files
Netcatty/application/state/useMonitorRuntime.ts
T
陈大猫 c8c4b14938 feat(remote-desktop): built-in VNC & RDP support, multi-monitor/RDPDR, telemetry, and Catty Computer Use (#4463)
* feat(remote-desktop): add built-in VNC and RDP support, JumpServer RDP, AI vision, monitoring, and recordings

* fix(build): move ironrdp-web patches out of patch-package directory

* perf(remote-desktop): coalesce pointer/snip events, zero-copy VNC paint, and memoize views

* fix(remote-desktop): prevent StrictMode WS one-time token race and improve new-host protocol switch

* style(remote-desktop): align toolbar height to 36px, refine overlays, and complete zh-CN/en i18n

* fix(rdp): fallback to TLS 1.2 kRSA when Electron BoringSSL rejects Windows RDP cert KEY_USAGE_BIT_INCORRECT

* feat(remote-desktop): responsive toolbar overflow, fix RDP HiDPI scaling, add Cmd-to-Ctrl, VNC refresh, and protocol-aware keys

* feat(remote-desktop): add RDP multi-monitor/RDPDR, telemetry, AI Computer Use, VNC upgrades, and perf hardening
2026-11-26 23:46:58 +08:00

308 lines
11 KiB
TypeScript

import { hostRestrictsExtraSshChannels } from "../../domain/host";
import { useEffect, useRef, useState } from "react";
import { buildHostConnectionTestPlan } from "../../domain/hostConnectionTest";
import {
hasCompanionRdp,
hasCompanionVnc,
isRemoteDesktopProtocol,
resolveRemoteDesktopConnectionSpec,
} from "../../domain/remoteDesktop";
import { resolveEffectiveTerminalHost } from "../../domain/terminalHostResolution";
import { getEffectiveKnownHosts } from "../../infrastructure/syncHelpers";
import { netcattyBridge } from "../../infrastructure/services/netcattyBridge";
import {
useAppLockChrome,
useAppSettingsRuntime,
useAppVaultRuntime,
} from "./appRuntimeBridge";
import { useMonitorPanels } from "./monitorPanelsStorage";
import {
monitorActions,
getMonitorHistoryCursor,
isIdleMonitorSnapshot,
publishMonitorSnapshot,
publishMonitorUnavailable,
} from "./monitorRuntimeStore";
import type {
MonitorPanelDemand,
MonitorRemoteDesktopProbeOptions,
} from "../../domain/monitorRuntime";
/** Root lifetime, independent of Vault navigation. No runtime state is persisted. */
export function useMonitorRuntime() {
const panels = useMonitorPanels();
const {
hosts,
keys,
identities,
groupConfigs,
proxyProfiles,
knownHosts,
isInitialized,
} = useAppVaultRuntime();
const { terminalSettings } = useAppSettingsRuntime();
const { locked } = useAppLockChrome();
const [running, setRunning] = useState(() => new Map<string, string>());
const [retryVersion, setRetryVersion] = useState(0);
const epoch = useRef<number | null>(null);
const runtimeId = useRef<string | null>(null);
const credentials = useRef(
new Map<string, { username: string; password: string }>(),
);
useEffect(() => {
monitorActions.start = (id) =>
setRunning((prev) => new Map(prev).set(id, crypto.randomUUID()));
monitorActions.stop = (id) =>
setRunning((prev) => {
const next = new Map(prev);
next.delete(id);
return next;
});
monitorActions.stopAll = () => {
credentials.current.clear();
setRunning(new Map());
void netcattyBridge.get()?.monitorStopAll?.();
};
monitorActions.retry = (id, override) => {
if (override) credentials.current.set(id, override);
void netcattyBridge
.get()
?.monitorRetry?.(id)
.then(() => setRetryVersion((v) => v + 1));
};
const offline = () => monitorActions.stopAll();
window.addEventListener("offline", offline);
return () => {
window.removeEventListener("offline", offline);
void netcattyBridge.get()?.monitorUpdate?.([]);
};
}, []);
useEffect(() => {
if (locked || !isInitialized) {
credentials.current.clear();
setRunning(new Map());
}
}, [locked, isInitialized]);
useEffect(() => {
const bridge = netcattyBridge.get();
if (!bridge?.monitorUpdate) {
publishMonitorUnavailable();
return;
}
const demand: MonitorPanelDemand[] =
locked || !isInitialized
? []
: panels
.filter((p) => running.has(p.id))
.map((panel) => ({
id: panel.id,
runId: running.get(panel.id)!,
interval: panel.interval,
hosts: panel.hostIds.map((id) => {
const host = hosts.find((h) => h.id === id);
if (!host) return { id };
if (isRemoteDesktopProtocol(host.protocol)) {
const plan = buildHostConnectionTestPlan({
host,
hosts,
keys,
identities,
groupConfigs,
proxyProfiles,
knownHosts: getEffectiveKnownHosts(knownHosts) ?? [],
terminalSettings,
sessionId: `monitor-probe-${id}`,
bootEpoch: 0,
});
if (!plan.ok || plan.kind !== "remote-desktop") {
return { id, status: "error" as const };
}
return {
id,
kind: "probe" as const,
probe: {
protocol: plan.options.protocol,
hostname: plan.options.hostname,
port: plan.options.port,
proxy: plan.options.proxy,
jumpHosts: plan.options.jumpHosts,
bannerCheck: true,
timeoutMs: 5000,
},
};
}
if (
(host.protocol && host.protocol !== "ssh") ||
hostRestrictsExtraSshChannels(host)
)
return { id };
const effective = resolveEffectiveTerminalHost({
host,
groupConfigs,
proxyProfiles,
});
const plan = buildHostConnectionTestPlan({
host,
hosts,
keys,
identities,
groupConfigs,
proxyProfiles,
knownHosts: getEffectiveKnownHosts(knownHosts) ?? [],
terminalSettings,
sessionId: `monitor-${id}`,
bootEpoch: 0,
authOverride: credentials.current.has(id)
? { ...credentials.current.get(id), authMethod: "password" }
: null,
});
if (!plan.ok || plan.kind !== "ssh") return { id, status: "error" as const };
if (plan.needsCredentialReentry)
return { id, status: "login" as const };
const companionProbes: MonitorRemoteDesktopProbeOptions[] = [];
for (const proto of ["rdp", "vnc"] as const) {
const enabled = proto === "rdp" ? hasCompanionRdp(effective) : hasCompanionVnc(effective);
if (!enabled) continue;
const spec = resolveRemoteDesktopConnectionSpec(effective, proto, identities);
companionProbes.push({
protocol: proto,
hostname: spec.hostname,
port: spec.port,
tunnelViaHostSsh: spec.tunnelViaHostSsh,
tunnelTargetHost: spec.tunnelTargetHost,
proxy: spec.tunnelViaHostSsh ? undefined : plan.options.proxy,
bannerCheck: true,
timeoutMs: 5000,
});
}
return {
id,
kind: "ssh" as const,
options: {
...plan.options,
singleChannelSsh: hostRestrictsExtraSshChannels(effective),
sshAuthReadyTimeoutMs: 120000,
agentForwarding: effective.agentForwarding,
},
...(companionProbes.length > 0 ? { companionProbes } : {}),
};
}),
}));
const activeHostIds = new Set(
demand.flatMap((p) => p.hosts.map((h) => h.id)),
);
for (const id of credentials.current.keys())
if (!activeHostIds.has(id)) credentials.current.delete(id);
void bridge
.monitorUpdate(demand)
.then(publishMonitorSnapshot)
.catch(() => {
credentials.current.clear();
setRunning((prev) => (prev.size ? new Map() : prev));
publishMonitorUnavailable();
});
}, [
panels,
running,
hosts,
keys,
identities,
groupConfigs,
proxyProfiles,
knownHosts,
terminalSettings,
locked,
isInitialized,
retryVersion,
]);
// Poll every second while anything is monitored (here or in another window);
// otherwise only every 10 s to notice monitors started elsewhere. Pause
// renderer polling while hidden and run an immediate catch-up poll on return
// to visible (main-process sampling stays untouched).
const hasRunningPanels = running.size > 0;
useEffect(() => {
const bridge = netcattyBridge.get();
let disposed = false;
let pending = false;
let resyncOnIdle = false;
let remoteRunning = false;
let timer: ReturnType<typeof setTimeout> | null = null;
const isHidden = () => {
if (typeof document === "undefined") return false;
if (typeof document.visibilityState === "string") {
return document.visibilityState === "hidden";
}
return document.hidden === true;
};
const clearTimer = () => {
if (timer) {
clearTimeout(timer);
timer = null;
}
};
const poll = async () => {
if (disposed || pending || isHidden() || !bridge?.monitorSnapshot) return;
pending = true;
try {
const snapshot = await bridge.monitorSnapshot(getMonitorHistoryCursor());
if (disposed) return;
remoteRunning = !isIdleMonitorSnapshot(snapshot);
if (
(epoch.current !== null && epoch.current !== snapshot.epoch) ||
(runtimeId.current !== null &&
runtimeId.current !== snapshot.runtimeId)
) {
credentials.current.clear();
setRunning(new Map());
}
epoch.current = snapshot.epoch;
runtimeId.current = snapshot.runtimeId;
publishMonitorSnapshot(snapshot);
} catch {
credentials.current.clear();
setRunning((prev) => (prev.size ? new Map() : prev));
publishMonitorUnavailable();
} finally {
pending = false;
}
};
const schedule = () => {
if (disposed || isHidden()) return;
if (resyncOnIdle) {
resyncOnIdle = false;
void poll().finally(schedule);
return;
}
clearTimer();
timer = setTimeout(() => {
timer = null;
void poll().finally(schedule);
}, hasRunningPanels || remoteRunning ? 1000 : 10_000);
};
const handleVisibilityChange = () => {
if (disposed) return;
if (isHidden()) {
resyncOnIdle = false;
clearTimer();
return;
}
clearTimer();
if (pending) {
resyncOnIdle = true;
return;
}
void poll().finally(schedule);
};
const doc = typeof document !== "undefined" ? document : null;
doc?.addEventListener?.("visibilitychange", handleVisibilityChange);
if (!isHidden()) {
void poll().finally(schedule);
}
return () => {
disposed = true;
clearTimer();
doc?.removeEventListener?.("visibilitychange", handleVisibilityChange);
};
}, [hasRunningPanels]);
}