2025-11-05 10:36:16 -06:00
|
|
|
import {
|
|
|
|
|
useEffect,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
useImperativeHandle,
|
|
|
|
|
forwardRef,
|
2025-11-17 09:46:05 -06:00
|
|
|
useCallback,
|
2025-11-05 10:36:16 -06:00
|
|
|
} from "react";
|
|
|
|
|
import { useXTerm } from "react-xtermjs";
|
|
|
|
|
import { FitAddon } from "@xterm/addon-fit";
|
|
|
|
|
import { ClipboardAddon } from "@xterm/addon-clipboard";
|
2026-03-08 18:02:14 -05:00
|
|
|
import { RobustClipboardProvider } from "@/lib/clipboard-provider";
|
2025-11-05 10:36:16 -06:00
|
|
|
import { Unicode11Addon } from "@xterm/addon-unicode11";
|
|
|
|
|
import { WebLinksAddon } from "@xterm/addon-web-links";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2026-02-12 22:28:13 -06:00
|
|
|
import { getBasePath } from "@/lib/base-path";
|
2025-11-05 10:36:16 -06:00
|
|
|
import {
|
|
|
|
|
getCookie,
|
|
|
|
|
isElectron,
|
|
|
|
|
logActivity,
|
|
|
|
|
getSnippets,
|
2026-01-02 00:33:10 -06:00
|
|
|
deleteCommandFromHistory,
|
|
|
|
|
getCommandHistory,
|
2025-11-05 10:36:16 -06:00
|
|
|
} from "@/ui/main-axios.ts";
|
2026-01-24 19:49:42 -06:00
|
|
|
import { TOTPDialog } from "@/ui/desktop/navigation/dialogs/TOTPDialog.tsx";
|
|
|
|
|
import { SSHAuthDialog } from "@/ui/desktop/navigation/dialogs/SSHAuthDialog.tsx";
|
|
|
|
|
import { WarpgateDialog } from "@/ui/desktop/navigation/dialogs/WarpgateDialog.tsx";
|
2026-02-12 22:28:13 -06:00
|
|
|
import { OPKSSHDialog } from "@/ui/desktop/navigation/dialogs/OPKSSHDialog.tsx";
|
|
|
|
|
import { HostKeyVerificationDialog } from "@/ui/desktop/navigation/dialogs/HostKeyVerificationDialog.tsx";
|
2025-11-05 10:36:16 -06:00
|
|
|
import {
|
|
|
|
|
TERMINAL_THEMES,
|
|
|
|
|
DEFAULT_TERMINAL_CONFIG,
|
|
|
|
|
TERMINAL_FONTS,
|
2025-12-31 22:20:12 -06:00
|
|
|
} from "@/constants/terminal-themes.ts";
|
2025-11-05 10:36:16 -06:00
|
|
|
import type { TerminalConfig } from "@/types";
|
2025-12-31 22:20:12 -06:00
|
|
|
import { useTheme } from "@/components/theme-provider.tsx";
|
|
|
|
|
import { useCommandTracker } from "@/ui/hooks/useCommandTracker.ts";
|
|
|
|
|
import { highlightTerminalOutput } from "@/lib/terminal-syntax-highlighter.ts";
|
|
|
|
|
import { useCommandHistory as useCommandHistoryHook } from "@/ui/hooks/useCommandHistory.ts";
|
|
|
|
|
import { useCommandHistory } from "@/ui/desktop/apps/features/terminal/command-history/CommandHistoryContext.tsx";
|
2025-11-17 09:46:05 -06:00
|
|
|
import { CommandAutocomplete } from "./command-history/CommandAutocomplete.tsx";
|
|
|
|
|
import { SimpleLoader } from "@/ui/desktop/navigation/animations/SimpleLoader.tsx";
|
2025-12-31 22:20:12 -06:00
|
|
|
import { useConfirmation } from "@/hooks/use-confirmation.ts";
|
2026-01-24 19:49:42 -06:00
|
|
|
import {
|
|
|
|
|
ConnectionLogProvider,
|
|
|
|
|
useConnectionLog,
|
|
|
|
|
} from "@/ui/desktop/navigation/connection-log/ConnectionLogContext.tsx";
|
|
|
|
|
import { ConnectionLog } from "@/ui/desktop/navigation/connection-log/ConnectionLog.tsx";
|
2026-03-08 18:02:14 -05:00
|
|
|
import { toast } from "sonner";
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
interface HostConfig {
|
|
|
|
|
id?: number;
|
2026-03-08 18:02:14 -05:00
|
|
|
instanceId?: string;
|
2025-11-05 10:36:16 -06:00
|
|
|
ip: string;
|
|
|
|
|
port: number;
|
|
|
|
|
username: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
key?: string;
|
|
|
|
|
keyPassword?: string;
|
|
|
|
|
keyType?: string;
|
|
|
|
|
authType?: string;
|
|
|
|
|
credentialId?: number;
|
|
|
|
|
terminalConfig?: TerminalConfig;
|
|
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface TerminalHandle {
|
|
|
|
|
disconnect: () => void;
|
|
|
|
|
fit: () => void;
|
|
|
|
|
sendInput: (data: string) => void;
|
|
|
|
|
notifyResize: () => void;
|
|
|
|
|
refresh: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SSHTerminalProps {
|
|
|
|
|
hostConfig: HostConfig;
|
|
|
|
|
isVisible: boolean;
|
|
|
|
|
title?: string;
|
|
|
|
|
showTitle?: boolean;
|
|
|
|
|
splitScreen?: boolean;
|
|
|
|
|
onClose?: () => void;
|
2026-03-08 18:02:14 -05:00
|
|
|
onTitleChange?: (title: string) => void;
|
2025-11-05 10:36:16 -06:00
|
|
|
initialPath?: string;
|
|
|
|
|
executeCommand?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
|
2025-11-05 10:36:16 -06:00
|
|
|
function SSHTerminal(
|
|
|
|
|
{
|
|
|
|
|
hostConfig,
|
|
|
|
|
isVisible,
|
|
|
|
|
splitScreen = false,
|
|
|
|
|
onClose,
|
2026-03-08 18:02:14 -05:00
|
|
|
onTitleChange,
|
2025-11-05 10:36:16 -06:00
|
|
|
initialPath,
|
|
|
|
|
executeCommand,
|
|
|
|
|
},
|
|
|
|
|
ref,
|
|
|
|
|
) {
|
|
|
|
|
if (
|
|
|
|
|
typeof window !== "undefined" &&
|
|
|
|
|
!(window as { testJWT?: () => string | null }).testJWT
|
|
|
|
|
) {
|
|
|
|
|
(window as { testJWT?: () => string | null }).testJWT = () => {
|
|
|
|
|
const jwt = getCookie("jwt");
|
|
|
|
|
return jwt;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { instance: terminal, ref: xtermRef } = useXTerm();
|
2025-11-17 09:46:05 -06:00
|
|
|
const commandHistoryContext = useCommandHistory();
|
2025-12-31 22:20:12 -06:00
|
|
|
const { confirmWithToast } = useConfirmation();
|
|
|
|
|
const { theme: appTheme } = useTheme();
|
2026-01-24 19:49:42 -06:00
|
|
|
const { addLog, isExpanded: isConnectionLogExpanded } = useConnectionLog();
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
const config = { ...DEFAULT_TERMINAL_CONFIG, ...hostConfig.terminalConfig };
|
2025-12-31 22:20:12 -06:00
|
|
|
|
|
|
|
|
const isDarkMode =
|
|
|
|
|
appTheme === "dark" ||
|
|
|
|
|
(appTheme === "system" &&
|
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
|
|
|
|
|
|
|
|
let themeColors;
|
|
|
|
|
if (config.theme === "termix") {
|
|
|
|
|
themeColors = isDarkMode
|
|
|
|
|
? TERMINAL_THEMES.termixDark.colors
|
|
|
|
|
: TERMINAL_THEMES.termixLight.colors;
|
|
|
|
|
} else {
|
|
|
|
|
themeColors =
|
|
|
|
|
TERMINAL_THEMES[config.theme]?.colors ||
|
|
|
|
|
TERMINAL_THEMES.termixDark.colors;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
const backgroundColor = themeColors.background;
|
|
|
|
|
const fitAddonRef = useRef<FitAddon | null>(null);
|
|
|
|
|
const webSocketRef = useRef<WebSocket | null>(null);
|
|
|
|
|
const resizeTimeout = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
const wasDisconnectedBySSH = useRef(false);
|
|
|
|
|
const pingIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
const [isConnected, setIsConnected] = useState(false);
|
|
|
|
|
const [isConnecting, setIsConnecting] = useState(false);
|
2025-12-31 22:20:12 -06:00
|
|
|
const [isFitted, setIsFitted] = useState(false);
|
2026-01-24 19:49:42 -06:00
|
|
|
const [connectionError, setConnectionError] = useState<string | null>(null);
|
|
|
|
|
const connectionErrorRef = useRef<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const updateConnectionError = useCallback((error: string | null) => {
|
|
|
|
|
connectionErrorRef.current = error;
|
|
|
|
|
setConnectionError(error);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const [, setIsAuthenticated] = useState(false);
|
|
|
|
|
const [totpRequired, setTotpRequired] = useState(false);
|
|
|
|
|
const [totpPrompt, setTotpPrompt] = useState<string>("");
|
|
|
|
|
const [isPasswordPrompt, setIsPasswordPrompt] = useState(false);
|
|
|
|
|
const [showAuthDialog, setShowAuthDialog] = useState(false);
|
|
|
|
|
const [authDialogReason, setAuthDialogReason] = useState<
|
|
|
|
|
"no_keyboard" | "auth_failed" | "timeout"
|
|
|
|
|
>("no_keyboard");
|
|
|
|
|
const [keyboardInteractiveDetected, setKeyboardInteractiveDetected] =
|
|
|
|
|
useState(false);
|
2026-01-24 19:49:42 -06:00
|
|
|
const [warpgateAuthRequired, setWarpgateAuthRequired] = useState(false);
|
|
|
|
|
const [warpgateAuthUrl, setWarpgateAuthUrl] = useState<string>("");
|
|
|
|
|
const [warpgateSecurityKey, setWarpgateSecurityKey] = useState<string>("");
|
|
|
|
|
const warpgateTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
2026-02-12 22:28:13 -06:00
|
|
|
|
|
|
|
|
const [opksshDialog, setOpksshDialog] = useState<{
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
authUrl: string;
|
|
|
|
|
requestId: string;
|
|
|
|
|
stage: "chooser" | "waiting" | "authenticating" | "completed" | "error";
|
|
|
|
|
error?: string;
|
|
|
|
|
} | null>(null);
|
|
|
|
|
const opksshTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
const opksshFailedRef = useRef(false);
|
|
|
|
|
const currentHostIdRef = useRef<number | null>(null);
|
|
|
|
|
const currentHostConfigRef = useRef<any>(null);
|
|
|
|
|
|
|
|
|
|
const [hostKeyVerification, setHostKeyVerification] = useState<{
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
scenario: "new" | "changed";
|
|
|
|
|
data: any;
|
|
|
|
|
} | null>(null);
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
const sessionIdRef = useRef<string | null>(null);
|
|
|
|
|
const isAttachingSessionRef = useRef<boolean>(false);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const isVisibleRef = useRef<boolean>(false);
|
|
|
|
|
const isFittingRef = useRef(false);
|
|
|
|
|
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
const reconnectAttempts = useRef(0);
|
|
|
|
|
const maxReconnectAttempts = 3;
|
|
|
|
|
const isUnmountingRef = useRef(false);
|
|
|
|
|
const shouldNotReconnectRef = useRef(false);
|
|
|
|
|
const isReconnectingRef = useRef(false);
|
|
|
|
|
const isConnectingRef = useRef(false);
|
2026-03-08 18:02:14 -05:00
|
|
|
const wasConnectedRef = useRef(false);
|
2026-01-24 19:49:42 -06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
isUnmountingRef.current = false;
|
|
|
|
|
shouldNotReconnectRef.current = false;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
isConnectingRef.current = false;
|
|
|
|
|
reconnectAttempts.current = 0;
|
2026-03-08 18:02:14 -05:00
|
|
|
wasConnectedRef.current = false;
|
|
|
|
|
isAttachingSessionRef.current = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [hostConfig.id]);
|
2025-12-31 22:20:12 -06:00
|
|
|
const connectionAttemptIdRef = useRef(0);
|
|
|
|
|
const totpTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
2025-11-05 10:36:16 -06:00
|
|
|
const connectionTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
const activityLoggedRef = useRef(false);
|
2025-11-17 09:46:05 -06:00
|
|
|
const keyHandlerAttachedRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
const { trackInput, getCurrentCommand, updateCurrentCommand } =
|
|
|
|
|
useCommandTracker({
|
|
|
|
|
hostId: hostConfig.id,
|
|
|
|
|
enabled: true,
|
|
|
|
|
onCommandExecuted: (command) => {
|
|
|
|
|
if (!autocompleteHistory.current.includes(command)) {
|
|
|
|
|
autocompleteHistory.current = [
|
|
|
|
|
command,
|
|
|
|
|
...autocompleteHistory.current,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const getCurrentCommandRef = useRef(getCurrentCommand);
|
|
|
|
|
const updateCurrentCommandRef = useRef(updateCurrentCommand);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getCurrentCommandRef.current = getCurrentCommand;
|
|
|
|
|
updateCurrentCommandRef.current = updateCurrentCommand;
|
|
|
|
|
}, [getCurrentCommand, updateCurrentCommand]);
|
|
|
|
|
|
|
|
|
|
const [showAutocomplete, setShowAutocomplete] = useState(false);
|
|
|
|
|
const [autocompleteSuggestions, setAutocompleteSuggestions] = useState<
|
|
|
|
|
string[]
|
|
|
|
|
>([]);
|
|
|
|
|
const [autocompleteSelectedIndex, setAutocompleteSelectedIndex] =
|
|
|
|
|
useState(0);
|
|
|
|
|
const [autocompletePosition, setAutocompletePosition] = useState({
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
});
|
|
|
|
|
const autocompleteHistory = useRef<string[]>([]);
|
|
|
|
|
const currentAutocompleteCommand = useRef<string>("");
|
|
|
|
|
|
|
|
|
|
const showAutocompleteRef = useRef(false);
|
|
|
|
|
const autocompleteSuggestionsRef = useRef<string[]>([]);
|
|
|
|
|
const autocompleteSelectedIndexRef = useRef(0);
|
|
|
|
|
|
|
|
|
|
const [showHistoryDialog, setShowHistoryDialog] = useState(false);
|
|
|
|
|
const [commandHistory, setCommandHistory] = useState<string[]>([]);
|
|
|
|
|
const [isLoadingHistory, setIsLoadingHistory] = useState(false);
|
|
|
|
|
|
|
|
|
|
const setIsLoadingRef = useRef(commandHistoryContext.setIsLoading);
|
|
|
|
|
const setCommandHistoryContextRef = useRef(
|
|
|
|
|
commandHistoryContext.setCommandHistory,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setIsLoadingRef.current = commandHistoryContext.setIsLoading;
|
|
|
|
|
setCommandHistoryContextRef.current =
|
|
|
|
|
commandHistoryContext.setCommandHistory;
|
|
|
|
|
}, [
|
|
|
|
|
commandHistoryContext.setIsLoading,
|
|
|
|
|
commandHistoryContext.setCommandHistory,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (showHistoryDialog && hostConfig.id) {
|
|
|
|
|
setIsLoadingHistory(true);
|
|
|
|
|
setIsLoadingRef.current(true);
|
2026-01-02 00:33:10 -06:00
|
|
|
getCommandHistory(hostConfig.id!)
|
2025-11-17 09:46:05 -06:00
|
|
|
.then((history) => {
|
|
|
|
|
setCommandHistory(history);
|
|
|
|
|
setCommandHistoryContextRef.current(history);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Failed to load command history:", error);
|
|
|
|
|
setCommandHistory([]);
|
|
|
|
|
setCommandHistoryContextRef.current([]);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setIsLoadingHistory(false);
|
|
|
|
|
setIsLoadingRef.current(false);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [showHistoryDialog, hostConfig.id]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const autocompleteEnabled =
|
2025-12-31 22:20:12 -06:00
|
|
|
localStorage.getItem("commandAutocomplete") === "true";
|
2025-11-17 09:46:05 -06:00
|
|
|
|
|
|
|
|
if (hostConfig.id && autocompleteEnabled) {
|
2026-01-02 00:33:10 -06:00
|
|
|
getCommandHistory(hostConfig.id!)
|
2025-11-17 09:46:05 -06:00
|
|
|
.then((history) => {
|
|
|
|
|
autocompleteHistory.current = history;
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Failed to load autocomplete history:", error);
|
|
|
|
|
autocompleteHistory.current = [];
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
autocompleteHistory.current = [];
|
|
|
|
|
}
|
|
|
|
|
}, [hostConfig.id]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
showAutocompleteRef.current = showAutocomplete;
|
|
|
|
|
}, [showAutocomplete]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
autocompleteSuggestionsRef.current = autocompleteSuggestions;
|
|
|
|
|
}, [autocompleteSuggestions]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
autocompleteSelectedIndexRef.current = autocompleteSelectedIndex;
|
|
|
|
|
}, [autocompleteSelectedIndex]);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const activityLoggingRef = useRef(false);
|
2025-12-31 22:20:12 -06:00
|
|
|
const sudoPromptShownRef = useRef(false);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
const lastSentSizeRef = useRef<{ cols: number; rows: number } | null>(null);
|
|
|
|
|
const pendingSizeRef = useRef<{ cols: number; rows: number } | null>(null);
|
|
|
|
|
const notifyTimerRef = useRef<NodeJS.Timeout | null>(null);
|
2025-11-17 09:46:05 -06:00
|
|
|
const lastFittedSizeRef = useRef<{ cols: number; rows: number } | null>(
|
|
|
|
|
null,
|
|
|
|
|
);
|
2025-11-05 10:36:16 -06:00
|
|
|
const DEBOUNCE_MS = 140;
|
|
|
|
|
|
|
|
|
|
const logTerminalActivity = async () => {
|
|
|
|
|
if (
|
|
|
|
|
!hostConfig.id ||
|
|
|
|
|
activityLoggedRef.current ||
|
|
|
|
|
activityLoggingRef.current
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activityLoggingRef.current = true;
|
|
|
|
|
activityLoggedRef.current = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const hostName =
|
|
|
|
|
hostConfig.name || `${hostConfig.username}@${hostConfig.ip}`;
|
|
|
|
|
await logActivity("terminal", hostConfig.id, hostName);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("Failed to log terminal activity:", err);
|
|
|
|
|
activityLoggedRef.current = false;
|
|
|
|
|
} finally {
|
|
|
|
|
activityLoggingRef.current = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
isVisibleRef.current = isVisible;
|
|
|
|
|
}, [isVisible]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const checkAuth = () => {
|
|
|
|
|
const jwtToken = getCookie("jwt");
|
|
|
|
|
const isAuth = !!(jwtToken && jwtToken.trim() !== "");
|
|
|
|
|
|
|
|
|
|
setIsAuthenticated((prev) => {
|
|
|
|
|
if (prev !== isAuth) {
|
|
|
|
|
return isAuth;
|
|
|
|
|
}
|
|
|
|
|
return prev;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
checkAuth();
|
|
|
|
|
|
|
|
|
|
const authCheckInterval = setInterval(checkAuth, 5000);
|
|
|
|
|
|
|
|
|
|
return () => clearInterval(authCheckInterval);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
function hardRefresh() {
|
|
|
|
|
try {
|
|
|
|
|
if (
|
|
|
|
|
terminal &&
|
|
|
|
|
typeof (
|
|
|
|
|
terminal as { refresh?: (start: number, end: number) => void }
|
|
|
|
|
).refresh === "function"
|
|
|
|
|
) {
|
|
|
|
|
(
|
|
|
|
|
terminal as { refresh?: (start: number, end: number) => void }
|
|
|
|
|
).refresh(0, terminal.rows - 1);
|
|
|
|
|
}
|
2025-11-17 09:46:05 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Terminal operation failed:", error);
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function performFit() {
|
|
|
|
|
if (
|
|
|
|
|
!fitAddonRef.current ||
|
|
|
|
|
!terminal ||
|
2025-12-31 22:20:12 -06:00
|
|
|
!isVisible ||
|
2025-11-05 10:36:16 -06:00
|
|
|
isFittingRef.current
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
const lastSize = lastFittedSizeRef.current;
|
|
|
|
|
if (
|
|
|
|
|
lastSize &&
|
|
|
|
|
lastSize.cols === terminal.cols &&
|
|
|
|
|
lastSize.rows === terminal.rows
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
isFittingRef.current = true;
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
try {
|
|
|
|
|
fitAddonRef.current?.fit();
|
|
|
|
|
if (terminal && terminal.cols > 0 && terminal.rows > 0) {
|
|
|
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
|
|
|
|
lastFittedSizeRef.current = {
|
|
|
|
|
cols: terminal.cols,
|
|
|
|
|
rows: terminal.rows,
|
|
|
|
|
};
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
2025-11-17 09:46:05 -06:00
|
|
|
setIsFitted(true);
|
|
|
|
|
} finally {
|
|
|
|
|
isFittingRef.current = false;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleTotpSubmit(code: string) {
|
|
|
|
|
if (webSocketRef.current && code) {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
totpTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: isPasswordPrompt ? "password_response" : "totp_response",
|
|
|
|
|
data: { code },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
setTotpRequired(false);
|
|
|
|
|
setTotpPrompt("");
|
|
|
|
|
setIsPasswordPrompt(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleTotpCancel() {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
totpTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
setTotpRequired(false);
|
|
|
|
|
setTotpPrompt("");
|
|
|
|
|
if (onClose) onClose();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
function handleWarpgateContinue() {
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
if (warpgateTimeoutRef.current) {
|
|
|
|
|
clearTimeout(warpgateTimeoutRef.current);
|
|
|
|
|
warpgateTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "warpgate_auth_continue",
|
|
|
|
|
data: {},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
setWarpgateAuthRequired(false);
|
|
|
|
|
setWarpgateAuthUrl("");
|
|
|
|
|
setWarpgateSecurityKey("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleWarpgateCancel() {
|
|
|
|
|
if (warpgateTimeoutRef.current) {
|
|
|
|
|
clearTimeout(warpgateTimeoutRef.current);
|
|
|
|
|
warpgateTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
setWarpgateAuthRequired(false);
|
|
|
|
|
setWarpgateAuthUrl("");
|
|
|
|
|
setWarpgateSecurityKey("");
|
|
|
|
|
if (onClose) onClose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleWarpgateOpenUrl() {
|
|
|
|
|
if (warpgateAuthUrl) {
|
|
|
|
|
window.open(warpgateAuthUrl, "_blank", "noopener,noreferrer");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
function handleAuthDialogSubmit(credentials: {
|
|
|
|
|
password?: string;
|
|
|
|
|
sshKey?: string;
|
|
|
|
|
keyPassword?: string;
|
|
|
|
|
}) {
|
|
|
|
|
if (webSocketRef.current && terminal) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "reconnect_with_credentials",
|
|
|
|
|
data: {
|
|
|
|
|
cols: terminal.cols,
|
|
|
|
|
rows: terminal.rows,
|
|
|
|
|
password: credentials.password,
|
|
|
|
|
sshKey: credentials.sshKey,
|
|
|
|
|
keyPassword: credentials.keyPassword,
|
|
|
|
|
hostConfig: {
|
|
|
|
|
...hostConfig,
|
|
|
|
|
password: credentials.password,
|
|
|
|
|
key: credentials.sshKey,
|
|
|
|
|
keyPassword: credentials.keyPassword,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
setShowAuthDialog(false);
|
|
|
|
|
setIsConnecting(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleAuthDialogCancel() {
|
|
|
|
|
setShowAuthDialog(false);
|
|
|
|
|
if (onClose) onClose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function scheduleNotify(cols: number, rows: number) {
|
|
|
|
|
if (!(cols > 0 && rows > 0)) return;
|
|
|
|
|
pendingSizeRef.current = { cols, rows };
|
|
|
|
|
if (notifyTimerRef.current) clearTimeout(notifyTimerRef.current);
|
|
|
|
|
notifyTimerRef.current = setTimeout(() => {
|
|
|
|
|
const next = pendingSizeRef.current;
|
|
|
|
|
const last = lastSentSizeRef.current;
|
|
|
|
|
if (!next) return;
|
|
|
|
|
if (last && last.cols === next.cols && last.rows === next.rows) return;
|
|
|
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "resize", data: next }),
|
|
|
|
|
);
|
|
|
|
|
lastSentSizeRef.current = next;
|
|
|
|
|
}
|
|
|
|
|
}, DEBOUNCE_MS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useImperativeHandle(
|
|
|
|
|
ref,
|
|
|
|
|
() => ({
|
|
|
|
|
disconnect: () => {
|
|
|
|
|
isUnmountingRef.current = true;
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
if (pingIntervalRef.current) {
|
|
|
|
|
clearInterval(pingIntervalRef.current);
|
|
|
|
|
pingIntervalRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (reconnectTimeoutRef.current) {
|
|
|
|
|
clearTimeout(reconnectTimeoutRef.current);
|
|
|
|
|
reconnectTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
totpTimeoutRef.current = null;
|
|
|
|
|
}
|
2026-01-24 19:49:42 -06:00
|
|
|
if (warpgateTimeoutRef.current) {
|
|
|
|
|
clearTimeout(warpgateTimeoutRef.current);
|
|
|
|
|
warpgateTimeoutRef.current = null;
|
|
|
|
|
}
|
2026-03-08 18:02:14 -05:00
|
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
|
|
|
webSocketRef.current.send(JSON.stringify({ type: "disconnect" }));
|
|
|
|
|
}
|
|
|
|
|
const tabId = hostConfig.id ?? "default";
|
|
|
|
|
localStorage.removeItem(`termix_session_${tabId}`);
|
|
|
|
|
sessionIdRef.current = null;
|
2025-11-05 10:36:16 -06:00
|
|
|
webSocketRef.current?.close();
|
|
|
|
|
setIsConnected(false);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
},
|
|
|
|
|
fit: () => {
|
|
|
|
|
fitAddonRef.current?.fit();
|
|
|
|
|
if (terminal) scheduleNotify(terminal.cols, terminal.rows);
|
|
|
|
|
hardRefresh();
|
|
|
|
|
},
|
|
|
|
|
sendInput: (data: string) => {
|
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
webSocketRef.current.send(JSON.stringify({ type: "input", data }));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
notifyResize: () => {
|
|
|
|
|
try {
|
|
|
|
|
const cols = terminal?.cols ?? undefined;
|
|
|
|
|
const rows = terminal?.rows ?? undefined;
|
|
|
|
|
if (typeof cols === "number" && typeof rows === "number") {
|
|
|
|
|
scheduleNotify(cols, rows);
|
|
|
|
|
hardRefresh();
|
|
|
|
|
}
|
2025-11-17 09:46:05 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Terminal operation failed:", error);
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
},
|
|
|
|
|
refresh: () => hardRefresh(),
|
|
|
|
|
}),
|
|
|
|
|
[terminal],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function getUseRightClickCopyPaste() {
|
|
|
|
|
return getCookie("rightClickCopyPaste") === "true";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function attemptReconnection() {
|
|
|
|
|
if (
|
|
|
|
|
isUnmountingRef.current ||
|
|
|
|
|
shouldNotReconnectRef.current ||
|
|
|
|
|
isReconnectingRef.current ||
|
|
|
|
|
isConnectingRef.current ||
|
2026-01-24 19:49:42 -06:00
|
|
|
wasDisconnectedBySSH.current ||
|
|
|
|
|
reconnectTimeoutRef.current !== null
|
2025-11-05 10:36:16 -06:00
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reconnectAttempts.current >= maxReconnectAttempts) {
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(t("terminal.maxReconnectAttemptsReached"));
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.maxReconnectAttemptsReached"),
|
|
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isReconnectingRef.current = true;
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
if (terminal && !isAttachingSessionRef.current) {
|
2025-11-05 10:36:16 -06:00
|
|
|
terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reconnectAttempts.current++;
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
addLog({
|
|
|
|
|
type: "info",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.reconnecting", {
|
2025-11-05 10:36:16 -06:00
|
|
|
attempt: reconnectAttempts.current,
|
|
|
|
|
max: maxReconnectAttempts,
|
|
|
|
|
}),
|
2026-01-24 19:49:42 -06:00
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
const delay = Math.min(
|
|
|
|
|
2000 * Math.pow(2, reconnectAttempts.current - 1),
|
|
|
|
|
8000,
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
reconnectTimeoutRef.current = setTimeout(() => {
|
2026-01-24 19:49:42 -06:00
|
|
|
reconnectTimeoutRef.current = null;
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
if (
|
|
|
|
|
isUnmountingRef.current ||
|
|
|
|
|
shouldNotReconnectRef.current ||
|
|
|
|
|
wasDisconnectedBySSH.current
|
|
|
|
|
) {
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reconnectAttempts.current > maxReconnectAttempts) {
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const jwtToken = getCookie("jwt");
|
|
|
|
|
if (!jwtToken || jwtToken.trim() === "") {
|
|
|
|
|
console.warn("Reconnection cancelled - no authentication token");
|
|
|
|
|
isReconnectingRef.current = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(t("terminal.authenticationRequired"));
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "auth",
|
|
|
|
|
message: t("terminal.authenticationRequired"),
|
|
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (terminal && hostConfig) {
|
2026-03-08 18:02:14 -05:00
|
|
|
if (!isAttachingSessionRef.current) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
const cols = terminal.cols;
|
|
|
|
|
const rows = terminal.rows;
|
|
|
|
|
connectToHost(cols, rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isReconnectingRef.current = false;
|
2025-12-31 22:20:12 -06:00
|
|
|
}, delay);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function connectToHost(cols: number, rows: number) {
|
|
|
|
|
if (isConnectingRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConnectingRef.current = true;
|
2025-12-31 22:20:12 -06:00
|
|
|
connectionAttemptIdRef.current++;
|
2026-03-08 18:02:14 -05:00
|
|
|
wasConnectedRef.current = false;
|
2025-12-31 22:20:12 -06:00
|
|
|
|
|
|
|
|
if (!isReconnectingRef.current) {
|
|
|
|
|
reconnectAttempts.current = 0;
|
2026-01-24 19:49:42 -06:00
|
|
|
shouldNotReconnectRef.current = false;
|
2025-12-31 22:20:12 -06:00
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
const isDev =
|
|
|
|
|
!isElectron() &&
|
|
|
|
|
process.env.NODE_ENV === "development" &&
|
|
|
|
|
(window.location.port === "3000" ||
|
|
|
|
|
window.location.port === "5173" ||
|
|
|
|
|
window.location.port === "");
|
|
|
|
|
|
|
|
|
|
const jwtToken = getCookie("jwt");
|
|
|
|
|
|
|
|
|
|
if (!jwtToken || jwtToken.trim() === "") {
|
|
|
|
|
console.error("No JWT token available for WebSocket connection");
|
|
|
|
|
setIsConnected(false);
|
|
|
|
|
setIsConnecting(false);
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError("Authentication required");
|
2025-11-05 10:36:16 -06:00
|
|
|
isConnectingRef.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseWsUrl = isDev
|
|
|
|
|
? `${window.location.protocol === "https:" ? "wss" : "ws"}://localhost:30002`
|
|
|
|
|
: isElectron()
|
|
|
|
|
? (() => {
|
|
|
|
|
const baseUrl =
|
|
|
|
|
(window as { configuredServerUrl?: string })
|
|
|
|
|
.configuredServerUrl || "http://127.0.0.1:30001";
|
|
|
|
|
const wsProtocol = baseUrl.startsWith("https://")
|
|
|
|
|
? "wss://"
|
|
|
|
|
: "ws://";
|
|
|
|
|
const wsHost = baseUrl.replace(/^https?:\/\//, "");
|
|
|
|
|
return `${wsProtocol}${wsHost}/ssh/websocket/`;
|
|
|
|
|
})()
|
2026-02-12 22:28:13 -06:00
|
|
|
: `${getBasePath()}/ssh/websocket/`;
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
webSocketRef.current &&
|
|
|
|
|
webSocketRef.current.readyState !== WebSocket.CLOSED
|
|
|
|
|
) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pingIntervalRef.current) {
|
|
|
|
|
clearInterval(pingIntervalRef.current);
|
|
|
|
|
pingIntervalRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wsUrl = `${baseWsUrl}?token=${encodeURIComponent(jwtToken)}`;
|
|
|
|
|
|
|
|
|
|
const ws = new WebSocket(wsUrl);
|
|
|
|
|
webSocketRef.current = ws;
|
|
|
|
|
wasDisconnectedBySSH.current = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(null);
|
2025-11-05 10:36:16 -06:00
|
|
|
shouldNotReconnectRef.current = false;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
setIsConnecting(true);
|
|
|
|
|
|
|
|
|
|
setupWebSocketListeners(ws, cols, rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setupWebSocketListeners(
|
|
|
|
|
ws: WebSocket,
|
|
|
|
|
cols: number,
|
|
|
|
|
rows: number,
|
|
|
|
|
) {
|
|
|
|
|
ws.addEventListener("open", () => {
|
|
|
|
|
connectionTimeoutRef.current = setTimeout(() => {
|
2026-01-24 19:49:42 -06:00
|
|
|
if (
|
|
|
|
|
!isConnected &&
|
|
|
|
|
!totpRequired &&
|
|
|
|
|
!isPasswordPrompt &&
|
|
|
|
|
!connectionErrorRef.current
|
|
|
|
|
) {
|
2025-11-05 10:36:16 -06:00
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
2026-01-24 19:49:42 -06:00
|
|
|
const timeoutMessage = t("terminal.connectionTimeout");
|
|
|
|
|
updateConnectionError(timeoutMessage);
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: timeoutMessage,
|
|
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
if (reconnectAttempts.current > 0) {
|
|
|
|
|
attemptReconnection();
|
2025-12-31 22:20:12 -06:00
|
|
|
} else {
|
2026-01-24 19:49:42 -06:00
|
|
|
setIsConnecting(false);
|
2025-12-31 22:20:12 -06:00
|
|
|
shouldNotReconnectRef.current = true;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-31 22:20:12 -06:00
|
|
|
}, 35000);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
currentHostIdRef.current = hostConfig.id;
|
|
|
|
|
currentHostConfigRef.current = hostConfig;
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
const persistenceEnabled =
|
|
|
|
|
localStorage.getItem("enableTerminalSessionPersistence") === "true";
|
|
|
|
|
const tabId = hostConfig.instanceId
|
|
|
|
|
? `${hostConfig.id}_${hostConfig.instanceId}`
|
|
|
|
|
: `${hostConfig.id}_${Date.now()}`;
|
|
|
|
|
const savedSessionId = persistenceEnabled
|
|
|
|
|
? localStorage.getItem(`termix_session_${tabId}`)
|
|
|
|
|
: null;
|
|
|
|
|
if (savedSessionId && !isReconnectingRef.current) {
|
|
|
|
|
sessionIdRef.current = savedSessionId;
|
|
|
|
|
isAttachingSessionRef.current = true;
|
|
|
|
|
|
|
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "attachSession",
|
|
|
|
|
data: {
|
|
|
|
|
sessionId: savedSessionId,
|
|
|
|
|
cols,
|
|
|
|
|
rows,
|
|
|
|
|
tabInstanceId: hostConfig.instanceId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
isAttachingSessionRef.current = false;
|
|
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "connectToHost",
|
|
|
|
|
data: { cols, rows, hostConfig, initialPath, executeCommand },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
terminal.onData((data) => {
|
2025-11-17 09:46:05 -06:00
|
|
|
trackInput(data);
|
2025-11-05 10:36:16 -06:00
|
|
|
ws.send(JSON.stringify({ type: "input", data }));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pingIntervalRef.current = setInterval(() => {
|
|
|
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
|
|
|
ws.send(JSON.stringify({ type: "ping" }));
|
|
|
|
|
}
|
|
|
|
|
}, 30000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ws.addEventListener("message", (event) => {
|
|
|
|
|
try {
|
|
|
|
|
const msg = JSON.parse(event.data);
|
|
|
|
|
if (msg.type === "data") {
|
|
|
|
|
if (typeof msg.data === "string") {
|
2025-12-31 22:20:12 -06:00
|
|
|
const syntaxHighlightingEnabled =
|
|
|
|
|
localStorage.getItem("terminalSyntaxHighlighting") === "true";
|
|
|
|
|
|
|
|
|
|
const outputData = syntaxHighlightingEnabled
|
|
|
|
|
? highlightTerminalOutput(msg.data)
|
|
|
|
|
: msg.data;
|
|
|
|
|
|
|
|
|
|
terminal.write(outputData);
|
|
|
|
|
const sudoPasswordPattern =
|
2026-01-24 19:49:42 -06:00
|
|
|
/(?:\[sudo\][^\n]*:\s*$|sudo:[^\n]*password[^\n]*required)/i;
|
2025-12-31 22:20:12 -06:00
|
|
|
const passwordToFill =
|
|
|
|
|
hostConfig.terminalConfig?.sudoPassword || hostConfig.password;
|
|
|
|
|
if (
|
|
|
|
|
config.sudoPasswordAutoFill &&
|
|
|
|
|
sudoPasswordPattern.test(msg.data) &&
|
|
|
|
|
passwordToFill &&
|
|
|
|
|
!sudoPromptShownRef.current
|
|
|
|
|
) {
|
|
|
|
|
sudoPromptShownRef.current = true;
|
|
|
|
|
confirmWithToast(
|
|
|
|
|
t("terminal.sudoPasswordPopupTitle"),
|
|
|
|
|
async () => {
|
|
|
|
|
if (
|
|
|
|
|
webSocketRef.current &&
|
|
|
|
|
webSocketRef.current.readyState === WebSocket.OPEN
|
|
|
|
|
) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "input",
|
|
|
|
|
data: passwordToFill + "\n",
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
sudoPromptShownRef.current = false;
|
|
|
|
|
}, 3000);
|
|
|
|
|
},
|
|
|
|
|
t("common.confirm"),
|
|
|
|
|
t("common.cancel"),
|
2026-01-24 19:49:42 -06:00
|
|
|
{ confirmOnEnter: true },
|
2025-12-31 22:20:12 -06:00
|
|
|
);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
sudoPromptShownRef.current = false;
|
|
|
|
|
}, 15000);
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
} else {
|
2025-12-31 22:20:12 -06:00
|
|
|
const syntaxHighlightingEnabled =
|
|
|
|
|
localStorage.getItem("terminalSyntaxHighlighting") === "true";
|
|
|
|
|
|
|
|
|
|
const stringData = String(msg.data);
|
|
|
|
|
const outputData = syntaxHighlightingEnabled
|
|
|
|
|
? highlightTerminalOutput(stringData)
|
|
|
|
|
: stringData;
|
|
|
|
|
|
|
|
|
|
terminal.write(outputData);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
} else if (msg.type === "error") {
|
|
|
|
|
const errorMessage = msg.message || t("terminal.unknownError");
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: errorMessage,
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
if (
|
|
|
|
|
errorMessage.toLowerCase().includes("connection") ||
|
|
|
|
|
errorMessage.toLowerCase().includes("timeout") ||
|
|
|
|
|
errorMessage.toLowerCase().includes("network")
|
|
|
|
|
) {
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(errorMessage);
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsConnected(false);
|
|
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
2026-01-24 19:49:42 -06:00
|
|
|
setIsConnecting(false);
|
2025-11-05 10:36:16 -06:00
|
|
|
wasDisconnectedBySSH.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(errorMessage.toLowerCase().includes("auth") &&
|
|
|
|
|
errorMessage.toLowerCase().includes("failed")) ||
|
|
|
|
|
errorMessage.toLowerCase().includes("permission denied") ||
|
|
|
|
|
(errorMessage.toLowerCase().includes("invalid") &&
|
|
|
|
|
(errorMessage.toLowerCase().includes("password") ||
|
|
|
|
|
errorMessage.toLowerCase().includes("key"))) ||
|
|
|
|
|
errorMessage.toLowerCase().includes("incorrect password")
|
|
|
|
|
) {
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(errorMessage);
|
|
|
|
|
setIsConnecting(false);
|
2025-11-05 10:36:16 -06:00
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(errorMessage);
|
|
|
|
|
setIsConnecting(false);
|
2025-11-05 10:36:16 -06:00
|
|
|
} else if (msg.type === "connected") {
|
2026-02-12 22:28:13 -06:00
|
|
|
opksshFailedRef.current = false;
|
2026-03-08 18:02:14 -05:00
|
|
|
wasConnectedRef.current = true;
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsConnected(true);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
isConnectingRef.current = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(null);
|
2025-11-05 10:36:16 -06:00
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (reconnectAttempts.current > 0) {
|
2026-01-24 19:49:42 -06:00
|
|
|
addLog({
|
|
|
|
|
type: "success",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.reconnected"),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
addLog({
|
|
|
|
|
type: "success",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.connected"),
|
|
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
reconnectAttempts.current = 0;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
|
|
|
|
|
logTerminalActivity();
|
|
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
const terminalConfig = {
|
|
|
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
|
|
|
...hostConfig.terminalConfig,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
terminalConfig.environmentVariables &&
|
|
|
|
|
terminalConfig.environmentVariables.length > 0
|
|
|
|
|
) {
|
|
|
|
|
for (const envVar of terminalConfig.environmentVariables) {
|
|
|
|
|
if (envVar.key && envVar.value && ws.readyState === 1) {
|
|
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "input",
|
|
|
|
|
data: `export ${envVar.key}="${envVar.value}"\n`,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (terminalConfig.startupSnippetId) {
|
|
|
|
|
try {
|
|
|
|
|
const snippets = await getSnippets();
|
|
|
|
|
const snippet = snippets.find(
|
|
|
|
|
(s: { id: number }) =>
|
|
|
|
|
s.id === terminalConfig.startupSnippetId,
|
|
|
|
|
);
|
|
|
|
|
if (snippet && ws.readyState === 1) {
|
|
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "input",
|
|
|
|
|
data: snippet.content + "\n",
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("Failed to execute startup snippet:", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (terminalConfig.autoMosh && ws.readyState === 1) {
|
|
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "input",
|
|
|
|
|
data: terminalConfig.moshCommand + "\n",
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-12-31 22:20:12 -06:00
|
|
|
}, 100);
|
2025-11-05 10:36:16 -06:00
|
|
|
} else if (msg.type === "disconnected") {
|
|
|
|
|
wasDisconnectedBySSH.current = true;
|
|
|
|
|
setIsConnected(false);
|
|
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
setIsConnecting(false);
|
2026-03-08 18:02:14 -05:00
|
|
|
if (wasConnectedRef.current) {
|
|
|
|
|
wasConnectedRef.current = false;
|
|
|
|
|
if (
|
|
|
|
|
onClose &&
|
|
|
|
|
!connectionErrorRef.current &&
|
|
|
|
|
!opksshFailedRef.current
|
|
|
|
|
) {
|
|
|
|
|
onClose();
|
|
|
|
|
}
|
|
|
|
|
} else if (!connectionErrorRef.current) {
|
|
|
|
|
updateConnectionError(
|
|
|
|
|
msg.message || t("terminal.connectionRejected"),
|
|
|
|
|
);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
} else if (msg.type === "totp_required") {
|
|
|
|
|
setTotpRequired(true);
|
2025-12-31 22:20:12 -06:00
|
|
|
setTotpPrompt(msg.prompt || t("terminal.totpCodeLabel"));
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsPasswordPrompt(false);
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
}
|
|
|
|
|
totpTimeoutRef.current = setTimeout(() => {
|
|
|
|
|
setTotpRequired(false);
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
}, 180000);
|
|
|
|
|
} else if (msg.type === "totp_retry") {
|
2025-11-05 10:36:16 -06:00
|
|
|
} else if (msg.type === "password_required") {
|
|
|
|
|
setTotpRequired(true);
|
2025-12-31 22:20:12 -06:00
|
|
|
setTotpPrompt(msg.prompt || t("common.password"));
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsPasswordPrompt(true);
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
}
|
|
|
|
|
totpTimeoutRef.current = setTimeout(() => {
|
|
|
|
|
setTotpRequired(false);
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
}, 180000);
|
2026-01-24 19:49:42 -06:00
|
|
|
} else if (msg.type === "warpgate_auth_required") {
|
|
|
|
|
setWarpgateAuthRequired(true);
|
|
|
|
|
setWarpgateAuthUrl(msg.url || "");
|
|
|
|
|
setWarpgateSecurityKey(msg.securityKey || "N/A");
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (warpgateTimeoutRef.current) {
|
|
|
|
|
clearTimeout(warpgateTimeoutRef.current);
|
|
|
|
|
}
|
|
|
|
|
warpgateTimeoutRef.current = setTimeout(() => {
|
|
|
|
|
setWarpgateAuthRequired(false);
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
}, 300000);
|
2026-02-12 22:28:13 -06:00
|
|
|
} else if (msg.type === "opkssh_auth_required") {
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (opksshFailedRef.current) {
|
|
|
|
|
setOpksshDialog(null);
|
|
|
|
|
if (opksshTimeoutRef.current) {
|
|
|
|
|
clearTimeout(opksshTimeoutRef.current);
|
|
|
|
|
opksshTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
updateConnectionError(t("terminal.opksshAuthFailed"));
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "auth",
|
|
|
|
|
message: t("terminal.opksshAuthFailed"),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
opksshFailedRef.current = true;
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "opkssh_start_auth",
|
|
|
|
|
data: { hostId: msg.hostId },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "opkssh_status") {
|
|
|
|
|
if (connectionErrorRef.current) return;
|
|
|
|
|
if (msg.stage === "chooser") {
|
|
|
|
|
setOpksshDialog({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
authUrl: msg.url || "",
|
|
|
|
|
requestId: msg.requestId || "",
|
|
|
|
|
stage: "chooser",
|
|
|
|
|
});
|
|
|
|
|
if (opksshTimeoutRef.current) {
|
|
|
|
|
clearTimeout(opksshTimeoutRef.current);
|
|
|
|
|
}
|
|
|
|
|
opksshTimeoutRef.current = setTimeout(() => {
|
|
|
|
|
setOpksshDialog(null);
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
}, 300000);
|
|
|
|
|
} else {
|
|
|
|
|
setOpksshDialog((prev) =>
|
|
|
|
|
prev ? { ...prev, stage: msg.stage } : null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "opkssh_completed") {
|
|
|
|
|
if (opksshTimeoutRef.current) {
|
|
|
|
|
clearTimeout(opksshTimeoutRef.current);
|
|
|
|
|
opksshTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
setOpksshDialog(null);
|
|
|
|
|
if (webSocketRef.current && terminal) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "opkssh_auth_completed",
|
|
|
|
|
data: {
|
|
|
|
|
hostId: currentHostIdRef.current,
|
|
|
|
|
cols: terminal.cols || 80,
|
|
|
|
|
rows: terminal.rows || 24,
|
|
|
|
|
hostConfig: currentHostConfigRef.current,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "opkssh_error") {
|
|
|
|
|
if (connectionErrorRef.current) return;
|
|
|
|
|
opksshFailedRef.current = true;
|
|
|
|
|
if (opksshDialog) {
|
|
|
|
|
setOpksshDialog((prev) =>
|
|
|
|
|
prev ? { ...prev, stage: "error", error: msg.error } : null,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
setOpksshDialog({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
authUrl: "",
|
|
|
|
|
requestId: msg.requestId || "",
|
|
|
|
|
stage: "error",
|
|
|
|
|
error: msg.error,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
} else if (msg.type === "opkssh_timeout") {
|
|
|
|
|
if (connectionErrorRef.current) return;
|
|
|
|
|
opksshFailedRef.current = true;
|
|
|
|
|
if (opksshDialog) {
|
|
|
|
|
setOpksshDialog((prev) =>
|
|
|
|
|
prev
|
|
|
|
|
? {
|
|
|
|
|
...prev,
|
|
|
|
|
stage: "error",
|
|
|
|
|
error: t("terminal.opksshTimeout"),
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
setOpksshDialog({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
authUrl: "",
|
|
|
|
|
requestId: msg.requestId || "",
|
|
|
|
|
stage: "error",
|
|
|
|
|
error: t("terminal.opksshTimeout"),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
} else if (msg.type === "opkssh_config_error") {
|
|
|
|
|
setOpksshDialog({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
authUrl: "",
|
|
|
|
|
requestId: msg.requestId || "",
|
|
|
|
|
stage: "error",
|
|
|
|
|
error: msg.instructions || msg.error,
|
|
|
|
|
});
|
2025-11-05 10:36:16 -06:00
|
|
|
} else if (msg.type === "keyboard_interactive_available") {
|
|
|
|
|
setKeyboardInteractiveDetected(true);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "auth_method_not_available") {
|
|
|
|
|
setAuthDialogReason("no_keyboard");
|
|
|
|
|
setShowAuthDialog(true);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
2026-02-12 22:28:13 -06:00
|
|
|
} else if (msg.type === "host_key_verification_required") {
|
|
|
|
|
setHostKeyVerification({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
scenario: "new",
|
|
|
|
|
data: msg.data,
|
|
|
|
|
});
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "host_key_changed") {
|
|
|
|
|
setHostKeyVerification({
|
|
|
|
|
isOpen: true,
|
|
|
|
|
scenario: "changed",
|
|
|
|
|
data: msg.data,
|
|
|
|
|
});
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
2026-03-08 18:02:14 -05:00
|
|
|
} else if (msg.type === "sessionCreated") {
|
|
|
|
|
sessionIdRef.current = msg.sessionId;
|
|
|
|
|
const persistenceEnabled =
|
|
|
|
|
localStorage.getItem("enableTerminalSessionPersistence") ===
|
|
|
|
|
"true";
|
|
|
|
|
if (persistenceEnabled && hostConfig.instanceId) {
|
|
|
|
|
const tabId = `${hostConfig.id}_${hostConfig.instanceId}`;
|
|
|
|
|
localStorage.setItem(`termix_session_${tabId}`, msg.sessionId);
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "sessionAttached") {
|
|
|
|
|
isAttachingSessionRef.current = false;
|
|
|
|
|
opksshFailedRef.current = false;
|
|
|
|
|
wasConnectedRef.current = true;
|
|
|
|
|
setIsConnected(true);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
isConnectingRef.current = false;
|
|
|
|
|
shouldNotReconnectRef.current = false;
|
|
|
|
|
updateConnectionError(null);
|
|
|
|
|
if (connectionTimeoutRef.current) {
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
connectionTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (reconnectTimeoutRef.current) {
|
|
|
|
|
clearTimeout(reconnectTimeoutRef.current);
|
|
|
|
|
reconnectTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
reconnectAttempts.current = 0;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
|
|
|
|
|
logTerminalActivity();
|
|
|
|
|
|
|
|
|
|
addLog({
|
|
|
|
|
type: "success",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.reconnected"),
|
|
|
|
|
});
|
|
|
|
|
} else if (msg.type === "sessionExpired") {
|
|
|
|
|
isAttachingSessionRef.current = false;
|
|
|
|
|
shouldNotReconnectRef.current = false;
|
|
|
|
|
if (hostConfig.instanceId) {
|
|
|
|
|
const tabId = `${hostConfig.id}_${hostConfig.instanceId}`;
|
|
|
|
|
localStorage.removeItem(`termix_session_${tabId}`);
|
|
|
|
|
}
|
|
|
|
|
sessionIdRef.current = null;
|
|
|
|
|
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
} else if (msg.type === "sessionTakenOver") {
|
|
|
|
|
if (sessionIdRef.current && hostConfig.instanceId) {
|
|
|
|
|
const tabId = `${hostConfig.id}_${hostConfig.instanceId}`;
|
|
|
|
|
localStorage.removeItem(`termix_session_${tabId}`);
|
|
|
|
|
sessionIdRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
setIsConnected(false);
|
|
|
|
|
setIsConnecting(true);
|
|
|
|
|
|
|
|
|
|
addLog({
|
|
|
|
|
type: "warning",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.sessionTakenOver"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cols = terminal?.cols || 80;
|
|
|
|
|
const rows = terminal?.rows || 24;
|
|
|
|
|
connectToHost(cols, rows);
|
2026-01-24 19:49:42 -06:00
|
|
|
} else if (msg.type === "connection_log") {
|
|
|
|
|
if (msg.data) {
|
|
|
|
|
addLog({
|
|
|
|
|
type: msg.data.level || "info",
|
|
|
|
|
stage: msg.data.stage || "auth",
|
|
|
|
|
message: msg.data.message,
|
|
|
|
|
details: msg.data.details,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
2026-02-12 22:28:13 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("WebSocket message handler error:", error);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
const currentAttemptId = connectionAttemptIdRef.current;
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
ws.addEventListener("close", (event) => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (currentAttemptId !== connectionAttemptIdRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsConnected(false);
|
|
|
|
|
isConnectingRef.current = false;
|
|
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
totpTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
if (event.code === 1006) {
|
|
|
|
|
console.error(
|
|
|
|
|
"[WebSocket] Abnormal closure detected - possible HTTPS/proxy issue",
|
|
|
|
|
);
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.websocketAbnormalClose"),
|
|
|
|
|
});
|
|
|
|
|
updateConnectionError(t("terminal.websocketAbnormalClose"));
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
if (event.code === 1008) {
|
|
|
|
|
console.error("WebSocket authentication failed:", event.reason);
|
2026-01-24 19:49:42 -06:00
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "auth",
|
|
|
|
|
message: "Authentication failed - please re-login",
|
|
|
|
|
});
|
|
|
|
|
updateConnectionError("Authentication failed - please re-login");
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsConnecting(false);
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
|
|
|
|
|
localStorage.removeItem("jwt");
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
2026-03-08 18:02:14 -05:00
|
|
|
!wasConnectedRef.current &&
|
|
|
|
|
!isAttachingSessionRef.current &&
|
2026-01-24 19:49:42 -06:00
|
|
|
event.wasClean &&
|
|
|
|
|
(event.code === 1005 || event.code === 1000)
|
|
|
|
|
) {
|
|
|
|
|
console.error("[WebSocket] Connection rejected by server");
|
|
|
|
|
addLog({
|
|
|
|
|
type: "error",
|
|
|
|
|
stage: "connection",
|
|
|
|
|
message: t("terminal.connectionRejected"),
|
|
|
|
|
});
|
|
|
|
|
updateConnectionError(t("terminal.connectionRejected"));
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldAttemptReconnection =
|
2025-11-05 10:36:16 -06:00
|
|
|
!wasDisconnectedBySSH.current &&
|
|
|
|
|
!isUnmountingRef.current &&
|
2025-12-31 22:20:12 -06:00
|
|
|
!shouldNotReconnectRef.current &&
|
2026-01-24 19:49:42 -06:00
|
|
|
!isConnectingRef.current;
|
|
|
|
|
|
|
|
|
|
if (shouldAttemptReconnection) {
|
2025-11-05 10:36:16 -06:00
|
|
|
wasDisconnectedBySSH.current = false;
|
|
|
|
|
attemptReconnection();
|
2026-01-24 19:49:42 -06:00
|
|
|
} else {
|
|
|
|
|
setIsConnecting(false);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
ws.addEventListener("error", (event) => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (currentAttemptId !== connectionAttemptIdRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
console.error("[WebSocket] Error:", event);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
setIsConnected(false);
|
|
|
|
|
isConnectingRef.current = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
updateConnectionError(t("terminal.websocketError"));
|
2025-11-05 10:36:16 -06:00
|
|
|
if (terminal) {
|
|
|
|
|
terminal.clear();
|
|
|
|
|
}
|
|
|
|
|
setIsConnecting(false);
|
2025-12-31 22:20:12 -06:00
|
|
|
|
|
|
|
|
if (totpTimeoutRef.current) {
|
|
|
|
|
clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
totpTimeoutRef.current = null;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
async function writeTextToClipboard(text: string): Promise<boolean> {
|
2025-11-05 10:36:16 -06:00
|
|
|
try {
|
|
|
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
|
|
|
await navigator.clipboard.writeText(text);
|
2026-03-08 18:02:14 -05:00
|
|
|
return true;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
2026-03-08 18:02:14 -05:00
|
|
|
} catch {
|
|
|
|
|
// fall through to legacy method
|
2025-11-17 09:46:05 -06:00
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
try {
|
2026-03-08 18:02:14 -05:00
|
|
|
const textarea = document.createElement("textarea");
|
|
|
|
|
textarea.value = text;
|
|
|
|
|
textarea.style.position = "fixed";
|
|
|
|
|
textarea.style.left = "-9999px";
|
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
|
textarea.focus();
|
|
|
|
|
textarea.select();
|
2025-11-05 10:36:16 -06:00
|
|
|
document.execCommand("copy");
|
|
|
|
|
document.body.removeChild(textarea);
|
2026-03-08 18:02:14 -05:00
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error(t("terminal.clipboardWriteFailed"));
|
|
|
|
|
return false;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readTextFromClipboard(): Promise<string> {
|
|
|
|
|
try {
|
|
|
|
|
if (navigator.clipboard && navigator.clipboard.readText) {
|
|
|
|
|
return await navigator.clipboard.readText();
|
|
|
|
|
}
|
2026-03-08 18:02:14 -05:00
|
|
|
} catch {
|
2026-03-14 20:05:05 -05:00
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
if (window.location.protocol !== "https:" && !isElectron()) {
|
|
|
|
|
toast.error(t("terminal.clipboardHttpWarning"));
|
2025-11-17 09:46:05 -06:00
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
const handleSelectCommand = useCallback(
|
|
|
|
|
(command: string) => {
|
|
|
|
|
if (!terminal || !webSocketRef.current) return;
|
|
|
|
|
|
|
|
|
|
for (const char of command) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: char }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
terminal.focus();
|
|
|
|
|
}, 100);
|
|
|
|
|
},
|
|
|
|
|
[terminal],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
commandHistoryContext.setOnSelectCommand(handleSelectCommand);
|
|
|
|
|
}, [handleSelectCommand]);
|
|
|
|
|
|
|
|
|
|
const handleAutocompleteSelect = useCallback(
|
|
|
|
|
(selectedCommand: string) => {
|
|
|
|
|
if (!webSocketRef.current) return;
|
|
|
|
|
|
|
|
|
|
const currentCmd = currentAutocompleteCommand.current;
|
|
|
|
|
const completion = selectedCommand.substring(currentCmd.length);
|
|
|
|
|
|
|
|
|
|
for (const char of completion) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: char }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCurrentCommand(selectedCommand);
|
|
|
|
|
|
|
|
|
|
setShowAutocomplete(false);
|
|
|
|
|
setAutocompleteSuggestions([]);
|
|
|
|
|
currentAutocompleteCommand.current = "";
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
terminal?.focus();
|
|
|
|
|
}, 50);
|
|
|
|
|
},
|
|
|
|
|
[terminal, updateCurrentCommand],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleDeleteCommand = useCallback(
|
|
|
|
|
async (command: string) => {
|
|
|
|
|
if (!hostConfig.id) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await deleteCommandFromHistory(hostConfig.id, command);
|
|
|
|
|
|
|
|
|
|
setCommandHistory((prev) => {
|
|
|
|
|
const newHistory = prev.filter((cmd) => cmd !== command);
|
|
|
|
|
setCommandHistoryContextRef.current(newHistory);
|
|
|
|
|
return newHistory;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
autocompleteHistory.current = autocompleteHistory.current.filter(
|
|
|
|
|
(cmd) => cmd !== command,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to delete command from history:", error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hostConfig.id],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
commandHistoryContext.setOnDeleteCommand(handleDeleteCommand);
|
|
|
|
|
}, [handleDeleteCommand]);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!terminal || !xtermRef.current) return;
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
|
|
|
...hostConfig.terminalConfig,
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
let themeColors;
|
|
|
|
|
if (config.theme === "termix") {
|
|
|
|
|
themeColors = isDarkMode
|
|
|
|
|
? TERMINAL_THEMES.termixDark.colors
|
|
|
|
|
: TERMINAL_THEMES.termixLight.colors;
|
|
|
|
|
} else {
|
|
|
|
|
themeColors =
|
|
|
|
|
TERMINAL_THEMES[config.theme]?.colors ||
|
|
|
|
|
TERMINAL_THEMES.termixDark.colors;
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
const fontConfig = TERMINAL_FONTS.find(
|
|
|
|
|
(f) => f.value === config.fontFamily,
|
|
|
|
|
);
|
|
|
|
|
const fontFamily = fontConfig?.fallback || TERMINAL_FONTS[0].fallback;
|
|
|
|
|
|
|
|
|
|
terminal.options = {
|
|
|
|
|
cursorBlink: config.cursorBlink,
|
|
|
|
|
cursorStyle: config.cursorStyle,
|
|
|
|
|
scrollback: config.scrollback,
|
|
|
|
|
fontSize: config.fontSize,
|
|
|
|
|
fontFamily,
|
|
|
|
|
allowTransparency: true,
|
2026-01-24 19:49:42 -06:00
|
|
|
convertEol: false,
|
2025-11-05 10:36:16 -06:00
|
|
|
windowsMode: false,
|
|
|
|
|
macOptionIsMeta: false,
|
|
|
|
|
macOptionClickForcesSelection: false,
|
|
|
|
|
rightClickSelectsWord: config.rightClickSelectsWord,
|
|
|
|
|
fastScrollModifier: config.fastScrollModifier,
|
|
|
|
|
fastScrollSensitivity: config.fastScrollSensitivity,
|
|
|
|
|
allowProposedApi: true,
|
|
|
|
|
minimumContrastRatio: config.minimumContrastRatio,
|
|
|
|
|
letterSpacing: config.letterSpacing,
|
|
|
|
|
lineHeight: config.lineHeight,
|
|
|
|
|
bellStyle: config.bellStyle as "none" | "sound" | "visual" | "both",
|
|
|
|
|
|
|
|
|
|
theme: {
|
|
|
|
|
background: themeColors.background,
|
|
|
|
|
foreground: themeColors.foreground,
|
|
|
|
|
cursor: themeColors.cursor,
|
|
|
|
|
cursorAccent: themeColors.cursorAccent,
|
|
|
|
|
selectionBackground: themeColors.selectionBackground,
|
|
|
|
|
selectionForeground: themeColors.selectionForeground,
|
|
|
|
|
black: themeColors.black,
|
|
|
|
|
red: themeColors.red,
|
|
|
|
|
green: themeColors.green,
|
|
|
|
|
yellow: themeColors.yellow,
|
|
|
|
|
blue: themeColors.blue,
|
|
|
|
|
magenta: themeColors.magenta,
|
|
|
|
|
cyan: themeColors.cyan,
|
|
|
|
|
white: themeColors.white,
|
|
|
|
|
brightBlack: themeColors.brightBlack,
|
|
|
|
|
brightRed: themeColors.brightRed,
|
|
|
|
|
brightGreen: themeColors.brightGreen,
|
|
|
|
|
brightYellow: themeColors.brightYellow,
|
|
|
|
|
brightBlue: themeColors.brightBlue,
|
|
|
|
|
brightMagenta: themeColors.brightMagenta,
|
|
|
|
|
brightCyan: themeColors.brightCyan,
|
|
|
|
|
brightWhite: themeColors.brightWhite,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fitAddon = new FitAddon();
|
2026-03-08 18:02:14 -05:00
|
|
|
const clipboardProvider = new RobustClipboardProvider();
|
|
|
|
|
const clipboardAddon = new ClipboardAddon(undefined, clipboardProvider);
|
2025-11-05 10:36:16 -06:00
|
|
|
const unicode11Addon = new Unicode11Addon();
|
|
|
|
|
const webLinksAddon = new WebLinksAddon();
|
|
|
|
|
|
|
|
|
|
fitAddonRef.current = fitAddon;
|
|
|
|
|
terminal.loadAddon(fitAddon);
|
|
|
|
|
terminal.loadAddon(clipboardAddon);
|
|
|
|
|
terminal.loadAddon(unicode11Addon);
|
|
|
|
|
terminal.loadAddon(webLinksAddon);
|
|
|
|
|
|
|
|
|
|
terminal.unicode.activeVersion = "11";
|
|
|
|
|
|
|
|
|
|
terminal.open(xtermRef.current);
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
fitAddonRef.current?.fit();
|
|
|
|
|
if (terminal.cols < 10 || terminal.rows < 3) {
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
fitAddonRef.current?.fit();
|
|
|
|
|
setIsFitted(true);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setIsFitted(true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const element = xtermRef.current;
|
|
|
|
|
const handleContextMenu = async (e: MouseEvent) => {
|
|
|
|
|
if (!getUseRightClickCopyPaste()) return;
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2026-03-14 20:05:05 -05:00
|
|
|
if (terminal.hasSelection()) {
|
|
|
|
|
const selection = terminal.getSelection();
|
|
|
|
|
if (selection) {
|
|
|
|
|
await writeTextToClipboard(selection);
|
|
|
|
|
terminal.clearSelection();
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
2026-03-14 20:05:05 -05:00
|
|
|
} else {
|
|
|
|
|
const text = await readTextFromClipboard();
|
|
|
|
|
if (text) terminal.paste(text);
|
2025-11-17 09:46:05 -06:00
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
};
|
|
|
|
|
element?.addEventListener("contextmenu", handleContextMenu);
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
const handleBackspaceMode = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key !== "Backspace") return;
|
|
|
|
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
|
|
|
if (config.backspaceMode !== "control-h") return;
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: "\x08" }),
|
|
|
|
|
);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
2026-02-12 22:28:13 -06:00
|
|
|
return false;
|
2025-11-05 10:36:16 -06:00
|
|
|
};
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
element?.addEventListener("keydown", handleBackspaceMode, true);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver(() => {
|
|
|
|
|
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
|
|
|
|
|
resizeTimeout.current = setTimeout(() => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (isVisible && terminal?.cols > 0) {
|
|
|
|
|
performFit();
|
|
|
|
|
}
|
2025-11-05 10:36:16 -06:00
|
|
|
}, 50);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
resizeObserver.observe(xtermRef.current);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isFittingRef.current = false;
|
|
|
|
|
resizeObserver.disconnect();
|
2026-03-08 18:02:14 -05:00
|
|
|
clipboardProvider.dispose();
|
2025-11-05 10:36:16 -06:00
|
|
|
element?.removeEventListener("contextmenu", handleContextMenu);
|
2026-02-12 22:28:13 -06:00
|
|
|
element?.removeEventListener("keydown", handleBackspaceMode, true);
|
2025-11-05 10:36:16 -06:00
|
|
|
if (notifyTimerRef.current) clearTimeout(notifyTimerRef.current);
|
|
|
|
|
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
|
2026-01-24 19:49:42 -06:00
|
|
|
};
|
|
|
|
|
}, [xtermRef, terminal, hostConfig, isDarkMode]);
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
const isMountedRef = useRef(false);
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
useEffect(() => {
|
2026-03-08 18:02:14 -05:00
|
|
|
isMountedRef.current = true;
|
|
|
|
|
|
|
|
|
|
const currentHostId = hostConfig.id;
|
|
|
|
|
const currentInstanceId = hostConfig.instanceId;
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
return () => {
|
2026-03-08 18:02:14 -05:00
|
|
|
if (!isMountedRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
currentHostIdRef.current !== currentHostId &&
|
|
|
|
|
currentHostIdRef.current !== null
|
|
|
|
|
) {
|
|
|
|
|
isUnmountingRef.current = true;
|
|
|
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
|
isReconnectingRef.current = false;
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
if (reconnectTimeoutRef.current)
|
|
|
|
|
clearTimeout(reconnectTimeoutRef.current);
|
|
|
|
|
if (connectionTimeoutRef.current)
|
|
|
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
|
|
|
if (totpTimeoutRef.current) clearTimeout(totpTimeoutRef.current);
|
|
|
|
|
if (pingIntervalRef.current) {
|
|
|
|
|
clearInterval(pingIntervalRef.current);
|
|
|
|
|
pingIntervalRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const persistenceEnabled =
|
|
|
|
|
localStorage.getItem("enableTerminalSessionPersistence") === "true";
|
|
|
|
|
if (
|
|
|
|
|
!persistenceEnabled &&
|
|
|
|
|
sessionIdRef.current &&
|
|
|
|
|
currentInstanceId
|
|
|
|
|
) {
|
|
|
|
|
const tabId = `${currentHostId}_${currentInstanceId}`;
|
|
|
|
|
localStorage.removeItem(`termix_session_${tabId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isMountedRef.current = false;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
};
|
2026-03-08 18:02:14 -05:00
|
|
|
}, [hostConfig.id, hostConfig.instanceId]);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!terminal) return;
|
|
|
|
|
|
|
|
|
|
const handleCustomKey = (e: KeyboardEvent): boolean => {
|
|
|
|
|
if (e.type !== "keydown") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
if (
|
2026-03-08 18:02:14 -05:00
|
|
|
e.ctrlKey &&
|
|
|
|
|
!e.shiftKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey &&
|
|
|
|
|
e.key.toLowerCase() === "c" &&
|
|
|
|
|
terminal.hasSelection()
|
2026-02-12 22:28:13 -06:00
|
|
|
) {
|
2026-03-08 18:02:14 -05:00
|
|
|
const selection = terminal.getSelection();
|
|
|
|
|
if (selection) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
writeTextToClipboard(selection);
|
|
|
|
|
terminal.clearSelection();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
((e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey) ||
|
|
|
|
|
(e.metaKey && !e.ctrlKey && !e.altKey) ||
|
|
|
|
|
(e.ctrlKey &&
|
|
|
|
|
!e.shiftKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey &&
|
|
|
|
|
e.key === "Insert")) &&
|
|
|
|
|
(e.key.toLowerCase() === "c" || e.key === "Insert")
|
|
|
|
|
) {
|
|
|
|
|
const selection = terminal.getSelection();
|
|
|
|
|
if (selection) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
writeTextToClipboard(selection);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-12 22:28:13 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-14 20:05:05 -05:00
|
|
|
if (
|
|
|
|
|
e.ctrlKey &&
|
|
|
|
|
!e.shiftKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey &&
|
|
|
|
|
e.key.toLowerCase() === "v"
|
|
|
|
|
) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
readTextFromClipboard().then((text) => {
|
|
|
|
|
if (text) terminal.paste(text);
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
if (e.ctrlKey && e.altKey && !e.metaKey && !e.shiftKey) {
|
|
|
|
|
const key = e.key.toLowerCase();
|
|
|
|
|
const blockedKeys = ["w", "t", "n", "q"];
|
|
|
|
|
if (blockedKeys.includes(key)) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
const ctrlCode = key.charCodeAt(0) - 96;
|
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "input",
|
|
|
|
|
data: String.fromCharCode(ctrlCode),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
if (showAutocompleteRef.current) {
|
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setShowAutocomplete(false);
|
|
|
|
|
setAutocompleteSuggestions([]);
|
|
|
|
|
currentAutocompleteCommand.current = "";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
const currentIndex = autocompleteSelectedIndexRef.current;
|
|
|
|
|
const suggestionsLength = autocompleteSuggestionsRef.current.length;
|
|
|
|
|
|
|
|
|
|
if (e.key === "ArrowDown") {
|
|
|
|
|
const newIndex =
|
|
|
|
|
currentIndex < suggestionsLength - 1 ? currentIndex + 1 : 0;
|
|
|
|
|
setAutocompleteSelectedIndex(newIndex);
|
|
|
|
|
} else if (e.key === "ArrowUp") {
|
|
|
|
|
const newIndex =
|
|
|
|
|
currentIndex > 0 ? currentIndex - 1 : suggestionsLength - 1;
|
|
|
|
|
setAutocompleteSelectedIndex(newIndex);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
e.key === "Enter" &&
|
|
|
|
|
autocompleteSuggestionsRef.current.length > 0
|
|
|
|
|
) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
const selectedCommand =
|
|
|
|
|
autocompleteSuggestionsRef.current[
|
|
|
|
|
autocompleteSelectedIndexRef.current
|
|
|
|
|
];
|
|
|
|
|
const currentCmd = currentAutocompleteCommand.current;
|
|
|
|
|
const completion = selectedCommand.substring(currentCmd.length);
|
|
|
|
|
|
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
for (const char of completion) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: char }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCurrentCommandRef.current(selectedCommand);
|
|
|
|
|
|
|
|
|
|
setShowAutocomplete(false);
|
|
|
|
|
setAutocompleteSuggestions([]);
|
|
|
|
|
currentAutocompleteCommand.current = "";
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
e.key === "Tab" &&
|
|
|
|
|
!e.ctrlKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey &&
|
|
|
|
|
!e.shiftKey
|
|
|
|
|
) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
const currentIndex = autocompleteSelectedIndexRef.current;
|
|
|
|
|
const suggestionsLength = autocompleteSuggestionsRef.current.length;
|
|
|
|
|
const newIndex =
|
|
|
|
|
currentIndex < suggestionsLength - 1 ? currentIndex + 1 : 0;
|
|
|
|
|
setAutocompleteSelectedIndex(newIndex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setShowAutocomplete(false);
|
|
|
|
|
setAutocompleteSuggestions([]);
|
|
|
|
|
currentAutocompleteCommand.current = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
if (
|
|
|
|
|
e.key === "Tab" &&
|
|
|
|
|
e.shiftKey &&
|
|
|
|
|
!e.ctrlKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey
|
|
|
|
|
) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: "\x1b[Z" }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
if (
|
|
|
|
|
e.key === "Tab" &&
|
|
|
|
|
!e.ctrlKey &&
|
|
|
|
|
!e.altKey &&
|
|
|
|
|
!e.metaKey &&
|
|
|
|
|
!e.shiftKey
|
|
|
|
|
) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
const autocompleteEnabled =
|
2025-12-31 22:20:12 -06:00
|
|
|
localStorage.getItem("commandAutocomplete") === "true";
|
2025-11-17 09:46:05 -06:00
|
|
|
|
|
|
|
|
if (!autocompleteEnabled) {
|
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: "\t" }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentCmd = getCurrentCommandRef.current().trim();
|
|
|
|
|
if (currentCmd.length > 0 && webSocketRef.current?.readyState === 1) {
|
|
|
|
|
const matches = autocompleteHistory.current
|
|
|
|
|
.filter(
|
|
|
|
|
(cmd) =>
|
|
|
|
|
cmd.startsWith(currentCmd) &&
|
|
|
|
|
cmd !== currentCmd &&
|
|
|
|
|
cmd.length > currentCmd.length,
|
|
|
|
|
)
|
|
|
|
|
.slice(0, 5);
|
|
|
|
|
|
|
|
|
|
if (matches.length === 1) {
|
|
|
|
|
const completedCommand = matches[0];
|
|
|
|
|
const completion = completedCommand.substring(currentCmd.length);
|
|
|
|
|
|
|
|
|
|
for (const char of completion) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({ type: "input", data: char }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCurrentCommandRef.current(completedCommand);
|
|
|
|
|
} else if (matches.length > 1) {
|
|
|
|
|
currentAutocompleteCommand.current = currentCmd;
|
|
|
|
|
setAutocompleteSuggestions(matches);
|
|
|
|
|
setAutocompleteSelectedIndex(0);
|
|
|
|
|
|
|
|
|
|
const cursorY = terminal.buffer.active.cursorY;
|
|
|
|
|
const cursorX = terminal.buffer.active.cursorX;
|
|
|
|
|
const rect = xtermRef.current?.getBoundingClientRect();
|
|
|
|
|
|
|
|
|
|
if (rect) {
|
|
|
|
|
const cellHeight =
|
|
|
|
|
terminal.rows > 0 ? rect.height / terminal.rows : 20;
|
|
|
|
|
const cellWidth =
|
|
|
|
|
terminal.cols > 0 ? rect.width / terminal.cols : 10;
|
|
|
|
|
|
|
|
|
|
const itemHeight = 32;
|
|
|
|
|
const footerHeight = 32;
|
|
|
|
|
const maxMenuHeight = 240;
|
|
|
|
|
const estimatedMenuHeight = Math.min(
|
|
|
|
|
matches.length * itemHeight + footerHeight,
|
|
|
|
|
maxMenuHeight,
|
|
|
|
|
);
|
|
|
|
|
const cursorBottomY = rect.top + (cursorY + 1) * cellHeight;
|
|
|
|
|
const cursorTopY = rect.top + cursorY * cellHeight;
|
|
|
|
|
const spaceBelow = window.innerHeight - cursorBottomY;
|
|
|
|
|
const spaceAbove = cursorTopY;
|
|
|
|
|
|
|
|
|
|
const showAbove =
|
|
|
|
|
spaceBelow < estimatedMenuHeight && spaceAbove > spaceBelow;
|
|
|
|
|
|
|
|
|
|
setAutocompletePosition({
|
|
|
|
|
top: showAbove
|
|
|
|
|
? Math.max(0, cursorTopY - estimatedMenuHeight)
|
|
|
|
|
: cursorBottomY,
|
|
|
|
|
left: Math.max(0, rect.left + cursorX * cellWidth),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setShowAutocomplete(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
terminal.attachCustomKeyEventHandler(handleCustomKey);
|
|
|
|
|
}, [terminal]);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
useEffect(() => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (!terminal || !hostConfig || !isVisible) return;
|
2025-11-05 10:36:16 -06:00
|
|
|
if (isConnected || isConnecting) return;
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
if (isReconnectingRef.current || reconnectTimeoutRef.current !== null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldNotReconnectRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 18:02:14 -05:00
|
|
|
if (
|
|
|
|
|
webSocketRef.current &&
|
|
|
|
|
(webSocketRef.current.readyState === WebSocket.OPEN ||
|
|
|
|
|
webSocketRef.current.readyState === WebSocket.CONNECTING)
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
if (terminal.cols < 10 || terminal.rows < 3) {
|
2025-11-05 10:36:16 -06:00
|
|
|
requestAnimationFrame(() => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (terminal.cols > 0 && terminal.rows > 0) {
|
|
|
|
|
setIsConnecting(true);
|
|
|
|
|
fitAddonRef.current?.fit();
|
2025-11-05 10:36:16 -06:00
|
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
2025-12-31 22:20:12 -06:00
|
|
|
connectToHost(terminal.cols, terminal.rows);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 22:20:12 -06:00
|
|
|
setIsConnecting(true);
|
|
|
|
|
fitAddonRef.current?.fit();
|
2026-02-12 22:28:13 -06:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
fitAddonRef.current?.fit();
|
|
|
|
|
if (terminal.cols > 0 && terminal.rows > 0) {
|
|
|
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
|
|
|
|
connectToHost(terminal.cols, terminal.rows);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-01-24 19:49:42 -06:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [terminal, hostConfig.id, isVisible, isConnected, isConnecting]);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-12-31 22:20:12 -06:00
|
|
|
if (!terminal || !fitAddonRef.current || !isVisible) return;
|
|
|
|
|
|
|
|
|
|
const fitTimeoutId = setTimeout(() => {
|
|
|
|
|
if (!isFittingRef.current && terminal.cols > 0 && terminal.rows > 0) {
|
|
|
|
|
performFit();
|
|
|
|
|
if (!splitScreen && !isConnecting) {
|
|
|
|
|
requestAnimationFrame(() => terminal.focus());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(fitTimeoutId);
|
|
|
|
|
}, [terminal, isVisible, splitScreen, isConnecting]);
|
2025-11-05 10:36:16 -06:00
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
const hasConnectionError = !!connectionError;
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
return (
|
|
|
|
|
<div className="h-full w-full relative" style={{ backgroundColor }}>
|
|
|
|
|
<div
|
|
|
|
|
ref={xtermRef}
|
|
|
|
|
className="h-full w-full"
|
|
|
|
|
style={{
|
2025-12-31 22:20:12 -06:00
|
|
|
pointerEvents: isVisible ? "auto" : "none",
|
2026-01-24 19:49:42 -06:00
|
|
|
visibility:
|
|
|
|
|
isConnected && isFitted && !connectionError
|
|
|
|
|
? "visible"
|
|
|
|
|
: "hidden",
|
2025-11-05 10:36:16 -06:00
|
|
|
}}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
if (terminal && !splitScreen) {
|
|
|
|
|
terminal.focus();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
<SimpleLoader
|
|
|
|
|
visible={isConnecting && !isConnectionLogExpanded}
|
|
|
|
|
message={t("terminal.connecting")}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<ConnectionLog
|
|
|
|
|
isConnecting={isConnecting}
|
|
|
|
|
isConnected={isConnected}
|
|
|
|
|
hasConnectionError={hasConnectionError}
|
|
|
|
|
position={hasConnectionError ? "top" : "bottom"}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
<TOTPDialog
|
|
|
|
|
isOpen={totpRequired}
|
|
|
|
|
prompt={totpPrompt}
|
|
|
|
|
onSubmit={handleTotpSubmit}
|
|
|
|
|
onCancel={handleTotpCancel}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<SSHAuthDialog
|
|
|
|
|
isOpen={showAuthDialog}
|
|
|
|
|
reason={authDialogReason}
|
|
|
|
|
onSubmit={handleAuthDialogSubmit}
|
|
|
|
|
onCancel={handleAuthDialogCancel}
|
|
|
|
|
hostInfo={{
|
|
|
|
|
ip: hostConfig.ip,
|
|
|
|
|
port: hostConfig.port,
|
|
|
|
|
username: hostConfig.username,
|
|
|
|
|
name: hostConfig.name,
|
|
|
|
|
}}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
<WarpgateDialog
|
|
|
|
|
isOpen={warpgateAuthRequired}
|
|
|
|
|
url={warpgateAuthUrl}
|
|
|
|
|
securityKey={warpgateSecurityKey}
|
|
|
|
|
onContinue={handleWarpgateContinue}
|
|
|
|
|
onCancel={handleWarpgateCancel}
|
|
|
|
|
onOpenUrl={handleWarpgateOpenUrl}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
{opksshDialog?.isOpen && (
|
|
|
|
|
<OPKSSHDialog
|
|
|
|
|
isOpen={opksshDialog.isOpen}
|
|
|
|
|
authUrl={opksshDialog.authUrl}
|
|
|
|
|
requestId={opksshDialog.requestId}
|
|
|
|
|
stage={opksshDialog.stage}
|
|
|
|
|
error={opksshDialog.error}
|
|
|
|
|
onCancel={() => {
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "opkssh_cancel",
|
|
|
|
|
data: { requestId: opksshDialog.requestId },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
setOpksshDialog(null);
|
|
|
|
|
if (opksshTimeoutRef.current) {
|
|
|
|
|
clearTimeout(opksshTimeoutRef.current);
|
|
|
|
|
opksshTimeoutRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onOpenUrl={() => {
|
|
|
|
|
window.open(opksshDialog.authUrl, "_blank");
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "opkssh_browser_opened",
|
|
|
|
|
data: { requestId: opksshDialog.requestId },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{hostKeyVerification?.isOpen && (
|
|
|
|
|
<HostKeyVerificationDialog
|
|
|
|
|
isOpen={true}
|
|
|
|
|
scenario={hostKeyVerification.scenario}
|
|
|
|
|
{...hostKeyVerification.data}
|
|
|
|
|
onAccept={() => {
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "host_key_verification_response",
|
|
|
|
|
data: { action: "accept" },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
setHostKeyVerification(null);
|
|
|
|
|
}}
|
|
|
|
|
onReject={() => {
|
|
|
|
|
if (webSocketRef.current) {
|
|
|
|
|
webSocketRef.current.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "host_key_verification_response",
|
|
|
|
|
data: { action: "reject" },
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
setHostKeyVerification(null);
|
|
|
|
|
setIsConnecting(false);
|
|
|
|
|
updateConnectionError(t("terminal.hostKeyRejected"));
|
|
|
|
|
}}
|
|
|
|
|
backgroundColor={backgroundColor}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-11-17 09:46:05 -06:00
|
|
|
<CommandAutocomplete
|
|
|
|
|
visible={showAutocomplete}
|
|
|
|
|
suggestions={autocompleteSuggestions}
|
|
|
|
|
selectedIndex={autocompleteSelectedIndex}
|
|
|
|
|
position={autocompletePosition}
|
|
|
|
|
onSelect={handleAutocompleteSelect}
|
|
|
|
|
/>
|
2025-11-05 10:36:16 -06:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-24 19:49:42 -06:00
|
|
|
export const Terminal = forwardRef<TerminalHandle, SSHTerminalProps>(
|
|
|
|
|
function Terminal(props, ref) {
|
|
|
|
|
return (
|
|
|
|
|
<ConnectionLogProvider>
|
|
|
|
|
<TerminalInner {...props} ref={ref} />
|
|
|
|
|
</ConnectionLogProvider>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const style = document.createElement("style");
|
|
|
|
|
style.innerHTML = `
|
|
|
|
|
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap');
|
|
|
|
|
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;700&display=swap');
|
|
|
|
|
@import url('https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap');
|
|
|
|
|
|
|
|
|
|
@font-face {
|
|
|
|
|
font-family: 'Caskaydia Cove Nerd Font Mono';
|
|
|
|
|
src: url('./fonts/CaskaydiaCoveNerdFontMono-Regular.ttf') format('truetype');
|
|
|
|
|
font-weight: normal;
|
|
|
|
|
font-style: normal;
|
|
|
|
|
font-display: swap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@font-face {
|
|
|
|
|
font-family: 'Caskaydia Cove Nerd Font Mono';
|
|
|
|
|
src: url('./fonts/CaskaydiaCoveNerdFontMono-Bold.ttf') format('truetype');
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
font-style: normal;
|
|
|
|
|
font-display: swap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@font-face {
|
|
|
|
|
font-family: 'Caskaydia Cove Nerd Font Mono';
|
|
|
|
|
src: url('./fonts/CaskaydiaCoveNerdFontMono-Italic.ttf') format('truetype');
|
|
|
|
|
font-weight: normal;
|
|
|
|
|
font-style: italic;
|
|
|
|
|
font-display: swap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@font-face {
|
|
|
|
|
font-family: 'Caskaydia Cove Nerd Font Mono';
|
|
|
|
|
src: url('./fonts/CaskaydiaCoveNerdFontMono-BoldItalic.ttf') format('truetype');
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
font-style: italic;
|
|
|
|
|
font-display: swap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.xterm .xterm-viewport::-webkit-scrollbar {
|
|
|
|
|
width: 8px;
|
|
|
|
|
background: transparent;
|
|
|
|
|
}
|
|
|
|
|
.xterm .xterm-viewport::-webkit-scrollbar-thumb {
|
2025-12-31 22:20:12 -06:00
|
|
|
background: rgba(0,0,0,0.3);
|
2025-11-05 10:36:16 -06:00
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
.xterm .xterm-viewport::-webkit-scrollbar-thumb:hover {
|
2025-12-31 22:20:12 -06:00
|
|
|
background: rgba(0,0,0,0.5);
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
.xterm .xterm-viewport {
|
|
|
|
|
scrollbar-width: thin;
|
2025-12-31 22:20:12 -06:00
|
|
|
scrollbar-color: rgba(0,0,0,0.3) transparent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dark .xterm .xterm-viewport::-webkit-scrollbar-thumb {
|
|
|
|
|
background: rgba(255,255,255,0.3);
|
|
|
|
|
}
|
|
|
|
|
.dark .xterm .xterm-viewport::-webkit-scrollbar-thumb:hover {
|
|
|
|
|
background: rgba(255,255,255,0.5);
|
|
|
|
|
}
|
|
|
|
|
.dark .xterm .xterm-viewport {
|
|
|
|
|
scrollbar-color: rgba(255,255,255,0.3) transparent;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.xterm {
|
2026-03-08 18:02:14 -05:00
|
|
|
font-feature-settings: "liga" 0, "calt" 0;
|
2025-11-05 10:36:16 -06:00
|
|
|
text-rendering: optimizeLegibility;
|
|
|
|
|
-webkit-font-smoothing: antialiased;
|
|
|
|
|
-moz-osx-font-smoothing: grayscale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.xterm .xterm-screen {
|
|
|
|
|
font-family: 'Caskaydia Cove Nerd Font Mono', 'SF Mono', Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace !important;
|
2026-03-08 18:02:14 -05:00
|
|
|
font-variant-ligatures: none;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.xterm .xterm-screen .xterm-char {
|
2026-03-08 18:02:14 -05:00
|
|
|
font-feature-settings: "liga" 0, "calt" 0;
|
2025-11-05 10:36:16 -06:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
document.head.appendChild(style);
|