Files
dsh-purge/bin/dsh-purge.js
T
叫我小杨同学 1a8195b4d4 fix: 全仓审计加固与死代码清理,版本 1.1.87
- 回滚/备份:空 bak 改名 .bad 跳过;升级刷新 bak 补上单数 marker 补丁;统一原子写
- 补丁命中:optional 命中不符记软失败;#42 插入型替换加探针防重复插入
- Windows:进程枚举改用 | 分隔 + UTF-8,修复中文路径换包不杀旧进程;换包失败拉起原版
- 安全:asar 解包防越界;红队路径校验 realpath 防空 symlink;更新仅 https;报告回放去换行注入
- 提示词钩子:停用后 rewriteFn 返回原装配文本并清理 assemble guard;缓存签名纳入红队/策略/override
- 红队:listEngagements 默认不算统计;listVulns/报告支持 evidenceLimit
- 子代理名额占位即时生效并回退;--apply 有失败项退出码 1
- 清理:删除 lib/mnemon-settings.js 及 13 处死代码/未使用 import;移除 test 目录与 test 脚本
2026-11-01 21:47:03 +08:00

165 lines
7.6 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
import * as core from "../lib/core.js";
import * as rules from "../lib/rules.js";
const args = process.argv.slice(2);
function printStatus(state) {
const out = [];
out.push("dsh-purge 状态 / Status");
out.push(" 宿主 / surface " + (state.surface || "web"));
out.push(" DSH_HOME " + state.dsh_home);
if (state.desktop_install) out.push(" 桌面安装 " + state.desktop_install);
if (state.ai_base) {
out.push(" 插件根 / root " + state.ai_base);
for (const [key, fp] of Object.entries(state.files)) {
out.push(` ${key.padEnd(22)} ${fp}`);
}
} else {
out.push(" 插件根 / root " + core.missingAiBaseMessage());
}
out.push(` shim 目录 ${state.shim_dir || "未定位 / not found"}`);
out.push(` 备份 / backup ${state.has_backup ? "有 / yes" : "无 / no"}`);
out.push(` 注入文件 ${state.override_path}`);
out.push(` 注入状态 ${state.override_status}`);
out.push("");
out.push(` 补丁 ${state.patches_applied}/${core.ALL_PATCHES.length} 已清洗, ${state.patches_pending} 待清洗`
+ (state.patches_drifted ? `, ${state.patches_drifted} 漂移(官方原文已改,需更新 pattern)` : ""));
if (state.host_version) {
const compat = state.host_supported === false ? ` ← 未适配:插件声明 ${state.host_target || "?"}` : "";
out.push(` 宿主版本 / host ${state.host_version}${compat}`);
}
for (const p of core.ALL_PATCHES) {
const s = state.patch_status[p.id];
const mark = s === "applied" ? "✓" : s === "pending" ? "✗" : s === "drifted" ? "!" : "·";
out.push(` ${mark} [#${String(p.id).padEnd(2)}] ${p.name.padEnd(30)} ${p.layer}/${p.layer_en} — ${s}`);
}
out.push("");
out.push(` shim: dsh.cmd=${state.shim_cmd} dsh.ps1=${state.shim_ps1} dsh=${state.shim_bin}`);
return out.join("\n");
}
async function main() {
const mode = args.find((a) => ["--apply", "--revert", "--uninstall", "--status", "--edit", "--help"].includes(a));
if (mode === "--help" || args.includes("-h")) {
console.log(`dsh-purge 用法:
dsh-purge --status 显示状态
dsh-purge --apply 应用全部清洗(提示词+代码+shim+override)
dsh-purge --revert 回滚还原
dsh-purge --uninstall 卸载插件(已应用则先还原)
dsh-purge --edit 编辑注入文件 prompt-inject.md
dsh-purge --help 帮助`);
return;
}
const state = await core.gatherState();
console.log(printStatus(state));
console.log("");
if (mode === "--apply") {
if (!state.ai_base) {
console.log("[ERROR] " + core.missingAiBaseMessage());
console.log(" fix: 把 DSH_BASE 指到含 dsh-agent-instructions/lib 的 @deepseek-ai 目录");
process.exit(1);
}
const inject = rules.resolveInjectText(state.dsh_home);
if (!inject.text) {
console.log("[ERROR] 提示词和规则集都是空的,必须先添加提示词");
process.exit(1);
}
const { hosts, report } = await core.applyPatchesAllHosts(core.ALL_PATCHES, {
preserveClientBundles: false,
forApply: true,
});
for (const h of hosts) console.log(` 宿主 / host → ${h.aiBase}`);
for (const r of report) {
const m = r.status === "applied" ? "✓ 已清洗" : r.status === "already" ? "- 已是最新" : r.status === "missing_file" ? "⚠ 文件缺失" : `✗ ${r.status}`;
console.log(` ${m} patch #${String(r.patch_id).padEnd(2)} ${r.name}`);
}
const flashHost = hosts[0]?.aiBase || state.ai_base;
const flash = core.silenceCmdFlash(flashHost);
console.log(` cmd-flash=${flash.entry} phase-1=${flash.phase1} ok=${flash.ok}`);
if (hosts.length > 1) {
console.log(` 多宿主 / multi-host: ${hosts.length} 份 @deepseek-ai 已清洗`);
}
console.log(inject.source === "rule"
? " - 使用当前规则集,不改写提示词文件"
: " - 使用已有提示词文件");
if (state.shim_dir) {
const r = await core.patchShim(state.shim_dir);
for (const [fname, st] of Object.entries(r)) {
if (st === "patched") console.log(` ✓ shim ${fname} 注入 / injected`);
else if (st === "already_patched") console.log(` - shim ${fname} 已注入 / already injected`);
else if (st === "missing") console.log(` - shim ${fname} 不存在 / missing`);
else console.log(` ⚠ shim ${fname}: ${st}`);
}
} else {
console.log(" ⚠ 未定位 shim 目录,跳过注入");
}
const failed = core.summarizeApplyReport(report).failed || [];
console.log("");
if (failed.length > 0) {
console.log(`✗ 完成但有 ${failed.length} 个补丁失败 / finished with ${failed.length} failed patch(es)`);
process.exit(1);
}
console.log("全部完成 / All done。重启 dsh 生效 / Restart dsh to take effect.");
} else if (mode === "--revert") {
if (state.ai_base || core.enumeratePatchAiBases().length) {
const { reverted, errors } = await core.revertAllHosts();
for (const p of reverted) console.log(` ✓ 已还原 / Reverted ${p}`);
for (const [p, e] of errors) console.log(` ⚠ 还原失败 ${p}: ${e}`);
if (reverted.length === 0) console.log(" - 没有补丁备份可还原 / no patch backup");
}
if (state.shim_dir) {
const r = await core.revertShim(state.shim_dir);
for (const [fname, st] of Object.entries(r)) {
if (st === "reverted" || st === "stripped_inject") console.log(` ✓ shim ${fname}: ${st}`);
else console.log(` - shim ${fname}: ${st}`);
}
}
console.log("");
console.log("回滚完成 / Revert done。重启 dsh 后恢复 / restart to restore.");
console.log("注: prompt-inject.md 保留(用户文件)/ prompt-inject.md kept (user file)");
} else if (mode === "--uninstall") {
const { uninstallPurge } = await import("../lib/uninstall.js");
const result = await uninstallPurge();
if (result.applied) console.log(" 已检测到补丁,已还原回原版。");
else console.log(" 未检测到已应用补丁,仍会清除插件文件。");
if (result.override?.removed) console.log(` 已删除 ${result.override.path}`);
if (result.stripped?.length) console.log(` 已从 profile 移除: ${result.stripped.join(", ")}`);
for (const e of result.errors || []) console.log(` ⚠ ${e}`);
if (!result.ok) {
console.log(" 卸载中止:还原失败,插件文件未删除。");
process.exit(1);
}
/* 命令行卸载没有「重启后助手」这一步:插件目录不会自己消失,得在这里删。
(界面卸载是把这一步交给独立助手,等端口释放后再删。) */
const fs = await import("node:fs");
for (const dir of result.cleanup || []) {
try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 8, retryDelay: 120 }); }
catch (e) { console.log(` ⚠ 未能删除 ${dir}: ${e && e.message ? e.message : e}`); }
}
console.log("");
console.log("卸载完成。请重启 dsh。");
} else if (mode === "--edit") {
const r = core.editOverride(state.dsh_home);
if (!r.ok && r.needCreate) {
await core.installOverride(state.dsh_home, true);
console.log(` ✓ 已创建默认注入文件:${state.override_path}`);
core.editOverride(state.dsh_home);
} else {
console.log(` → 已打开 ${r.editor} 编辑注入文件:${r.path}`);
}
console.log(" 编辑完成后重启 dsh 生效。");
} else {
console.log("默认不清洗,使用 --apply 应用清洗。");
}
}
main().catch((e) => {
console.error("[dsh-purge] error:", e);
process.exit(1);
});