@evandu/super-pi 0.1.260 → 0.1.261

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,14 +10,15 @@ export class HostTrustRequired extends Error {
10
10
  }
11
11
  }
12
12
  export const sh = (value) => `'${value.replaceAll("'", "'\\''")}'`;
13
- export async function connectSsh(profile) {
13
+ export async function connectSsh(profile, options = {}) {
14
+ options.signal?.throwIfAborted();
14
15
  const { Client } = await import("ssh2");
15
16
  const client = new Client();
16
17
  let observed = "";
17
18
  let trusted = false;
18
19
  const settings = {
19
20
  host: profile.host, port: profile.port, username: profile.username,
20
- readyTimeout: 20_000, keepaliveInterval: 15_000, keepaliveCountMax: 3,
21
+ readyTimeout: 0, tryKeyboard: true, keepaliveInterval: 15_000, keepaliveCountMax: 3,
21
22
  hostVerifier: (key) => {
22
23
  observed = `SHA256:${createHash("sha256").update(key).digest("base64").replace(/=+$/, "")}`;
23
24
  trusted = profile.fingerprint === observed;
@@ -33,10 +34,59 @@ export async function connectSsh(profile) {
33
34
  else
34
35
  settings.agent = process.env.SSH_AUTH_SOCK || (process.platform === "win32" ? "\\\\.\\pipe\\openssh-ssh-agent" : undefined);
35
36
  return new Promise((resolve, reject) => {
36
- client.once("ready", () => { client.removeListener("error", failed); client.on("error", () => { }); resolve(client); });
37
- const failed = (error) => { client.end(); reject(observed && !trusted ? new HostTrustRequired(observed, Boolean(profile.fingerprint)) : new Error(error.level === "client-authentication" ? "SSH 认证失败,请检查用户名和认证信息" : "SSH 连接失败,请检查地址、端口、网络和认证信息")); };
37
+ let settled = false, round = 0, interactive = false;
38
+ let timer;
39
+ const cleanup = () => { clearTimeout(timer); options.signal?.removeEventListener("abort", cancelled); };
40
+ const stop = (reason) => { if (settled)
41
+ return; settled = true; cleanup(); client.destroy(); reject(reason); };
42
+ const arm = (waiting) => {
43
+ clearTimeout(timer);
44
+ timer = setTimeout(() => stop(new Error(waiting ? "SSH 交互验证超时,请重新连接" : "SSH 连接超时,请检查网络后重试")), waiting ? options.verificationTimeoutMs ?? 180_000 : options.handshakeTimeoutMs ?? 20_000);
45
+ };
46
+ const cancelled = () => stop(options.signal?.reason instanceof Error ? options.signal.reason : new Error("SSH 连接已取消"));
47
+ const failed = (error) => stop(observed && !trusted ? new HostTrustRequired(observed, Boolean(profile.fingerprint)) : new Error(error.level === "client-authentication" ? interactive ? "SSH 交互验证未通过,请重新连接后重试" : "SSH 认证失败,请检查用户名和认证信息" : "SSH 连接失败,请检查地址、端口、网络和认证信息"));
48
+ client.once("ready", () => { if (settled)
49
+ return; settled = true; cleanup(); client.removeListener("error", failed); client.on("error", () => { }); resolve(client); });
38
50
  client.once("error", failed);
39
- client.connect(settings);
51
+ client.once("close", () => { if (!settled)
52
+ stop(new Error("SSH 连接在验证完成前关闭,请重新连接")); });
53
+ client.on("keyboard-interactive", (name, instruction, _language, prompts, finish) => {
54
+ if (settled)
55
+ return;
56
+ interactive = true;
57
+ if (!options.onVerification) {
58
+ if (!prompts.length) {
59
+ finish([]);
60
+ return;
61
+ }
62
+ stop(new Error("SSH 服务器要求交互验证,请通过服务器连接设置完成验证"));
63
+ return;
64
+ }
65
+ const currentRound = ++round;
66
+ arm(true);
67
+ void Promise.resolve().then(() => options.onVerification({ name, instruction, prompts: prompts.map(item => ({ prompt: item.prompt, echo: item.echo === true })) })).then(answers => {
68
+ if (settled || currentRound !== round)
69
+ return;
70
+ if (answers.length !== prompts.length || answers.some(answer => typeof answer !== "string")) {
71
+ stop(new Error("SSH 交互验证响应格式无效"));
72
+ return;
73
+ }
74
+ arm(true);
75
+ finish(answers);
76
+ }, reason => stop(reason instanceof Error ? reason : new Error("SSH 交互验证已取消")));
77
+ });
78
+ options.signal?.addEventListener("abort", cancelled, { once: true });
79
+ if (options.signal?.aborted) {
80
+ cancelled();
81
+ return;
82
+ }
83
+ arm(false);
84
+ try {
85
+ client.connect(settings);
86
+ }
87
+ catch (error) {
88
+ stop(error instanceof Error ? error : new Error("无法启动 SSH 连接"));
89
+ }
40
90
  });
41
91
  }
42
92
  export function sshExec(client, command, options = {}) {
@@ -1,3 +1,13 @@
1
+ export interface SshVerification {
2
+ id: string;
3
+ name: string;
4
+ instruction: string;
5
+ expiresAt: string;
6
+ prompts: {
7
+ prompt: string;
8
+ echo: boolean;
9
+ }[];
10
+ }
1
11
  export interface ManagedServerProfile {
2
12
  id: string;
3
13
  name: string;
@@ -13,10 +23,20 @@ export interface ManagedServerProfile {
13
23
  identity?: string;
14
24
  deployedAt?: string;
15
25
  remoteRoot?: string;
16
- ssh: "offline" | "connecting" | "online" | "needsTrust";
26
+ ssh: "offline" | "connecting" | "online" | "needsTrust" | "awaitingVerification";
17
27
  service: "unknown" | "offline" | "online";
18
28
  modelChannel: "offline" | "online";
19
- phase: "idle" | "connecting" | "needsTrust" | "deploying" | "ready" | "failed";
29
+ phase: "idle" | "connecting" | "needsTrust" | "awaitingVerification" | "deploying" | "ready" | "failed";
30
+ verification?: SshVerification;
31
+ existingService?: {
32
+ running: boolean;
33
+ attached: boolean;
34
+ version?: string;
35
+ dataDirectory?: string;
36
+ hasData?: boolean;
37
+ canStart?: boolean;
38
+ requiresToken?: boolean;
39
+ };
20
40
  step?: string;
21
41
  error?: string;
22
42
  log?: string[];
@@ -33,4 +53,5 @@ export interface ManagedServerInput {
33
53
  password?: string;
34
54
  privateKey?: string;
35
55
  passphrase?: string;
56
+ serviceAccessToken?: string;
36
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.260",
3
+ "version": "0.1.261",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -48,6 +48,8 @@
48
48
  "pack:server": "npm run build && node scripts/pack-server.mjs"
49
49
  },
50
50
  "check:logic": [
51
+ "scripts/existing-service.test.mjs",
52
+ "scripts/ssh-keyboard-interactive.test.mjs",
51
53
  "scripts/title-cold-start.test.mjs",
52
54
  "scripts/desktop-runtime.test.mjs",
53
55
  "scripts/managed-ssh.test.mjs",
@@ -304,6 +306,7 @@
304
306
  "scripts/session-reminder-pmo-rights.test.mjs"
305
307
  ],
306
308
  "check:browser": [
309
+ "scripts/ssh-keyboard-browser.test.mjs",
307
310
  "scripts/desktop-browser.test.mjs",
308
311
  "scripts/managed-remote-install.test.mjs",
309
312
  "scripts/multi-server-browser.test.mjs",