feat(pi-ssh): support SSH agent authentication

This commit is contained in:
云服务部-叶林立
2026-08-26 20:01:45 +08:00
parent b7758b239e
commit 874d4da096
10 changed files with 98 additions and 22 deletions
+13 -4
View File
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
import { stdin, stdout } from "node:process";
import readline from "node:readline/promises";
import {
effectiveIdentityAgent,
effectiveValue,
effectiveValues,
listDirectSshAliases,
@@ -106,11 +107,19 @@ async function importHost(config, alias) {
const identityFiles = effectiveValues(effective, "identityfile")
.map((path) => path.replace(/^"|"$/g, ""))
.filter((path) => existsSync(expandUserPath(path)));
const defaultMode = identityFiles.length > 0 ? "key" : "password";
const modeInput = (await question(`Authentication [key/password] (${defaultMode}): `)).toLowerCase();
const agentSocket = effectiveIdentityAgent(effective);
const agentAvailable = agentSocket && (agentSocket === "pageant" || existsSync(expandUserPath(agentSocket)));
const defaultMode = agentAvailable ? "agent" : identityFiles.length > 0 ? "key" : "password";
const modeInput = (await question(`Authentication [agent/key/password] (${defaultMode}): `)).toLowerCase();
const mode = modeInput || defaultMode;
let auth;
if (mode === "key") {
if (mode === "agent") {
const socketPath = await question(`SSH agent socket${agentSocket ? ` (${agentSocket})` : ""}: `) || agentSocket;
if (!socketPath || (socketPath !== "pageant" && !existsSync(expandUserPath(socketPath)))) {
throw new Error(`SSH agent socket does not exist: ${socketPath ?? ""}`);
}
auth = { type: "agent", socketPath };
} else if (mode === "key") {
const suggested = identityFiles[0] ?? "";
const identityFile = await question(`Private key path${suggested ? ` (${suggested})` : ""}: `) || suggested;
if (!identityFile || !existsSync(expandUserPath(identityFile))) throw new Error(`private key does not exist: ${identityFile}`);
@@ -123,7 +132,7 @@ async function importHost(config, alias) {
if (!password) throw new Error("server password cannot be empty");
auth = { type: "password", password, method: "auto" };
} else {
throw new Error("authentication must be key or password");
throw new Error("authentication must be agent, key, or password");
}
console.log("Obtaining SSH host key fingerprint...");