feat(xq0011): 支持检查项选择并增加岗位等核心字段到异常名单
This commit is contained in:
@@ -1,14 +1,54 @@
|
||||
// 场景配置方案(多配置)· SQLite 实现:同一场景可保存多套命名参数组合,运行台/定时任务按需选用。
|
||||
// 表:plugin_configs(shell.db),按账号 + 插件隔离,同名覆盖。
|
||||
// 安全:password 类型字段一律不落盘(与 last-fields.js 一致,仅保存非敏感项)。
|
||||
// 安全:password 类型字段 AES-256-GCM 加密落盘(本机 MachineGuid 派生密钥,换机密文失效),
|
||||
// 每套方案可绑定不同账号密码,定时任务到点直接用方案字段执行。
|
||||
"use strict";
|
||||
|
||||
const crypto = require("crypto");
|
||||
const { execSync } = require("child_process");
|
||||
const { db } = require("./db");
|
||||
|
||||
const MAX_CONFIGS_PER_PLUGIN = 10; // 单插件最多 10 套方案,防异常堆满
|
||||
const ENC_PREFIX = "enc:v1:"; // 密文字段标记(iv12 + ct + tag16 → base64)
|
||||
|
||||
// 过滤敏感字段:schema = 插件的 fields 元数据,password 类型不入库
|
||||
function sanitize(fields, schema) {
|
||||
// ---- 本机绑定加解密(MachineGuid 派生密钥,进程内缓存)----
|
||||
let _key = null;
|
||||
function secretKey() {
|
||||
if (_key) return _key;
|
||||
let guid = "";
|
||||
try {
|
||||
const out = execSync('reg query "HKLM\\SOFTWARE\\Microsoft\\Cryptography" /v MachineGuid', { encoding: "utf8" });
|
||||
guid = (/MachineGuid\s+REG_SZ\s+(\S+)/.exec(out) || [])[1] || "";
|
||||
} catch (_) { /* 读不到时密钥退化为纯盐(仅本机临时可用) */ }
|
||||
_key = crypto.createHash("sha256").update(`37du-plugin-cfg:${guid}`).digest();
|
||||
return _key;
|
||||
}
|
||||
|
||||
function encVal(v) {
|
||||
if (typeof v !== "string" || !v || v.startsWith(ENC_PREFIX)) return v;
|
||||
const iv = crypto.randomBytes(12);
|
||||
const c = crypto.createCipheriv("aes-256-gcm", secretKey(), iv);
|
||||
const ct = Buffer.concat([c.update(v, "utf8"), c.final(), c.getAuthTag()]);
|
||||
return ENC_PREFIX + Buffer.concat([iv, ct]).toString("base64");
|
||||
}
|
||||
|
||||
function decVal(v) {
|
||||
if (typeof v !== "string" || !v.startsWith(ENC_PREFIX)) return v;
|
||||
try {
|
||||
const buf = Buffer.from(v.slice(ENC_PREFIX.length), "base64");
|
||||
const iv = buf.subarray(0, 12);
|
||||
const tag = buf.subarray(buf.length - 16);
|
||||
const ct = buf.subarray(12, buf.length - 16);
|
||||
const d = crypto.createDecipheriv("aes-256-gcm", secretKey(), iv);
|
||||
d.setAuthTag(tag);
|
||||
return Buffer.concat([d.update(ct), d.final()]).toString("utf8");
|
||||
} catch (_) {
|
||||
return ""; // 换机/密文损坏:解不出当空值,避免把密文当密码用
|
||||
}
|
||||
}
|
||||
|
||||
// password 类型字段加密落盘(已加密值幂等跳过);schema = 插件的 fields 元数据
|
||||
function encryptSecrets(fields, schema) {
|
||||
const secret = new Set(
|
||||
(Array.isArray(schema) ? schema : [])
|
||||
.filter((f) => f && f.type === "password")
|
||||
@@ -16,16 +56,17 @@ function sanitize(fields, schema) {
|
||||
);
|
||||
const out = {};
|
||||
for (const [k, v] of Object.entries(fields || {})) {
|
||||
if (secret.has(k)) continue;
|
||||
out[k] = v;
|
||||
out[k] = secret.has(k) ? encVal(v) : v;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function rowToCfg(row) {
|
||||
if (!row) return null;
|
||||
let fields = {};
|
||||
try { fields = JSON.parse(row.fields || "{}"); } catch (_) { fields = {}; }
|
||||
let raw = {};
|
||||
try { raw = JSON.parse(row.fields || "{}"); } catch (_) { raw = {}; }
|
||||
const fields = {};
|
||||
for (const [k, v] of Object.entries(raw)) fields[k] = decVal(v); // 按 enc 前缀解密
|
||||
return { id: String(row.id), name: row.name, fields, at: row.at || "" };
|
||||
}
|
||||
|
||||
@@ -48,7 +89,7 @@ function getConfig(account, pluginId, cfgId) {
|
||||
function saveConfig(account, pluginId, name, fields, schema) {
|
||||
const trimmed = String(name || "").trim().slice(0, 20);
|
||||
if (!trimmed) return null;
|
||||
const keep = sanitize(fields, schema);
|
||||
const keep = encryptSecrets(fields, schema);
|
||||
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 = ?"
|
||||
|
||||
@@ -327,7 +327,10 @@ async function tick() {
|
||||
const t = Date.parse(s.last_run_at.replace(" ", "T"));
|
||||
if (!Number.isNaN(t)) last = new Date(t);
|
||||
}
|
||||
const nxt = nextMatch(cron, last || now);
|
||||
// 锚点:有 last_run_at 用之(下个点过线 120s 内仍触发);新任务无记忆时回看一个容差窗,
|
||||
// 否则"刚过去 12s 的首个到点"会被 now 锚点直接跳到下一天,新建任务永远不触发
|
||||
const anchor = last || new Date(now.getTime() - CATCHUP_SKIP_MS);
|
||||
const nxt = nextMatch(cron, anchor);
|
||||
if (!nxt || nxt.getTime() > now.getTime()) continue; // 未来触发点
|
||||
if (!isDueNow(nxt, now)) {
|
||||
// 错过:壳子停机/离线期间到点,时间过了就过了,不补跑(要补就人工运行一次)。
|
||||
|
||||
Reference in New Issue
Block a user