mirror of
https://github.com/YuzuZensai/Termix.git
synced 2026-11-12 17:46:55 +00:00
* fix(sso): send the PKCE verifier for GitHub login (#1534) * fix(remote-desktop): clip cursor overflow without disabling zoom (#1535) * test(remote-desktop): preserve sessions beyond one hour (#1536) * fix(hosts): expose controls for overflowing editor tabs (#1537) * fix(hosts): expose controls for overflowing editor tabs * test(hosts): mock resize observation in admin panel tests * fix(status): skip TCP probes while host sessions are active (#1538) * fix(database): batch session activity persistence (#1539) * docs(api): describe cookie and API key authentication (#1540) * fix(snippets): repair missing note column on SQLite upgrades (#1541) * fix(docker): retain stored SSH credential association (#1543) * fix(file-manager): render transfer toasts without plugin hooks (#1546) * feat(hosts): keep compact row actions inline (#1547) * fix(auth): allow retrying unavailable second-factor interfaces (#1549) * fix(auth): reject unresolved legacy identity provisioning (#1550) * fix(desktop): ship the plugin SDK inside Mac App Store builds (#1551) * fix(tmux): pass full session info to the terminal's session picker (#1552) The tmux.sessions service reduced detected sessions to bare names, but the picker reads session.name, so every entry rendered blank and selecting one sent an empty name, which created a new session instead of attaching. * fix(ssh): read force keyboard-interactive as a boolean when resolving hosts (#1553) The column holds the text "true"/"false", and the resolver passed it through as-is. The string "false" is truthy, so the password provider treated every saved host as forced keyboard-interactive and left the password out. Jump hops are built straight from the resolved host, so a password hop whose server doesn't offer keyboard-interactive failed with "All configured authentication methods failed". * fix(database): batch database saves for bulk host writes (#1554) * fix(database): yield to the event loop between database saves Each SQLite save serializes and encrypts the whole database into new buffers, which V8 only releases once the event loop turns. Boot migrations and bulk host import write plugin settings per host and per key in an awaited loop, so hundreds of full copies piled up and the container ran out of memory. * fix(database): save once per bulk host write instead of once per row On SQLite every repository write force-saves the whole database. Boot plugin data migrations, host defaults materialization and the bulk host routes write one row per host per setting, so they serialized and encrypted the full database hundreds of times in a row. DatabaseSaveTrigger.batched wraps a function so force saves made while it runs collapse into a single save when it finishes. Nested scopes fold into the outer one, and work that outlives its scope saves normally. * feat(hosts): add instance-wide predefined tag suggestions (#1548) * feat(hosts): add shared predefined tag suggestions * style(hosts): format tag catalog routes * test(database): advance fake timers past the save yield (#1555) * fix(i18n): complete Simplified Chinese core and plugin translations (#1542) * fix(i18n): complete Chinese onboarding and navigation labels * fix(i18n): translate remaining Chinese core and plugin interfaces * fix(i18n): translate host editor tab overflow controls * fix(i18n): use consistent Chinese fleet terminology * fix(i18n): localize hardcoded controls and plugin views * fix(i18n): translate built-in homepage widget catalog * test(homepage): follow translated timezone placeholder * fix(i18n): translate plugin-provided homepage widgets * fix(i18n): localize feature settings section titles * fix: 2.8 oidc accounts unable to sign in after upgrading (#1381) * fix: plugins losing the saved ssh login when copying a host (#1391) * fix: fleets, proxmox and automations not getting the host sudo password * fix: host imports running a defaults pass and metrics restart per host (#1384) * fix: high cpu from status probes and full database saves on every sample (#1300) * fix: user data export freezing the server (#1393) * fix: op:// secret references rejected as ssh keys on credentials (#1394) * fix: slow file deletes from the trash lookups on every delete (#1390) * fix: add openapi docs and error handling to host tag routes * test: compare download stream buffers directly so it stops timing out * chore: drop em dash from host row comment * chore: update release notes for 2.9.1 * fix: restore space to add host tags and redesign predefined tags editor * fix: host key silently accepted when the host is missing from the database (#1397) * fix: clearer host login failed status label and fix its translations (#1396) * chore: add host key and status label fixes to release notes * fix: sso and ldap provider dialogs overflowing the screen and using mismatched toggles * chore: increment ver * fix(audit): store plugin entries with no acting user as a null user_id (#1556) Plugin audit entries written outside a request used the literal "system" as user_id. That column references users.id, so the insert was refused (Postgres logs it as an FK violation) and the entry was silently dropped. Write null instead and keep "system" / plugin:<id> in username. * chore: add sso/ldap dialog and audit log fixes to release notes * fix: tunnels and host settings missing from shared hosts on desktop * fix: remote desktop logins missing from shared hosts on desktop * fix: simplify host status to online/offline and keep it live without a refresh * chore: sync Crowdin translations for 2.9.1 --------- Co-authored-by: ZacharyZcR <zacharyzcr1984@gmail.com>
606 lines
16 KiB
TypeScript
606 lines
16 KiB
TypeScript
/* eslint-disable react-hooks/exhaustive-deps */
|
|
import React, { useState, useEffect, useRef } from "react";
|
|
import { DraggableWindow } from "./DraggableWindow.tsx";
|
|
import { FileViewer } from "./FileViewer.tsx";
|
|
import { useWindowManager } from "./WindowManager.tsx";
|
|
import {
|
|
downloadSSHFileStream,
|
|
readSSHFile,
|
|
writeSSHFile,
|
|
getSSHStatus,
|
|
connectSSH,
|
|
setSudoPassword,
|
|
} from "../api/ssh-file-operations-api";
|
|
import { SudoPasswordDialog } from "../SudoPasswordDialog.tsx";
|
|
import { toast } from "sonner";
|
|
import { DownloadProgressToast } from "./DownloadProgressToast";
|
|
import type { SSHHost } from "../host-types";
|
|
import { useTranslation } from "@termix/plugin-sdk/frontend";
|
|
|
|
interface FileItem {
|
|
name: string;
|
|
type: "file" | "directory" | "link";
|
|
path: string;
|
|
size?: number;
|
|
modified?: string;
|
|
permissions?: string;
|
|
owner?: string;
|
|
group?: string;
|
|
}
|
|
|
|
interface FileWindowProps {
|
|
windowId: string;
|
|
file: FileItem;
|
|
sshSessionId: string;
|
|
sshHost: SSHHost;
|
|
initialX?: number;
|
|
initialY?: number;
|
|
onFileNotFound?: (file: FileItem) => void;
|
|
}
|
|
|
|
function isDisplayableText(str: string): boolean {
|
|
let printable = 0;
|
|
for (let i = 0; i < Math.min(str.length, 1000); i++) {
|
|
const code = str.charCodeAt(i);
|
|
if (
|
|
(code >= 32 && code <= 126) ||
|
|
code === 9 ||
|
|
code === 10 ||
|
|
code === 13 ||
|
|
code >= 128
|
|
) {
|
|
printable++;
|
|
}
|
|
}
|
|
return printable / Math.min(str.length, 1000) > 0.85;
|
|
}
|
|
|
|
export function FileWindow({
|
|
windowId,
|
|
file,
|
|
sshSessionId,
|
|
sshHost,
|
|
initialX = 100,
|
|
initialY = 100,
|
|
onFileNotFound,
|
|
}: FileWindowProps) {
|
|
const { closeWindow, maximizeWindow, focusWindow, windows } =
|
|
useWindowManager();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const [content, setContent] = useState<string>("");
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
const [isEditable, setIsEditable] = useState(false);
|
|
const [pendingContent, setPendingContent] = useState<string>("");
|
|
const [resetKey, setResetKey] = useState(0);
|
|
const [mediaDimensions, setMediaDimensions] = useState<
|
|
{ width: number; height: number } | undefined
|
|
>();
|
|
const [externalEditorPath, setExternalEditorPath] = useState<string>("");
|
|
const autoSaveTimerRef = useRef<NodeJS.Timeout | null>(null);
|
|
const externalEditorRef = useRef<{
|
|
editId: string;
|
|
unsubscribe?: () => void;
|
|
} | null>(null);
|
|
|
|
const currentWindow = windows.find((w) => w.id === windowId);
|
|
|
|
useEffect(() => {
|
|
if (!window.electronAPI?.isElectron) return;
|
|
|
|
window.electronAPI
|
|
.getSetting?.("fileManager.externalEditorPath")
|
|
.then((value) => {
|
|
if (typeof value === "string") setExternalEditorPath(value);
|
|
})
|
|
.catch(() => {});
|
|
}, []);
|
|
|
|
const ensureSSHConnection = async () => {
|
|
try {
|
|
const status = await getSSHStatus(sshSessionId);
|
|
|
|
if (!status.connected) {
|
|
await connectSSH(sshSessionId, {
|
|
hostId: sshHost.id,
|
|
ip: sshHost.ip,
|
|
port: sshHost.port,
|
|
username: sshHost.username,
|
|
password: sshHost.password,
|
|
sshKey: sshHost.key,
|
|
keyPassword: sshHost.keyPassword,
|
|
authType: sshHost.authType,
|
|
credentialId: sshHost.credentialId,
|
|
userId: sshHost.userId,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("SSH connection check/reconnect failed:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const loadFileContent = async () => {
|
|
if (file.type !== "file") return;
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
await ensureSSHConnection();
|
|
|
|
const response = await readSSHFile(sshSessionId, file.path);
|
|
let fileContent = response.content || "";
|
|
|
|
if (response.encoding === "base64") {
|
|
try {
|
|
const bytes = Uint8Array.from(atob(fileContent), (c) =>
|
|
c.charCodeAt(0),
|
|
);
|
|
const decoded = new TextDecoder("utf-8").decode(bytes);
|
|
if (isDisplayableText(decoded)) {
|
|
fileContent = decoded;
|
|
}
|
|
} catch (err) {
|
|
console.error("Failed to decode base64 content:", err);
|
|
}
|
|
}
|
|
|
|
setContent(fileContent);
|
|
setPendingContent(fileContent);
|
|
setResetKey((k) => k + 1);
|
|
|
|
if (!file.size) {
|
|
const contentSize = new Blob([fileContent]).size;
|
|
file.size = contentSize;
|
|
}
|
|
|
|
const mediaExtensions = [
|
|
"jpg",
|
|
"jpeg",
|
|
"png",
|
|
"gif",
|
|
"bmp",
|
|
"svg",
|
|
"webp",
|
|
"tiff",
|
|
"ico",
|
|
"mp3",
|
|
"wav",
|
|
"ogg",
|
|
"aac",
|
|
"flac",
|
|
"m4a",
|
|
"wma",
|
|
"mp4",
|
|
"avi",
|
|
"mov",
|
|
"wmv",
|
|
"flv",
|
|
"mkv",
|
|
"webm",
|
|
"m4v",
|
|
"zip",
|
|
"rar",
|
|
"7z",
|
|
"tar",
|
|
"gz",
|
|
"bz2",
|
|
"xz",
|
|
"exe",
|
|
"dll",
|
|
"so",
|
|
"dylib",
|
|
"bin",
|
|
"iso",
|
|
];
|
|
|
|
const extension = file.name.split(".").pop()?.toLowerCase();
|
|
setIsEditable(!mediaExtensions.includes(extension || ""));
|
|
} catch (error: unknown) {
|
|
console.error("Failed to load file:", error);
|
|
|
|
const err = error as {
|
|
message?: string;
|
|
isFileNotFound?: boolean;
|
|
response?: {
|
|
status?: number;
|
|
data?: {
|
|
tooLarge?: boolean;
|
|
error?: string;
|
|
fileNotFound?: boolean;
|
|
};
|
|
};
|
|
};
|
|
const errorData = err?.response?.data;
|
|
if (errorData?.tooLarge) {
|
|
toast.error(
|
|
t("fileManager.fileTooLarge", { error: errorData.error }),
|
|
{
|
|
duration: 10000,
|
|
},
|
|
);
|
|
} else if (
|
|
err.message?.includes("connection") ||
|
|
err.message?.includes("established")
|
|
) {
|
|
toast.error(
|
|
`SSH connection failed. Please check your connection to ${sshHost.name} (${sshHost.ip}:${sshHost.port})`,
|
|
);
|
|
} else {
|
|
const errorMessage =
|
|
errorData?.error || err.message || "Unknown error";
|
|
const isFileNotFound =
|
|
err.isFileNotFound ||
|
|
errorData?.fileNotFound ||
|
|
err.response?.status === 404 ||
|
|
errorMessage.includes("File not found") ||
|
|
errorMessage.includes("No such file or directory") ||
|
|
errorMessage.includes("cannot access") ||
|
|
errorMessage.includes("not found") ||
|
|
errorMessage.includes("Resource not found");
|
|
|
|
if (isFileNotFound && onFileNotFound) {
|
|
onFileNotFound(file);
|
|
toast.error(
|
|
t("fileManager.fileNotFoundAndRemoved", { name: file.name }),
|
|
);
|
|
|
|
closeWindow(windowId);
|
|
return;
|
|
} else {
|
|
toast.error(
|
|
t("fileManager.failedToLoadFile", {
|
|
error: errorMessage.includes("Server error occurred")
|
|
? t("fileManager.serverErrorOccurred")
|
|
: errorMessage,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
loadFileContent();
|
|
}, [file, sshSessionId, sshHost]);
|
|
|
|
const handleRevert = async () => {
|
|
const loadFileContent = async () => {
|
|
if (file.type !== "file") return;
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
await ensureSSHConnection();
|
|
|
|
const response = await readSSHFile(sshSessionId, file.path, {
|
|
force: true,
|
|
});
|
|
const fileContent = response.content || "";
|
|
setContent(fileContent);
|
|
setPendingContent("");
|
|
setResetKey((k) => k + 1);
|
|
|
|
if (!file.size) {
|
|
const contentSize = new Blob([fileContent]).size;
|
|
file.size = contentSize;
|
|
}
|
|
} catch (error: unknown) {
|
|
console.error("Failed to load file content:", error);
|
|
const err = error as { message?: string };
|
|
toast.error(
|
|
`${t("fileManager.failedToLoadFile")}: ${err.message || t("fileManager.unknownError")}`,
|
|
);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
loadFileContent();
|
|
};
|
|
|
|
const [sudoSaveContent, setSudoSaveContent] = useState<string | null>(null);
|
|
|
|
const handleSudoSave = async (password: string) => {
|
|
const pending = sudoSaveContent;
|
|
setSudoSaveContent(null);
|
|
if (pending === null) return;
|
|
try {
|
|
await setSudoPassword(sshSessionId, password);
|
|
} catch {
|
|
toast.error(t("fileManager.sudoAuthFailed"));
|
|
return;
|
|
}
|
|
await handleSave(pending);
|
|
};
|
|
|
|
const handleSave = async (newContent: string) => {
|
|
try {
|
|
setIsSaving(true);
|
|
|
|
await ensureSSHConnection();
|
|
|
|
await writeSSHFile(sshSessionId, file.path, newContent);
|
|
setContent(newContent);
|
|
setPendingContent("");
|
|
|
|
if (autoSaveTimerRef.current) {
|
|
clearTimeout(autoSaveTimerRef.current);
|
|
autoSaveTimerRef.current = null;
|
|
}
|
|
|
|
toast.success(t("fileManager.fileSavedSuccessfully"));
|
|
} catch (error: unknown) {
|
|
console.error("Failed to save file:", error);
|
|
|
|
const err = error as {
|
|
message?: string;
|
|
response?: { data?: { needsSudo?: boolean } };
|
|
};
|
|
if (err.response?.data?.needsSudo) {
|
|
setSudoSaveContent(newContent);
|
|
} else if (
|
|
err.message?.includes("connection") ||
|
|
err.message?.includes("established")
|
|
) {
|
|
toast.error(
|
|
t("fileManager.sshConnectionFailed", {
|
|
name: sshHost.name,
|
|
ip: sshHost.ip,
|
|
port: sshHost.port,
|
|
}),
|
|
);
|
|
} else {
|
|
toast.error(
|
|
`${t("fileManager.failedToSaveFile")}: ${err.message || t("fileManager.unknownError")}`,
|
|
);
|
|
}
|
|
} finally {
|
|
setIsSaving(false);
|
|
}
|
|
};
|
|
|
|
const closeExternalEditor = async () => {
|
|
const session = externalEditorRef.current;
|
|
if (!session) return;
|
|
|
|
session.unsubscribe?.();
|
|
externalEditorRef.current = null;
|
|
|
|
try {
|
|
await window.electronAPI?.closeExternalEditor?.(session.editId);
|
|
} catch (error) {
|
|
console.error("Failed to close external editor session:", error);
|
|
}
|
|
};
|
|
|
|
const handleOpenExternalEditor = async () => {
|
|
if (!window.electronAPI?.isElectron) {
|
|
toast.error(t("fileManager.externalEditorDesktopOnly"));
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await closeExternalEditor();
|
|
const result = await window.electronAPI.openExternalEditor({
|
|
fileName: file.name,
|
|
content: pendingContent || content,
|
|
encoding: "utf8",
|
|
editorPath: externalEditorPath || null,
|
|
});
|
|
|
|
if (!result.success || !result.editId) {
|
|
throw new Error(result.error || "Failed to open external editor");
|
|
}
|
|
|
|
const unsubscribe = window.electronAPI.onExternalEditorSaved?.(
|
|
(payload) => {
|
|
if (payload.editId !== result.editId) return;
|
|
void handleSave(payload.content);
|
|
},
|
|
);
|
|
|
|
externalEditorRef.current = {
|
|
editId: result.editId,
|
|
unsubscribe,
|
|
};
|
|
toast.success(t("fileManager.externalEditorOpened"));
|
|
} catch (error: unknown) {
|
|
console.error("Failed to open external editor:", error);
|
|
const err = error as { message?: string };
|
|
toast.error(
|
|
`${t("fileManager.failedToOpenExternalEditor")}: ${
|
|
err.message || t("fileManager.unknownError")
|
|
}`,
|
|
);
|
|
}
|
|
};
|
|
|
|
const handleChooseExternalEditor = async () => {
|
|
if (!window.electronAPI?.isElectron) {
|
|
toast.error(t("fileManager.externalEditorDesktopOnly"));
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const result = await window.electronAPI.showOpenDialog({
|
|
title: t("fileManager.chooseExternalEditor"),
|
|
properties: ["openFile"],
|
|
});
|
|
const editorPath = result.filePaths?.[0];
|
|
if (result.canceled || !editorPath) return;
|
|
|
|
await window.electronAPI.setSetting?.(
|
|
"fileManager.externalEditorPath",
|
|
editorPath,
|
|
);
|
|
setExternalEditorPath(editorPath);
|
|
toast.success(t("fileManager.externalEditorSelected"));
|
|
} catch (error: unknown) {
|
|
console.error("Failed to select external editor:", error);
|
|
const err = error as { message?: string };
|
|
toast.error(
|
|
`${t("fileManager.failedToSelectExternalEditor")}: ${
|
|
err.message || t("fileManager.unknownError")
|
|
}`,
|
|
);
|
|
}
|
|
};
|
|
|
|
const handleContentChange = (newContent: string) => {
|
|
setPendingContent(newContent);
|
|
|
|
if (autoSaveTimerRef.current) {
|
|
clearTimeout(autoSaveTimerRef.current);
|
|
autoSaveTimerRef.current = null;
|
|
}
|
|
|
|
if (newContent !== content) {
|
|
autoSaveTimerRef.current = setTimeout(async () => {
|
|
try {
|
|
await handleSave(newContent);
|
|
toast.success(t("fileManager.fileAutoSaved"));
|
|
} catch (error) {
|
|
console.error("Auto-save failed:", error);
|
|
toast.error(t("fileManager.autoSaveFailed"));
|
|
}
|
|
}, 60000);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (autoSaveTimerRef.current) {
|
|
clearTimeout(autoSaveTimerRef.current);
|
|
}
|
|
void closeExternalEditor();
|
|
};
|
|
}, []);
|
|
|
|
const handleDownload = async () => {
|
|
const toastId = toast.loading(
|
|
<DownloadProgressToast t={t} fileName={file.name} loaded={0} />,
|
|
{ duration: Infinity },
|
|
);
|
|
try {
|
|
await ensureSSHConnection();
|
|
|
|
await downloadSSHFileStream(
|
|
sshSessionId,
|
|
file.path,
|
|
({ loaded, total }) => {
|
|
toast.loading(
|
|
<DownloadProgressToast
|
|
t={t}
|
|
fileName={file.name}
|
|
loaded={loaded}
|
|
total={total}
|
|
/>,
|
|
{ id: toastId, duration: Infinity },
|
|
);
|
|
},
|
|
);
|
|
toast.success(
|
|
t("fileManager.fileDownloadedSuccessfully", { name: file.name }),
|
|
{ id: toastId, duration: undefined },
|
|
);
|
|
} catch (error: unknown) {
|
|
console.error("Failed to download file:", error);
|
|
|
|
const err = error as { message?: string };
|
|
if (
|
|
err.message?.includes("connection") ||
|
|
err.message?.includes("established")
|
|
) {
|
|
toast.error(
|
|
`SSH connection failed. Please check your connection to ${sshHost.name} (${sshHost.ip}:${sshHost.port})`,
|
|
{ id: toastId, duration: undefined },
|
|
);
|
|
} else {
|
|
toast.error(
|
|
`Failed to download file: ${err.message || "Unknown error"}`,
|
|
{ id: toastId, duration: undefined },
|
|
);
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleClose = () => {
|
|
void closeExternalEditor();
|
|
closeWindow(windowId);
|
|
};
|
|
|
|
const handleMaximize = () => {
|
|
maximizeWindow(windowId);
|
|
};
|
|
|
|
const handleFocus = () => {
|
|
focusWindow(windowId);
|
|
};
|
|
|
|
const handleMediaDimensionsChange = (dimensions: {
|
|
width: number;
|
|
height: number;
|
|
}) => {
|
|
setMediaDimensions(dimensions);
|
|
};
|
|
|
|
if (!currentWindow) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<DraggableWindow
|
|
title={file.name}
|
|
initialX={initialX}
|
|
initialY={initialY}
|
|
initialWidth={800}
|
|
initialHeight={600}
|
|
minWidth={400}
|
|
minHeight={300}
|
|
onClose={handleClose}
|
|
onMaximize={handleMaximize}
|
|
onFocus={handleFocus}
|
|
isMaximized={currentWindow.isMaximized}
|
|
zIndex={currentWindow.zIndex}
|
|
targetSize={mediaDimensions}
|
|
>
|
|
<FileViewer
|
|
file={file}
|
|
content={pendingContent || content}
|
|
savedContent={content}
|
|
isLoading={isLoading}
|
|
isSaving={isSaving}
|
|
resetKey={resetKey}
|
|
onRevert={handleRevert}
|
|
isEditable={isEditable}
|
|
onContentChange={handleContentChange}
|
|
onSave={(newContent) => handleSave(newContent)}
|
|
onDownload={handleDownload}
|
|
onOpenExternal={
|
|
window.electronAPI?.isElectron && isEditable
|
|
? handleOpenExternalEditor
|
|
: undefined
|
|
}
|
|
onChooseExternalEditor={
|
|
window.electronAPI?.isElectron && isEditable
|
|
? handleChooseExternalEditor
|
|
: undefined
|
|
}
|
|
onMediaDimensionsChange={handleMediaDimensionsChange}
|
|
/>
|
|
<SudoPasswordDialog
|
|
open={sudoSaveContent !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) setSudoSaveContent(null);
|
|
}}
|
|
onSubmit={(password) => void handleSudoSave(password)}
|
|
/>
|
|
</DraggableWindow>
|
|
);
|
|
}
|