mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-08-29 18:31:33 +00:00
* feat(sshid) - sshid.io equivalent for termix (#919) * feat(ssh-id): database schema, migrations and field encryption Adds ssh_identities, ssh_identity_keys and ssh_identity_ca tables (public keys stored plaintext for the unauthenticated resolver; CA private key registered for per-user field encryption), with UNIQUE(user_id), an index on ssh_identity_keys(identity_id), and idempotent CREATE TABLE migrations. * feat(ssh-id): backend API — resolver, key management, CA and certificates Mounts /sshid (nginx route added). Public text/plain authorized_keys resolver (+ exact /:algo filter, HTML viewer) and CA public-key endpoint; no-store + noindex headers on every resolver response including early 404s. Authenticated management: claim/rename/delete handle, add/import/generate/enable/delete keys, and a per-user CA (create/rotate/delete) with pure-Node OpenSSH certificate issuance. Audit logging on all mutations; UNIQUE races map to a precise 409. Unit tests for key parsing and certificate signing (ssh-keygen-validated). * feat(ssh-id): frontend panel, API client and i18n SSH ID panel wired into the app rail and AppShell: claim handle, resolver URL + curl one-liner, key list, generate, paste/import, CA enable/rotate/remove with server trust command, and per-key certificate issuance. API client re-exported through main-axios.ts; all strings i18n'd. * style(ssh-id): align panel and resolver page with Termix theme - Rebuild the SSH ID sidebar panel with the theme's square components (SectionCard / SettingRow / FakeSwitch) instead of rounded ad-hoc cards; use accent-brand and destructive tokens rather than raw red/green. - Fix panel scrolling: move overflow to a block scroll container so the cards keep their natural height instead of being clipped. - Restyle the public resolver HTML page (/sshid/u/:handle) to the Termix dark theme: square corners, #18181b/#303032 palette, #f59145 accent, uppercase section labels. - Tidy copy: 'Save To Credentials' label, drop the redundant generate intro, and correct the generate tooltip (the key is stored when saving to vault). * feat: rename to Termix ID, improve UI, backend inconsistencies, and general bug fixes --------- Co-authored-by: LukeGus <bugattiguy527@gmail.com> * ci(deps): bump actions/checkout from 6 to 7 in the github-actions group (#922) Bumps the github-actions group with 1 update: [actions/checkout](https://github.com/actions/checkout). Updates `actions/checkout` from 6 to 7 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v6...v7) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump the dev-patch-updates group with 11 updates (#923) Bumps the dev-patch-updates group with 11 updates: | Package | From | To | | --- | --- | --- | | [@codemirror/search](https://github.com/codemirror/search) | `6.7.0` | `6.7.1` | | [@codemirror/view](https://github.com/codemirror/view) | `6.43.0` | `6.43.1` | | [@tailwindcss/vite](https://github.com/tailwindlabs/tailwindcss/tree/HEAD/packages/@tailwindcss-vite) | `4.3.0` | `4.3.1` | | [@vitest/coverage-v8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-v8) | `4.1.8` | `4.1.9` | | [@vitest/ui](https://github.com/vitest-dev/vitest/tree/HEAD/packages/ui) | `4.1.8` | `4.1.9` | | [eslint-plugin-react-refresh](https://github.com/ArnaudBarre/eslint-plugin-react-refresh) | `0.5.2` | `0.5.3` | | [lint-staged](https://github.com/lint-staged/lint-staged) | `17.0.7` | `17.0.8` | | [prettier](https://github.com/prettier/prettier) | `3.8.3` | `3.8.4` | | [sharp](https://github.com/lovell/sharp) | `0.35.1` | `0.35.2` | | [tailwindcss](https://github.com/tailwindlabs/tailwindcss/tree/HEAD/packages/tailwindcss) | `4.3.0` | `4.3.1` | | [vitest](https://github.com/vitest-dev/vitest/tree/HEAD/packages/vitest) | `4.1.8` | `4.1.9` | Updates `@codemirror/search` from 6.7.0 to 6.7.1 - [Changelog](https://github.com/codemirror/search/blob/main/CHANGELOG.md) - [Commits](https://github.com/codemirror/search/commits) Updates `@codemirror/view` from 6.43.0 to 6.43.1 - [Changelog](https://github.com/codemirror/view/blob/main/CHANGELOG.md) - [Commits](https://github.com/codemirror/view/commits) Updates `@tailwindcss/vite` from 4.3.0 to 4.3.1 - [Release notes](https://github.com/tailwindlabs/tailwindcss/releases) - [Changelog](https://github.com/tailwindlabs/tailwindcss/blob/main/CHANGELOG.md) - [Commits](https://github.com/tailwindlabs/tailwindcss/commits/v4.3.1/packages/@tailwindcss-vite) Updates `@vitest/coverage-v8` from 4.1.8 to 4.1.9 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Changelog](https://github.com/vitest-dev/vitest/blob/main/docs/releases.md) - [Commits](https://github.com/vitest-dev/vitest/commits/v4.1.9/packages/coverage-v8) Updates `@vitest/ui` from 4.1.8 to 4.1.9 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Changelog](https://github.com/vitest-dev/vitest/blob/main/docs/releases.md) - [Commits](https://github.com/vitest-dev/vitest/commits/v4.1.9/packages/ui) Updates `eslint-plugin-react-refresh` from 0.5.2 to 0.5.3 - [Release notes](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/releases) - [Changelog](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/blob/main/CHANGELOG.md) - [Commits](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/compare/v0.5.2...v0.5.3) Updates `lint-staged` from 17.0.7 to 17.0.8 - [Release notes](https://github.com/lint-staged/lint-staged/releases) - [Changelog](https://github.com/lint-staged/lint-staged/blob/main/CHANGELOG.md) - [Commits](https://github.com/lint-staged/lint-staged/compare/v17.0.7...v17.0.8) Updates `prettier` from 3.8.3 to 3.8.4 - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.8.3...3.8.4) Updates `sharp` from 0.35.1 to 0.35.2 - [Release notes](https://github.com/lovell/sharp/releases) - [Commits](https://github.com/lovell/sharp/compare/v0.35.1...v0.35.2) Updates `tailwindcss` from 4.3.0 to 4.3.1 - [Release notes](https://github.com/tailwindlabs/tailwindcss/releases) - [Changelog](https://github.com/tailwindlabs/tailwindcss/blob/main/CHANGELOG.md) - [Commits](https://github.com/tailwindlabs/tailwindcss/commits/v4.3.1/packages/tailwindcss) Updates `vitest` from 4.1.8 to 4.1.9 - [Release notes](https://github.com/vitest-dev/vitest/releases) - [Changelog](https://github.com/vitest-dev/vitest/blob/main/docs/releases.md) - [Commits](https://github.com/vitest-dev/vitest/commits/v4.1.9/packages/vitest) --- updated-dependencies: - dependency-name: "@codemirror/search" dependency-version: 6.7.1 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: "@codemirror/view" dependency-version: 6.43.1 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: "@tailwindcss/vite" dependency-version: 4.3.1 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: "@vitest/coverage-v8" dependency-version: 4.1.9 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: "@vitest/ui" dependency-version: 4.1.9 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: eslint-plugin-react-refresh dependency-version: 0.5.3 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: lint-staged dependency-version: 17.0.8 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: prettier dependency-version: 3.8.4 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: sharp dependency-version: 0.35.2 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: tailwindcss dependency-version: 4.3.1 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates - dependency-name: vitest dependency-version: 4.1.9 dependency-type: direct:development update-type: version-update:semver-patch dependency-group: dev-patch-updates ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump nanoid in the prod-patch-updates group (#925) Bumps the prod-patch-updates group with 1 update: [nanoid](https://github.com/ai/nanoid). Updates `nanoid` from 5.1.11 to 5.1.15 - [Release notes](https://github.com/ai/nanoid/releases) - [Changelog](https://github.com/ai/nanoid/blob/main/CHANGELOG.md) - [Commits](https://github.com/ai/nanoid/compare/5.1.11...5.1.15) --- updated-dependencies: - dependency-name: nanoid dependency-version: 5.1.15 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: prod-patch-updates ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump the major-updates group with 5 updates (#926) Bumps the major-updates group with 5 updates: | Package | From | To | | --- | --- | --- | | [js-yaml](https://github.com/nodeca/js-yaml) | `4.2.0` | `5.0.0` | | [@eslint/js](https://github.com/eslint/eslint/tree/HEAD/packages/js) | `9.39.4` | `10.0.1` | | [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) | `25.9.2` | `26.0.0` | | [concurrently](https://github.com/open-cli-tools/concurrently) | `9.2.1` | `10.0.3` | | [eslint](https://github.com/eslint/eslint) | `9.39.4` | `10.5.0` | Updates `js-yaml` from 4.2.0 to 5.0.0 - [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md) - [Commits](https://github.com/nodeca/js-yaml/compare/4.2.0...5.0.0) Updates `@eslint/js` from 9.39.4 to 10.0.1 - [Release notes](https://github.com/eslint/eslint/releases) - [Commits](https://github.com/eslint/eslint/commits/v10.0.1/packages/js) Updates `@types/node` from 25.9.2 to 26.0.0 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Updates `concurrently` from 9.2.1 to 10.0.3 - [Release notes](https://github.com/open-cli-tools/concurrently/releases) - [Commits](https://github.com/open-cli-tools/concurrently/compare/v9.2.1...v10.0.3) Updates `eslint` from 9.39.4 to 10.5.0 - [Release notes](https://github.com/eslint/eslint/releases) - [Commits](https://github.com/eslint/eslint/compare/v9.39.4...v10.5.0) --- updated-dependencies: - dependency-name: js-yaml dependency-version: 5.0.0 dependency-type: direct:production update-type: version-update:semver-major dependency-group: major-updates - dependency-name: "@eslint/js" dependency-version: 10.0.1 dependency-type: direct:development update-type: version-update:semver-major dependency-group: major-updates - dependency-name: "@types/node" dependency-version: 26.0.0 dependency-type: direct:development update-type: version-update:semver-major dependency-group: major-updates - dependency-name: concurrently dependency-version: 10.0.3 dependency-type: direct:development update-type: version-update:semver-major dependency-group: major-updates - dependency-name: eslint dependency-version: 10.5.0 dependency-type: direct:development update-type: version-update:semver-major dependency-group: major-updates ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat(ssh): add HashiCorp Vault SSH signer authentication * fix: small fixes to vault feature to align with Termix codebase * chore: add view docs links for vault/termix id * fix: file upload fails with 400 and missing schema migrations on upgrade (#929) Two bugs introduced in v2.4.1: 1. uploadFileStream uses fileManagerApi.post() which triggers axios's transformRequest to JSON-serialize the FormData because the instance default Content-Type is application/json. Change to postForm() which sets Content-Type: multipart/form-data so the browser XHR sends the correct multipart body with boundary. 2. Two schema items added to schema.ts were not included in migrateSchema() in db/index.ts, causing 500 errors on existing installations upgrading from v2.4.0: - user_preferences.status_color_scheme (no such column) - dashboard_service_links table (no such table) Fixes #928 Co-authored-by: sash <sash@fominykh.io> * fix: support PuTTY PPK ssh keys (#930) * fix: chunk large file manager uploads (#932) * fix: route dashboard hosts by protocol (#934) * fix: resolve tunnel endpoints reliably (#935) * Fix Electron OIDC browser auth failures (#936) * Allow RDP connections without stored credentials (#937) * Sync role credential shares for OIDC users (#938) * Fix terminal link dialog layering (#940) * Confirm large files before opening editor (#942) * Confirm closing active host connections (#943) * Preserve file path case in file manager UI (#941) * fix: preserve unicode guacamole tokens (#933) * Persist VNC authentication settings (#944) * Fix Guacamole websocket base path (#946) * Promote file manager terminals to tabs (#939) * Guard Guacamole disconnect during startup (#945) * chore: increment ver * feat: bitwarden ssh agent integration * feat: serial connections support * fix: various small bug fixes * feat: open all sessions in a folder and terminal custom theme color support * feat: cross host file manager clipboard and several small bug fixes * feat: tailscale/wireguard support and added a new status state for when backend is checking status * feat: grafana like server stats history, new alert system, ntfy/webhook support * feat: new grid and widget based homepage function * feat: new donate button in dashboard * fix: alert ui incorrectly using termix css and fixed issue with alert system not loading * chore: fix ci checks (#966) * Fix dashboard service link creation (#950) * Fix jump host SOCKS5 proxy selection (#951) * Fix jump host SOCKS5 proxy selection * fix: type jump host socks proxy config * Fix tmux detection path handling (#949) * Support GUACD_URL environment config (#952) * Retry autostart tunnel host fetches (#953) * Retry autostart tunnel host fetches * chore: format tunnel route * Fix PUID html ownership in Docker entrypoint (#954) * feat: allow custom tunnel endpoints (#977) * fix: skip metrics start for non-ssh hosts (#976) * Fix Proxmox import auth fallback (#956) * Fix Proxmox import auth fallback * chore: format proxmox import auth * Fix SSH heading syntax highlighting (#955) * Fix SSH heading syntax highlighting * chore: format terminal highlighter * Initialize auth before fullscreen terminal routes (#957) * Add WebAuthn passkey authentication (#959) * chore: add Biome tooling (#965) * chore: add biome tooling * chore: support tailwind syntax in biome * Fix VNC required argument handshake (#968) * Prioritize host results in command palette search (#969) * Prevent sidebar host hover layout shift (#970) * Add terminal font zoom with mouse wheel (#971) * Add host temperature metrics card (#972) * Make file downloads reliable in desktop app (#973) * Add app rail hover expansion setting (#974) * fix: use correct translation key for nav.close (#964) * fix(tunnel): skip endpoint credential validation for direct tunnels (#963) * fix: SSH port connection bug (#975) * fix: chunked upload for files >=1.5GB to bypass browser ArrayBuffer limit (#948) * feat: support SSH agent auth across SSH features (#960) * feat: add Podman container runtime support (#958) * chore: update readme and release notes * fix: stabilize Windows app icon (#978) * Add safe host sharing export (#979) * Add external editor support for file manager (#985) * Rework SSH credential password handling (#984) * Support password fallback for SSH key credentials * Complete SSH credential password fallback * Fix runtime base path for auth callbacks (#982) * Fix TUI terminal output highlighting (#983) Co-authored-by: LukeGus <bugattiguy527@gmail.com> * Add app fullscreen mode (#993) * Fix host metrics startup polling (#986) * chore: update release notes * fix: line chart text overlap * chore: update RELEASE_NOTES.md * chore: add donation goal to readme * chore: update readme * fix: hide full-screen button in electron app * fix: failed unit tests * chore: lint, format, and bump version to 2.5.0 * chore: remove timeout from crowdin translate * chore: sync Crowdin translations for 2.5.0 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: DivByZero <mr.oplus@yahoo.fr> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: devdanetra <46488477+devdanetra@users.noreply.github.com> Co-authored-by: Aleksandr Fominykh <neoformalex@users.noreply.github.com> Co-authored-by: sash <sash@fominykh.io> Co-authored-by: ZacharyZcR <zacharyzcr1984@gmail.com>
3156 lines
104 KiB
TypeScript
3156 lines
104 KiB
TypeScript
/* eslint-disable react-hooks/exhaustive-deps */
|
|
import {
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
useImperativeHandle,
|
|
forwardRef,
|
|
useCallback,
|
|
} from "react";
|
|
import { createPortal } from "react-dom";
|
|
import { useXTerm } from "react-xtermjs";
|
|
import { FitAddon } from "@xterm/addon-fit";
|
|
import { ClipboardAddon } from "@xterm/addon-clipboard";
|
|
import { RobustClipboardProvider } from "@/lib/clipboard-provider";
|
|
import { copyToClipboard } from "@/lib/clipboard";
|
|
import { Unicode11Addon } from "@xterm/addon-unicode11";
|
|
import { WebLinksAddon } from "@xterm/addon-web-links";
|
|
import { useTranslation } from "react-i18next";
|
|
import { getBasePath } from "@/lib/base-path";
|
|
import {
|
|
getCookie,
|
|
isElectron,
|
|
isEmbeddedMode,
|
|
logActivity,
|
|
getSnippets,
|
|
deleteCommandFromHistory,
|
|
getCommandHistory,
|
|
getHostPassword,
|
|
getServerConfig,
|
|
} from "@/main-axios.ts";
|
|
import { TOTPDialog } from "@/ssh/dialogs/TOTPDialog.tsx";
|
|
import { SSHAuthDialog } from "@/ssh/dialogs/SSHAuthDialog.tsx";
|
|
import { PassphraseDialog } from "@/ssh/dialogs/PassphraseDialog.tsx";
|
|
import { WarpgateDialog } from "@/ssh/dialogs/WarpgateDialog.tsx";
|
|
import { OPKSSHDialog } from "@/ssh/dialogs/OPKSSHDialog.tsx";
|
|
import { HostKeyVerificationDialog } from "@/ssh/dialogs/HostKeyVerificationDialog.tsx";
|
|
import { TmuxSessionPicker } from "@/ssh/dialogs/TmuxSessionPicker.tsx";
|
|
import {
|
|
DEFAULT_TERMINAL_CONFIG,
|
|
TERMINAL_FONTS,
|
|
} from "@/lib/terminal-themes.ts";
|
|
import "./terminal-global-styles.ts";
|
|
import { useTheme } from "@/components/theme-provider.tsx";
|
|
import { globalShortcutHandler } from "@/lib/global-shortcut-handler";
|
|
import { useCommandTracker } from "@/features/terminal/command-history/useCommandTracker.ts";
|
|
import { highlightTerminalOutput } from "@/lib/terminal-syntax-highlighter.ts";
|
|
import { useCommandHistory } from "@/features/terminal/command-history/CommandHistoryContext.tsx";
|
|
import { CommandAutocomplete } from "./command-history/CommandAutocomplete.tsx";
|
|
import { SimpleLoader } from "@/lib/SimpleLoader.tsx";
|
|
import { useConfirmation } from "@/hooks/use-confirmation.ts";
|
|
import {
|
|
ConnectionLogProvider,
|
|
useConnectionLog,
|
|
} from "@/ssh/connection-log/ConnectionLogContext.tsx";
|
|
import { ConnectionLog } from "@/ssh/connection-log/ConnectionLog.tsx";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/button";
|
|
import { resolveTermixThemeColors } from "./terminal-theme.ts";
|
|
import type { TerminalHandle, TerminalHostConfig } from "./terminal-types.ts";
|
|
export type { TerminalHandle, TerminalHostConfig } from "./terminal-types.ts";
|
|
|
|
type HostKeyVerificationData = Omit<
|
|
React.ComponentProps<typeof HostKeyVerificationDialog>,
|
|
"isOpen" | "scenario" | "onAccept" | "onReject" | "backgroundColor"
|
|
>;
|
|
|
|
interface SSHTerminalProps {
|
|
hostConfig: TerminalHostConfig;
|
|
isVisible: boolean;
|
|
title?: string;
|
|
showTitle?: boolean;
|
|
splitScreen?: boolean;
|
|
onClose?: () => void;
|
|
onTitleChange?: (title: string) => void;
|
|
initialPath?: string;
|
|
executeCommand?: string;
|
|
/** Attach to this tmux session right after connecting (tmux monitor). */
|
|
tmuxAttachSession?: string;
|
|
onOpenFileManager?: (path?: string) => void;
|
|
onOpenFileInEditor?: (filePath: string) => void;
|
|
previewTheme?: string | null;
|
|
/** When true, suppress automatic focus on connect/visibility change. */
|
|
disableAutoFocus?: boolean;
|
|
}
|
|
|
|
const TERMINAL_FONT_ZOOM_MIN = 8;
|
|
const TERMINAL_FONT_ZOOM_MAX = 36;
|
|
const ALTERNATE_SCREEN_SEQUENCE = /\x1b\[\?(47|1047|1049)([hl])/g;
|
|
|
|
function updateAlternateScreenMode(output: string, currentMode: boolean) {
|
|
ALTERNATE_SCREEN_SEQUENCE.lastIndex = 0;
|
|
let isActive = currentMode;
|
|
let sawSequence = false;
|
|
let match: RegExpExecArray | null;
|
|
|
|
while ((match = ALTERNATE_SCREEN_SEQUENCE.exec(output)) !== null) {
|
|
sawSequence = true;
|
|
isActive = match[2] === "h";
|
|
}
|
|
|
|
return { isActive, sawSequence };
|
|
}
|
|
|
|
const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
|
|
function SSHTerminal(
|
|
{
|
|
hostConfig,
|
|
isVisible,
|
|
splitScreen = false,
|
|
onClose,
|
|
onTitleChange,
|
|
initialPath,
|
|
executeCommand,
|
|
tmuxAttachSession,
|
|
onOpenFileManager,
|
|
onOpenFileInEditor,
|
|
previewTheme,
|
|
disableAutoFocus = false,
|
|
},
|
|
ref,
|
|
) {
|
|
const { t } = useTranslation();
|
|
const { instance: terminal, ref: xtermRef } = useXTerm();
|
|
const commandHistoryContext = useCommandHistory();
|
|
const { confirmWithToast } = useConfirmation();
|
|
const { theme: appTheme } = useTheme();
|
|
const { addLog, isExpanded: isConnectionLogExpanded } = useConnectionLog();
|
|
|
|
const savedTheme = localStorage.getItem(
|
|
`terminal_theme_host_${hostConfig.id}`,
|
|
);
|
|
const config = {
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
...hostConfig.terminalConfig,
|
|
theme:
|
|
savedTheme ||
|
|
hostConfig.terminalConfig?.theme ||
|
|
DEFAULT_TERMINAL_CONFIG.theme,
|
|
};
|
|
|
|
const activeTheme = previewTheme || config.theme;
|
|
const themeColors = resolveTermixThemeColors(
|
|
activeTheme,
|
|
appTheme,
|
|
config.customThemeColors,
|
|
);
|
|
const backgroundImage = config.backgroundImage || "";
|
|
const backgroundImageOpacity = config.backgroundImageOpacity ?? 0.15;
|
|
const backgroundColor = backgroundImage
|
|
? "transparent"
|
|
: 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 pongReceivedRef = useRef(true);
|
|
const pongTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
const [isConnected, setIsConnected] = useState(false);
|
|
const [isConnecting, setIsConnecting] = useState(false);
|
|
const [isFitted, setIsFitted] = useState(false);
|
|
const [connectionError, setConnectionError] = useState<string | null>(null);
|
|
const connectionErrorRef = useRef<string | null>(null);
|
|
const [showDisconnectedOverlay, setShowDisconnectedOverlay] =
|
|
useState(false);
|
|
|
|
const updateConnectionError = useCallback((error: string | null) => {
|
|
connectionErrorRef.current = error;
|
|
setConnectionError(error);
|
|
}, []);
|
|
|
|
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 [showPassphraseDialog, setShowPassphraseDialog] = useState(false);
|
|
const [, setKeyboardInteractiveDetected] = useState(false);
|
|
const [warpgateAuthRequired, setWarpgateAuthRequired] = useState(false);
|
|
const [warpgateAuthUrl, setWarpgateAuthUrl] = useState<string>("");
|
|
const [warpgateSecurityKey, setWarpgateSecurityKey] = useState<string>("");
|
|
const warpgateTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
const [opksshDialog, setOpksshDialog] = useState<{
|
|
isOpen: boolean;
|
|
authUrl: string;
|
|
requestId: string;
|
|
stage: "chooser" | "waiting" | "authenticating" | "completed" | "error";
|
|
error?: string;
|
|
providers?: Array<{ alias: string; issuer: string }>;
|
|
} | null>(null);
|
|
const opksshTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
const opksshFailedRef = useRef(false);
|
|
const currentHostIdRef = useRef<number | null>(null);
|
|
const currentHostConfigRef = useRef<TerminalHostConfig | null>(null);
|
|
|
|
// Vault SSH signer interactive OIDC flow
|
|
const [vaultDialog, setVaultDialog] = useState<{
|
|
stage: "waiting" | "error";
|
|
error?: string;
|
|
} | null>(null);
|
|
const vaultFailedRef = useRef(false);
|
|
const vaultPopupRef = useRef<Window | null>(null);
|
|
const vaultTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
const [hostKeyVerification, setHostKeyVerification] = useState<{
|
|
isOpen: boolean;
|
|
scenario: "new" | "changed";
|
|
data: HostKeyVerificationData;
|
|
} | null>(null);
|
|
|
|
const sessionIdRef = useRef<string | null>(null);
|
|
const isAttachingSessionRef = useRef<boolean>(false);
|
|
// Consumed on first connectToHost call so retries don't re-attempt a stale session
|
|
const pendingRestoredSessionIdRef = useRef<string | null>(
|
|
hostConfig.restoredSessionId ?? null,
|
|
);
|
|
const [linkClickDialog, setLinkClickDialog] = useState<{
|
|
url: string;
|
|
} | null>(null);
|
|
|
|
const [tmuxSessionPicker, setTmuxSessionPicker] = useState<{
|
|
sessions: Array<{
|
|
name: string;
|
|
created: number;
|
|
lastActivity: number;
|
|
windows: number;
|
|
attachedClients: number;
|
|
}>;
|
|
} | null>(null);
|
|
const tmuxSessionNameRef = useRef<string | null>(null);
|
|
const [isTmuxAttached, setIsTmuxAttached] = useState(false);
|
|
const tmuxCopyModeHintShownRef = useRef(false);
|
|
|
|
const isVisibleRef = useRef<boolean>(false);
|
|
const isFittingRef = useRef(false);
|
|
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
const reconnectAttempts = useRef(0);
|
|
const maxReconnectAttempts = 8;
|
|
const isUnmountingRef = useRef(false);
|
|
const shouldNotReconnectRef = useRef(false);
|
|
const isReconnectingRef = useRef(false);
|
|
const isConnectingRef = useRef(false);
|
|
const wasConnectedRef = useRef(false);
|
|
const wasSessionExpiredRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
isUnmountingRef.current = false;
|
|
shouldNotReconnectRef.current = false;
|
|
isReconnectingRef.current = false;
|
|
isConnectingRef.current = false;
|
|
reconnectAttempts.current = 0;
|
|
wasConnectedRef.current = false;
|
|
isAttachingSessionRef.current = false;
|
|
|
|
return () => {};
|
|
}, [hostConfig.id]);
|
|
const connectionAttemptIdRef = useRef(0);
|
|
const totpTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
const connectionTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
const activityLoggedRef = useRef(false);
|
|
const commandHistoryTrackingEnabled =
|
|
hostConfig.enableCommandHistory !== false;
|
|
|
|
const { trackInput, getCurrentCommand, updateCurrentCommand } =
|
|
useCommandTracker({
|
|
hostId: hostConfig.id,
|
|
enabled: commandHistoryTrackingEnabled,
|
|
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] = useState(false);
|
|
const [, setCommandHistory] = useState<string[]>([]);
|
|
const [, 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);
|
|
getCommandHistory(hostConfig.id!)
|
|
.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 =
|
|
localStorage.getItem("commandAutocomplete") === "true";
|
|
|
|
if (hostConfig.id && autocompleteEnabled) {
|
|
getCommandHistory(hostConfig.id!)
|
|
.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]);
|
|
|
|
const activityLoggingRef = useRef(false);
|
|
const passwordPromptShownRef = useRef(false);
|
|
const alternateScreenModeRef = useRef(false);
|
|
|
|
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);
|
|
const lastFittedSizeRef = useRef<{ cols: number; rows: number } | null>(
|
|
null,
|
|
);
|
|
const terminalFontSizeRef = useRef(config.fontSize);
|
|
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 = () => {
|
|
setIsAuthenticated((prev) => {
|
|
if (!prev) {
|
|
return true;
|
|
}
|
|
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);
|
|
}
|
|
} catch (error) {
|
|
console.error("Terminal operation failed:", error);
|
|
}
|
|
}
|
|
|
|
function performFit() {
|
|
if (
|
|
!fitAddonRef.current ||
|
|
!terminal ||
|
|
!isVisible ||
|
|
isFittingRef.current
|
|
) {
|
|
return;
|
|
}
|
|
|
|
isFittingRef.current = true;
|
|
|
|
try {
|
|
fitAddonRef.current.fit();
|
|
if (terminal && terminal.cols > 0 && terminal.rows > 0) {
|
|
const lastSize = lastFittedSizeRef.current;
|
|
if (
|
|
!lastSize ||
|
|
lastSize.cols !== terminal.cols ||
|
|
lastSize.rows !== terminal.rows
|
|
) {
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
|
lastFittedSizeRef.current = {
|
|
cols: terminal.cols,
|
|
rows: terminal.rows,
|
|
};
|
|
}
|
|
}
|
|
setIsFitted(true);
|
|
} finally {
|
|
isFittingRef.current = false;
|
|
}
|
|
}
|
|
|
|
function zoomTerminalFont(deltaY: number) {
|
|
const direction = deltaY < 0 ? 1 : -1;
|
|
const currentFontSize =
|
|
terminal.options.fontSize ??
|
|
terminalFontSizeRef.current ??
|
|
DEFAULT_TERMINAL_CONFIG.fontSize;
|
|
const nextFontSize = Math.min(
|
|
TERMINAL_FONT_ZOOM_MAX,
|
|
Math.max(TERMINAL_FONT_ZOOM_MIN, currentFontSize + direction),
|
|
);
|
|
|
|
if (nextFontSize === currentFontSize) {
|
|
return;
|
|
}
|
|
|
|
terminalFontSizeRef.current = nextFontSize;
|
|
terminal.options.fontSize = nextFontSize;
|
|
performFit();
|
|
hardRefresh();
|
|
}
|
|
|
|
function handleTotpSubmit(code: string) {
|
|
if (webSocketRef.current && code) {
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
totpTimeoutRef.current = null;
|
|
}
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: isPasswordPrompt ? "password_response" : "totp_response",
|
|
data: { code },
|
|
}),
|
|
);
|
|
setTotpRequired(false);
|
|
setTotpPrompt("");
|
|
setIsPasswordPrompt(false);
|
|
}
|
|
}
|
|
|
|
function handleTotpCancel() {
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
totpTimeoutRef.current = null;
|
|
}
|
|
setTotpRequired(false);
|
|
setTotpPrompt("");
|
|
if (onClose) onClose();
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
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 handlePassphraseSubmit(passphrase: string) {
|
|
if (webSocketRef.current && terminal) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "reconnect_with_credentials",
|
|
data: {
|
|
cols: terminal.cols,
|
|
rows: terminal.rows,
|
|
keyPassword: passphrase,
|
|
hostConfig: {
|
|
...hostConfig,
|
|
keyPassword: passphrase,
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
setShowPassphraseDialog(false);
|
|
setIsConnecting(true);
|
|
}
|
|
}
|
|
|
|
function handlePassphraseCancel() {
|
|
setShowPassphraseDialog(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);
|
|
}
|
|
|
|
function formatTerminalOutput(output: string): string {
|
|
const alternateScreen = updateAlternateScreenMode(
|
|
output,
|
|
alternateScreenModeRef.current,
|
|
);
|
|
alternateScreenModeRef.current = alternateScreen.isActive;
|
|
|
|
const syntaxHighlightingEnabled =
|
|
hostConfig.terminalConfig?.syntaxHighlighting !== false;
|
|
if (
|
|
!syntaxHighlightingEnabled ||
|
|
alternateScreen.sawSequence ||
|
|
alternateScreen.isActive
|
|
) {
|
|
return output;
|
|
}
|
|
|
|
return highlightTerminalOutput(
|
|
output,
|
|
hostConfig.terminalConfig?.syntaxHighlightingOptions,
|
|
);
|
|
}
|
|
|
|
async function resolvePasswordForPrompt(isSudoPrompt: boolean) {
|
|
let passwordToFill = isSudoPrompt
|
|
? hostConfig.terminalConfig?.sudoPassword || hostConfig.password
|
|
: hostConfig.password || hostConfig.terminalConfig?.sudoPassword;
|
|
|
|
if (!passwordToFill && hostConfig.id) {
|
|
passwordToFill = isSudoPrompt
|
|
? (await getHostPassword(hostConfig.id, "sudoPassword")) ||
|
|
(await getHostPassword(hostConfig.id, "password")) ||
|
|
undefined
|
|
: (await getHostPassword(hostConfig.id, "password")) ||
|
|
(await getHostPassword(hostConfig.id, "sudoPassword")) ||
|
|
undefined;
|
|
}
|
|
|
|
return passwordToFill;
|
|
}
|
|
|
|
function maybeOfferPasswordFill(strippedData: string) {
|
|
const passwordPromptPattern =
|
|
/(?:\[sudo\][^\n\r]*:\s*$|sudo:[^\n\r]*password[^\n\r]*required|password for [^\n\r]*:\s*$|Password:\s*$|password:\s*$)/im;
|
|
if (!passwordPromptPattern.test(strippedData)) return;
|
|
|
|
const hasStoredPassword =
|
|
hostConfig.terminalConfig?.sudoPassword ||
|
|
hostConfig.password ||
|
|
hostConfig.hasSudoPassword ||
|
|
hostConfig.hasPassword;
|
|
if (!hasStoredPassword || passwordPromptShownRef.current) return;
|
|
|
|
passwordPromptShownRef.current = true;
|
|
const isSudoPrompt = /(?:\[sudo\]|sudo:)/i.test(strippedData);
|
|
|
|
confirmWithToast(
|
|
t("terminal.passwordPromptFillTitle"),
|
|
async () => {
|
|
const passwordToFill = await resolvePasswordForPrompt(isSudoPrompt);
|
|
if (
|
|
passwordToFill &&
|
|
webSocketRef.current &&
|
|
webSocketRef.current.readyState === WebSocket.OPEN
|
|
) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "input",
|
|
data: passwordToFill + "\n",
|
|
}),
|
|
);
|
|
}
|
|
setTimeout(() => {
|
|
passwordPromptShownRef.current = false;
|
|
}, 3000);
|
|
},
|
|
t("common.confirm"),
|
|
t("common.cancel"),
|
|
{ confirmOnEnter: true },
|
|
);
|
|
setTimeout(() => {
|
|
passwordPromptShownRef.current = false;
|
|
}, 15000);
|
|
}
|
|
|
|
useImperativeHandle(
|
|
ref,
|
|
() => ({
|
|
disconnect: () => {
|
|
isUnmountingRef.current = true;
|
|
shouldNotReconnectRef.current = true;
|
|
isReconnectingRef.current = false;
|
|
if (pingIntervalRef.current) {
|
|
clearInterval(pingIntervalRef.current);
|
|
pingIntervalRef.current = null;
|
|
}
|
|
if (pongTimeoutRef.current) {
|
|
clearTimeout(pongTimeoutRef.current);
|
|
pongTimeoutRef.current = null;
|
|
}
|
|
if (reconnectTimeoutRef.current) {
|
|
clearTimeout(reconnectTimeoutRef.current);
|
|
reconnectTimeoutRef.current = null;
|
|
}
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
totpTimeoutRef.current = null;
|
|
}
|
|
if (warpgateTimeoutRef.current) {
|
|
clearTimeout(warpgateTimeoutRef.current);
|
|
warpgateTimeoutRef.current = null;
|
|
}
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
webSocketRef.current.send(JSON.stringify({ type: "disconnect" }));
|
|
}
|
|
sessionIdRef.current = null;
|
|
webSocketRef.current?.close();
|
|
setIsConnected(false);
|
|
setIsConnecting(false);
|
|
},
|
|
reconnect: () => {
|
|
isUnmountingRef.current = false;
|
|
shouldNotReconnectRef.current = false;
|
|
isReconnectingRef.current = false;
|
|
isConnectingRef.current = false;
|
|
reconnectAttempts.current = 0;
|
|
wasDisconnectedBySSH.current = false;
|
|
wasConnectedRef.current = false;
|
|
updateConnectionError(null);
|
|
setShowDisconnectedOverlay(false);
|
|
if (terminal) {
|
|
terminal.clear();
|
|
const cols = terminal.cols;
|
|
const rows = terminal.rows;
|
|
connectToHost(cols, rows);
|
|
}
|
|
},
|
|
isConnected: () => isConnected,
|
|
fit: () => {
|
|
if (!fitAddonRef.current || !terminal || isFittingRef.current) return;
|
|
isFittingRef.current = true;
|
|
try {
|
|
fitAddonRef.current.fit();
|
|
if (terminal.cols > 0 && terminal.rows > 0) {
|
|
const lastSize = lastFittedSizeRef.current;
|
|
if (
|
|
!lastSize ||
|
|
lastSize.cols !== terminal.cols ||
|
|
lastSize.rows !== terminal.rows
|
|
) {
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
|
lastFittedSizeRef.current = {
|
|
cols: terminal.cols,
|
|
rows: terminal.rows,
|
|
};
|
|
}
|
|
}
|
|
setIsFitted(true);
|
|
} finally {
|
|
isFittingRef.current = false;
|
|
}
|
|
},
|
|
focus: () => terminal?.focus(),
|
|
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();
|
|
}
|
|
} catch (error) {
|
|
console.error("Terminal operation failed:", error);
|
|
}
|
|
},
|
|
refresh: () => hardRefresh(),
|
|
getApplicationCursorKeysMode: () =>
|
|
terminal?.modes?.applicationCursorKeysMode ?? false,
|
|
openFileManager: () => {
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
webSocketRef.current.send(JSON.stringify({ type: "get_cwd" }));
|
|
} else {
|
|
onOpenFileManager?.("/");
|
|
}
|
|
},
|
|
}),
|
|
[isConnected, terminal],
|
|
);
|
|
|
|
function getUseRightClickCopyPaste() {
|
|
return getCookie("rightClickCopyPaste") !== "false";
|
|
}
|
|
|
|
function attemptReconnection() {
|
|
if (
|
|
isUnmountingRef.current ||
|
|
shouldNotReconnectRef.current ||
|
|
isReconnectingRef.current ||
|
|
isConnectingRef.current ||
|
|
wasDisconnectedBySSH.current ||
|
|
reconnectTimeoutRef.current !== null
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (reconnectAttempts.current >= maxReconnectAttempts) {
|
|
setIsConnecting(false);
|
|
shouldNotReconnectRef.current = true;
|
|
setShowDisconnectedOverlay(true);
|
|
addLog({
|
|
type: "error",
|
|
stage: "connection",
|
|
message: t("terminal.maxReconnectAttemptsReached"),
|
|
});
|
|
return;
|
|
}
|
|
|
|
isReconnectingRef.current = true;
|
|
|
|
if (terminal && !isAttachingSessionRef.current) {
|
|
terminal.clear();
|
|
}
|
|
|
|
reconnectAttempts.current++;
|
|
|
|
addLog({
|
|
type: "info",
|
|
stage: "connection",
|
|
message: t("terminal.reconnecting", {
|
|
attempt: reconnectAttempts.current,
|
|
max: maxReconnectAttempts,
|
|
}),
|
|
});
|
|
|
|
const delay = Math.min(
|
|
2000 * Math.pow(2, reconnectAttempts.current - 1),
|
|
8000,
|
|
);
|
|
|
|
reconnectTimeoutRef.current = setTimeout(() => {
|
|
reconnectTimeoutRef.current = null;
|
|
|
|
if (
|
|
isUnmountingRef.current ||
|
|
shouldNotReconnectRef.current ||
|
|
wasDisconnectedBySSH.current
|
|
) {
|
|
isReconnectingRef.current = false;
|
|
return;
|
|
}
|
|
|
|
if (reconnectAttempts.current > maxReconnectAttempts) {
|
|
isReconnectingRef.current = false;
|
|
return;
|
|
}
|
|
|
|
if (terminal && hostConfig) {
|
|
if (!isAttachingSessionRef.current) {
|
|
terminal.clear();
|
|
}
|
|
const cols = terminal.cols;
|
|
const rows = terminal.rows;
|
|
connectToHost(cols, rows);
|
|
}
|
|
|
|
isReconnectingRef.current = false;
|
|
}, delay);
|
|
}
|
|
|
|
async function connectToHost(cols: number, rows: number) {
|
|
if (isConnectingRef.current) {
|
|
return;
|
|
}
|
|
|
|
isConnectingRef.current = true;
|
|
connectionAttemptIdRef.current++;
|
|
wasConnectedRef.current = false;
|
|
|
|
if (!isReconnectingRef.current) {
|
|
reconnectAttempts.current = 0;
|
|
shouldNotReconnectRef.current = false;
|
|
}
|
|
|
|
const isDev =
|
|
!isElectron() &&
|
|
process.env.NODE_ENV === "development" &&
|
|
(window.location.port === "3000" ||
|
|
window.location.port === "5173" ||
|
|
window.location.port === "");
|
|
|
|
let baseWsUrl: string;
|
|
|
|
if (isDev) {
|
|
baseWsUrl = `${window.location.protocol === "https:" ? "wss" : "ws"}://localhost:30002`;
|
|
} else if (isElectron()) {
|
|
let configuredUrl = (window as { configuredServerUrl?: string | null })
|
|
.configuredServerUrl;
|
|
|
|
if (!configuredUrl && !isEmbeddedMode()) {
|
|
try {
|
|
const serverConfig = await getServerConfig();
|
|
configuredUrl = serverConfig?.serverUrl || null;
|
|
if (configuredUrl) {
|
|
(
|
|
window as Window &
|
|
typeof globalThis & {
|
|
configuredServerUrl?: string | null;
|
|
}
|
|
).configuredServerUrl = configuredUrl;
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to resolve Electron server URL:", error);
|
|
}
|
|
}
|
|
|
|
if (isEmbeddedMode()) {
|
|
baseWsUrl = "ws://127.0.0.1:30002";
|
|
const storedJwt = localStorage.getItem("jwt");
|
|
if (storedJwt) {
|
|
baseWsUrl += `?token=${encodeURIComponent(storedJwt)}`;
|
|
}
|
|
} else if (!configuredUrl) {
|
|
console.error("No configured server URL available for Electron SSH");
|
|
setIsConnected(false);
|
|
setIsConnecting(false);
|
|
updateConnectionError(t("errors.failedToLoadServer"));
|
|
isConnectingRef.current = false;
|
|
return;
|
|
} else {
|
|
const wsProtocol = configuredUrl.startsWith("https://")
|
|
? "wss://"
|
|
: "ws://";
|
|
const wsHost = configuredUrl
|
|
.replace(/^https?:\/\//, "")
|
|
.replace(/\/$/, "");
|
|
baseWsUrl = `${wsProtocol}${wsHost}/ssh/websocket/`;
|
|
const storedJwt = localStorage.getItem("jwt");
|
|
if (storedJwt) {
|
|
baseWsUrl += `?token=${encodeURIComponent(storedJwt)}`;
|
|
}
|
|
}
|
|
} else {
|
|
baseWsUrl = `${getBasePath()}/ssh/websocket/`;
|
|
}
|
|
|
|
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 ws = new WebSocket(baseWsUrl);
|
|
webSocketRef.current = ws;
|
|
wasDisconnectedBySSH.current = false;
|
|
updateConnectionError(null);
|
|
shouldNotReconnectRef.current = false;
|
|
isReconnectingRef.current = false;
|
|
setIsConnecting(true);
|
|
|
|
setupWebSocketListeners(ws, cols, rows);
|
|
}
|
|
|
|
function setupWebSocketListeners(
|
|
ws: WebSocket,
|
|
cols: number,
|
|
rows: number,
|
|
) {
|
|
ws.addEventListener("open", () => {
|
|
alternateScreenModeRef.current = false;
|
|
connectionTimeoutRef.current = setTimeout(() => {
|
|
if (
|
|
!isConnected &&
|
|
!totpRequired &&
|
|
!isPasswordPrompt &&
|
|
!connectionErrorRef.current
|
|
) {
|
|
if (terminal) {
|
|
terminal.clear();
|
|
}
|
|
const timeoutMessage = t("terminal.connectionTimeout");
|
|
updateConnectionError(timeoutMessage);
|
|
addLog({
|
|
type: "error",
|
|
stage: "connection",
|
|
message: timeoutMessage,
|
|
});
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
if (reconnectAttempts.current > 0) {
|
|
attemptReconnection();
|
|
} else {
|
|
setIsConnecting(false);
|
|
shouldNotReconnectRef.current = true;
|
|
}
|
|
}
|
|
}, 35000);
|
|
|
|
currentHostIdRef.current = hostConfig.id;
|
|
currentHostConfigRef.current = hostConfig;
|
|
|
|
// Consume the pending restored session ID once; retries get null so they create fresh connections
|
|
const restoredSessionId = pendingRestoredSessionIdRef.current;
|
|
pendingRestoredSessionIdRef.current = null;
|
|
|
|
if (restoredSessionId) {
|
|
sessionIdRef.current = restoredSessionId;
|
|
isAttachingSessionRef.current = true;
|
|
|
|
ws.send(
|
|
JSON.stringify({
|
|
type: "attachSession",
|
|
data: {
|
|
sessionId: restoredSessionId,
|
|
cols,
|
|
rows,
|
|
tabInstanceId: hostConfig.instanceId,
|
|
},
|
|
}),
|
|
);
|
|
} else {
|
|
isAttachingSessionRef.current = false;
|
|
ws.send(
|
|
JSON.stringify({
|
|
type: "connectToHost",
|
|
data: {
|
|
cols,
|
|
rows,
|
|
hostConfig,
|
|
initialPath,
|
|
executeCommand,
|
|
tmuxAttachSession,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
terminal.onData((data) => {
|
|
if (data === "\r" || data === "\n") {
|
|
const currentCmd = getCurrentCommand().trim();
|
|
const termixMatch = currentCmd.match(/^termix\s+(.+)$/);
|
|
if (termixMatch && onOpenFileInEditor) {
|
|
const filePath = termixMatch[1].trim();
|
|
trackInput(data);
|
|
terminal.write("\r\n");
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
ws.send(
|
|
JSON.stringify({
|
|
type: "open_file_in_editor",
|
|
path: filePath,
|
|
}),
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
trackInput(data);
|
|
ws.send(JSON.stringify({ type: "input", data }));
|
|
});
|
|
|
|
pongReceivedRef.current = true;
|
|
pingIntervalRef.current = setInterval(() => {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
if (!pongReceivedRef.current) {
|
|
console.warn(
|
|
"[WebSocket] Pong timeout - connection appears dead, closing",
|
|
);
|
|
ws.close();
|
|
return;
|
|
}
|
|
pongReceivedRef.current = false;
|
|
ws.send(JSON.stringify({ type: "ping" }));
|
|
}
|
|
}, 30000);
|
|
});
|
|
|
|
ws.addEventListener("message", (event) => {
|
|
try {
|
|
const msg = JSON.parse(event.data);
|
|
if (msg.type === "pong") {
|
|
pongReceivedRef.current = true;
|
|
return;
|
|
}
|
|
if (msg.type === "data") {
|
|
if (typeof msg.data === "string") {
|
|
if (showAutocompleteRef.current) {
|
|
showAutocompleteRef.current = false;
|
|
setShowAutocomplete(false);
|
|
setAutocompleteSuggestions([]);
|
|
currentAutocompleteCommand.current = "";
|
|
}
|
|
|
|
terminal.write(formatTerminalOutput(msg.data));
|
|
// Strip ANSI escape codes before testing — newer sudo versions (Ubuntu 26.04+)
|
|
// emit colored prompts with embedded escape sequences that break the regex.
|
|
const strippedData = msg.data.replace(
|
|
/\x1b(?:[@-Z\\-_]|\[[0-9;?>=!]*[@-~])/g,
|
|
"",
|
|
);
|
|
maybeOfferPasswordFill(strippedData);
|
|
} else {
|
|
const stringData = String(msg.data);
|
|
terminal.write(formatTerminalOutput(stringData));
|
|
}
|
|
} else if (msg.type === "error") {
|
|
const errorMessage = msg.message || t("terminal.unknownError");
|
|
|
|
addLog({
|
|
type: "error",
|
|
stage: "connection",
|
|
message: errorMessage,
|
|
});
|
|
|
|
if (
|
|
errorMessage.toLowerCase().includes("connection") ||
|
|
errorMessage.toLowerCase().includes("timeout") ||
|
|
errorMessage.toLowerCase().includes("network")
|
|
) {
|
|
updateConnectionError(errorMessage);
|
|
setIsConnected(false);
|
|
if (terminal) {
|
|
terminal.clear();
|
|
}
|
|
setIsConnecting(false);
|
|
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")
|
|
) {
|
|
updateConnectionError(errorMessage);
|
|
setIsConnecting(false);
|
|
shouldNotReconnectRef.current = true;
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
return;
|
|
}
|
|
|
|
updateConnectionError(errorMessage);
|
|
setIsConnecting(false);
|
|
} else if (msg.type === "connected") {
|
|
opksshFailedRef.current = false;
|
|
vaultFailedRef.current = false;
|
|
wasConnectedRef.current = true;
|
|
setIsConnected(true);
|
|
setIsConnecting(false);
|
|
isConnectingRef.current = false;
|
|
updateConnectionError(null);
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
if (reconnectAttempts.current > 0) {
|
|
addLog({
|
|
type: "success",
|
|
stage: "connection",
|
|
message: t("terminal.reconnected"),
|
|
});
|
|
} else {
|
|
addLog({
|
|
type: "success",
|
|
stage: "connection",
|
|
message: t("terminal.connected"),
|
|
});
|
|
}
|
|
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",
|
|
}),
|
|
);
|
|
}
|
|
}, 100);
|
|
} else if (msg.type === "session_ended") {
|
|
wasDisconnectedBySSH.current = true;
|
|
setIsConnected(false);
|
|
setIsConnecting(false);
|
|
shouldNotReconnectRef.current = true;
|
|
if (onClose) {
|
|
onClose();
|
|
}
|
|
} else if (msg.type === "disconnected") {
|
|
wasDisconnectedBySSH.current = true;
|
|
shouldNotReconnectRef.current = true;
|
|
setIsConnected(false);
|
|
setIsConnecting(false);
|
|
if (msg.graceful) {
|
|
wasConnectedRef.current = false;
|
|
if (onClose) onClose();
|
|
} else if (wasConnectedRef.current) {
|
|
wasConnectedRef.current = false;
|
|
setShowDisconnectedOverlay(true);
|
|
} else if (!connectionErrorRef.current) {
|
|
updateConnectionError(
|
|
msg.message || t("terminal.connectionRejected"),
|
|
);
|
|
}
|
|
} else if (msg.type === "totp_required") {
|
|
setTotpRequired(true);
|
|
setTotpPrompt(msg.prompt || t("terminal.totpCodeLabel"));
|
|
setIsPasswordPrompt(false);
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
}
|
|
totpTimeoutRef.current = setTimeout(() => {
|
|
setTotpRequired(false);
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
}, 180000);
|
|
} else if (msg.type === "totp_retry") {
|
|
// Existing prompt remains visible while the backend asks for another code.
|
|
} else if (msg.type === "password_required") {
|
|
setTotpRequired(true);
|
|
setTotpPrompt(msg.prompt || t("common.password"));
|
|
setIsPasswordPrompt(true);
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
}
|
|
totpTimeoutRef.current = setTimeout(() => {
|
|
setTotpRequired(false);
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
}, 180000);
|
|
} 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);
|
|
} 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 === "vault_auth_required") {
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
if (vaultFailedRef.current) {
|
|
setVaultDialog({
|
|
stage: "error",
|
|
error: t("terminal.vaultAuthFailed"),
|
|
});
|
|
updateConnectionError(t("terminal.vaultAuthFailed"));
|
|
addLog({
|
|
type: "error",
|
|
stage: "auth",
|
|
message: t("terminal.vaultAuthFailed"),
|
|
});
|
|
} else {
|
|
vaultFailedRef.current = true;
|
|
webSocketRef.current?.send(
|
|
JSON.stringify({
|
|
type: "vault_start_auth",
|
|
data: { hostId: msg.hostId },
|
|
}),
|
|
);
|
|
}
|
|
} else if (msg.type === "vault_auth_url") {
|
|
if (connectionErrorRef.current) return;
|
|
try {
|
|
vaultPopupRef.current = window.open(
|
|
msg.url,
|
|
"termix-vault-oidc",
|
|
"width=540,height=720",
|
|
);
|
|
} catch {
|
|
vaultPopupRef.current = null;
|
|
}
|
|
setVaultDialog({ stage: "waiting" });
|
|
if (vaultTimeoutRef.current) clearTimeout(vaultTimeoutRef.current);
|
|
vaultTimeoutRef.current = setTimeout(() => {
|
|
setVaultDialog(null);
|
|
webSocketRef.current?.close();
|
|
}, 300000);
|
|
} else if (msg.type === "vault_completed") {
|
|
if (vaultTimeoutRef.current) {
|
|
clearTimeout(vaultTimeoutRef.current);
|
|
vaultTimeoutRef.current = null;
|
|
}
|
|
try {
|
|
vaultPopupRef.current?.close();
|
|
} catch {
|
|
// popup may already be closed
|
|
}
|
|
setVaultDialog(null);
|
|
if (webSocketRef.current && terminal) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "vault_auth_completed",
|
|
data: {
|
|
hostId: currentHostIdRef.current,
|
|
cols: terminal.cols || 80,
|
|
rows: terminal.rows || 24,
|
|
hostConfig: currentHostConfigRef.current,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
} else if (msg.type === "vault_error") {
|
|
if (connectionErrorRef.current) return;
|
|
vaultFailedRef.current = true;
|
|
if (vaultTimeoutRef.current) {
|
|
clearTimeout(vaultTimeoutRef.current);
|
|
vaultTimeoutRef.current = null;
|
|
}
|
|
try {
|
|
vaultPopupRef.current?.close();
|
|
} catch {
|
|
// popup may already be closed
|
|
}
|
|
setVaultDialog({ stage: "error", error: msg.error });
|
|
setIsConnecting(false);
|
|
} 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",
|
|
providers: msg.providers,
|
|
});
|
|
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,
|
|
});
|
|
} 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;
|
|
}
|
|
} else if (msg.type === "cwd") {
|
|
onOpenFileManager?.(msg.path as string);
|
|
} else if (msg.type === "open_file_in_editor") {
|
|
onOpenFileInEditor?.(msg.path as string);
|
|
} else if (msg.type === "passphrase_required") {
|
|
setShowPassphraseDialog(true);
|
|
setIsConnecting(false);
|
|
if (connectionTimeoutRef.current) {
|
|
clearTimeout(connectionTimeoutRef.current);
|
|
connectionTimeoutRef.current = null;
|
|
}
|
|
} 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;
|
|
}
|
|
} else if (msg.type === "sessionCreated") {
|
|
sessionIdRef.current = msg.sessionId;
|
|
if (hostConfig.instanceId) {
|
|
import("@/main-axios").then(({ patchOpenTab }) => {
|
|
patchOpenTab(hostConfig.instanceId!, {
|
|
backendSessionId: msg.sessionId,
|
|
}).catch(() => {});
|
|
});
|
|
}
|
|
} else if (msg.type === "sessionAttached") {
|
|
isAttachingSessionRef.current = false;
|
|
opksshFailedRef.current = false;
|
|
vaultFailedRef.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;
|
|
sessionIdRef.current = null;
|
|
wasSessionExpiredRef.current = true;
|
|
if (hostConfig.instanceId) {
|
|
import("@/main-axios").then(({ patchOpenTab }) => {
|
|
patchOpenTab(hostConfig.instanceId!, {
|
|
backendSessionId: null,
|
|
}).catch(() => {});
|
|
});
|
|
}
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
} else if (msg.type === "sessionTakenOver") {
|
|
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);
|
|
} else if (msg.type === "tmux_sessions_available") {
|
|
setTmuxSessionPicker({
|
|
sessions: msg.sessions,
|
|
});
|
|
} else if (
|
|
msg.type === "tmux_session_created" ||
|
|
msg.type === "tmux_session_attached"
|
|
) {
|
|
const sessionName =
|
|
typeof msg.sessionName === "string" ? msg.sessionName : "";
|
|
tmuxSessionNameRef.current = sessionName || "(active)";
|
|
setIsTmuxAttached(true);
|
|
addLog({
|
|
type: "info",
|
|
stage: "connection",
|
|
message:
|
|
msg.type === "tmux_session_created"
|
|
? t("terminal.tmuxSessionCreated", {
|
|
name: sessionName || "new",
|
|
})
|
|
: t("terminal.tmuxSessionAttached", {
|
|
name: sessionName,
|
|
}),
|
|
});
|
|
} else if (msg.type === "tmux_unavailable") {
|
|
setTimeout(() => {
|
|
toast.warning(t("terminal.tmuxUnavailable"), {
|
|
duration: 8000,
|
|
});
|
|
}, 500);
|
|
addLog({
|
|
type: "warning",
|
|
stage: "connection",
|
|
message: t("terminal.tmuxUnavailable"),
|
|
});
|
|
} else if (msg.type === "tmux_detached") {
|
|
tmuxSessionNameRef.current = null;
|
|
setIsTmuxAttached(false);
|
|
toast.info(t("terminal.tmuxDetached"), { duration: 3000 });
|
|
} 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,
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("WebSocket message handler error:", error);
|
|
}
|
|
});
|
|
|
|
const currentAttemptId = connectionAttemptIdRef.current;
|
|
|
|
ws.addEventListener("close", (event) => {
|
|
if (currentAttemptId !== connectionAttemptIdRef.current) {
|
|
return;
|
|
}
|
|
|
|
setIsConnected(false);
|
|
isConnectingRef.current = false;
|
|
|
|
if (pingIntervalRef.current) {
|
|
clearInterval(pingIntervalRef.current);
|
|
pingIntervalRef.current = null;
|
|
}
|
|
|
|
if (pongTimeoutRef.current) {
|
|
clearTimeout(pongTimeoutRef.current);
|
|
pongTimeoutRef.current = null;
|
|
}
|
|
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
totpTimeoutRef.current = null;
|
|
}
|
|
|
|
if (wasSessionExpiredRef.current) {
|
|
wasSessionExpiredRef.current = false;
|
|
const cols = terminal?.cols || 80;
|
|
const rows = terminal?.rows || 24;
|
|
connectToHost(cols, rows);
|
|
return;
|
|
}
|
|
|
|
if (event.code === 1006) {
|
|
console.warn(
|
|
"[WebSocket] Abnormal closure detected - attempting reconnection",
|
|
);
|
|
addLog({
|
|
type: "warning",
|
|
stage: "connection",
|
|
message: t("terminal.websocketAbnormalClose"),
|
|
});
|
|
|
|
if (wasConnectedRef.current) {
|
|
attemptReconnection();
|
|
} else {
|
|
updateConnectionError(t("terminal.websocketAbnormalClose"));
|
|
setIsConnecting(false);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (event.code === 1008) {
|
|
console.error("WebSocket authentication failed:", event.reason);
|
|
addLog({
|
|
type: "error",
|
|
stage: "auth",
|
|
message: "Authentication failed - please re-login",
|
|
});
|
|
updateConnectionError("Authentication failed - please re-login");
|
|
setIsConnecting(false);
|
|
shouldNotReconnectRef.current = true;
|
|
|
|
return;
|
|
}
|
|
|
|
if (
|
|
!wasConnectedRef.current &&
|
|
!isAttachingSessionRef.current &&
|
|
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 =
|
|
!wasDisconnectedBySSH.current &&
|
|
!isUnmountingRef.current &&
|
|
!shouldNotReconnectRef.current &&
|
|
!isConnectingRef.current;
|
|
|
|
if (shouldAttemptReconnection) {
|
|
wasDisconnectedBySSH.current = false;
|
|
attemptReconnection();
|
|
} else {
|
|
setIsConnecting(false);
|
|
}
|
|
});
|
|
|
|
ws.addEventListener("error", (event) => {
|
|
if (currentAttemptId !== connectionAttemptIdRef.current) {
|
|
return;
|
|
}
|
|
|
|
console.error("[WebSocket] Error:", event);
|
|
|
|
setIsConnected(false);
|
|
isConnectingRef.current = false;
|
|
updateConnectionError(t("terminal.websocketError"));
|
|
if (terminal) {
|
|
terminal.clear();
|
|
}
|
|
setIsConnecting(false);
|
|
|
|
if (pingIntervalRef.current) {
|
|
clearInterval(pingIntervalRef.current);
|
|
pingIntervalRef.current = null;
|
|
}
|
|
|
|
if (totpTimeoutRef.current) {
|
|
clearTimeout(totpTimeoutRef.current);
|
|
totpTimeoutRef.current = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
async function writeTextToClipboard(text: string): Promise<boolean> {
|
|
const ok = await copyToClipboard(text);
|
|
if (!ok) toast.error(t("terminal.clipboardWriteFailed"));
|
|
return ok;
|
|
}
|
|
|
|
async function readTextFromClipboard(): Promise<string> {
|
|
try {
|
|
if (window.electronClipboard) {
|
|
return window.electronClipboard.readText();
|
|
}
|
|
if (navigator.clipboard && navigator.clipboard.readText) {
|
|
return await navigator.clipboard.readText();
|
|
}
|
|
} catch {
|
|
// fall through
|
|
}
|
|
if (window.location.protocol !== "https:" && !isElectron()) {
|
|
toast.error(t("terminal.clipboardHttpWarning"));
|
|
}
|
|
return "";
|
|
}
|
|
|
|
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]);
|
|
|
|
// Separate theme and options updates to avoid terminal re-initialization flashes
|
|
useEffect(() => {
|
|
if (!terminal) return;
|
|
|
|
const config = {
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
...hostConfig.terminalConfig,
|
|
};
|
|
|
|
const activeTheme = previewTheme || config.theme;
|
|
const themeColors = resolveTermixThemeColors(
|
|
activeTheme,
|
|
appTheme,
|
|
config.customThemeColors,
|
|
);
|
|
|
|
const fontConfig = TERMINAL_FONTS.find(
|
|
(f) => f.value === config.fontFamily,
|
|
);
|
|
const fontFamily = fontConfig?.fallback || TERMINAL_FONTS[0].fallback;
|
|
|
|
// Update terminal options individually to avoid re-initialization flashes
|
|
terminal.options.cursorBlink = config.cursorBlink;
|
|
terminal.options.cursorStyle = config.cursorStyle;
|
|
terminal.options.scrollback = config.scrollback;
|
|
terminal.options.fontSize = config.fontSize;
|
|
terminalFontSizeRef.current = config.fontSize;
|
|
terminal.options.fontFamily = fontFamily;
|
|
terminal.options.rightClickSelectsWord = config.rightClickSelectsWord;
|
|
terminal.options.fastScrollSensitivity = config.fastScrollSensitivity;
|
|
terminal.options.minimumContrastRatio = config.minimumContrastRatio;
|
|
terminal.options.letterSpacing = config.letterSpacing;
|
|
terminal.options.lineHeight = config.lineHeight;
|
|
terminal.options.bellStyle = config.bellStyle as
|
|
| "none"
|
|
| "sound"
|
|
| "visual"
|
|
| "both";
|
|
|
|
terminal.options.theme = {
|
|
background: config.backgroundImage
|
|
? "transparent"
|
|
: 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,
|
|
};
|
|
|
|
// Ensure terminal is correctly fitted if font-related options change
|
|
if (fitAddonRef.current && isFitted) {
|
|
performFit();
|
|
}
|
|
|
|
// Refresh terminal to apply new theme colors to existing buffer content
|
|
hardRefresh();
|
|
}, [terminal, hostConfig.terminalConfig, previewTheme, appTheme, isFitted]);
|
|
|
|
useEffect(() => {
|
|
if (!terminal || !xtermRef.current) return;
|
|
|
|
const config = {
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
...hostConfig.terminalConfig,
|
|
};
|
|
|
|
const fontConfig = TERMINAL_FONTS.find(
|
|
(f) => f.value === config.fontFamily,
|
|
);
|
|
const fontFamily = fontConfig?.fallback || TERMINAL_FONTS[0].fallback;
|
|
|
|
const activeTheme = previewTheme || config.theme;
|
|
const themeColors = resolveTermixThemeColors(
|
|
activeTheme,
|
|
appTheme,
|
|
config.customThemeColors,
|
|
);
|
|
|
|
// Set initial options before opening the terminal
|
|
terminal.options = {
|
|
cursorBlink: config.cursorBlink,
|
|
cursorStyle: config.cursorStyle,
|
|
scrollback: config.scrollback,
|
|
fontSize: config.fontSize,
|
|
fontFamily,
|
|
allowTransparency: true, // MUST be set before open()
|
|
convertEol: false,
|
|
macOptionIsMeta: true,
|
|
macOptionClickForcesSelection: false,
|
|
rightClickSelectsWord: config.rightClickSelectsWord,
|
|
fastScrollSensitivity: config.fastScrollSensitivity,
|
|
allowProposedApi: true,
|
|
minimumContrastRatio: config.minimumContrastRatio,
|
|
letterSpacing: config.letterSpacing,
|
|
lineHeight: config.lineHeight,
|
|
bellStyle: config.bellStyle as "none" | "sound" | "visual" | "both",
|
|
theme: {
|
|
background: config.backgroundImage
|
|
? "transparent"
|
|
: 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();
|
|
const clipboardProvider = new RobustClipboardProvider();
|
|
const clipboardAddon = new ClipboardAddon(undefined, clipboardProvider);
|
|
const unicode11Addon = new Unicode11Addon();
|
|
const webLinksAddon = new WebLinksAddon((_event, uri) => {
|
|
const url =
|
|
uri.startsWith("http://") || uri.startsWith("https://")
|
|
? uri
|
|
: `https://${uri}`;
|
|
|
|
const hostBehavior = hostConfig.terminalConfig?.linkClickBehavior;
|
|
const globalBehavior =
|
|
localStorage.getItem("terminalLinkClickBehavior") ?? "confirm";
|
|
const behavior = hostBehavior ?? globalBehavior;
|
|
|
|
if (behavior === "direct") {
|
|
window.open(url, "_blank");
|
|
} else {
|
|
setLinkClickDialog({ url });
|
|
}
|
|
});
|
|
|
|
fitAddonRef.current = fitAddon;
|
|
terminal.loadAddon(fitAddon);
|
|
terminal.loadAddon(clipboardAddon);
|
|
terminal.loadAddon(unicode11Addon);
|
|
terminal.loadAddon(webLinksAddon);
|
|
|
|
terminal.unicode.activeVersion = "11";
|
|
|
|
terminal.open(xtermRef.current);
|
|
|
|
const xtermTextarea = xtermRef.current.querySelector("textarea");
|
|
if (xtermTextarea) {
|
|
xtermTextarea.setAttribute("autocomplete", "off");
|
|
xtermTextarea.setAttribute("autocorrect", "off");
|
|
xtermTextarea.setAttribute("autocapitalize", "none");
|
|
xtermTextarea.setAttribute("spellcheck", "false");
|
|
xtermTextarea.setAttribute("data-gramm", "false");
|
|
xtermTextarea.setAttribute("data-gramm_editor", "false");
|
|
xtermTextarea.setAttribute("data-enable-grammarly", "false");
|
|
}
|
|
|
|
terminal.onTitleChange((title) => {
|
|
if (title) onTitleChange?.(title);
|
|
});
|
|
document.fonts.ready.then(() => {
|
|
terminal.refresh(0, terminal.rows - 1);
|
|
fitAddon.fit();
|
|
});
|
|
|
|
terminal.attachCustomWheelEventHandler((ev) => {
|
|
if (ev.ctrlKey || ev.metaKey) {
|
|
zoomTerminalFont(ev.deltaY);
|
|
return false;
|
|
}
|
|
|
|
const cfg = {
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
...hostConfig.terminalConfig,
|
|
};
|
|
const mod = cfg.fastScrollModifier;
|
|
const modHeld =
|
|
(mod === "alt" && ev.altKey) ||
|
|
(mod === "ctrl" && ev.ctrlKey) ||
|
|
(mod === "shift" && ev.shiftKey);
|
|
if (modHeld) {
|
|
const lines = Math.round(
|
|
(Math.abs(ev.deltaY) / 100) * (cfg.fastScrollSensitivity ?? 5),
|
|
);
|
|
terminal.scrollLines(ev.deltaY > 0 ? lines : -lines);
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
fitAddonRef.current?.fit();
|
|
// Double-rAF ensures layout is fully settled (fonts, flexbox, etc.) before
|
|
// committing the fitted size, preventing the "terminal too short" glitch.
|
|
requestAnimationFrame(() => {
|
|
requestAnimationFrame(() => {
|
|
fitAddonRef.current?.fit();
|
|
setIsFitted(true);
|
|
});
|
|
});
|
|
|
|
const element = xtermRef.current;
|
|
const handleContextMenu = (e: MouseEvent) => {
|
|
if (e.ctrlKey && onOpenFileManager) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
onOpenFileManager();
|
|
return;
|
|
}
|
|
|
|
if (getUseRightClickCopyPaste()) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
if (terminal.hasSelection()) {
|
|
const text = terminal.getSelection();
|
|
writeTextToClipboard(text).then(() => terminal.clearSelection());
|
|
} else {
|
|
readTextFromClipboard().then((text) => {
|
|
if (text) terminal.paste(text);
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
};
|
|
element?.addEventListener("contextmenu", handleContextMenu);
|
|
|
|
const handlePaste = (e: ClipboardEvent) => {
|
|
const text = e.clipboardData?.getData("text");
|
|
if (text) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
terminal.paste(text);
|
|
}
|
|
};
|
|
element?.addEventListener("paste", handlePaste);
|
|
|
|
let tmuxDragTracking = false;
|
|
const handleTmuxDragStart = (e: MouseEvent) => {
|
|
if (e.button !== 0) return;
|
|
if (!tmuxSessionNameRef.current) return;
|
|
tmuxDragTracking = true;
|
|
};
|
|
const handleTmuxDragMove = () => {
|
|
if (!tmuxDragTracking) return;
|
|
tmuxDragTracking = false;
|
|
if (tmuxCopyModeHintShownRef.current) return;
|
|
tmuxCopyModeHintShownRef.current = true;
|
|
toast.info(t("terminal.tmuxCopyHint"), { duration: 5000 });
|
|
};
|
|
const handleTmuxDragEnd = () => {
|
|
tmuxDragTracking = false;
|
|
};
|
|
element?.addEventListener("mousedown", handleTmuxDragStart);
|
|
element?.addEventListener("mousemove", handleTmuxDragMove);
|
|
element?.addEventListener("mouseup", handleTmuxDragEnd);
|
|
|
|
const handleBackspaceMode = (e: KeyboardEvent) => {
|
|
if (e.key !== "Backspace") return;
|
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
|
|
const config = {
|
|
...DEFAULT_TERMINAL_CONFIG,
|
|
...hostConfig.terminalConfig,
|
|
};
|
|
if (config.backspaceMode !== "control-h") return;
|
|
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({ type: "input", data: "\x08" }),
|
|
);
|
|
}
|
|
return false;
|
|
};
|
|
|
|
// On macOS Electron, Tab key events can be swallowed by Chromium's focus
|
|
// traversal system before xterm.js sees them. Calling preventDefault() in
|
|
// the capture phase blocks that traversal while still allowing the event to
|
|
// reach xterm.js's internal handler (which fires our attachCustomKeyEventHandler).
|
|
const handleTabCapture = (e: KeyboardEvent) => {
|
|
if (e.key === "Tab") {
|
|
e.preventDefault();
|
|
}
|
|
};
|
|
|
|
element?.addEventListener("keydown", handleBackspaceMode, true);
|
|
element?.addEventListener("keydown", handleTabCapture, true);
|
|
|
|
const resizeObserver = new ResizeObserver(() => {
|
|
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
|
|
resizeTimeout.current = setTimeout(() => {
|
|
if (isVisible) {
|
|
performFit();
|
|
}
|
|
}, 50);
|
|
});
|
|
|
|
const observeTarget = xtermRef.current.parentElement ?? xtermRef.current;
|
|
resizeObserver.observe(observeTarget);
|
|
|
|
return () => {
|
|
isFittingRef.current = false;
|
|
resizeObserver.disconnect();
|
|
clipboardProvider.dispose();
|
|
element?.removeEventListener("contextmenu", handleContextMenu);
|
|
element?.removeEventListener("paste", handlePaste);
|
|
element?.removeEventListener("mousedown", handleTmuxDragStart);
|
|
element?.removeEventListener("mousemove", handleTmuxDragMove);
|
|
element?.removeEventListener("mouseup", handleTmuxDragEnd);
|
|
element?.removeEventListener("keydown", handleBackspaceMode, true);
|
|
element?.removeEventListener("keydown", handleTabCapture, true);
|
|
if (notifyTimerRef.current) clearTimeout(notifyTimerRef.current);
|
|
if (resizeTimeout.current) clearTimeout(resizeTimeout.current);
|
|
};
|
|
}, [xtermRef, terminal]);
|
|
|
|
const isMountedRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
isMountedRef.current = true;
|
|
|
|
const currentHostId = hostConfig.id;
|
|
return () => {
|
|
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;
|
|
}
|
|
if (pongTimeoutRef.current) {
|
|
clearTimeout(pongTimeoutRef.current);
|
|
pongTimeoutRef.current = null;
|
|
}
|
|
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.close();
|
|
}
|
|
|
|
isMountedRef.current = false;
|
|
}
|
|
};
|
|
}, [hostConfig.id, hostConfig.instanceId]);
|
|
|
|
useEffect(() => {
|
|
if (!terminal) return;
|
|
|
|
const handleCustomKey = (e: KeyboardEvent): boolean => {
|
|
if (e.type !== "keydown") {
|
|
return true;
|
|
}
|
|
|
|
// Forward global app shortcuts to AppShell directly — xterm swallows
|
|
// all keydown events and synthetic re-dispatch is unreliable.
|
|
// stopPropagation prevents the same event from also firing the window listener.
|
|
if (e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey) {
|
|
const globalCodes = [
|
|
"BracketRight",
|
|
"BracketLeft",
|
|
"Backslash",
|
|
"Minus",
|
|
];
|
|
if (globalCodes.includes(e.code)) {
|
|
e.stopPropagation();
|
|
globalShortcutHandler.current?.(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey) {
|
|
const arrowCodes = [
|
|
"ArrowLeft",
|
|
"ArrowRight",
|
|
"ArrowUp",
|
|
"ArrowDown",
|
|
];
|
|
if (arrowCodes.includes(e.code)) {
|
|
e.stopPropagation();
|
|
globalShortcutHandler.current?.(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (
|
|
e.ctrlKey &&
|
|
!e.shiftKey &&
|
|
!e.altKey &&
|
|
!e.metaKey &&
|
|
e.key.toLowerCase() === "c" &&
|
|
terminal.hasSelection()
|
|
) {
|
|
const selection = terminal.getSelection();
|
|
if (selection) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
writeTextToClipboard(selection);
|
|
terminal.clearSelection();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (
|
|
((e.metaKey && !e.shiftKey && !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;
|
|
}
|
|
}
|
|
|
|
if (
|
|
e.ctrlKey &&
|
|
e.shiftKey &&
|
|
!e.altKey &&
|
|
!e.metaKey &&
|
|
e.key.toLowerCase() === "c"
|
|
) {
|
|
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.key.toLowerCase() === "v"
|
|
) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
readTextFromClipboard().then((text) => {
|
|
if (text) terminal.paste(text);
|
|
});
|
|
return false;
|
|
}
|
|
|
|
if (
|
|
e.ctrlKey &&
|
|
!e.shiftKey &&
|
|
!e.altKey &&
|
|
!e.metaKey &&
|
|
e.key.toLowerCase() === "v"
|
|
) {
|
|
// Let the browser handle Ctrl+V natively, the paste event
|
|
// listener will intercept the result without triggering the
|
|
// clipboard permission popup
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
if (
|
|
e.key === "Tab" &&
|
|
!e.ctrlKey &&
|
|
!e.altKey &&
|
|
!e.metaKey &&
|
|
!e.shiftKey
|
|
) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
|
|
const sendTabToShell = () => {
|
|
if (webSocketRef.current?.readyState === 1) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({ type: "input", data: "\t" }),
|
|
);
|
|
}
|
|
};
|
|
|
|
const autocompleteEnabled =
|
|
localStorage.getItem("commandAutocomplete") === "true";
|
|
|
|
if (!autocompleteEnabled) {
|
|
sendTabToShell();
|
|
return false;
|
|
}
|
|
|
|
const currentCmd = getCurrentCommandRef.current().trim();
|
|
if (currentCmd.length === 0) {
|
|
sendTabToShell();
|
|
return false;
|
|
}
|
|
|
|
if (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);
|
|
} else {
|
|
sendTabToShell();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
terminal.attachCustomKeyEventHandler(handleCustomKey);
|
|
}, [terminal]);
|
|
|
|
useEffect(() => {
|
|
if (!terminal || !hostConfig || !isVisible) return;
|
|
if (isConnected || isConnecting) return;
|
|
|
|
if (isReconnectingRef.current || reconnectTimeoutRef.current !== null) {
|
|
return;
|
|
}
|
|
|
|
if (shouldNotReconnectRef.current) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
webSocketRef.current &&
|
|
(webSocketRef.current.readyState === WebSocket.OPEN ||
|
|
webSocketRef.current.readyState === WebSocket.CONNECTING)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
setIsConnecting(true);
|
|
fitAddonRef.current?.fit();
|
|
requestAnimationFrame(() => {
|
|
requestAnimationFrame(() => {
|
|
fitAddonRef.current?.fit();
|
|
if (terminal.cols > 0 && terminal.rows > 0) {
|
|
scheduleNotify(terminal.cols, terminal.rows);
|
|
connectToHost(terminal.cols, terminal.rows);
|
|
}
|
|
});
|
|
});
|
|
}, [terminal, hostConfig.id, isVisible, isConnected, isConnecting]);
|
|
|
|
useEffect(() => {
|
|
if (!terminal || !fitAddonRef.current) return;
|
|
|
|
if (!isVisible) {
|
|
lastFittedSizeRef.current = null;
|
|
lastSentSizeRef.current = null;
|
|
return;
|
|
}
|
|
|
|
const fitTimeoutId = setTimeout(() => {
|
|
if (!isFittingRef.current && terminal.cols > 0 && terminal.rows > 0) {
|
|
performFit();
|
|
if (!splitScreen && !isConnecting && !disableAutoFocus) {
|
|
requestAnimationFrame(() => terminal.focus());
|
|
}
|
|
}
|
|
}, 50);
|
|
|
|
return () => clearTimeout(fitTimeoutId);
|
|
}, [terminal, isVisible, splitScreen, isConnecting]);
|
|
|
|
const hasConnectionError = !!connectionError;
|
|
|
|
return (
|
|
<div
|
|
className="h-full w-full relative"
|
|
style={{
|
|
backgroundColor: backgroundImage ? "transparent" : backgroundColor,
|
|
...(backgroundImage && {
|
|
backgroundImage: `url(${backgroundImage})`,
|
|
backgroundSize: "cover",
|
|
backgroundPosition: "center",
|
|
backgroundRepeat: "no-repeat",
|
|
}),
|
|
}}
|
|
>
|
|
{backgroundImage && (
|
|
<div
|
|
className="absolute inset-0 pointer-events-none"
|
|
style={{
|
|
backgroundColor: themeColors.background,
|
|
opacity: 1 - backgroundImageOpacity,
|
|
}}
|
|
/>
|
|
)}
|
|
<div
|
|
ref={xtermRef}
|
|
className="h-full w-full relative"
|
|
style={{
|
|
pointerEvents: isVisible ? "auto" : "none",
|
|
visibility:
|
|
isConnected && isFitted && !connectionError
|
|
? "visible"
|
|
: "hidden",
|
|
}}
|
|
onClick={() => {
|
|
if (terminal && !splitScreen) {
|
|
terminal.focus();
|
|
}
|
|
}}
|
|
/>
|
|
|
|
{isTmuxAttached && isConnected && (
|
|
<button
|
|
onClick={() => {
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({ type: "tmux_detach" }),
|
|
);
|
|
}
|
|
}}
|
|
title={t("terminal.tmuxDetach")}
|
|
className="absolute top-2 right-2 z-[110] px-2 py-1 text-xs rounded bg-black/60 text-white/70 hover:text-white hover:bg-black/80 transition-colors"
|
|
>
|
|
tmux:detach
|
|
</button>
|
|
)}
|
|
|
|
<SimpleLoader
|
|
visible={isConnecting && !isConnectionLogExpanded}
|
|
message={t("terminal.connecting")}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
|
|
{showDisconnectedOverlay && !isConnecting && (
|
|
<div
|
|
className="absolute inset-0 flex flex-col items-center justify-center gap-3 z-[120]"
|
|
style={{ backgroundColor }}
|
|
>
|
|
<p className="text-sm text-muted-foreground">
|
|
{t("terminal.connectionLost")}
|
|
</p>
|
|
<div className="flex gap-2">
|
|
<Button
|
|
onClick={() => {
|
|
setShowDisconnectedOverlay(false);
|
|
isUnmountingRef.current = false;
|
|
shouldNotReconnectRef.current = false;
|
|
isReconnectingRef.current = false;
|
|
isConnectingRef.current = false;
|
|
reconnectAttempts.current = 0;
|
|
wasDisconnectedBySSH.current = false;
|
|
wasConnectedRef.current = false;
|
|
updateConnectionError(null);
|
|
if (terminal) {
|
|
terminal.clear();
|
|
connectToHost(terminal.cols, terminal.rows);
|
|
}
|
|
}}
|
|
>
|
|
{t("terminal.reconnect")}
|
|
</Button>
|
|
{onClose && (
|
|
<Button variant="outline" onClick={onClose}>
|
|
{t("terminal.closeTab")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<ConnectionLog
|
|
isConnecting={isConnecting}
|
|
isConnected={isConnected}
|
|
hasConnectionError={hasConnectionError && !showDisconnectedOverlay}
|
|
position={hasConnectionError ? "top" : "bottom"}
|
|
/>
|
|
|
|
<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}
|
|
/>
|
|
|
|
<PassphraseDialog
|
|
isOpen={showPassphraseDialog}
|
|
onSubmit={handlePassphraseSubmit}
|
|
onCancel={handlePassphraseCancel}
|
|
hostInfo={{
|
|
ip: hostConfig.ip,
|
|
port: hostConfig.port,
|
|
username: hostConfig.username,
|
|
name: hostConfig.name,
|
|
}}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
|
|
<WarpgateDialog
|
|
isOpen={warpgateAuthRequired}
|
|
url={warpgateAuthUrl}
|
|
securityKey={warpgateSecurityKey}
|
|
onContinue={handleWarpgateContinue}
|
|
onCancel={handleWarpgateCancel}
|
|
onOpenUrl={handleWarpgateOpenUrl}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
|
|
{opksshDialog?.isOpen && (
|
|
<OPKSSHDialog
|
|
isOpen={opksshDialog.isOpen}
|
|
authUrl={opksshDialog.authUrl}
|
|
requestId={opksshDialog.requestId}
|
|
stage={opksshDialog.stage}
|
|
error={opksshDialog.error}
|
|
providers={opksshDialog.providers}
|
|
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 },
|
|
}),
|
|
);
|
|
}
|
|
}}
|
|
onSelectProvider={(alias) => {
|
|
if (!opksshDialog.authUrl) return;
|
|
const selectUrl = `${opksshDialog.authUrl}/select?op=${encodeURIComponent(alias)}`;
|
|
window.open(selectUrl, "_blank");
|
|
if (webSocketRef.current) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "opkssh_browser_opened",
|
|
data: { requestId: opksshDialog.requestId },
|
|
}),
|
|
);
|
|
}
|
|
setOpksshDialog((prev) =>
|
|
prev ? { ...prev, stage: "waiting" } : null,
|
|
);
|
|
}}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
)}
|
|
|
|
{vaultDialog && (
|
|
<div className="absolute inset-0 z-50 flex items-center justify-center bg-black/60">
|
|
<div className="w-[420px] max-w-[90%] border border-border bg-background p-5 shadow-lg">
|
|
<h3 className="text-sm font-bold mb-2 text-foreground">
|
|
{vaultDialog.stage === "error"
|
|
? t("terminal.vaultAuthFailed")
|
|
: t("terminal.vaultAuthTitle")}
|
|
</h3>
|
|
{vaultDialog.stage === "error" ? (
|
|
<p className="text-xs text-destructive mb-4 break-words">
|
|
{vaultDialog.error || t("terminal.vaultAuthFailed")}
|
|
</p>
|
|
) : (
|
|
<p className="text-xs text-muted-foreground mb-4">
|
|
{t("terminal.vaultAuthDescription")}
|
|
</p>
|
|
)}
|
|
<div className="flex justify-end gap-2">
|
|
{vaultDialog.stage === "waiting" && (
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => {
|
|
try {
|
|
vaultPopupRef.current?.focus();
|
|
} catch {
|
|
// popup may be gone
|
|
}
|
|
}}
|
|
>
|
|
{t("terminal.vaultReopen")}
|
|
</Button>
|
|
)}
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={() => {
|
|
if (vaultTimeoutRef.current) {
|
|
clearTimeout(vaultTimeoutRef.current);
|
|
vaultTimeoutRef.current = null;
|
|
}
|
|
try {
|
|
vaultPopupRef.current?.close();
|
|
} catch {
|
|
// popup may already be closed
|
|
}
|
|
webSocketRef.current?.send(
|
|
JSON.stringify({
|
|
type: "vault_cancel",
|
|
data: { hostId: currentHostIdRef.current },
|
|
}),
|
|
);
|
|
setVaultDialog(null);
|
|
}}
|
|
>
|
|
{vaultDialog.stage === "error"
|
|
? t("common.close")
|
|
: t("hosts.cancelBtn")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{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}
|
|
/>
|
|
)}
|
|
|
|
{tmuxSessionPicker && (
|
|
<TmuxSessionPicker
|
|
isOpen={true}
|
|
sessions={tmuxSessionPicker.sessions}
|
|
onSelect={(sessionName) => {
|
|
setTmuxSessionPicker(null);
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "tmux_attach",
|
|
data: { sessionName },
|
|
}),
|
|
);
|
|
}
|
|
setTimeout(() => terminal?.focus(), 50);
|
|
}}
|
|
onCreateNew={() => {
|
|
setTmuxSessionPicker(null);
|
|
if (webSocketRef.current?.readyState === WebSocket.OPEN) {
|
|
webSocketRef.current.send(
|
|
JSON.stringify({
|
|
type: "tmux_attach",
|
|
data: { sessionName: "" },
|
|
}),
|
|
);
|
|
}
|
|
setTimeout(() => terminal?.focus(), 50);
|
|
}}
|
|
onCancel={() => setTmuxSessionPicker(null)}
|
|
backgroundColor={backgroundColor}
|
|
/>
|
|
)}
|
|
|
|
<CommandAutocomplete
|
|
visible={showAutocomplete}
|
|
suggestions={autocompleteSuggestions}
|
|
selectedIndex={autocompleteSelectedIndex}
|
|
position={autocompletePosition}
|
|
onSelect={handleAutocompleteSelect}
|
|
/>
|
|
|
|
{linkClickDialog &&
|
|
createPortal(
|
|
<div
|
|
className="fixed inset-0 flex items-center justify-center z-[10000]"
|
|
style={{ backgroundColor: "rgba(0,0,0,0.5)" }}
|
|
>
|
|
<div
|
|
className="flex flex-col gap-3 p-4 rounded shadow-lg max-w-sm w-full mx-4"
|
|
style={{ backgroundColor }}
|
|
>
|
|
<p className="text-xs font-bold uppercase tracking-widest text-muted-foreground">
|
|
{t("terminal.linkDialogTitle")}
|
|
</p>
|
|
<p className="text-sm break-all text-foreground select-all">
|
|
{linkClickDialog.url}
|
|
</p>
|
|
<div className="flex gap-2 justify-end">
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => {
|
|
writeTextToClipboard(linkClickDialog.url);
|
|
setLinkClickDialog(null);
|
|
}}
|
|
>
|
|
{t("terminal.linkDialogCopy")}
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
onClick={() => {
|
|
window.open(
|
|
linkClickDialog.url,
|
|
"_blank",
|
|
"noopener,noreferrer",
|
|
);
|
|
setLinkClickDialog(null);
|
|
}}
|
|
>
|
|
{t("terminal.linkDialogOpen")}
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => setLinkClickDialog(null)}
|
|
>
|
|
{t("common.cancel")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>,
|
|
document.body,
|
|
)}
|
|
</div>
|
|
);
|
|
},
|
|
);
|
|
|
|
export const Terminal = forwardRef<TerminalHandle, SSHTerminalProps>(
|
|
function Terminal(props, ref) {
|
|
return (
|
|
<ConnectionLogProvider>
|
|
<TerminalInner {...props} ref={ref} />
|
|
</ConnectionLogProvider>
|
|
);
|
|
},
|
|
);
|