mirror of
https://bitbucket.org/siakitem/my-pi.git
synced 2026-08-28 08:35:57 +00:00
feat(pi-ssh): support SSH agent authentication
This commit is contained in:
@@ -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...");
|
||||
|
||||
Reference in New Issue
Block a user