Split SSH helpers into dedicated modules
- Add auth, command, config, and tunnel exports - Update desktop SSH environment imports - Add tests for auth and command helpers
This commit is contained in:
1 parent
20edab789d
commit
e4b6dd4efa
11 files changed
+273
-246
No files matched your search
@@ -0,0 +1,88 @@
|
||||
import * as Crypto from "node:crypto";
|
||||
|
||||
import type { DesktopSshEnvironmentTarget, DesktopUpdateChannel } from "@t3tools/contracts";
|
||||
|
||||
const PUBLISHABLE_T3_VERSION_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u;
|
||||
|
||||
export function parseSshResolveOutput(alias: string, stdout: string): DesktopSshEnvironmentTarget {
|
||||
const values = new Map<string, string>();
|
||||
for (const line of stdout.split(/\r?\n/u)) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const [key, ...rest] = trimmed.split(/\s+/u);
|
||||
if (!key || rest.length === 0 || values.has(key)) {
|
||||
continue;
|
||||
}
|
||||
values.set(key, rest.join(" ").trim());
|
||||
}
|
||||
|
||||
const hostname = values.get("hostname")?.trim() || alias;
|
||||
const username = values.get("user")?.trim() || null;
|
||||
const rawPort = values.get("port")?.trim() ?? "";
|
||||
const parsedPort = Number.parseInt(rawPort, 10);
|
||||
|
||||
return {
|
||||
alias,
|
||||
hostname,
|
||||
username,
|
||||
port: Number.isInteger(parsedPort) ? parsedPort : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function targetConnectionKey(target: DesktopSshEnvironmentTarget): string {
|
||||
return `${target.alias}\u0000${target.hostname}\u0000${target.username ?? ""}\u0000${target.port ?? ""}`;
|
||||
}
|
||||
|
||||
export function remoteStateKey(target: DesktopSshEnvironmentTarget): string {
|
||||
return Crypto.createHash("sha256").update(targetConnectionKey(target)).digest("hex").slice(0, 16);
|
||||
}
|
||||
|
||||
export function buildSshHostSpec(target: DesktopSshEnvironmentTarget): string {
|
||||
const destination = target.alias.trim() || target.hostname.trim();
|
||||
if (destination.length === 0) {
|
||||
throw new Error("SSH target is missing its alias/hostname.");
|
||||
}
|
||||
return target.username ? `${target.username}@${destination}` : destination;
|
||||
}
|
||||
|
||||
export function baseSshArgs(
|
||||
target: DesktopSshEnvironmentTarget,
|
||||
input?: { readonly batchMode?: "yes" | "no" },
|
||||
): string[] {
|
||||
return [
|
||||
"-o",
|
||||
`BatchMode=${input?.batchMode ?? "no"}`,
|
||||
"-o",
|
||||
"ConnectTimeout=10",
|
||||
...(target.port !== null ? ["-p", String(target.port)] : []),
|
||||
];
|
||||
}
|
||||
|
||||
export function getLastNonEmptyOutputLine(stdout: string): string | null {
|
||||
return (
|
||||
stdout
|
||||
.trim()
|
||||
.split(/\r?\n/u)
|
||||
.map((entry) => entry.trim())
|
||||
.findLast((entry) => entry.length > 0) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveRemoteT3CliPackageSpec(input: {
|
||||
readonly appVersion: string;
|
||||
readonly updateChannel: DesktopUpdateChannel;
|
||||
readonly isDevelopment?: boolean;
|
||||
}): string {
|
||||
const appVersion = input.appVersion.trim();
|
||||
if (!input.isDevelopment && PUBLISHABLE_T3_VERSION_PATTERN.test(appVersion)) {
|
||||
return `t3@${appVersion}`;
|
||||
}
|
||||
|
||||
if (input.isDevelopment) {
|
||||
return "t3@nightly";
|
||||
}
|
||||
|
||||
return input.updateChannel === "nightly" ? "t3@nightly" : "t3@latest";
|
||||
}
|
||||
Reference in new issue
Block a user