@nsyan/ssh 0.1.0 → 0.1.1
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.
- package/index.ts +68 -41
- package/package.json +1 -1
- package/src/core/deploy.ts +12 -6
- package/src/core/doctor.ts +37 -6
- package/src/core/guard.ts +59 -26
- package/src/core/hash.ts +12 -8
- package/src/runtime.ts +288 -91
- package/src/scan/save.ts +1 -3
- package/src/transports/ftp.ts +6 -1
- package/src/transports/openssh.ts +189 -15
- package/src/transports/ssh2.ts +25 -10
package/index.ts
CHANGED
|
@@ -3,11 +3,12 @@ import { Type } from "typebox";
|
|
|
3
3
|
import { readAudit, defaultAuditPath } from "./src/core/audit.js";
|
|
4
4
|
import { loadHosts, saveHosts, defaultConfigPath } from "./src/core/config.js";
|
|
5
5
|
import { doctorLines } from "./src/core/doctor.js";
|
|
6
|
+
import { closeControlMasters } from "./src/transports/openssh.js";
|
|
6
7
|
import { parseSshConfig } from "./src/scan/ssh-config.js";
|
|
7
8
|
import { projectDeployFiles } from "./src/scan/project.js";
|
|
8
9
|
import { saveHostCandidate } from "./src/scan/save.js";
|
|
9
|
-
import { defaultSecretsPath, putSecret } from "./src/core/secrets.js";
|
|
10
|
-
import { HostKeyUnknownError } from "./src/core/types.js";
|
|
10
|
+
import { defaultSecretsPath, deleteSecret, putSecret } from "./src/core/secrets.js";
|
|
11
|
+
import { HostKeyUnknownError, type HostProfile } from "./src/core/types.js";
|
|
11
12
|
import { readFileSync } from "node:fs";
|
|
12
13
|
import { homedir } from "node:os";
|
|
13
14
|
import { join } from "node:path";
|
|
@@ -64,23 +65,44 @@ async function readSecret(
|
|
|
64
65
|
* 首次连接的指纹必须给人看过才算数。
|
|
65
66
|
* 未知密钥先失败,把指纹摆出来让人确认,确认后才写 known_hosts。
|
|
66
67
|
*/
|
|
68
|
+
async function confirmUnknownKey(
|
|
69
|
+
error: unknown,
|
|
70
|
+
ui: { confirm: (title: string, message: string) => Promise<boolean> },
|
|
71
|
+
): Promise<boolean> {
|
|
72
|
+
if (!(error instanceof HostKeyUnknownError)) return false;
|
|
73
|
+
return ui.confirm("未知主机密钥", `${error.message}\n\n用 ssh-keygen -lf 核对后,确认记录这个指纹并继续?`);
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
async function testWithTofu(
|
|
68
77
|
hostName: string | undefined,
|
|
69
78
|
ui: { confirm: (title: string, message: string) => Promise<boolean> },
|
|
70
|
-
)
|
|
79
|
+
) {
|
|
80
|
+
try {
|
|
81
|
+
// 命令侧允许缺省打活动主机;工具调用仍然必须显式传 host。
|
|
82
|
+
return await runtime.testHost({ host: hostName, allowActive: true });
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (!(await confirmUnknownKey(error, ui))) throw error;
|
|
85
|
+
return runtime.testHost({ host: hostName, allowActive: true, acceptNewHostKey: true });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 测一个还没入库的候选。通过才写盘,拒绝指纹 = 不保存。 */
|
|
90
|
+
async function testProfileWithTofu(
|
|
91
|
+
profile: HostProfile,
|
|
92
|
+
ui: { confirm: (title: string, message: string) => Promise<boolean> },
|
|
93
|
+
) {
|
|
71
94
|
try {
|
|
72
|
-
return await runtime.
|
|
95
|
+
return await runtime.testProfile(profile);
|
|
73
96
|
} catch (error) {
|
|
74
|
-
if (!(error
|
|
75
|
-
|
|
76
|
-
if (!accepted) throw new Error("用户没有确认主机密钥,已停止。");
|
|
77
|
-
return runtime.testHost({ host: hostName, acceptNewHostKey: true });
|
|
97
|
+
if (!(await confirmUnknownKey(error, ui))) throw error;
|
|
98
|
+
return runtime.testProfile(profile, true);
|
|
78
99
|
}
|
|
79
100
|
}
|
|
80
101
|
|
|
81
102
|
export default function sshExtension(pi: ExtensionAPI) {
|
|
82
103
|
pi.on("session_shutdown", () => {
|
|
83
104
|
void runtime.closeTunnels();
|
|
105
|
+
void closeControlMasters();
|
|
84
106
|
});
|
|
85
107
|
|
|
86
108
|
pi.registerTool({
|
|
@@ -420,28 +442,27 @@ export default function sshExtension(pi: ExtensionAPI) {
|
|
|
420
442
|
const current = loadHosts();
|
|
421
443
|
const choice = params.choice ?? (current.hosts[params.name] ? undefined : "rename");
|
|
422
444
|
if (!choice) return text("同名主机已存在。choice 需要是 overwrite、rename 或 skip。", { saved: false });
|
|
423
|
-
|
|
445
|
+
const saved = saveHostCandidate(current, params, choice);
|
|
446
|
+
if (!saved.saved) return text("同名主机已跳过", { saved: false });
|
|
447
|
+
const candidate = saved.file.hosts[saved.name];
|
|
448
|
+
let wroteSecret = false;
|
|
449
|
+
let profile = candidate;
|
|
424
450
|
if (!params.privateKeyPath) {
|
|
425
451
|
const password = await readSecret(ctx, "登录密码", "密码不进入对话记录");
|
|
426
452
|
if (!password) return text("没有密码,未保存", { saved: false });
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
if (!saved.saved) return text("同名主机已跳过", { saved: false });
|
|
431
|
-
if (secretRef?.startsWith("pending:")) {
|
|
432
|
-
putSecret(defaultSecretsPath(), saved.name, secretRef.slice("pending:".length));
|
|
433
|
-
saved.file.hosts[saved.name].auth.secretRef = `file:${defaultSecretsPath()}#${saved.name}`;
|
|
453
|
+
putSecret(defaultSecretsPath(), saved.name, password);
|
|
454
|
+
wroteSecret = true;
|
|
455
|
+
profile = { ...candidate, auth: { ...candidate.auth, secretRef: `file:${defaultSecretsPath()}#${saved.name}` } };
|
|
434
456
|
}
|
|
435
|
-
saveHosts(defaultConfigPath(), saved.file);
|
|
436
457
|
try {
|
|
437
458
|
const tested = params.acceptNewHostKey === true
|
|
438
|
-
? await runtime.
|
|
439
|
-
: await
|
|
440
|
-
|
|
459
|
+
? await runtime.testProfile(profile, true)
|
|
460
|
+
: await testProfileWithTofu(profile, ctx.ui);
|
|
461
|
+
saved.file.hosts[saved.name] = profile;
|
|
462
|
+
saveHosts(defaultConfigPath(), saved.file);
|
|
463
|
+
return text(`已保存 ${saved.name},${tested.auth} ${tested.fingerprint} ${tested.latencyMs}ms`, { saved: true, name: saved.name, ...tested });
|
|
441
464
|
} catch (error) {
|
|
442
|
-
|
|
443
|
-
delete next.hosts[saved.name];
|
|
444
|
-
saveHosts(defaultConfigPath(), next);
|
|
465
|
+
if (wroteSecret) deleteSecret(defaultSecretsPath(), saved.name);
|
|
445
466
|
return failure(error);
|
|
446
467
|
}
|
|
447
468
|
},
|
|
@@ -467,8 +488,8 @@ export default function sshExtension(pi: ExtensionAPI) {
|
|
|
467
488
|
}
|
|
468
489
|
if (sub === "test") {
|
|
469
490
|
try {
|
|
470
|
-
const result = await
|
|
471
|
-
ctx.ui.notify(`${name || "活动主机"} ${result.auth} ${result.fingerprint} ${result.latencyMs}ms`, result.ok ? "info" : "error");
|
|
491
|
+
const result = await testWithTofu(name, ctx.ui);
|
|
492
|
+
ctx.ui.notify(`${name || "活动主机"} ${result.auth} ${result.fingerprint || "无指纹"} ${result.latencyMs}ms`, result.ok ? "info" : "error");
|
|
472
493
|
} catch (error) {
|
|
473
494
|
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
474
495
|
}
|
|
@@ -517,35 +538,41 @@ export default function sshExtension(pi: ExtensionAPI) {
|
|
|
517
538
|
const keyPath = await ctx.ui.input("私钥路径,密码登录留空", current?.auth.privateKeyPath ?? "");
|
|
518
539
|
const prod = await ctx.ui.confirm("生产主机", "这台机器的标签要包含 prod 吗?生产主机的写操作一律确认。");
|
|
519
540
|
if (!hostName || !address || !user) return;
|
|
520
|
-
let secretRef = current?.auth.secretRef;
|
|
521
|
-
if (!keyPath) {
|
|
522
|
-
const password = await readSecret(ctx, "登录密码", "密码不进入对话记录");
|
|
523
|
-
if (!password && !secretRef) return;
|
|
524
|
-
if (password) {
|
|
525
|
-
putSecret(defaultSecretsPath(), hostName, password);
|
|
526
|
-
secretRef = `file:${defaultSecretsPath()}#${hostName}`;
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
541
|
const saved = saveHostCandidate(loadHosts(), {
|
|
530
542
|
name: hostName,
|
|
531
543
|
host: address,
|
|
532
544
|
user,
|
|
533
545
|
privateKeyPath: keyPath || undefined,
|
|
534
|
-
secretRef,
|
|
535
546
|
tags: prod ? ["prod"] : current?.tags?.filter((tag) => tag !== "prod"),
|
|
536
547
|
}, sub === "edit" ? "overwrite" : "rename");
|
|
537
|
-
if (saved.saved) saveHosts(defaultConfigPath(), saved.file);
|
|
538
548
|
if (!saved.saved) {
|
|
539
549
|
ctx.ui.notify("未保存", "warning");
|
|
540
550
|
return;
|
|
541
551
|
}
|
|
552
|
+
const candidate = saved.file.hosts[saved.name];
|
|
553
|
+
let wroteSecret = false;
|
|
554
|
+
// 编辑时沿用原来的凭据;新密码按最终名字存,不用用户填的名字。
|
|
555
|
+
let secretRef = keyPath ? undefined : current?.auth.secretRef;
|
|
556
|
+
if (!keyPath) {
|
|
557
|
+
const password = await readSecret(ctx, "登录密码", "密码不进入对话记录");
|
|
558
|
+
if (!password && !secretRef) {
|
|
559
|
+
ctx.ui.notify("没有密码,未保存", "warning");
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (password) {
|
|
563
|
+
putSecret(defaultSecretsPath(), saved.name, password);
|
|
564
|
+
wroteSecret = true;
|
|
565
|
+
secretRef = `file:${defaultSecretsPath()}#${saved.name}`;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const profile = { ...candidate, auth: { ...candidate.auth, secretRef } };
|
|
542
569
|
try {
|
|
543
|
-
const tested = await
|
|
544
|
-
|
|
570
|
+
const tested = await testProfileWithTofu(profile, ctx.ui);
|
|
571
|
+
saved.file.hosts[saved.name] = profile;
|
|
572
|
+
saveHosts(defaultConfigPath(), saved.file);
|
|
573
|
+
ctx.ui.notify(`已保存 ${saved.name},${tested.auth} ${tested.fingerprint || "无指纹"} ${tested.latencyMs}ms`, "info");
|
|
545
574
|
} catch (error) {
|
|
546
|
-
|
|
547
|
-
delete next.hosts[saved.name];
|
|
548
|
-
saveHosts(defaultConfigPath(), next);
|
|
575
|
+
if (wroteSecret) deleteSecret(defaultSecretsPath(), saved.name);
|
|
549
576
|
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
550
577
|
}
|
|
551
578
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nsyan/ssh",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "AI 远程运维扩展 for pi coding agent:SSH/SFTP/FTP 主机管理、危险命令确认、审计与可回滚部署 | Remote ops for the pi agent: SSH, SFTP and FTP with confirmation, audit and rollback",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-extension",
|
package/src/core/deploy.ts
CHANGED
|
@@ -76,13 +76,19 @@ export function healthLoopCommand(healthCmd: string, timeoutSec = HEALTH_BUDGET_
|
|
|
76
76
|
].join("\n");
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* 压缩包里任一成员逃出目标目录,整包拒绝。绝对路径和 `..` 都算逃逸。
|
|
81
|
+
* 空行必须在这里滤掉:`tar -tzf` 的输出总以换行结尾,split 之后最后一项是空串,
|
|
82
|
+
* 当成成员会让任何一个合法压缩包都被判越界。
|
|
83
|
+
*/
|
|
80
84
|
export function rejectArchiveMembers(members: string[], destination: string): string[] {
|
|
81
|
-
return members
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
return members
|
|
86
|
+
.map((member) => member.trim())
|
|
87
|
+
.filter((value) => value !== "")
|
|
88
|
+
.filter((value) => {
|
|
89
|
+
if (value.startsWith("/") || value.includes("\\")) return true;
|
|
90
|
+
return !withinRoot(destination, `${destination}/${value}`) && normalizeMember(value) !== "";
|
|
91
|
+
});
|
|
86
92
|
}
|
|
87
93
|
|
|
88
94
|
function normalizeMember(value: string): string {
|
package/src/core/doctor.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { accessSync, constants } from "node:fs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
5
|
+
import { delimiter, join } from "node:path";
|
|
6
6
|
|
|
7
7
|
function ok(path: string, mode: number): boolean {
|
|
8
8
|
try {
|
|
@@ -13,15 +13,46 @@ function ok(path: string, mode: number): boolean {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* 按 PATH 找可执行文件,和 spawn 用裸命令名时的查找方式一致。
|
|
18
|
+
* 不能写死 /usr/bin:Homebrew、/usr/local/bin、NixOS 都不在那里,
|
|
19
|
+
* 写死会把实际可用的 ssh 报成不可用。
|
|
20
|
+
*/
|
|
21
|
+
export function which(command: string, env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
22
|
+
const path = env.PATH ?? env.Path ?? "";
|
|
23
|
+
for (const dir of path.split(delimiter)) {
|
|
24
|
+
if (!dir) continue;
|
|
25
|
+
const candidate = join(dir, command);
|
|
26
|
+
if (ok(candidate, constants.X_OK)) return candidate;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** openssh 传输依赖这三个:ssh 跑命令,scp 传文件,sftp 列目录和按行读。 */
|
|
32
|
+
function opensshLines(): string[] {
|
|
33
|
+
const found = ["ssh", "scp", "sftp"].map((name) => ({ name, path: which(name) }));
|
|
34
|
+
const missing = found.filter((item) => !item.path).map((item) => item.name);
|
|
35
|
+
if (missing.length === 0) {
|
|
36
|
+
return [`本机 openssh 可用(${found.map((item) => item.path).join("、")})`];
|
|
37
|
+
}
|
|
38
|
+
const present = found.filter((item) => item.path);
|
|
39
|
+
return [
|
|
40
|
+
`PATH 里缺 ${missing.join("、")},openssh 传输不可用${present.length > 0 ? `(只找到 ${present.map((item) => item.path).join("、")})` : ""}。` +
|
|
41
|
+
"需要 ProxyJump、证书或 ssh-agent 时请装上,否则把主机的 transport 设成 ssh2。",
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
|
|
16
45
|
function keychainLine(): string {
|
|
17
46
|
if (process.platform === "linux") {
|
|
18
|
-
|
|
19
|
-
|
|
47
|
+
const tool = which("secret-tool");
|
|
48
|
+
return tool
|
|
49
|
+
? `钥匙串可读写(${tool})`
|
|
20
50
|
: "没有 secret-tool,密码只进 0600 的 ssh-secrets.json";
|
|
21
51
|
}
|
|
22
52
|
if (process.platform === "darwin") {
|
|
23
|
-
|
|
24
|
-
|
|
53
|
+
const tool = which("security");
|
|
54
|
+
return tool
|
|
55
|
+
? `钥匙串可读取已有项(${tool});新密码不进 security 的进程参数,写入 0600 的 ssh-secrets.json`
|
|
25
56
|
: "没有 security,密码只进 0600 的 ssh-secrets.json";
|
|
26
57
|
}
|
|
27
58
|
return "当前系统没有钥匙串,密码只进 0600 的 ssh-secrets.json";
|
|
@@ -35,7 +66,7 @@ export function doctorLines(): string[] {
|
|
|
35
66
|
} catch {
|
|
36
67
|
lines.push("ssh2 不可用");
|
|
37
68
|
}
|
|
38
|
-
lines.push(
|
|
69
|
+
lines.push(...opensshLines());
|
|
39
70
|
const known = join(homedir(), ".ssh", "known_hosts");
|
|
40
71
|
lines.push(ok(join(homedir(), ".ssh"), constants.W_OK) ? `known_hosts 目录可写(${known})` : "~/.ssh 不可写,首次连接无法记录主机密钥");
|
|
41
72
|
const auditDir = join(homedir(), ".pi", "agent");
|
package/src/core/guard.ts
CHANGED
|
@@ -6,6 +6,40 @@ export function shellQuote(value: string): string {
|
|
|
6
6
|
return `'${value.replace(SHELL_SINGLE_QUOTE, `'"'"'`)}'`;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* 把命令放进自己的进程组,返回脚本行。
|
|
11
|
+
*
|
|
12
|
+
* 不能靠 `set -m` 的退出码判断:dash 下 `set -m` 在没有 tty 时返回 0,
|
|
13
|
+
* 但 job control 并没开启(`$-` 里没有 `m`),进程组也建不起来,
|
|
14
|
+
* 于是 `kill -TERM -$pid` 变成 `kill: No such process`,超时清理形同虚设。
|
|
15
|
+
* 必须看 `$-` 里到底有没有 `m`。
|
|
16
|
+
*
|
|
17
|
+
* - 有 job control(bash):`( cmd ) &` 天然建新进程组,`kill -$pgid` 有效。
|
|
18
|
+
* - 没有 job control(dash/sh):用 `setsid` 单独开会话并成为组长,`kill -$pgid` 同样有效。
|
|
19
|
+
* 没有 setsid 时退回「超时只关通道」,并把降级事实写到 stderr。
|
|
20
|
+
*/
|
|
21
|
+
function spawnInOwnGroup(command: string): string[] {
|
|
22
|
+
return [
|
|
23
|
+
"case \"$-\" in",
|
|
24
|
+
" *m*)",
|
|
25
|
+
` ( ${command} ) &`,
|
|
26
|
+
" pid=$!",
|
|
27
|
+
" ;;",
|
|
28
|
+
" *)",
|
|
29
|
+
" if command -v setsid >/dev/null 2>&1; then",
|
|
30
|
+
` setsid sh -c ${shellQuote(command)} &`,
|
|
31
|
+
" pid=$!",
|
|
32
|
+
" else",
|
|
33
|
+
" echo 'pi-ssh: 远端 shell 没有 job control 也没有 setsid,超时只能关掉通道,进程组不会被杀' >&2",
|
|
34
|
+
` ( ${command} ) &`,
|
|
35
|
+
" pid=$!",
|
|
36
|
+
" PI_SSH_NO_GROUP=1",
|
|
37
|
+
" fi",
|
|
38
|
+
" ;;",
|
|
39
|
+
"esac",
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
|
|
9
43
|
/**
|
|
10
44
|
* 包一层进程组看门狗。
|
|
11
45
|
* 先把通道复制到 fd 3,再把 fd 0 指到 /dev/null,然后才把命令放进后台。
|
|
@@ -13,18 +47,9 @@ export function shellQuote(value: string): string {
|
|
|
13
47
|
*/
|
|
14
48
|
function jobControl(command: string, afterPid: string[]): string[] {
|
|
15
49
|
return [
|
|
16
|
-
"if ! set -m 2>/dev/null; then",
|
|
17
|
-
" echo 'pi-ssh: 远端 shell 不支持 set -m,超时只会关掉通道' >&2",
|
|
18
|
-
" exec 3<&0",
|
|
19
|
-
" exec 0</dev/null",
|
|
20
|
-
` ${command}`,
|
|
21
|
-
" exit $?",
|
|
22
|
-
"fi",
|
|
23
50
|
"exec 3<&0",
|
|
24
51
|
"exec 0</dev/null",
|
|
25
|
-
|
|
26
|
-
"pid=$!",
|
|
27
|
-
"set +m",
|
|
52
|
+
...spawnInOwnGroup(command),
|
|
28
53
|
...afterPid,
|
|
29
54
|
];
|
|
30
55
|
}
|
|
@@ -33,6 +58,7 @@ function watcher(): string[] {
|
|
|
33
58
|
return [
|
|
34
59
|
"(",
|
|
35
60
|
" while read -r _ <&3; do :; done",
|
|
61
|
+
" [ -n \"$PI_SSH_NO_GROUP\" ] && exit 0",
|
|
36
62
|
" kill -TERM -$pid 2>/dev/null",
|
|
37
63
|
" sleep 2",
|
|
38
64
|
" kill -KILL -$pid 2>/dev/null",
|
|
@@ -48,29 +74,36 @@ function watcher(): string[] {
|
|
|
48
74
|
/**
|
|
49
75
|
* sudo 先从通道读走一行密码。
|
|
50
76
|
* 密码只通过不带引号的 heredoc 进 sudo 的 stdin,不进 printf 的参数,远端 ps 看不到。
|
|
77
|
+
* heredoc 必须挂在最外层那个调用(子 shell 或 setsid)上:
|
|
78
|
+
* 放进 `sh -c '...'` 的引号里,`$PI_SSH_PW` 就到不了内层 shell(没 export)。
|
|
51
79
|
*/
|
|
52
80
|
export function wrapSudo(command: string): string {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
"$PI_SSH_PW",
|
|
56
|
-
"PI_SSH_PW_END",
|
|
57
|
-
];
|
|
81
|
+
const inner = `sudo -S -p '' -- ${command}`;
|
|
82
|
+
const feed = ["$PI_SSH_PW", "PI_SSH_PW_END"];
|
|
58
83
|
return [
|
|
59
84
|
"IFS= read -r PI_SSH_PW || exit 1",
|
|
60
|
-
"if ! set -m 2>/dev/null; then",
|
|
61
|
-
" echo 'pi-ssh: 远端 shell 不支持 set -m,超时只会关掉通道' >&2",
|
|
62
|
-
" exec 3<&0",
|
|
63
|
-
" exec 0</dev/null",
|
|
64
|
-
...feed,
|
|
65
|
-
" exit $?",
|
|
66
|
-
"fi",
|
|
67
85
|
"exec 3<&0",
|
|
68
86
|
"exec 0</dev/null",
|
|
69
|
-
"
|
|
87
|
+
"case \"$-\" in",
|
|
88
|
+
" *m*)",
|
|
89
|
+
` ( ${inner} ) <<PI_SSH_PW_END &`,
|
|
90
|
+
...feed,
|
|
91
|
+
" pid=$!",
|
|
92
|
+
" ;;",
|
|
93
|
+
" *)",
|
|
94
|
+
" if command -v setsid >/dev/null 2>&1; then",
|
|
95
|
+
` setsid sh -c ${shellQuote(inner)} <<PI_SSH_PW_END &`,
|
|
96
|
+
...feed,
|
|
97
|
+
" pid=$!",
|
|
98
|
+
" else",
|
|
99
|
+
" echo 'pi-ssh: 远端 shell 没有 job control 也没有 setsid,超时只能关掉通道,进程组不会被杀' >&2",
|
|
100
|
+
` ( ${inner} ) <<PI_SSH_PW_END &`,
|
|
70
101
|
...feed,
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
102
|
+
" pid=$!",
|
|
103
|
+
" PI_SSH_NO_GROUP=1",
|
|
104
|
+
" fi",
|
|
105
|
+
" ;;",
|
|
106
|
+
"esac",
|
|
74
107
|
...watcher(),
|
|
75
108
|
"unset PI_SSH_PW",
|
|
76
109
|
"exit $status",
|
package/src/core/hash.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
// core/hash.ts —— 本地与远端都用同一种 sha256。远端按命令依次降级。
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { createReadStream } from "node:fs";
|
|
4
|
+
import { stat } from "node:fs/promises";
|
|
5
5
|
import { shellQuote } from "./guard.js";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* 本地文件的 sha256。
|
|
9
|
+
* 目录必须先挡掉:`readableWebStream()` 对目录抛的 EISDIR 会逃成 unhandledRejection,
|
|
10
|
+
* 在长驻宿主里直接把进程打掉,`try/catch` 拦不住。用 createReadStream 的异步迭代器,
|
|
11
|
+
* 错误才会老老实实落到 await 上。
|
|
12
|
+
*/
|
|
7
13
|
export async function sha256File(path: string): Promise<{ hash: string; bytes: number }> {
|
|
8
14
|
const info = await stat(path);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
for await (const chunk of handle.readableWebStream() as AsyncIterable<Uint8Array>) hash.update(chunk);
|
|
13
|
-
} finally {
|
|
14
|
-
await handle.close();
|
|
15
|
+
if (info.isDirectory()) {
|
|
16
|
+
throw new Error(`${path} 是目录。上传不接受目录:先在本地打成一个压缩包,再传包。`);
|
|
15
17
|
}
|
|
18
|
+
const hash = createHash("sha256");
|
|
19
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk as Buffer);
|
|
16
20
|
return { hash: hash.digest("hex"), bytes: info.size };
|
|
17
21
|
}
|
|
18
22
|
|