Files
Netcatty/components/HostDetailsPanel.helpers.ts
T
陈大猫 d536b22dee fix(host-details): show inherited group proxy and jump hosts (#4188)
* fix(host-details): show inherited group proxy and jump hosts

Host editor summaries for HTTP/SOCKS5/command proxy and ProxyJump
chains now display the same values applyGroupDefaults uses at
connect time, without writing inherited settings onto the host
until the user edits them.

Fixes #4187

* fix(host-details): keep custom proxy and empty jump-host overrides

Selecting Custom while a group saved proxy is inherited now keeps a host-owned draft instead of snapping back to that profile. Removing the last hop of a multi-hop inherited chain keeps an empty override so applyGroupDefaults cannot restore the group hops.

* fix(host-details): keep empty override when clearing inherited chain

Clear Chain on an inherited-only jump-host list now stores { hostIds: [] }
instead of deleting the field so group hops cannot snap back.
2026-11-08 21:30:36 +08:00

227 lines
7.1 KiB
TypeScript

import type { GroupConfig } from "../domain/models";
import { applyGroupDefaults } from "../domain/groupConfig";
import {
findProxyProfile,
hasIncompleteProxyIdentity,
hasMissingProxyIdentity,
hasUnreadableProxyCredential,
isCompleteProxyConfig,
normalizeManualProxyConfig,
} from "../domain/proxyProfiles";
import type { Host, Identity, ProxyConfig, ProxyProfile } from "../types";
import {
LINUX_DISTRO_OPTIONS,
NETWORK_DEVICE_OPTIONS,
POSIX_PLATFORM_OPTIONS,
} from "../domain/host";
export const parseOptionalPortInput = (value: string): number | undefined =>
value ? Number(value) : undefined;
export const resolvePrimaryProtocolSwitchPort = (
currentPort: number | undefined,
nextProtocol: "ssh" | "telnet",
hasGroupTelnetPortDefault: boolean,
hasGroupSshPortDefault: boolean,
): number | undefined => {
if (nextProtocol === "telnet") {
// Don't override if group provides a Telnet default
if (hasGroupTelnetPortDefault || hasGroupSshPortDefault) return currentPort;
if (currentPort === 22 || currentPort === undefined) return 23;
return currentPort;
}
if (nextProtocol === "ssh") {
if (hasGroupSshPortDefault) return currentPort;
if (currentPort === 23 || currentPort === undefined) return 22;
return currentPort;
}
return currentPort;
};
export const resolvePrimaryProtocolSavePort = (
protocol: Host["protocol"],
currentPort: number | undefined,
hasGroupSshPortDefault: boolean,
hasGroupTelnetPortDefault: boolean,
): number | undefined => {
if (protocol === "telnet") {
if (currentPort !== undefined) return currentPort;
if (hasGroupTelnetPortDefault || hasGroupSshPortDefault) return undefined;
return 23;
}
return currentPort ?? (hasGroupSshPortDefault ? undefined : 22);
};
export const resolveDetailsTelnetPort = (
host: Host,
groupDefaults?: Partial<GroupConfig>,
): number => {
if (host.telnetPort !== undefined && host.telnetPort !== null) return host.telnetPort;
if (groupDefaults?.telnetPort !== undefined && groupDefaults.telnetPort !== null) {
return groupDefaults.telnetPort;
}
if (host.protocol === "telnet") {
if (host.port !== undefined && host.port !== null) return host.port;
if (groupDefaults?.port !== undefined && groupDefaults.port !== null) return groupDefaults.port;
}
return 23;
};
export const resolveDetailsTelnetUsername = (
host: Host,
groupDefaults?: Partial<GroupConfig>,
): string =>
host.telnetUsername !== undefined
? host.telnetUsername
: groupDefaults?.telnetUsername !== undefined
? groupDefaults.telnetUsername
: host.username ?? groupDefaults?.username ?? "";
export const resolveDetailsTelnetPassword = (
host: Host,
groupDefaults?: Partial<GroupConfig>,
): string =>
host.telnetPassword !== undefined
? host.telnetPassword
: groupDefaults?.telnetPassword !== undefined
? groupDefaults.telnetPassword
: host.password ?? groupDefaults?.password ?? "";
export const prepareTelnetCredentialsForSave = (host: Host): Host =>
host.telnetIdentityId
? {
...host,
telnetUsername: undefined,
telnetPassword: undefined,
}
: host;
export const LINUX_DISTRO_OPTION_IDS = [
...LINUX_DISTRO_OPTIONS,
...POSIX_PLATFORM_OPTIONS,
...NETWORK_DEVICE_OPTIONS,
];
export type ProxySaveValidationError =
| "required"
| "port"
| "missingSaved"
| "missingIdentity"
| "incompleteIdentity"
| "unreadableIdentity";
export const validateProxyConfigForSave = ({
proxyConfig,
proxyProfileId,
proxyProfiles,
identities,
}: {
proxyConfig?: ProxyConfig;
proxyProfileId?: string;
proxyProfiles?: ProxyProfile[];
identities?: Identity[];
}): ProxySaveValidationError | null => {
const selectedProxyProfile = findProxyProfile(proxyProfileId, proxyProfiles ?? []);
if (proxyProfileId && !selectedProxyProfile) return "missingSaved";
const normalizedProxyConfig = normalizeManualProxyConfig(proxyConfig);
if (normalizedProxyConfig && !isCompleteProxyConfig(normalizedProxyConfig)) {
return normalizedProxyConfig.host ? "port" : "required";
}
const effectiveProxyConfig = selectedProxyProfile?.config ?? proxyConfig;
if (hasMissingProxyIdentity(effectiveProxyConfig, identities ?? [])) {
return "missingIdentity";
}
if (hasIncompleteProxyIdentity(effectiveProxyConfig, identities ?? [])) {
return "incompleteIdentity";
}
if (hasUnreadableProxyCredential(effectiveProxyConfig, identities ?? [])) {
return "unreadableIdentity";
}
return null;
};
export const prepareProxyConfigForSave = (params: {
proxyConfig?: ProxyConfig;
proxyProfileId?: string;
proxyProfiles?: ProxyProfile[];
identities?: Identity[];
}): {
error?: ProxySaveValidationError;
normalizedProxyConfig?: ProxyConfig;
} => {
const error = validateProxyConfigForSave(params);
if (error) return { error };
return {
normalizedProxyConfig: normalizeManualProxyConfig(params.proxyConfig),
};
};
export const EMPTY_CUSTOM_PROXY_DRAFT: ProxyConfig = { type: "http", host: "", port: 8080 };
export const resolveDisplayProxyProfileId = (
form: Pick<Host, "directProxy" | "proxyProfileId" | "proxyConfig">,
inheritedProfileId?: string,
): string | undefined => {
if (form.directProxy || form.proxyConfig !== undefined) return form.proxyProfileId;
return form.proxyProfileId ?? inheritedProfileId;
};
export const nextFormAfterSelectProxyProfile = (
prev: Host,
profileId: string | undefined,
groupDefaults?: Partial<GroupConfig>,
): Host => {
const { proxyConfig: _proxyConfig, proxyProfileId: _proxyProfileId, directProxy: _directProxy, ...rest } = prev;
if (!profileId) {
const inheritedConfig = !prev.directProxy
&& prev.proxyProfileId === undefined
&& prev.proxyConfig === undefined
&& groupDefaults
? applyGroupDefaults(prev, groupDefaults).proxyConfig
: undefined;
return {
...rest,
proxyConfig: prev.proxyConfig ?? inheritedConfig ?? EMPTY_CUSTOM_PROXY_DRAFT,
} as Host;
}
return { ...rest, proxyProfileId: profileId } as Host;
};
export const resolveFormHostChainIds = (
host: Host,
groupDefaults?: Partial<GroupConfig>,
): string[] => {
if (host.hostChain !== undefined) return host.hostChain.hostIds || [];
if (!groupDefaults) return [];
return applyGroupDefaults(host, groupDefaults).hostChain?.hostIds || [];
};
export const nextFormAfterClearHostChain = (
prev: Host,
groupDefaults?: Partial<GroupConfig>,
): Host => {
const groupWouldRestore = Boolean(
groupDefaults
&& applyGroupDefaults({ ...prev, hostChain: undefined }, groupDefaults).hostChain?.hostIds?.length,
);
if (prev.hostChain === undefined || groupWouldRestore) {
return { ...prev, hostChain: { hostIds: [] } };
}
const { hostChain: _hostChain, ...rest } = prev;
return rest as Host;
};
export const nextFormAfterRemoveHostFromChain = (
prev: Host,
index: number,
groupDefaults?: Partial<GroupConfig>,
): Host => {
const ids = resolveFormHostChainIds(prev, groupDefaults).filter((_, i) => i !== index);
if (ids.length > 0) return { ...prev, hostChain: { hostIds: ids } };
return nextFormAfterClearHostChain(prev, groupDefaults);
};