pi-ssh-manager 0.0.0-stage → 0.1.0
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +90 -2
- package/package.json +52 -4
- package/src/config-reader.ts +824 -0
- package/src/index.ts +181 -0
- package/src/manager.ts +371 -0
- package/src/model.ts +144 -0
package/src/model.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
export interface SshHostRecord {
|
|
4
|
+
alias: string;
|
|
5
|
+
hostname: string;
|
|
6
|
+
port: number;
|
|
7
|
+
user?: string;
|
|
8
|
+
identityFiles: string[];
|
|
9
|
+
description?: string;
|
|
10
|
+
options: Record<string, string | string[]>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SshHostPatch {
|
|
14
|
+
host: string;
|
|
15
|
+
hostname?: string;
|
|
16
|
+
port?: number;
|
|
17
|
+
user?: string;
|
|
18
|
+
identityFiles?: string[];
|
|
19
|
+
description?: string;
|
|
20
|
+
options?: Record<string, string | string[]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SshStore {
|
|
24
|
+
version: 1;
|
|
25
|
+
hosts: SshHostRecord[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SshPaths {
|
|
29
|
+
home: string;
|
|
30
|
+
sshDirectory: string;
|
|
31
|
+
nativeConfig: string;
|
|
32
|
+
directory: string;
|
|
33
|
+
state: string;
|
|
34
|
+
config: string;
|
|
35
|
+
keys: string;
|
|
36
|
+
knownHosts: string;
|
|
37
|
+
backups: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getSshPaths(home: string): SshPaths {
|
|
41
|
+
home = path.resolve(home);
|
|
42
|
+
const sshDirectory = path.join(home, ".ssh");
|
|
43
|
+
const directory = path.join(sshDirectory, "onprsSSH");
|
|
44
|
+
return {
|
|
45
|
+
home,
|
|
46
|
+
sshDirectory,
|
|
47
|
+
nativeConfig: path.join(sshDirectory, "config"),
|
|
48
|
+
directory,
|
|
49
|
+
state: path.join(directory, "hosts.json"),
|
|
50
|
+
config: path.join(directory, "config"),
|
|
51
|
+
keys: path.join(directory, "keys"),
|
|
52
|
+
knownHosts: path.join(directory, "known_hosts"),
|
|
53
|
+
backups: path.join(directory, "backups"),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function validateAlias(alias: string): string {
|
|
58
|
+
if (typeof alias !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,254}$/.test(alias)) {
|
|
59
|
+
throw new Error("[INVALID_HOST] 主机别名须为 1~255 个字母、数字、点、下划线或短横线,且以字母或数字开头。");
|
|
60
|
+
}
|
|
61
|
+
return alias;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function text(value: unknown, name: string, maximum = 2048): string {
|
|
65
|
+
if (typeof value !== "string" || value.length > maximum || /[\x00-\x1f\x7f]/.test(value)) {
|
|
66
|
+
throw new Error(`[INVALID_VALUE] ${name} 必须是长度不超过 ${maximum} 的单行文本。`);
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function normalizeOptions(value: unknown): Record<string, string | string[]> {
|
|
72
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
73
|
+
throw new Error("[INVALID_OPTIONS] options 必须是 SSH 选项对象。");
|
|
74
|
+
}
|
|
75
|
+
const entries = Object.entries(value);
|
|
76
|
+
if (entries.length > 96) throw new Error("[INVALID_OPTIONS] SSH 选项不能超过 96 项。");
|
|
77
|
+
const reserved = new Set(["host", "match", "include", "hostname", "port", "user", "identityfile"]);
|
|
78
|
+
const result: Record<string, string | string[]> = Object.create(null);
|
|
79
|
+
for (const [key, raw] of entries) {
|
|
80
|
+
const name = key.toLowerCase();
|
|
81
|
+
if (!/^[a-z][a-z0-9]+$/.test(name) || reserved.has(name) || Object.hasOwn(result, name)) {
|
|
82
|
+
throw new Error(`[INVALID_OPTION] 选项名称无效或重复:${key};连接字段请使用对应参数。`);
|
|
83
|
+
}
|
|
84
|
+
const values = Array.isArray(raw) ? raw : [raw];
|
|
85
|
+
if (values.length === 0 || values.length > 16) throw new Error(`[INVALID_OPTION] ${key} 须包含 1~16 个值。`);
|
|
86
|
+
const normalized = values.map((item) => {
|
|
87
|
+
const value = text(item, key);
|
|
88
|
+
if (!value.trim() || value.trimStart().startsWith("#")) throw new Error(`[INVALID_OPTION] ${key} 的值为空。`);
|
|
89
|
+
return value;
|
|
90
|
+
});
|
|
91
|
+
result[name] = Array.isArray(raw) ? normalized : normalized[0];
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function normalizeHost(patch: SshHostPatch, previous?: SshHostRecord): SshHostRecord {
|
|
97
|
+
const alias = validateAlias(patch.host);
|
|
98
|
+
const hostname = text(patch.hostname ?? previous?.hostname, "hostname", 255);
|
|
99
|
+
if (!hostname || /[\s#"'\\]/.test(hostname) || hostname.startsWith("-")) {
|
|
100
|
+
throw new Error("[INVALID_HOSTNAME] hostname 必须是有效的单个主机名或 IP 地址。");
|
|
101
|
+
}
|
|
102
|
+
const port = patch.port ?? previous?.port ?? 22;
|
|
103
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("[INVALID_PORT] 端口须为 1~65535 的整数。");
|
|
104
|
+
const user = text(patch.user ?? previous?.user ?? "", "user", 255);
|
|
105
|
+
if (/[\s#"'\\]/.test(user) || user.startsWith("-")) throw new Error("[INVALID_USER] SSH 用户名无效。");
|
|
106
|
+
const identityFiles = patch.identityFiles ?? previous?.identityFiles ?? [];
|
|
107
|
+
if (!Array.isArray(identityFiles) || identityFiles.length > 8) throw new Error("[INVALID_IDENTITY] identityFiles 须为最多 8 项的路径数组。");
|
|
108
|
+
const identities = [...new Set(identityFiles.map((item) => {
|
|
109
|
+
const value = text(item, "identityFiles");
|
|
110
|
+
if (!value.trim()) throw new Error("[INVALID_IDENTITY] 密钥路径不能为空。");
|
|
111
|
+
return value;
|
|
112
|
+
}))];
|
|
113
|
+
const description = text(patch.description ?? previous?.description ?? "", "description", 1000);
|
|
114
|
+
return {
|
|
115
|
+
alias,
|
|
116
|
+
hostname,
|
|
117
|
+
port,
|
|
118
|
+
...(user ? { user } : {}),
|
|
119
|
+
identityFiles: identities,
|
|
120
|
+
...(description ? { description } : {}),
|
|
121
|
+
options: normalizeOptions(patch.options ?? previous?.options ?? {}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function quoteSshPath(value: string): string {
|
|
126
|
+
return `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function renderSshConfig(hosts: SshHostRecord[]): string {
|
|
130
|
+
const lines = ["# 由 onprsSSH 管理;请通过 Pi 的 onprsSSH 工具或 /onprsSSH 修改。", ""];
|
|
131
|
+
for (const host of [...hosts].sort((a, b) => a.alias.localeCompare(b.alias, "en"))) {
|
|
132
|
+
if (host.description) lines.push(`# ${host.description}`);
|
|
133
|
+
lines.push(`Host ${host.alias}`, ` HostName ${host.hostname}`, ` Port ${host.port}`);
|
|
134
|
+
if (host.user) lines.push(` User ${host.user}`);
|
|
135
|
+
for (const identity of host.identityFiles) lines.push(` IdentityFile ${quoteSshPath(identity)}`);
|
|
136
|
+
for (const [key, value] of Object.entries(host.options).sort(([a], [b]) => a.localeCompare(b, "en"))) {
|
|
137
|
+
for (const item of Array.isArray(value) ? value : [value]) lines.push(` ${key} ${item}`);
|
|
138
|
+
}
|
|
139
|
+
lines.push("");
|
|
140
|
+
}
|
|
141
|
+
// Include 与父文件共享 Host 匹配状态,结束时恢复全局作用域。
|
|
142
|
+
lines.push("Host *", "");
|
|
143
|
+
return lines.join("\n");
|
|
144
|
+
}
|