feat(shell): 插件密码加密、失败熔断与客户端自动更新(v1.11.3)
- 插件配置中 format=password 字段改为 AES-256-GCM 密文落盘(本机密钥,历史明文自动迁移) - 同一插件连续失败 3 次自动禁用(仅统计进程启动后失败),升级自动解除 - 检测到新版本后台下载,空闲窗口提前提示并自动重启升级,设置页可配安全窗口
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
// 场景配置方案(多配置)· SQLite 实现:同一场景可保存多套命名参数组合,运行台/定时任务按需选用。
|
||||
// 表:plugin_configs(shell.db),按账号 + 插件隔离,同名覆盖。
|
||||
// 安全:password 字段一律**明文落盘**——不做任何加解密,避免换机/密钥异常导致密码解析出错、
|
||||
// 平台连续密码错误锁号;旧版本 enc:v1 密文数据不兼容,密码需在界面重新保存方案。
|
||||
// 安全:plugin.json 声明 format=password 的字段用 AES-256-GCM 加密落盘(见 local-crypto);
|
||||
// 读取时按 enc:v1: 前缀解密,旧明文数据原样可用,下次保存自动转密文。
|
||||
"use strict";
|
||||
|
||||
const { db } = require("./db");
|
||||
const localCrypto = require("./local-crypto");
|
||||
|
||||
const MAX_CONFIGS_PER_PLUGIN = 10; // 单插件最多 10 套方案,防异常堆满
|
||||
|
||||
@@ -12,7 +13,7 @@ function rowToCfg(row) {
|
||||
if (!row) return null;
|
||||
let fields = {};
|
||||
try { fields = JSON.parse(row.fields || "{}"); } catch (_) { fields = {}; }
|
||||
return { id: String(row.id), name: row.name, fields, at: row.at || "" };
|
||||
return { id: String(row.id), name: row.name, fields: localCrypto.decryptFields(fields), at: row.at || "" };
|
||||
}
|
||||
|
||||
function listConfigs(account, pluginId) {
|
||||
@@ -31,11 +32,12 @@ function getConfig(account, pluginId, cfgId) {
|
||||
}
|
||||
|
||||
// 保存/覆盖:同名方案更新字段,否则新增;返回新/更新后的配置
|
||||
// (fields 原样明文落盘,schema 参数保留仅为兼容旧调用方)
|
||||
function saveConfig(account, pluginId, name, fields, schema) {
|
||||
// passwordKeys = 该插件 format=password 字段名数组(由 registry.passwordKeysOf 提供)
|
||||
function saveConfig(account, pluginId, name, fields, passwordKeys) {
|
||||
const trimmed = String(name || "").trim().slice(0, 20);
|
||||
if (!trimmed) return null;
|
||||
const keep = fields || {};
|
||||
const keys = Array.isArray(passwordKeys) ? passwordKeys : [];
|
||||
const keep = localCrypto.encryptFields(fields || {}, keys);
|
||||
const at = new Date().toLocaleString("zh-CN", { hour12: false });
|
||||
const exists = db.prepare(
|
||||
"SELECT id FROM plugin_configs WHERE account = ? AND plugin_id = ? AND name = ?"
|
||||
@@ -65,4 +67,25 @@ function deleteConfig(account, pluginId, cfgId) {
|
||||
return r.changes > 0;
|
||||
}
|
||||
|
||||
module.exports = { MAX_CONFIGS_PER_PLUGIN, listConfigs, getConfig, saveConfig, deleteConfig };
|
||||
// 历史明文迁移:把 password 字段加密回写(幂等;keysFor(pluginId) 返回该插件密码字段名)
|
||||
function migrate(keysFor) {
|
||||
let n = 0;
|
||||
const rows = db.prepare("SELECT id, plugin_id, fields FROM plugin_configs").all();
|
||||
for (const row of rows) {
|
||||
let fields;
|
||||
try { fields = JSON.parse(row.fields || "{}"); } catch (_) { continue; }
|
||||
if (!fields || typeof fields !== "object" || Array.isArray(fields)) continue;
|
||||
const keys = keysFor(row.plugin_id) || [];
|
||||
if (!keys.length) continue;
|
||||
const need = keys.some(
|
||||
(k) => typeof fields[k] === "string" && fields[k] && !localCrypto.isEncrypted(fields[k])
|
||||
);
|
||||
if (!need) continue;
|
||||
db.prepare("UPDATE plugin_configs SET fields = ? WHERE id = ?")
|
||||
.run(JSON.stringify(localCrypto.encryptFields(fields, keys)), row.id);
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
module.exports = { MAX_CONFIGS_PER_PLUGIN, listConfigs, getConfig, saveConfig, deleteConfig, migrate };
|
||||
|
||||
Reference in New Issue
Block a user