- 插件配置中 format=password 字段改为 AES-256-GCM 密文落盘(本机密钥,历史明文自动迁移) - 同一插件连续失败 3 次自动禁用(仅统计进程启动后失败),升级自动解除 - 检测到新版本后台下载,空闲窗口提前提示并自动重启升级,设置页可配安全窗口
165 lines
6.1 KiB
JavaScript
165 lines
6.1 KiB
JavaScript
// 插件注册表:读取 plugins/<id>/plugin.json 元数据(manifest 驱动,与 Python meta.py 语义一致)。
|
||
"use strict";
|
||
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
const config = require("./config");
|
||
const { makeLogger } = require("./logger");
|
||
|
||
const logger = makeLogger("registry");
|
||
|
||
function pluginRoot() {
|
||
return config.pluginRoot();
|
||
}
|
||
|
||
// 本地已落盘插件条目(目录包含 __init__.py 或 plugin.json,后者兜底下载中断的残缺目录)
|
||
function listLocalPlugins() {
|
||
const root = pluginRoot();
|
||
const out = [];
|
||
if (!fs.existsSync(root)) return out;
|
||
for (const name of fs.readdirSync(root)) {
|
||
if (name.startsWith("_") || name === "__pycache__") continue;
|
||
const full = path.join(root, name);
|
||
let stat;
|
||
try {
|
||
stat = fs.statSync(full);
|
||
} catch (e) {
|
||
continue;
|
||
}
|
||
if (stat.isDirectory()) {
|
||
if (fs.existsSync(path.join(full, "__init__.py")) || fs.existsSync(path.join(full, "plugin.json"))) {
|
||
out.push({ id: name, dir: full });
|
||
}
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// 读取插件 manifest;缺文件/解析失败返回 null(不阻断)
|
||
function readManifest(pluginId) {
|
||
const mf = path.join(pluginRoot(), pluginId, "plugin.json");
|
||
if (!fs.existsSync(mf)) return null;
|
||
try {
|
||
const data = JSON.parse(fs.readFileSync(mf, "utf-8"));
|
||
return data && typeof data === "object" ? data : null;
|
||
} catch (e) {
|
||
logger.warning(`插件 ${pluginId} plugin.json 解析失败:${e.message}`);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// JSON-Schema(object.properties) → 前端表单 [{key,label,type,default,options,description}]
|
||
function fieldsFromSchema(schema) {
|
||
if (!schema || schema.type !== "object" || !schema.properties) return null;
|
||
const out = [];
|
||
for (const [key, prop] of Object.entries(schema.properties)) {
|
||
const item = { key, label: prop.title || key, default: prop.default ?? "" };
|
||
const t = prop.type || "string";
|
||
if (t === "integer" || t === "number") item.type = "int";
|
||
else if (t === "string" && Array.isArray(prop.enum)) {
|
||
item.type = "select";
|
||
item.options = prop.enum;
|
||
} else if (t === "string" && prop.format === "password") item.type = "password";
|
||
else if (t === "array") {
|
||
item.type = "multiselect";
|
||
if (prop.items && Array.isArray(prop.items.enum)) item.options = prop.items.enum;
|
||
} else item.type = "text";
|
||
if (prop.description) item.description = prop.description;
|
||
out.push(item);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// 插件级执行超时(秒):plugin.json.runTimeoutSeconds;0=不限时,缺省/非法返回 null(回退壳默认)
|
||
function parseRunTimeout(manifest) {
|
||
if (manifest && manifest.runTimeoutSeconds !== undefined) {
|
||
const v = Number(manifest.runTimeoutSeconds);
|
||
if (Number.isFinite(v) && v >= 0) return v;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 组装插件元数据(manifest 驱动,与 Python meta._plugin_schema 一致)
|
||
function pluginSchema(pluginId) {
|
||
const manifest = readManifest(pluginId);
|
||
const base = {
|
||
id: pluginId,
|
||
name: pluginId,
|
||
version: "0",
|
||
description: "",
|
||
uses_month: false,
|
||
ui: false,
|
||
capabilities: [],
|
||
permissions: {},
|
||
fields: [],
|
||
};
|
||
if (!manifest) return base;
|
||
const fields = fieldsFromSchema(manifest.fields);
|
||
return {
|
||
id: manifest.id || pluginId,
|
||
name: manifest.name || pluginId,
|
||
version: manifest.version || "0",
|
||
description: manifest.description || "",
|
||
category: manifest.category || "", // 场景分类(左侧场景库按此分组折叠,空则归"其他")
|
||
uses_month: !!manifest.uses_month,
|
||
ui: !!manifest.ui,
|
||
platform: manifest.platform || "", // hr / oa / ""(前端平台徽章 + 运行并发闸按(账号,平台)隔离)
|
||
platforms: Array.isArray(manifest.platforms) && manifest.platforms.length
|
||
? manifest.platforms // 插件声明的全部平台依赖(运行前登录态预检按此逐个检查)
|
||
: manifest.platform ? [manifest.platform] : [], // 缺省回退单 platform 字段
|
||
min_runtime_version: manifest.minRuntimeVersion || "", // 版本兼容校验声明(安装时强校验,此处透传前端展示)
|
||
timeout_seconds: parseRunTimeout(manifest), // 插件级执行超时(秒,0=不限时,null=回退壳默认)
|
||
capabilities: manifest.capabilities || [],
|
||
permissions: manifest.permissions || {},
|
||
// 依赖的 SDK(requires.sdks 的键)与声明范围,供前端/设置页展示"缺少哪个 SDK"
|
||
requires_sdks: Object.entries((manifest.requires || {}).sdks || {}).map(([id, conf]) => ({
|
||
id,
|
||
compat: String((conf || {}).compat || "*"),
|
||
optional: !!((conf || {}).optional),
|
||
})),
|
||
fields: fields || [],
|
||
};
|
||
}
|
||
|
||
function find(pluginId) {
|
||
const dir = path.join(pluginRoot(), pluginId);
|
||
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) throw new Error(`插件不存在: ${pluginId}`);
|
||
const manifest = readManifest(pluginId);
|
||
if (!manifest && !fs.existsSync(path.join(dir, "__init__.py"))) {
|
||
throw new Error(`插件不存在: ${pluginId}`);
|
||
}
|
||
return { id: pluginId, dir, manifest, ...pluginSchema(pluginId) };
|
||
}
|
||
|
||
function findByName(name) {
|
||
// 供 /api/runs 以插件名查找(与 PLUGINS.find 语义对齐)
|
||
return find(name);
|
||
}
|
||
|
||
// 按插件 id 取密码字段名(format=password)。
|
||
// 目录名与 manifest.id 可能不一致:生产机安装目录用 xq0078,仓库里是「XQ0078 中文名」。
|
||
// 先按目录名直查,失败再遍历比对 manifest.id,避免配置加密因命名差异静默失效。
|
||
function passwordKeysOf(pluginId) {
|
||
const localCrypto = require("./local-crypto"); // 延迟 require:本模块被 config 间接依赖,避免加载期环
|
||
try {
|
||
return localCrypto.passwordKeys(find(pluginId).fields);
|
||
} catch (_) { /* 目录名不等于 id:继续遍历 */ }
|
||
try {
|
||
for (const entry of listLocalPlugins()) {
|
||
const schema = pluginSchema(entry.id);
|
||
if (schema.id === pluginId) return localCrypto.passwordKeys(schema.fields);
|
||
}
|
||
} catch (_) { /* 忽略 */ }
|
||
return [];
|
||
}
|
||
|
||
module.exports = {
|
||
pluginRoot,
|
||
listLocalPlugins,
|
||
readManifest,
|
||
fieldsFromSchema,
|
||
pluginSchema,
|
||
find,
|
||
passwordKeysOf,
|
||
};
|