// 场景配置方案(多配置)· SQLite 实现:同一场景可保存多套命名参数组合,运行台/定时任务按需选用。 // 表:plugin_configs(shell.db),按账号 + 插件隔离,同名覆盖。 // 安全: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 套方案,防异常堆满 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: localCrypto.decryptFields(fields), at: row.at || "" }; } function listConfigs(account, pluginId) { const rows = db.prepare( "SELECT id, name, fields, at FROM plugin_configs WHERE account = ? AND plugin_id = ? ORDER BY id" ).all(account, pluginId); return rows.map(rowToCfg); } // 按 id 取单个方案(定时任务到点解析参数用;方案已删除返回 null) function getConfig(account, pluginId, cfgId) { const row = db.prepare( "SELECT id, name, fields, at FROM plugin_configs WHERE account = ? AND plugin_id = ? AND id = ?" ).get(account, pluginId, String(cfgId || "")); return rowToCfg(row); } // 保存/覆盖:同名方案更新字段,否则新增;返回新/更新后的配置 // 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 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 = ?" ).get(account, pluginId, trimmed); if (exists) { db.prepare( "UPDATE plugin_configs SET fields = ?, at = ? WHERE id = ?" ).run(JSON.stringify(keep), at, exists.id); return rowToCfg(db.prepare("SELECT id, name, fields, at FROM plugin_configs WHERE id = ?").get(exists.id)); } const count = db.prepare( "SELECT COUNT(*) AS n FROM plugin_configs WHERE account = ? AND plugin_id = ?" ).get(account, pluginId); if (count.n >= MAX_CONFIGS_PER_PLUGIN) { throw Object.assign(new Error(`每个场景最多保存 ${MAX_CONFIGS_PER_PLUGIN} 套配置`), { status: 400 }); } const r = db.prepare( "INSERT INTO plugin_configs (account, plugin_id, name, fields, at) VALUES (?, ?, ?, ?, ?)" ).run(account, pluginId, trimmed, JSON.stringify(keep), at); return rowToCfg(db.prepare("SELECT id, name, fields, at FROM plugin_configs WHERE id = ?").get(r.lastInsertRowid)); } function deleteConfig(account, pluginId, cfgId) { const r = db.prepare( "DELETE FROM plugin_configs WHERE account = ? AND plugin_id = ? AND id = ?" ).run(account, pluginId, cfgId); return r.changes > 0; } // 历史明文迁移:把 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 };