Files
Netcatty/components/terminal/TerminalConnectionProgress.tsx
T
Yohannandyangheng 75c155b748 feat(vault): add host connection test to the host editor (#2984)
* feat(vault): add host connection test to the host editor

Add a 'Test' button next to Save in the host editor that opens a dialog reusing the terminal's connection progress/auth/host-key UI, and runs a headless connection probe:

- Main process: netcatty:test-connection reuses startSSHSession in a testMode that authenticates then tears down without opening a shell, reports via netcatty:test:result, and supports cancellation (netcatty:test-connection:cancel).
- Renderer: useHostConnectionTest hook + HostConnectionTestDialog component; single-credential auth (no agent/default-key fallback) with short test timeouts (8s TCP / 15s auth).
- Reuses terminal chain progress, host-key verification, and auth prompts; success keeps the same dialog with retry/close.

* fix(ssh): cancel test connections during chain/proxy dial

Register the headless test-connection transport before connectThroughChain / createProxySocket runs, and update its pendingConn / chainConnections as hops and proxy sockets are established. This lets cancelTestConnection abort a slow/unreachable bastion or proxy dial instead of leaving it running until timeout.

* fix(ssh): address host connection test review feedback

- Route jump-host keyboard-interactive prompts through the external scope in test mode
- Always offer keyboard-interactive for requiresMfa hosts in test mode, including key/certificate-only auth
- Derive the renderer test deadline from the probe options and cancel the attempt on timeout
- Run the macOS Local Network preflight before connection tests
- Add regression tests for jump-host MFA, MFA auth order, test deadline, and macOS preflight

Author: yangheng
Model: claude-opus-5

---------

Co-authored-by: yangheng <492238647@qq.com>
2026-10-08 14:42:18 +08:00

132 lines
5.2 KiB
TypeScript

/**
* Terminal Connection Progress
* Displays connection progress with logs and timeout
*/
import { Loader2, Play } from 'lucide-react';
import React from 'react';
import { useI18n } from '../../application/i18n/I18nProvider';
import { Button } from '../ui/button';
import { ScrollArea } from '../ui/scroll-area';
export interface TerminalConnectionProgressProps {
status: 'connecting' | 'connected' | 'disconnected';
error: string | null;
timeLeft: number;
isAwaitingUserInput?: boolean;
showEnterReconnectHint?: boolean;
isCancelling: boolean;
showLogs: boolean;
progressLogs: string[];
onCancelConnect: () => void;
onCloseSession: () => void;
onRetry?: () => void;
reconnectLabel?: string;
closeLabel?: string;
}
export interface TerminalConnectionLogListProps {
progressLogs: string[];
error?: string | null;
}
export const TerminalConnectionLogList: React.FC<TerminalConnectionLogListProps> = ({
progressLogs,
error,
}) => (
<div className="rounded-md border border-border/35 bg-background/40">
<ScrollArea className="max-h-44">
<div className="space-y-1 p-2.5 pb-4 pr-4 text-xs text-foreground/90">
{progressLogs.map((line, idx) => (
<div key={idx} className="flex items-start gap-2">
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-emerald-500" />
<div className="min-w-0 break-words leading-5">{line}</div>
</div>
))}
{error && (
<div className="flex items-start gap-2 text-destructive">
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-destructive" />
<div className="min-w-0 break-words leading-5">{error}</div>
</div>
)}
</div>
</ScrollArea>
</div>
);
export const TerminalConnectionProgress: React.FC<TerminalConnectionProgressProps> = ({
status,
error,
timeLeft,
isAwaitingUserInput = false,
showEnterReconnectHint = false,
isCancelling: _isCancelling,
showLogs,
progressLogs,
onCancelConnect: _onCancelConnect,
onCloseSession,
onRetry,
reconnectLabel,
closeLabel,
}) => {
const { t } = useI18n();
return (
<>
<div className="flex items-start justify-between gap-3 text-[11px] text-muted-foreground">
<div className="flex min-w-0 items-start gap-2">
{status === 'connecting' ? (
<>
<Loader2 className="h-3 w-3 mt-0.5 flex-shrink-0 animate-spin" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5">
{isAwaitingUserInput
? t('terminal.progress.waitingForUserInput')
: t('terminal.progress.timeoutIn', { seconds: timeLeft })}
</span>
</>
) : status === 'connected' ? (
<>
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-emerald-500" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5 text-emerald-500">
{t('terminal.progress.connected')}
</span>
</>
) : (
<>
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-destructive" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5 text-destructive">
{error || t('terminal.progress.disconnected')}
</span>
</>
)}
</div>
</div>
{showLogs && (
<TerminalConnectionLogList progressLogs={progressLogs} error={error} />
)}
{status !== 'connecting' && (
<div className="flex flex-wrap items-center justify-between gap-2">
{showEnterReconnectHint && (
<div className="min-w-0 break-words text-[11px] leading-5 text-muted-foreground">
{t('terminal.progress.enterReconnectHint')}
</div>
)}
<div className="ml-auto flex shrink-0 justify-end gap-2">
<Button variant="ghost" size="sm" className="h-7 px-3 text-[11px]" onClick={onCloseSession}>
{closeLabel ?? t('terminal.toolbar.closeSession')}
</Button>
{onRetry && (
<Button size="sm" className="h-7 px-3 text-[11px]" onClick={onRetry}>
<Play className="h-3 w-3 mr-1.5" /> {reconnectLabel ?? t('terminal.progress.startOver')}
</Button>
)}
</div>
</div>
)}
</>
);
};
export default TerminalConnectionProgress;