mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 10:53:56 +00:00
423 lines
14 KiB
TypeScript
423 lines
14 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import type { GroupConfig, Host, Identity, KnownHost, ProxyProfile, SSHKey, TerminalSettings } from "../../domain/models";
|
|
import type { HostKeyInfo } from "../../domain/hostKey";
|
|
import { toHostKeyInfo } from "../../domain/hostKey";
|
|
import { createKnownHostFromHostKeyInfo } from "../../domain/knownHosts";
|
|
import {
|
|
buildHostConnectionTestPlan,
|
|
formatConnectionTestProgressLog,
|
|
resolveHostConnectionTestDeadlineMs,
|
|
resolveRemoteDesktopConnectionTestDeadlineMs,
|
|
type HostConnectionTestAuthOverride,
|
|
type RemoteDesktopProbeResult,
|
|
} from "../../domain/hostConnectionTest";
|
|
import {
|
|
isRemoteDesktopProtocol,
|
|
resolveRemoteDesktopConnectionSpec,
|
|
} from "../../domain/remoteDesktop";
|
|
import { resolveEffectiveTerminalHost } from "../../domain/terminalHostResolution";
|
|
import { getKnownRemoteDesktopFingerprints } from "../../components/remoteDesktop/knownRemoteDesktopCerts";
|
|
import { useTerminalBackend } from "./useTerminalBackend";
|
|
|
|
export type ChainProgress = {
|
|
currentHop: number;
|
|
totalHops: number;
|
|
currentHostLabel: string;
|
|
connectionPhase: string;
|
|
} | null;
|
|
|
|
export type HostKeyVerificationState = {
|
|
requestId: string;
|
|
hostKeyInfo: HostKeyInfo;
|
|
};
|
|
|
|
export type HostConnectionTestStatus = "idle" | "connecting" | "connected" | "disconnected";
|
|
|
|
export type HostConnectionTestState = {
|
|
status: HostConnectionTestStatus;
|
|
error: string | null;
|
|
needsAuth: boolean;
|
|
progressValue: number;
|
|
chainProgress: ChainProgress;
|
|
progressLogs: string[];
|
|
hostKeyVerification: HostKeyVerificationState | null;
|
|
timeLeft: number;
|
|
isAwaitingUserInput: boolean;
|
|
isCancelling: boolean;
|
|
probeResult: RemoteDesktopProbeResult | null;
|
|
activeProtocol: "ssh" | "rdp" | "vnc";
|
|
};
|
|
|
|
export type HostConnectionTestInput = {
|
|
host: Host;
|
|
hosts: Host[];
|
|
keys: SSHKey[];
|
|
identities?: Identity[];
|
|
knownHosts?: KnownHost[];
|
|
groupConfigs?: GroupConfig[];
|
|
proxyProfiles?: ProxyProfile[];
|
|
terminalSettings?: TerminalSettings;
|
|
onAddKnownHost?: (knownHost: KnownHost) => void;
|
|
};
|
|
|
|
const messageOf = (err: unknown): string => (err instanceof Error ? err.message : String(err));
|
|
|
|
export function useHostConnectionTest(input: HostConnectionTestInput) {
|
|
const {
|
|
host,
|
|
hosts,
|
|
keys,
|
|
identities = [],
|
|
knownHosts,
|
|
groupConfigs = [],
|
|
proxyProfiles = [],
|
|
terminalSettings,
|
|
onAddKnownHost,
|
|
} = input;
|
|
const backend = useTerminalBackend();
|
|
|
|
const [status, setStatus] = useState<HostConnectionTestStatus>("idle");
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [needsAuth, setNeedsAuth] = useState(false);
|
|
const [progressValue, setProgressValue] = useState(0);
|
|
const [chainProgress, setChainProgress] = useState<ChainProgress>(null);
|
|
const [progressLogs, setProgressLogs] = useState<string[]>([]);
|
|
const [hostKeyVerification, setHostKeyVerification] = useState<HostKeyVerificationState | null>(null);
|
|
const [timeLeft, setTimeLeft] = useState(0);
|
|
const [isAwaitingUserInput, setIsAwaitingUserInput] = useState(false);
|
|
const [isCancelling, setIsCancelling] = useState(false);
|
|
const [probeResult, setProbeResult] = useState<RemoteDesktopProbeResult | null>(null);
|
|
const [activeProtocol, setActiveProtocol] = useState<"ssh" | "rdp" | "vnc">(
|
|
isRemoteDesktopProtocol(host.protocol) ? host.protocol : "ssh",
|
|
);
|
|
|
|
const sessionIdRef = useRef<string | null>(null);
|
|
const activeKindRef = useRef<"ssh" | "remote-desktop">("ssh");
|
|
const lastProtocolOverrideRef = useRef<"ssh" | "rdp" | "vnc" | undefined>(undefined);
|
|
const bootEpochRef = useRef(0);
|
|
const pendingHostKeyRequestIdRef = useRef<string | null>(null);
|
|
const disposedRef = useRef(false);
|
|
// Deadline of the active attempt, derived from the options actually sent to
|
|
// the bridge so the countdown matches the probe's real timeouts.
|
|
const deadlineMsRef = useRef(0);
|
|
|
|
const resetState = useCallback(() => {
|
|
setError(null);
|
|
setNeedsAuth(false);
|
|
setProgressValue(0);
|
|
setChainProgress(null);
|
|
setProgressLogs([]);
|
|
setHostKeyVerification(null);
|
|
setIsAwaitingUserInput(false);
|
|
setIsCancelling(false);
|
|
setTimeLeft(0);
|
|
setProbeResult(null);
|
|
pendingHostKeyRequestIdRef.current = null;
|
|
}, []);
|
|
|
|
const clearSession = useCallback(() => {
|
|
const sessionId = sessionIdRef.current;
|
|
const kind = activeKindRef.current;
|
|
sessionIdRef.current = null;
|
|
if (sessionId && !disposedRef.current) {
|
|
if (kind === "remote-desktop") {
|
|
void backend.cancelRemoteDesktopTestConnection(sessionId).catch(() => {});
|
|
} else {
|
|
void backend.cancelTestConnection(sessionId).catch(() => {});
|
|
}
|
|
}
|
|
}, [backend]);
|
|
|
|
// Unmount cleanup. The per-attempt subscriptions below dispose themselves via
|
|
// their own effect returns; nothing here touches them.
|
|
useEffect(() => {
|
|
disposedRef.current = false;
|
|
return () => {
|
|
disposedRef.current = true;
|
|
};
|
|
}, []);
|
|
|
|
// Subscribe to chain progress for the active attempt.
|
|
useEffect(() => {
|
|
const unsub = backend.onChainProgress?.((sessionId, hop, total, label, phase, phaseError) => {
|
|
if (sessionId !== sessionIdRef.current) return;
|
|
if (total > 1) {
|
|
setChainProgress({
|
|
currentHop: hop,
|
|
totalHops: total,
|
|
currentHostLabel: label,
|
|
connectionPhase: phase,
|
|
});
|
|
}
|
|
setProgressLogs((prev) => [
|
|
...prev,
|
|
formatConnectionTestProgressLog({ hop, total, label, phase, error: phaseError }),
|
|
]);
|
|
const hopProgress = (hop / total) * 80 + 10;
|
|
setProgressValue((prev) => Math.max(prev, Math.min(95, hopProgress)));
|
|
setIsAwaitingUserInput(phase === "auth-attempt" && phaseError === "waiting for user input...");
|
|
});
|
|
return () => unsub?.();
|
|
}, [backend]);
|
|
|
|
// Subscribe to host-key verification for the active attempt.
|
|
useEffect(() => {
|
|
const unsub = backend.onHostKeyVerification?.((request) => {
|
|
if (request.sessionId !== sessionIdRef.current) return;
|
|
pendingHostKeyRequestIdRef.current = request.requestId;
|
|
setHostKeyVerification({
|
|
requestId: request.requestId,
|
|
hostKeyInfo: toHostKeyInfo(request),
|
|
});
|
|
});
|
|
return () => unsub?.();
|
|
}, [backend]);
|
|
|
|
const respondHostKey = useCallback(
|
|
(requestId: string, accept: boolean, addToKnownHosts: boolean, hostKeyInfo: HostKeyInfo) => {
|
|
if (accept && addToKnownHosts && onAddKnownHost) {
|
|
onAddKnownHost(createKnownHostFromHostKeyInfo(hostKeyInfo));
|
|
}
|
|
void backend.respondHostKeyVerification(requestId, accept, addToKnownHosts);
|
|
pendingHostKeyRequestIdRef.current = null;
|
|
setHostKeyVerification(null);
|
|
},
|
|
[backend, onAddKnownHost],
|
|
);
|
|
|
|
const handleHostKeyContinue = useCallback(() => {
|
|
if (!hostKeyVerification) return;
|
|
respondHostKey(hostKeyVerification.requestId, true, false, hostKeyVerification.hostKeyInfo);
|
|
}, [hostKeyVerification, respondHostKey]);
|
|
|
|
const handleHostKeyAddAndContinue = useCallback(() => {
|
|
if (!hostKeyVerification) return;
|
|
respondHostKey(hostKeyVerification.requestId, true, true, hostKeyVerification.hostKeyInfo);
|
|
}, [hostKeyVerification, respondHostKey]);
|
|
|
|
const handleHostKeyClose = useCallback(() => {
|
|
if (!hostKeyVerification) return;
|
|
respondHostKey(hostKeyVerification.requestId, false, false, hostKeyVerification.hostKeyInfo);
|
|
}, [hostKeyVerification, respondHostKey]);
|
|
|
|
const startTest = useCallback(
|
|
async (
|
|
authOverride?: HostConnectionTestAuthOverride | null,
|
|
protocolOverride?: "ssh" | "rdp" | "vnc",
|
|
) => {
|
|
clearSession();
|
|
resetState();
|
|
|
|
lastProtocolOverrideRef.current = protocolOverride;
|
|
const targetProtocol: "ssh" | "rdp" | "vnc" =
|
|
protocolOverride ?? (isRemoteDesktopProtocol(host.protocol) ? host.protocol : "ssh");
|
|
setActiveProtocol(targetProtocol);
|
|
|
|
let knownRemoteDesktopFingerprints: string[] | undefined;
|
|
if (targetProtocol === "rdp" || targetProtocol === "vnc") {
|
|
const effective = resolveEffectiveTerminalHost({
|
|
host,
|
|
groupConfigs,
|
|
proxyProfiles,
|
|
});
|
|
const spec = resolveRemoteDesktopConnectionSpec(effective, targetProtocol, identities);
|
|
const certHost = spec.hostHostname || spec.hostname;
|
|
if (certHost) {
|
|
knownRemoteDesktopFingerprints = getKnownRemoteDesktopFingerprints(
|
|
targetProtocol,
|
|
certHost,
|
|
spec.port,
|
|
);
|
|
}
|
|
}
|
|
|
|
const sessionId = crypto.randomUUID();
|
|
const bootEpoch = (bootEpochRef.current += 1);
|
|
sessionIdRef.current = sessionId;
|
|
|
|
const plan = buildHostConnectionTestPlan({
|
|
host,
|
|
hosts,
|
|
keys,
|
|
identities,
|
|
knownHosts,
|
|
groupConfigs,
|
|
proxyProfiles,
|
|
terminalSettings: terminalSettings
|
|
? {
|
|
keepaliveInterval: terminalSettings.keepaliveInterval,
|
|
keepaliveCountMax: terminalSettings.keepaliveCountMax,
|
|
verifyHostKeys: terminalSettings.verifyHostKeys,
|
|
}
|
|
: undefined,
|
|
sessionId,
|
|
bootEpoch,
|
|
authOverride,
|
|
protocolOverride,
|
|
knownRemoteDesktopFingerprints,
|
|
});
|
|
|
|
if (plan.ok === false) {
|
|
setError(plan.error);
|
|
setStatus("disconnected");
|
|
return;
|
|
}
|
|
|
|
if (plan.kind === "remote-desktop") {
|
|
activeKindRef.current = "remote-desktop";
|
|
deadlineMsRef.current = resolveRemoteDesktopConnectionTestDeadlineMs(plan.options);
|
|
setStatus("connecting");
|
|
setIsAwaitingUserInput(false);
|
|
|
|
try {
|
|
const result = await backend.testRemoteDesktopConnection(plan.options);
|
|
if (disposedRef.current || sessionIdRef.current !== sessionId) return;
|
|
setProbeResult(result);
|
|
setChainProgress(null);
|
|
if (result.ok) {
|
|
setProgressValue(100);
|
|
setStatus("connected");
|
|
} else {
|
|
setError(result.error || "Connection test failed");
|
|
setStatus("disconnected");
|
|
}
|
|
} catch (err) {
|
|
if (disposedRef.current || sessionIdRef.current !== sessionId) return;
|
|
setError(messageOf(err));
|
|
setStatus("disconnected");
|
|
}
|
|
return;
|
|
}
|
|
|
|
activeKindRef.current = "ssh";
|
|
if (plan.needsCredentialReentry) {
|
|
setNeedsAuth(true);
|
|
setStatus("connecting");
|
|
return;
|
|
}
|
|
|
|
deadlineMsRef.current = resolveHostConnectionTestDeadlineMs(plan.options);
|
|
setStatus("connecting");
|
|
setIsAwaitingUserInput(false);
|
|
|
|
try {
|
|
await backend.testConnection(plan.options);
|
|
if (disposedRef.current || sessionIdRef.current !== sessionId) return;
|
|
setProgressValue(100);
|
|
setChainProgress(null);
|
|
setStatus("connected");
|
|
} catch (err) {
|
|
if (disposedRef.current || sessionIdRef.current !== sessionId) return;
|
|
setError(messageOf(err));
|
|
setStatus("disconnected");
|
|
}
|
|
},
|
|
[
|
|
backend,
|
|
clearSession,
|
|
groupConfigs,
|
|
host,
|
|
hosts,
|
|
identities,
|
|
keys,
|
|
knownHosts,
|
|
proxyProfiles,
|
|
resetState,
|
|
terminalSettings,
|
|
],
|
|
);
|
|
|
|
const markProbeCertTrusted = useCallback(() => {
|
|
setProbeResult((prev) => {
|
|
if (!prev || !prev.cert) return prev;
|
|
return {
|
|
...prev,
|
|
cert: {
|
|
...prev.cert,
|
|
trustStatus: "trusted",
|
|
},
|
|
};
|
|
});
|
|
}, []);
|
|
|
|
const submitAuth = useCallback(
|
|
(auth: HostConnectionTestAuthOverride) => {
|
|
void startTest(auth);
|
|
},
|
|
[startTest],
|
|
);
|
|
|
|
const cancelTest = useCallback(() => {
|
|
setIsCancelling(true);
|
|
// Reject any pending host-key prompt so the dial fails fast, then clear
|
|
// this attempt's session. An in-flight TCP/auth dial without a prompt
|
|
// settles on its own (it is not registered as a terminal session).
|
|
if (pendingHostKeyRequestIdRef.current) {
|
|
void backend.respondHostKeyVerification(pendingHostKeyRequestIdRef.current, false, false);
|
|
pendingHostKeyRequestIdRef.current = null;
|
|
setHostKeyVerification(null);
|
|
}
|
|
clearSession();
|
|
setStatus("idle");
|
|
setNeedsAuth(false);
|
|
setError(null);
|
|
}, [backend, clearSession]);
|
|
|
|
// Connection timeout countdown + gentle progress while connecting.
|
|
useEffect(() => {
|
|
if (status !== "connecting" || needsAuth || hostKeyVerification || isAwaitingUserInput) {
|
|
return;
|
|
}
|
|
const deadlineMs = deadlineMsRef.current;
|
|
if (deadlineMs <= 0) return;
|
|
setTimeLeft(Math.ceil(deadlineMs / 1000));
|
|
const countdown = setInterval(() => {
|
|
setTimeLeft((prev) => (prev > 0 ? prev - 1 : 0));
|
|
}, 1000);
|
|
const timeout = setTimeout(() => {
|
|
// Cancel the probe and invalidate this attempt so a late success or
|
|
// failure from the bridge cannot overwrite the timeout outcome.
|
|
clearSession();
|
|
setError("Connection timed out. Please try again.");
|
|
setStatus("disconnected");
|
|
}, deadlineMs);
|
|
const prog = setInterval(() => {
|
|
setProgressValue((prev) => {
|
|
if (prev >= 95) return prev;
|
|
const remaining = 95 - prev;
|
|
return Math.min(95, prev + Math.max(1, remaining * 0.15));
|
|
});
|
|
}, 200);
|
|
return () => {
|
|
clearInterval(countdown);
|
|
clearTimeout(timeout);
|
|
clearInterval(prog);
|
|
};
|
|
}, [status, needsAuth, hostKeyVerification, isAwaitingUserInput, clearSession]);
|
|
|
|
return {
|
|
state: {
|
|
status,
|
|
error,
|
|
needsAuth,
|
|
progressValue,
|
|
chainProgress,
|
|
progressLogs,
|
|
hostKeyVerification,
|
|
timeLeft,
|
|
isAwaitingUserInput,
|
|
isCancelling,
|
|
probeResult,
|
|
activeProtocol,
|
|
} satisfies HostConnectionTestState,
|
|
startTest,
|
|
submitAuth,
|
|
cancelTest,
|
|
retry: () => void startTest(undefined, lastProtocolOverrideRef.current),
|
|
markProbeCertTrusted,
|
|
handleHostKeyContinue,
|
|
handleHostKeyAddAndContinue,
|
|
handleHostKeyClose,
|
|
};
|
|
}
|