mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-07-17 05:43:36 +00:00
feat: save quick connect sessions as hosts (#1055)
This commit is contained in:
@@ -5,6 +5,7 @@ import { Input } from "@/components/input";
|
||||
import type { Host } from "@/types/ui-types";
|
||||
import { getCredentials } from "@/api/credentials-api";
|
||||
import { mapCredentials } from "./HostManagerData";
|
||||
import { createQuickConnectHost } from "./quick-connect-host";
|
||||
|
||||
interface QuickConnectPanelProps {
|
||||
onConnect: (host: Host, type: "terminal" | "files") => void;
|
||||
@@ -34,38 +35,15 @@ export function QuickConnectPanel({ onConnect }: QuickConnectPanelProps) {
|
||||
|
||||
const connect = (type: "terminal" | "files") => {
|
||||
if (!host || !username) return;
|
||||
const hostConfig: Host = {
|
||||
id: `quick-connect-${Date.now()}`,
|
||||
name: `${username}@${host}`,
|
||||
const hostConfig = createQuickConnectHost({
|
||||
ip: host,
|
||||
port: parseInt(port) || 22,
|
||||
username,
|
||||
authType,
|
||||
password: authType === "password" ? password : undefined,
|
||||
key: authType === "key" ? privateKey : undefined,
|
||||
credentialId: authType === "credential" ? credentialId : undefined,
|
||||
folder: "",
|
||||
online: false,
|
||||
cpu: null,
|
||||
ram: null,
|
||||
lastAccess: new Date().toISOString(),
|
||||
pin: false,
|
||||
defaultPath: "",
|
||||
serverTunnels: [],
|
||||
quickActions: [],
|
||||
enableTerminal: true,
|
||||
enableFileManager: true,
|
||||
enableTunnel: true,
|
||||
enableDocker: true,
|
||||
enableSsh: true,
|
||||
enableRdp: false,
|
||||
enableVnc: false,
|
||||
enableTelnet: false,
|
||||
sshPort: parseInt(port) || 22,
|
||||
rdpPort: 3389,
|
||||
vncPort: 5900,
|
||||
telnetPort: 23,
|
||||
};
|
||||
password,
|
||||
key: privateKey,
|
||||
credentialId,
|
||||
});
|
||||
onConnect(hostConfig, type);
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createQuickConnectHost,
|
||||
quickConnectHostToPayload,
|
||||
} from "./quick-connect-host";
|
||||
|
||||
describe("quick connect host", () => {
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
it("preserves password authentication when saving the connection", () => {
|
||||
vi.spyOn(Date, "now").mockReturnValue(1234);
|
||||
|
||||
const host = createQuickConnectHost({
|
||||
ip: "server.example.com",
|
||||
port: 2222,
|
||||
username: "root",
|
||||
authType: "password",
|
||||
password: "secret",
|
||||
});
|
||||
|
||||
expect(host.id).toBe("quick-connect-1234");
|
||||
expect(quickConnectHostToPayload(host)).toMatchObject({
|
||||
name: "root@server.example.com",
|
||||
ip: "server.example.com",
|
||||
port: 2222,
|
||||
username: "root",
|
||||
authType: "password",
|
||||
password: "secret",
|
||||
connectionType: "ssh",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps only the selected credential authentication data", () => {
|
||||
const host = createQuickConnectHost({
|
||||
ip: "10.0.0.2",
|
||||
port: 22,
|
||||
username: "deploy",
|
||||
authType: "credential",
|
||||
credentialId: "42",
|
||||
password: "ignored",
|
||||
key: "ignored",
|
||||
});
|
||||
const payload = quickConnectHostToPayload(host);
|
||||
|
||||
expect(payload.credentialId).toBe(42);
|
||||
expect(payload.password).toBeUndefined();
|
||||
expect(payload.key).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
import type { SSHHostData } from "@/types";
|
||||
import type { Host } from "@/types/ui-types";
|
||||
|
||||
type QuickConnectInput = Pick<
|
||||
Host,
|
||||
"ip" | "port" | "username" | "authType" | "password" | "key" | "credentialId"
|
||||
>;
|
||||
|
||||
export function createQuickConnectHost(input: QuickConnectInput): Host {
|
||||
return {
|
||||
id: `quick-connect-${Date.now()}`,
|
||||
name: `${input.username}@${input.ip}`,
|
||||
ip: input.ip,
|
||||
port: input.port,
|
||||
username: input.username,
|
||||
authType: input.authType,
|
||||
password: input.authType === "password" ? input.password : undefined,
|
||||
key: input.authType === "key" ? input.key : undefined,
|
||||
credentialId:
|
||||
input.authType === "credential" ? input.credentialId : undefined,
|
||||
folder: "",
|
||||
online: false,
|
||||
cpu: null,
|
||||
ram: null,
|
||||
lastAccess: new Date().toISOString(),
|
||||
pin: false,
|
||||
defaultPath: "",
|
||||
serverTunnels: [],
|
||||
quickActions: [],
|
||||
enableTerminal: true,
|
||||
enableCommandHistory: true,
|
||||
enableFileManager: true,
|
||||
enableTunnel: true,
|
||||
enableDocker: true,
|
||||
enableProxmox: false,
|
||||
enableTmuxMonitor: false,
|
||||
enableSsh: true,
|
||||
enableRdp: false,
|
||||
enableVnc: false,
|
||||
enableTelnet: false,
|
||||
sshPort: input.port,
|
||||
rdpPort: 3389,
|
||||
vncPort: 5900,
|
||||
telnetPort: 23,
|
||||
};
|
||||
}
|
||||
|
||||
export function quickConnectHostToPayload(host: Host): SSHHostData {
|
||||
return {
|
||||
name: host.name,
|
||||
ip: host.ip,
|
||||
port: host.port,
|
||||
username: host.username,
|
||||
authType: host.authType,
|
||||
password: host.password,
|
||||
key: host.key,
|
||||
credentialId: host.credentialId
|
||||
? Number.parseInt(host.credentialId, 10)
|
||||
: null,
|
||||
folder: host.folder,
|
||||
pin: host.pin,
|
||||
defaultPath: host.defaultPath,
|
||||
enableTerminal: true,
|
||||
enableSessionLogging: true,
|
||||
enableCommandHistory: host.enableCommandHistory,
|
||||
enableFileManager: host.enableFileManager,
|
||||
enableTunnel: host.enableTunnel,
|
||||
enableDocker: host.enableDocker,
|
||||
enableProxmox: host.enableProxmox,
|
||||
enableTmuxMonitor: host.enableTmuxMonitor,
|
||||
showTerminalInSidebar: true,
|
||||
showFileManagerInSidebar: true,
|
||||
showTunnelInSidebar: true,
|
||||
showDockerInSidebar: true,
|
||||
showServerStatsInSidebar: true,
|
||||
connectionType: "ssh",
|
||||
enableSsh: true,
|
||||
enableRdp: false,
|
||||
enableVnc: false,
|
||||
enableTelnet: false,
|
||||
sshPort: host.sshPort,
|
||||
rdpPort: host.rdpPort,
|
||||
vncPort: host.vncPort,
|
||||
telnetPort: host.telnetPort,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user