@zqagent/shared 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/LICENSE +190 -0
- package/README.md +21 -2
- package/dist/approval-view.cjs +99 -0
- package/dist/approval-view.cjs.map +1 -0
- package/dist/approval-view.d.cts +39 -0
- package/dist/approval-view.d.ts +39 -0
- package/dist/approval-view.js +69 -0
- package/dist/approval-view.js.map +1 -0
- package/dist/fleet.cjs +263 -0
- package/dist/fleet.cjs.map +1 -0
- package/dist/fleet.d.cts +284 -0
- package/dist/fleet.d.ts +284 -0
- package/dist/fleet.js +214 -0
- package/dist/fleet.js.map +1 -0
- package/dist/hub-api.cjs +901 -0
- package/dist/hub-api.cjs.map +1 -0
- package/dist/hub-api.d.cts +1677 -0
- package/dist/hub-api.d.ts +1677 -0
- package/dist/hub-api.js +776 -0
- package/dist/hub-api.js.map +1 -0
- package/dist/index-DokiNlGJ.d.cts +182 -0
- package/dist/index-DokiNlGJ.d.ts +182 -0
- package/dist/index.cjs +861 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +779 -0
- package/dist/index.js.map +1 -0
- package/dist/llm-presets.cjs +92 -0
- package/dist/llm-presets.cjs.map +1 -0
- package/dist/llm-presets.d.cts +28 -0
- package/dist/llm-presets.d.ts +28 -0
- package/dist/llm-presets.js +66 -0
- package/dist/llm-presets.js.map +1 -0
- package/dist/media.cjs +111 -0
- package/dist/media.cjs.map +1 -0
- package/dist/media.d.cts +59 -0
- package/dist/media.d.ts +59 -0
- package/dist/media.js +79 -0
- package/dist/media.js.map +1 -0
- package/dist/messages-CiXjdR1Z.d.cts +257 -0
- package/dist/messages-CiXjdR1Z.d.ts +257 -0
- package/dist/model-capabilities.cjs +105 -0
- package/dist/model-capabilities.cjs.map +1 -0
- package/dist/model-capabilities.d.cts +46 -0
- package/dist/model-capabilities.d.ts +46 -0
- package/dist/model-capabilities.js +74 -0
- package/dist/model-capabilities.js.map +1 -0
- package/dist/notify/index.cjs +375 -0
- package/dist/notify/index.cjs.map +1 -0
- package/dist/notify/index.d.cts +2 -0
- package/dist/notify/index.d.ts +2 -0
- package/dist/notify/index.js +330 -0
- package/dist/notify/index.js.map +1 -0
- package/dist/secrets.cjs +121 -0
- package/dist/secrets.cjs.map +1 -0
- package/dist/secrets.d.cts +25 -0
- package/dist/secrets.d.ts +25 -0
- package/dist/secrets.js +82 -0
- package/dist/secrets.js.map +1 -0
- package/dist/wire/index.cjs +1163 -0
- package/dist/wire/index.cjs.map +1 -0
- package/dist/wire/index.d.cts +166 -0
- package/dist/wire/index.d.ts +166 -0
- package/dist/wire/index.js +1081 -0
- package/dist/wire/index.js.map +1 -0
- package/dist/wire/node.cjs +224 -0
- package/dist/wire/node.cjs.map +1 -0
- package/dist/wire/node.d.cts +14 -0
- package/dist/wire/node.d.ts +14 -0
- package/dist/wire/node.js +195 -0
- package/dist/wire/node.js.map +1 -0
- package/package.json +99 -3
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// src/secrets.ts
|
|
2
|
+
import * as crypto from "crypto";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
function encryptWithKey(key, plain) {
|
|
6
|
+
const iv = crypto.randomBytes(12);
|
|
7
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
8
|
+
const enc = Buffer.concat([cipher.update(plain, "utf8"), cipher.final()]);
|
|
9
|
+
const tag = cipher.getAuthTag();
|
|
10
|
+
return [
|
|
11
|
+
"v1",
|
|
12
|
+
iv.toString("base64"),
|
|
13
|
+
tag.toString("base64"),
|
|
14
|
+
enc.toString("base64")
|
|
15
|
+
].join(":");
|
|
16
|
+
}
|
|
17
|
+
function decryptWithKey(key, payload) {
|
|
18
|
+
try {
|
|
19
|
+
const parts = payload.split(":");
|
|
20
|
+
if (parts.length !== 4 || parts[0] !== "v1") return null;
|
|
21
|
+
const iv = Buffer.from(parts[1], "base64");
|
|
22
|
+
const tag = Buffer.from(parts[2], "base64");
|
|
23
|
+
const data = Buffer.from(parts[3], "base64");
|
|
24
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
25
|
+
decipher.setAuthTag(tag);
|
|
26
|
+
return Buffer.concat([
|
|
27
|
+
decipher.update(data),
|
|
28
|
+
decipher.final()
|
|
29
|
+
]).toString("utf8");
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function loadOrCreateMasterKey(masterKeyFile) {
|
|
35
|
+
if (fs.existsSync(masterKeyFile)) {
|
|
36
|
+
const hex = fs.readFileSync(masterKeyFile, "utf8").trim();
|
|
37
|
+
if (/^[0-9a-f]{64}$/i.test(hex)) {
|
|
38
|
+
return Buffer.from(hex, "hex");
|
|
39
|
+
}
|
|
40
|
+
console.warn(`\u26A0\uFE0F \u4E3B\u5BC6\u94A5\u6587\u4EF6\u635F\u574F\uFF0C\u5DF2\u91CD\u65B0\u751F\u6210\uFF08\u65E7\u5BC6\u6587\u5C06\u65E0\u6CD5\u89E3\u5BC6\uFF09`);
|
|
41
|
+
}
|
|
42
|
+
const key = crypto.randomBytes(32);
|
|
43
|
+
fs.mkdirSync(path.dirname(masterKeyFile), { recursive: true, mode: 448 });
|
|
44
|
+
fs.writeFileSync(masterKeyFile, key.toString("hex"), {
|
|
45
|
+
mode: 384,
|
|
46
|
+
encoding: "utf8"
|
|
47
|
+
});
|
|
48
|
+
return key;
|
|
49
|
+
}
|
|
50
|
+
function masterKeyFromHex(hex) {
|
|
51
|
+
const h = hex.trim();
|
|
52
|
+
return /^[0-9a-f]{64}$/i.test(h) ? Buffer.from(h, "hex") : null;
|
|
53
|
+
}
|
|
54
|
+
var SecretBox = class _SecretBox {
|
|
55
|
+
constructor(key) {
|
|
56
|
+
this.key = key;
|
|
57
|
+
}
|
|
58
|
+
key;
|
|
59
|
+
/** 主密钥来源一:本机密钥文件(不存在则创建) */
|
|
60
|
+
static fromFile(masterKeyFile) {
|
|
61
|
+
return new _SecretBox(loadOrCreateMasterKey(masterKeyFile));
|
|
62
|
+
}
|
|
63
|
+
/** 主密钥来源二:hex 字符串(如环境变量 SECRET_KEY);非法返回 null */
|
|
64
|
+
static fromHex(hex) {
|
|
65
|
+
const key = masterKeyFromHex(hex);
|
|
66
|
+
return key ? new _SecretBox(key) : null;
|
|
67
|
+
}
|
|
68
|
+
encrypt(plain) {
|
|
69
|
+
return encryptWithKey(this.key, plain);
|
|
70
|
+
}
|
|
71
|
+
decrypt(payload) {
|
|
72
|
+
return decryptWithKey(this.key, payload);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
export {
|
|
76
|
+
SecretBox,
|
|
77
|
+
decryptWithKey,
|
|
78
|
+
encryptWithKey,
|
|
79
|
+
loadOrCreateMasterKey,
|
|
80
|
+
masterKeyFromHex
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=secrets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/secrets.ts"],"sourcesContent":["/**\n * 对称加密共享模块\n *\n * 从 CLI 的 connection.ts 抽出的 AES-256-GCM 加解密实现,供 CLI 与\n * 云端 Server 复用。密文格式 `v1:iv:tag:ciphertext`(均 base64)。\n *\n * 安全说明:这是「本机混淆式加密」(纯 Node 无外部依赖),主密钥与密文\n * 可存于同一台机器,能防止明文泄露 / 被 grep / 误提交,但无法抵御能读取\n * 本机主密钥文件的攻击者(如需 OS 级密钥链,可替换为 keytar / libsecret 等)。\n */\nimport * as crypto from \"crypto\";\nimport * as fs from \"fs\";\nimport * as path from \"path\";\n\n/** 用给定主密钥做 AES-256-GCM 加密,输出 `v1:iv:tag:ciphertext`(base64) */\nexport function encryptWithKey(key: Buffer, plain: string): string {\n const iv = crypto.randomBytes(12);\n const cipher = crypto.createCipheriv(\"aes-256-gcm\", key, iv);\n const enc = Buffer.concat([cipher.update(plain, \"utf8\"), cipher.final()]);\n const tag = cipher.getAuthTag();\n return [\n \"v1\",\n iv.toString(\"base64\"),\n tag.toString(\"base64\"),\n enc.toString(\"base64\"),\n ].join(\":\");\n}\n\n/** 用给定主密钥解密;格式非法 / 认证失败返回 null */\nexport function decryptWithKey(key: Buffer, payload: string): string | null {\n try {\n const parts = payload.split(\":\");\n if (parts.length !== 4 || parts[0] !== \"v1\") return null;\n const iv = Buffer.from(parts[1]!, \"base64\");\n const tag = Buffer.from(parts[2]!, \"base64\");\n const data = Buffer.from(parts[3]!, \"base64\");\n const decipher = crypto.createDecipheriv(\"aes-256-gcm\", key, iv);\n decipher.setAuthTag(tag);\n return Buffer.concat([\n decipher.update(data),\n decipher.final(),\n ]).toString(\"utf8\");\n } catch {\n return null;\n }\n}\n\n/** 读取或生成主密钥(32 字节随机,权限 600)。文件损坏则重建。 */\nexport function loadOrCreateMasterKey(masterKeyFile: string): Buffer {\n if (fs.existsSync(masterKeyFile)) {\n const hex = fs.readFileSync(masterKeyFile, \"utf8\").trim();\n if (/^[0-9a-f]{64}$/i.test(hex)) {\n return Buffer.from(hex, \"hex\");\n }\n console.warn(`⚠️ 主密钥文件损坏,已重新生成(旧密文将无法解密)`);\n }\n const key = crypto.randomBytes(32);\n fs.mkdirSync(path.dirname(masterKeyFile), { recursive: true, mode: 0o700 });\n fs.writeFileSync(masterKeyFile, key.toString(\"hex\"), {\n mode: 0o600,\n encoding: \"utf8\",\n });\n return key;\n}\n\n/** 从 64 位 hex 字符串解析主密钥;非法返回 null */\nexport function masterKeyFromHex(hex: string): Buffer | null {\n const h = hex.trim();\n return /^[0-9a-f]{64}$/i.test(h) ? Buffer.from(h, \"hex\") : null;\n}\n\n/**\n * 密文工具:持有固定主密钥,对字符串做加解密。\n * CLI 与 Server 各自构造实例(CLI 用 ~/.config/zqagent/secret.key,\n * Server 用 DATA_DIR/secret.key 或 SECRET_KEY 环境变量)。\n */\nexport class SecretBox {\n constructor(private readonly key: Buffer) {}\n\n /** 主密钥来源一:本机密钥文件(不存在则创建) */\n static fromFile(masterKeyFile: string): SecretBox {\n return new SecretBox(loadOrCreateMasterKey(masterKeyFile));\n }\n\n /** 主密钥来源二:hex 字符串(如环境变量 SECRET_KEY);非法返回 null */\n static fromHex(hex: string): SecretBox | null {\n const key = masterKeyFromHex(hex);\n return key ? new SecretBox(key) : null;\n }\n\n encrypt(plain: string): string {\n return encryptWithKey(this.key, plain);\n }\n\n decrypt(payload: string): string | null {\n return decryptWithKey(this.key, payload);\n }\n}\n"],"mappings":";AAUA,YAAY,YAAY;AACxB,YAAY,QAAQ;AACpB,YAAY,UAAU;AAGf,SAAS,eAAe,KAAa,OAAuB;AACjE,QAAM,KAAY,mBAAY,EAAE;AAChC,QAAM,SAAgB,sBAAe,eAAe,KAAK,EAAE;AAC3D,QAAM,MAAM,OAAO,OAAO,CAAC,OAAO,OAAO,OAAO,MAAM,GAAG,OAAO,MAAM,CAAC,CAAC;AACxE,QAAM,MAAM,OAAO,WAAW;AAC9B,SAAO;AAAA,IACL;AAAA,IACA,GAAG,SAAS,QAAQ;AAAA,IACpB,IAAI,SAAS,QAAQ;AAAA,IACrB,IAAI,SAAS,QAAQ;AAAA,EACvB,EAAE,KAAK,GAAG;AACZ;AAGO,SAAS,eAAe,KAAa,SAAgC;AAC1E,MAAI;AACF,UAAM,QAAQ,QAAQ,MAAM,GAAG;AAC/B,QAAI,MAAM,WAAW,KAAK,MAAM,CAAC,MAAM,KAAM,QAAO;AACpD,UAAM,KAAK,OAAO,KAAK,MAAM,CAAC,GAAI,QAAQ;AAC1C,UAAM,MAAM,OAAO,KAAK,MAAM,CAAC,GAAI,QAAQ;AAC3C,UAAM,OAAO,OAAO,KAAK,MAAM,CAAC,GAAI,QAAQ;AAC5C,UAAM,WAAkB,wBAAiB,eAAe,KAAK,EAAE;AAC/D,aAAS,WAAW,GAAG;AACvB,WAAO,OAAO,OAAO;AAAA,MACnB,SAAS,OAAO,IAAI;AAAA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