Compare commits

...

10 Commits

41 changed files with 8289 additions and 2042 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "wechat-data-analysis-desktop",
"version": "1.3.0",
"version": "1.7.12",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "wechat-data-analysis-desktop",
"version": "1.3.0",
"version": "1.7.12",
"dependencies": {
"electron-updater": "^6.7.3"
},
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "wechat-data-analysis-desktop",
"private": true,
"version": "1.3.0",
"version": "1.7.12",
"main": "src/main.cjs",
"scripts": {
"dev": "node scripts/dev.cjs",
+201 -19
View File
@@ -22,12 +22,12 @@ const fs = require("fs");
const http = require("http");
const net = require("net");
const path = require("path");
const { Worker } = require("worker_threads");
const {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
migrateOutputDirectory,
normalizeDirectoryPath,
rollbackOutputDirectoryChange,
} = require("./output-dir.cjs");
const DEFAULT_BACKEND_HOST = String(process.env.WECHAT_TOOL_HOST || "127.0.0.1").trim() || "127.0.0.1";
@@ -45,6 +45,7 @@ let isQuitting = false;
let desktopSettings = null;
let backendPortChangeInProgress = false;
let outputDirChangeInProgress = false;
let outputDirChangeProgressState = null;
const gotSingleInstanceLock = app.requestSingleInstanceLock();
if (!gotSingleInstanceLock) {
@@ -279,7 +280,9 @@ function resolveOutputDir() {
if (!dataDir) return null;
const envOutputDir = safeNormalizeDirectory(process.env.WECHAT_TOOL_OUTPUT_DIR || "");
const settingsOutputDir = app.isPackaged ? safeNormalizeDirectory(loadDesktopSettings()?.outputDir || "") : "";
// Allow dev-mode desktop runs to persist the chosen output directory too.
// An explicit environment variable still wins so local launch overrides keep working.
const settingsOutputDir = safeNormalizeDirectory(loadDesktopSettings()?.outputDir || "");
let chosen = null;
try {
@@ -705,6 +708,7 @@ function getOutputDirInfo() {
const defaultPath = getDefaultOutputDir() || "";
const currentPath = resolveOutputDir() || defaultPath;
const hasPending = desktopSettings.pendingOutputDir !== null;
const canChange = !!defaultPath && !!currentPath;
const pendingPath =
desktopSettings.pendingOutputDir === null
? ""
@@ -718,8 +722,8 @@ function getOutputDirInfo() {
pendingPath,
hasPending,
lastError: String(desktopSettings.lastOutputDirError || "").trim(),
canChange: !!app.isPackaged,
changeUnavailableReason: app.isPackaged ? "" : "开发模式不支持界面修改 output 目录",
canChange,
changeUnavailableReason: canChange ? "" : "无法定位 output 目录",
};
}
@@ -749,10 +753,6 @@ function setIgnoredUpdateVersion(version) {
}
async function applyOutputDirChange(nextValue) {
if (!app.isPackaged) {
throw new Error("开发模式不支持界面修改 output 目录");
}
const defaultPath = getDefaultOutputDir();
const currentPath = resolveOutputDir();
if (!defaultPath || !currentPath) {
@@ -785,15 +785,41 @@ async function applyOutputDirChange(nextValue) {
const wasBackendRunning = !!backendProc;
let migration = null;
let settingsSwitched = false;
let retainedBackupPath = "";
let backupCleanupWarning = "";
try {
setOutputDirChangeProgressState({
active: true,
stage: "preparing",
message: wasBackendRunning ? "正在暂停后端并准备迁移 output 目录" : "正在准备迁移 output 目录",
percent: 1,
});
if (wasBackendRunning) {
await stopBackendAndWait({ timeoutMs: 10_000 });
}
migration = migrateOutputDirectory({
currentDir: currentPath,
nextDir: nextPath,
migration = await runOutputDirWorker(
"migrate",
{
currentDir: currentPath,
nextDir: nextPath,
},
(progress) => {
setOutputDirChangeProgressState({
active: true,
...progress,
});
}
);
setOutputDirChangeProgressState({
active: true,
stage: "switching",
message: "正在应用新的 output 目录设置",
percent: 99,
currentFile: "",
});
setOutputDirSetting(nextPath);
@@ -803,11 +829,38 @@ async function applyOutputDirChange(nextValue) {
ensureOutputLink();
if (wasBackendRunning) {
setOutputDirChangeProgressState({
active: true,
stage: "restarting",
message: "正在重启后端并应用新的 output 目录",
percent: 99,
});
startBackend();
await waitForBackend({ timeoutMs: 30_000 });
}
retainedBackupPath = migration?.backupDir || "";
if (retainedBackupPath) {
try {
cleanupOutputDirectoryBackup(retainedBackupPath);
retainedBackupPath = "";
} catch (cleanupErr) {
backupCleanupWarning = `;旧 output 目录未能自动删除:${cleanupErr?.message || cleanupErr}`;
logMain(
`[main] failed to clean output dir backup ${retainedBackupPath}: ${cleanupErr?.message || cleanupErr}`
);
}
}
setOutputDirChangeProgressState({
active: true,
stage: "complete",
message: migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换",
percent: 100,
});
const info = getOutputDirInfo();
const successMessage =
(migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换") + backupCleanupWarning;
return {
success: true,
changed: true,
@@ -815,16 +868,22 @@ async function applyOutputDirChange(nextValue) {
defaultPath: info.defaultPath,
isDefault: info.isDefault,
pendingPath: info.pendingPath,
backupPath: migration?.backupDir || "",
backupPath: retainedBackupPath,
sourceWasEmpty: !!migration?.sourceWasEmpty,
message: migration?.sourceWasEmpty ? "output 目录已切换" : "output 目录已迁移并切换",
message: successMessage,
};
} catch (err) {
const message = err?.message || String(err);
let rollbackMessage = "";
if (migration?.changed) {
try {
rollbackOutputDirectoryChange({
setOutputDirChangeProgressState({
active: true,
stage: "rolling-back",
message: "迁移失败,正在回滚 output 目录",
percent: 99,
});
await runOutputDirWorker("rollback", {
previousDir: currentPath,
currentDir: nextPath,
backupDir: migration.backupDir,
@@ -969,6 +1028,119 @@ function setWindowProgressBar(value) {
} catch {}
}
function makeIdleOutputDirChangeProgressState() {
return {
active: false,
stage: "idle",
message: "",
percent: 0,
bytesTransferred: 0,
bytesTotal: 0,
itemsTransferred: 0,
itemsTotal: 0,
currentFile: "",
error: "",
};
}
function clampOutputDirProgressNumber(value) {
const n = Number(value);
if (!Number.isFinite(n) || n < 0) return 0;
return n;
}
function normalizeOutputDirChangeProgressState(next = {}) {
const active = next?.active !== false;
const percent = Math.max(0, Math.min(100, Math.round(Number(next?.percent || 0))));
return {
active,
stage: String(next?.stage || (active ? "running" : "idle")),
message: String(next?.message || ""),
percent,
bytesTransferred: clampOutputDirProgressNumber(next?.bytesTransferred),
bytesTotal: clampOutputDirProgressNumber(next?.bytesTotal),
itemsTransferred: clampOutputDirProgressNumber(next?.itemsTransferred),
itemsTotal: clampOutputDirProgressNumber(next?.itemsTotal),
currentFile: String(next?.currentFile || ""),
error: String(next?.error || ""),
};
}
function getOutputDirChangeProgressState() {
if (!outputDirChangeProgressState) {
outputDirChangeProgressState = makeIdleOutputDirChangeProgressState();
}
return outputDirChangeProgressState;
}
function setOutputDirChangeProgressState(next = {}) {
outputDirChangeProgressState = normalizeOutputDirChangeProgressState(next);
sendToRenderer("app:outputDirChangeProgress", outputDirChangeProgressState);
if (!outputDirChangeProgressState.active) {
setWindowProgressBar(-1);
return outputDirChangeProgressState;
}
const ratio =
outputDirChangeProgressState.percent > 0
? Math.max(0.02, Math.min(1, outputDirChangeProgressState.percent / 100))
: 2;
setWindowProgressBar(ratio);
return outputDirChangeProgressState;
}
function clearOutputDirChangeProgressState() {
return setOutputDirChangeProgressState({ active: false });
}
function getOutputDirWorkerScriptPath() {
return path.join(__dirname, "output-dir-worker.cjs");
}
function runOutputDirWorker(action, payload, onProgress) {
return new Promise((resolve, reject) => {
const worker = new Worker(getOutputDirWorkerScriptPath(), {
workerData: {
action: String(action || "migrate"),
payload,
},
});
let settled = false;
const finish = (err, result) => {
if (settled) return;
settled = true;
if (err) reject(err);
else resolve(result);
};
worker.on("message", (message) => {
if (!message || typeof message !== "object") return;
if (message.type === "progress") {
if (typeof onProgress === "function") onProgress(message.progress || {});
return;
}
if (message.type === "result") {
finish(null, message.result);
return;
}
if (message.type === "error") {
finish(new Error(message.error?.message || "output 目录迁移失败"));
}
});
worker.once("error", (err) => {
finish(err);
});
worker.once("exit", (code) => {
if (settled || code === 0) return;
finish(new Error(`output 目录任务异常退出(code=${code}`));
});
});
}
function looksLikeHtml(input) {
if (!input) return false;
const s = String(input);
@@ -1611,14 +1783,21 @@ function startBackend() {
if (backendProc) return backendProc;
startWcdbSidecar();
const resolvedDataPath = resolveDataDir() || getUserDataDir() || repoRoot();
const resolvedOutputPath = resolveOutputDir() || getDefaultOutputDir() || path.join(resolvedDataPath, "output");
const env = {
...process.env,
WECHAT_TOOL_HOST: getBackendBindHost(),
WECHAT_TOOL_PORT: String(getBackendPort()),
WECHAT_TOOL_DATA_DIR: resolvedDataPath,
WECHAT_TOOL_OUTPUT_DIR: resolvedOutputPath,
// Make sure Python prints UTF-8 to stdout/stderr.
PYTHONIOENCODING: process.env.PYTHONIOENCODING || "utf-8",
};
ensureWcdbSidecarEnv(env);
logMain(
`[main] startBackend packaged=${app.isPackaged} port=${env.WECHAT_TOOL_PORT} dataDir=${env.WECHAT_TOOL_DATA_DIR} outputDir=${env.WECHAT_TOOL_OUTPUT_DIR}`
);
// In packaged mode we expect to provide the generated Nuxt output dir via env.
if (app.isPackaged && !env.WECHAT_TOOL_UI_DIR) {
@@ -1626,8 +1805,6 @@ function startBackend() {
}
if (app.isPackaged) {
env.WECHAT_TOOL_DATA_DIR = resolveDataDir() || app.getPath("userData");
env.WECHAT_TOOL_OUTPUT_DIR = resolveOutputDir() || getDefaultOutputDir() || path.join(env.WECHAT_TOOL_DATA_DIR, "output");
try {
fs.mkdirSync(env.WECHAT_TOOL_DATA_DIR, { recursive: true });
fs.mkdirSync(env.WECHAT_TOOL_OUTPUT_DIR, { recursive: true });
@@ -2156,8 +2333,8 @@ function registerWindowIpc() {
pendingPath: "",
hasPending: false,
lastError: err?.message || String(err),
canChange: !!app.isPackaged,
changeUnavailableReason: app.isPackaged ? "" : "开发模式不支持界面修改 output 目录",
canChange: false,
changeUnavailableReason: "无法读取 output 目录信息",
};
}
});
@@ -2166,6 +2343,10 @@ function registerWindowIpc() {
return resolveOutputDir() || "";
});
ipcMain.handle("app:getOutputDirChangeProgress", () => {
return getOutputDirChangeProgressState();
});
ipcMain.handle("app:openOutputDir", async () => {
const outDir = resolveOutputDir();
if (!outDir) throw new Error("无法定位 output 目录");
@@ -2202,6 +2383,7 @@ function registerWindowIpc() {
};
} finally {
outputDirChangeInProgress = false;
clearOutputDirChangeProgressState();
}
});
+74
View File
@@ -0,0 +1,74 @@
const { parentPort, workerData } = require("worker_threads");
const { migrateOutputDirectory, rollbackOutputDirectoryChange } = require("./output-dir.cjs");
function serializeError(err) {
return {
message: err?.message || String(err),
stack: err?.stack ? String(err.stack) : "",
};
}
function createProgressPoster() {
let lastStage = "";
let lastPercent = -1;
let lastSentAt = 0;
return (progress) => {
const stage = String(progress?.stage || "");
const percent = Number(progress?.percent || 0);
const now = Date.now();
const shouldSend =
stage !== lastStage ||
percent >= 100 ||
percent <= 0 ||
percent >= lastPercent + 1 ||
now - lastSentAt >= 120;
if (!shouldSend) return;
lastStage = stage;
lastPercent = percent;
lastSentAt = now;
parentPort?.postMessage({ type: "progress", progress });
};
}
async function main() {
const action = String(workerData?.action || "migrate");
const payload = workerData?.payload && typeof workerData.payload === "object" ? workerData.payload : {};
if (action === "migrate") {
const result = await migrateOutputDirectory({
...payload,
onProgress: createProgressPoster(),
});
parentPort?.postMessage({ type: "result", result });
return;
}
if (action === "rollback") {
parentPort?.postMessage({
type: "progress",
progress: {
stage: "rolling-back",
message: "迁移失败,正在回滚 output 目录",
percent: 99,
bytesTransferred: 0,
bytesTotal: 0,
itemsTransferred: 0,
itemsTotal: 0,
currentFile: "",
},
});
rollbackOutputDirectoryChange(payload);
parentPort?.postMessage({ type: "result", result: { success: true } });
return;
}
throw new Error(`不支持的 output 目录 worker 动作:${action}`);
}
main().catch((err) => {
parentPort?.postMessage({ type: "error", error: serializeError(err) });
process.exitCode = 1;
});
+281 -8
View File
@@ -1,5 +1,6 @@
const fs = require("fs");
const path = require("path");
const { pipeline } = require("stream/promises");
const SENTINEL_NAMES = [
"account_keys.json",
@@ -10,6 +11,17 @@ const SENTINEL_NAMES = [
"logs",
];
const PROGRESS_STAGE_MESSAGES = {
preparing: "正在准备迁移 output 目录",
scanning: "正在扫描 output 目录",
copying: "正在复制 output 数据",
verifying: "正在校验已复制的数据",
switching: "正在切换 output 目录",
"rolling-back": "迁移失败,正在回滚 output 目录",
restarting: "正在重启后端并应用新的 output 目录",
complete: "output 目录迁移完成",
};
function normalizeDirectoryPath(value) {
const text = String(value || "").trim();
if (!text) return "";
@@ -136,7 +148,233 @@ function ensureTargetIsUsable(targetDir) {
}
}
function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
function clampNonNegativeNumber(value) {
const n = Number(value);
if (!Number.isFinite(n) || n < 0) return 0;
return n;
}
function computeProgressPercent(stage, bytesTransferred, bytesTotal, itemsTransferred, itemsTotal) {
if (stage === "preparing") return 1;
if (stage === "scanning") return 2;
if (stage === "verifying") return 96;
if (stage === "switching") return 99;
if (stage === "complete") return 100;
if (stage === "copying") {
const ratio =
bytesTotal > 0
? Math.min(1, bytesTransferred / bytesTotal)
: itemsTotal > 0
? Math.min(1, itemsTransferred / itemsTotal)
: 1;
return Math.max(5, Math.min(94, Math.round(5 + ratio * 89)));
}
return 0;
}
function buildProgressSnapshot({
stage = "preparing",
bytesTransferred = 0,
bytesTotal = 0,
itemsTransferred = 0,
itemsTotal = 0,
currentFile = "",
}) {
const normalizedStage = String(stage || "preparing");
const safeBytesTransferred = clampNonNegativeNumber(bytesTransferred);
const safeBytesTotal = clampNonNegativeNumber(bytesTotal);
const safeItemsTransferred = clampNonNegativeNumber(itemsTransferred);
const safeItemsTotal = clampNonNegativeNumber(itemsTotal);
return {
stage: normalizedStage,
message: PROGRESS_STAGE_MESSAGES[normalizedStage] || "正在迁移 output 目录",
percent: computeProgressPercent(
normalizedStage,
safeBytesTransferred,
safeBytesTotal,
safeItemsTransferred,
safeItemsTotal
),
bytesTransferred: safeBytesTransferred,
bytesTotal: safeBytesTotal,
itemsTransferred: safeItemsTransferred,
itemsTotal: safeItemsTotal,
currentFile: String(currentFile || ""),
};
}
function emitProgress(onProgress, payload) {
if (typeof onProgress !== "function") return;
onProgress(buildProgressSnapshot(payload));
}
function sortDirectoryEntries(entries) {
return entries.sort((a, b) => String(a.name || "").localeCompare(String(b.name || "")));
}
function depthOfRelativePath(relativePath) {
const text = String(relativePath || "").trim();
if (!text) return 0;
return text.split(path.sep).length;
}
function collectCopyManifest(sourceDir) {
const directories = [];
const files = [];
let totalBytes = 0;
const stack = [""];
while (stack.length > 0) {
const relativeDir = stack.pop();
const absoluteDir = relativeDir ? path.join(sourceDir, relativeDir) : sourceDir;
const dirEntries = sortDirectoryEntries(fs.readdirSync(absoluteDir, { withFileTypes: true }));
for (const dirent of dirEntries) {
const relativePath = relativeDir ? path.join(relativeDir, dirent.name) : dirent.name;
const absolutePath = path.join(sourceDir, relativePath);
const stat = fs.lstatSync(absolutePath);
if (dirent.isDirectory()) {
directories.push({
relativePath,
mode: stat.mode,
atime: stat.atime,
mtime: stat.mtime,
});
stack.push(relativePath);
continue;
}
if (dirent.isFile()) {
files.push({
relativePath,
size: stat.size,
mode: stat.mode,
atime: stat.atime,
mtime: stat.mtime,
});
totalBytes += stat.size;
continue;
}
if (dirent.isSymbolicLink()) {
throw new Error(`output 目录包含不支持的符号链接:${relativePath}`);
}
throw new Error(`output 目录包含不支持的文件类型:${relativePath}`);
}
}
directories.sort((a, b) => depthOfRelativePath(a.relativePath) - depthOfRelativePath(b.relativePath));
return {
directories,
files,
totalBytes,
totalItems: directories.length + files.length,
};
}
function applyStatMetadata(targetPath, statLike) {
try {
if (Number.isInteger(statLike?.mode)) {
fs.chmodSync(targetPath, statLike.mode);
}
} catch {}
try {
if (statLike?.atime && statLike?.mtime) {
fs.utimesSync(targetPath, statLike.atime, statLike.mtime);
}
} catch {}
}
async function copyFileWithProgress({ sourcePath, targetPath, mode, onChunk }) {
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
const readStream = fs.createReadStream(sourcePath);
readStream.on("data", (chunk) => {
if (typeof onChunk === "function") onChunk(chunk.length);
});
const writeStream = fs.createWriteStream(targetPath, {
flags: "w",
mode: Number.isInteger(mode) ? mode : undefined,
});
await pipeline(readStream, writeStream);
}
async function copyOutputTree({ sourceDir, targetDir, manifest, onProgress }) {
fs.mkdirSync(targetDir, { recursive: true });
let bytesTransferred = 0;
let itemsTransferred = 0;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
});
for (const dirEntry of manifest.directories) {
const targetPath = path.join(targetDir, dirEntry.relativePath);
fs.mkdirSync(targetPath, { recursive: true });
itemsTransferred += 1;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: dirEntry.relativePath,
});
}
for (const fileEntry of manifest.files) {
const sourcePath = path.join(sourceDir, fileEntry.relativePath);
const targetPath = path.join(targetDir, fileEntry.relativePath);
await copyFileWithProgress({
sourcePath,
targetPath,
mode: fileEntry.mode,
onChunk: (delta) => {
bytesTransferred += clampNonNegativeNumber(delta);
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: fileEntry.relativePath,
});
},
});
applyStatMetadata(targetPath, fileEntry);
itemsTransferred += 1;
emitProgress(onProgress, {
stage: "copying",
bytesTransferred,
bytesTotal: manifest.totalBytes,
itemsTransferred,
itemsTotal: manifest.totalItems,
currentFile: fileEntry.relativePath,
});
}
for (let i = manifest.directories.length - 1; i >= 0; i -= 1) {
const dirEntry = manifest.directories[i];
applyStatMetadata(path.join(targetDir, dirEntry.relativePath), dirEntry);
}
}
async function migrateOutputDirectory({ currentDir, nextDir, now = new Date(), onProgress } = {}) {
const currentPath = normalizeDirectoryPath(currentDir);
const targetPath = normalizeDirectoryPath(nextDir);
if (!currentPath || !targetPath) {
@@ -155,15 +393,19 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
throw new Error("新旧 output 路径不能互相包含");
}
emitProgress(onProgress, { stage: "scanning" });
ensureTargetIsUsable(targetPath);
const sourceExists = pathExists(currentPath);
if (sourceExists && !isDirectory(currentPath)) {
throw new Error("当前 output 路径不是目录");
}
const sourceWasEmpty = !sourceExists || !hasDirectoryContents(currentPath);
if (sourceWasEmpty) {
emitProgress(onProgress, { stage: "switching" });
fs.mkdirSync(targetPath, { recursive: true });
emitProgress(onProgress, { stage: "complete", itemsTransferred: 1, itemsTotal: 1 });
return {
changed: true,
currentDir: currentPath,
@@ -173,18 +415,34 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
};
}
const manifest = collectCopyManifest(currentPath);
const tempTarget = makeUniqueSiblingPath(targetPath, "migrating", now);
const backupDir = makeUniqueSiblingPath(currentPath, "backup", now);
fs.cpSync(currentPath, tempTarget, {
recursive: true,
force: false,
errorOnExist: true,
preserveTimestamps: true,
});
try {
await copyOutputTree({
sourceDir: currentPath,
targetDir: tempTarget,
manifest,
onProgress,
});
emitProgress(onProgress, {
stage: "verifying",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
verifyCopiedOutputTree(currentPath, tempTarget);
emitProgress(onProgress, {
stage: "switching",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
if (pathExists(targetPath)) {
fs.rmSync(targetPath, { recursive: true, force: true });
}
@@ -209,6 +467,13 @@ function migrateOutputDirectory({ currentDir, nextDir, now = new Date() }) {
throw err;
}
emitProgress(onProgress, {
stage: "complete",
bytesTransferred: manifest.totalBytes,
bytesTotal: manifest.totalBytes,
itemsTransferred: manifest.totalItems,
itemsTotal: manifest.totalItems,
});
return {
changed: true,
currentDir: currentPath,
@@ -242,7 +507,15 @@ function rollbackOutputDirectoryChange({ previousDir, currentDir, backupDir, sou
} catch {}
}
function cleanupOutputDirectoryBackup(backupDir) {
const backupPath = normalizeDirectoryPath(backupDir);
if (!backupPath || !pathExists(backupPath)) return false;
fs.rmSync(backupPath, { recursive: true, force: true });
return !pathExists(backupPath);
}
module.exports = {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
hasDirectoryContents,
+6
View File
@@ -84,10 +84,16 @@ contextBridge.exposeInMainWorld("wechatDesktop", {
// Data/output folder helpers
getOutputDirInfo: () => ipcRenderer.invoke("app:getOutputDirInfo"),
getOutputDir: () => ipcRenderer.invoke("app:getOutputDir"),
getOutputDirChangeProgress: () => ipcRenderer.invoke("app:getOutputDirChangeProgress"),
setOutputDir: (dir) => ipcRenderer.invoke("app:setOutputDir", String(dir ?? "")),
openOutputDir: () => ipcRenderer.invoke("app:openOutputDir"),
getAccountInfo: (account) => ipcRenderer.invoke("app:getAccountInfo", String(account || "")),
deleteAccountData: (account) => ipcRenderer.invoke("app:deleteAccountData", String(account || "")),
onOutputDirChangeProgress: (callback) => {
const handler = (_event, progress) => callback(progress);
ipcRenderer.on("app:outputDirChangeProgress", handler);
return () => ipcRenderer.removeListener("app:outputDirChangeProgress", handler);
},
// Auto update
getVersion: () => ipcRenderer.invoke("app:getVersion"),
+35 -10
View File
@@ -5,6 +5,7 @@ const os = require("os");
const path = require("path");
const {
cleanupOutputDirectoryBackup,
getDefaultOutputDirPath,
getEffectiveOutputDirPath,
migrateOutputDirectory,
@@ -55,14 +56,14 @@ test("getEffectiveOutputDirPath prefers env, then settings, then default", () =>
}
});
test("migrateOutputDirectory switches empty source to a new directory", () => {
test("migrateOutputDirectory switches empty source to a new directory", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
try {
fs.mkdirSync(currentDir, { recursive: true });
const result = migrateOutputDirectory({ currentDir, nextDir });
const result = await migrateOutputDirectory({ currentDir, nextDir });
assert.equal(result.changed, true);
assert.equal(result.sourceWasEmpty, true);
assert.equal(result.backupDir, "");
@@ -73,7 +74,7 @@ test("migrateOutputDirectory switches empty source to a new directory", () => {
}
});
test("migrateOutputDirectory blocks non-empty targets", () => {
test("migrateOutputDirectory blocks non-empty targets", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
@@ -84,8 +85,8 @@ test("migrateOutputDirectory blocks non-empty targets", () => {
fs.mkdirSync(nextDir, { recursive: true });
fs.writeFileSync(path.join(nextDir, "existing.txt"), "occupied");
assert.throws(
() => migrateOutputDirectory({ currentDir, nextDir }),
await assert.rejects(
migrateOutputDirectory({ currentDir, nextDir }),
/已有内容/
);
} finally {
@@ -93,15 +94,15 @@ test("migrateOutputDirectory blocks non-empty targets", () => {
}
});
test("migrateOutputDirectory blocks invalid current paths", () => {
test("migrateOutputDirectory blocks invalid current paths", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
try {
fs.writeFileSync(currentDir, "not-a-directory");
assert.throws(
() => migrateOutputDirectory({ currentDir, nextDir }),
await assert.rejects(
migrateOutputDirectory({ currentDir, nextDir }),
/不是目录/
);
} finally {
@@ -109,7 +110,7 @@ test("migrateOutputDirectory blocks invalid current paths", () => {
}
});
test("migrateOutputDirectory copies data and leaves the old directory as a backup", () => {
test("migrateOutputDirectory copies data and leaves the old directory as a backup", async () => {
const root = makeTempDir();
const currentDir = path.join(root, "current-output");
const nextDir = path.join(root, "custom-output");
@@ -120,7 +121,13 @@ test("migrateOutputDirectory copies data and leaves the old directory as a backu
fs.writeFileSync(path.join(currentDir, "databases", "wxid_test", "session.db"), "session");
fs.writeFileSync(path.join(currentDir, "databases", "wxid_test", "contact.db"), "contact");
const result = migrateOutputDirectory({ currentDir, nextDir, now: new Date("2026-03-30T08:00:00Z") });
const progressEvents = [];
const result = await migrateOutputDirectory({
currentDir,
nextDir,
now: new Date("2026-03-30T08:00:00Z"),
onProgress: (progress) => progressEvents.push(progress),
});
assert.equal(result.changed, true);
assert.equal(result.sourceWasEmpty, false);
assert.match(path.basename(result.backupDir), /^current-output\.backup-\d{14}$/);
@@ -129,6 +136,9 @@ test("migrateOutputDirectory copies data and leaves the old directory as a backu
assert.ok(fs.existsSync(path.join(nextDir, "databases", "wxid_test", "session.db")));
assert.ok(fs.existsSync(result.backupDir));
assert.equal(fs.existsSync(currentDir), false);
assert.ok(progressEvents.some((event) => event.stage === "scanning"));
assert.ok(progressEvents.some((event) => event.stage === "copying" && event.percent > 0));
assert.ok(progressEvents.some((event) => event.stage === "complete" && event.percent === 100));
} finally {
cleanupDir(root);
}
@@ -160,3 +170,18 @@ test("rollbackOutputDirectoryChange restores the previous directory", () => {
cleanupDir(root);
}
});
test("cleanupOutputDirectoryBackup removes a completed migration backup directory", () => {
const root = makeTempDir();
const backupDir = path.join(root, "current-output.backup-20260330080100");
try {
fs.mkdirSync(path.join(backupDir, "databases"), { recursive: true });
fs.writeFileSync(path.join(backupDir, "databases", "session.db"), "restored");
assert.equal(cleanupOutputDirectoryBackup(backupDir), true);
assert.equal(fs.existsSync(backupDir), false);
} finally {
cleanupDir(root);
}
});
+1 -1
View File
@@ -101,7 +101,7 @@ const showDesktopTitleBar = computed(() => isDesktop.value)
const showSidebar = computed(() => {
const path = String(route.path || '')
if (path === '/') return false
if (path === '/' || path === '/import') return false
if (path === '/decrypt' || path === '/detection-result' || path === '/decrypt-result') return false
return !(path === '/wrapped' || path.startsWith('/wrapped/'))
})
+104 -1
View File
@@ -214,6 +214,22 @@
<div v-if="desktopOutputDirCanChange" class="text-[11px] text-[#909090]">
修改后会迁移整个 output 目录如果目标目录已有内容会先阻止并提示
</div>
<div v-if="desktopOutputDirProgress" class="rounded-[6px] border border-[#d8efe2] bg-[#f4fbf7] px-2.5 py-2">
<div class="flex items-center justify-between gap-3 text-[11px] text-[#1b6b43]">
<div class="min-w-0 truncate">{{ desktopOutputDirProgressText }}</div>
<div class="shrink-0 tabular-nums">{{ desktopOutputDirProgressPercentText }}</div>
</div>
<div class="mt-1.5 h-2 overflow-hidden rounded-full bg-[#dceee3]">
<div
class="h-full rounded-full bg-[#07b75b] transition-[width] duration-200 ease-out"
:class="desktopOutputDirProgressIndeterminate ? 'animate-pulse' : ''"
:style="{ width: desktopOutputDirProgressBarWidth }"
/>
</div>
<div v-if="desktopOutputDirProgressDetail" class="mt-1 text-[10px] text-[#5d7a68] break-all">
{{ desktopOutputDirProgressDetail }}
</div>
</div>
<div v-if="desktopOutputDirMessage" class="rounded-[6px] border border-[#d8efe2] bg-[#f4fbf7] px-2.5 py-1.5 text-[11px] text-[#1b6b43] whitespace-pre-wrap">
{{ desktopOutputDirMessage }}
</div>
@@ -410,6 +426,8 @@ const desktopOutputDirMessage = ref('')
const desktopOutputDirIsDefault = ref(true)
const desktopOutputDirCanChange = ref(true)
const desktopOutputDirUnavailableReason = ref('')
const desktopOutputDirProgress = ref(null)
let removeDesktopOutputDirProgressListener = null
const desktopOutputDirText = computed(() => {
if (!isDesktopEnv.value) return '仅桌面端可用'
const v = String(desktopOutputDir.value || '').trim()
@@ -424,6 +442,48 @@ const desktopOutputDirPendingText = computed(() => {
const v = String(desktopOutputDirPending.value || '').trim()
return v || ''
})
const desktopOutputDirProgressPercent = computed(() => {
const n = Number(desktopOutputDirProgress.value?.percent || 0)
if (!Number.isFinite(n) || n < 0) return 0
return Math.max(0, Math.min(100, Math.round(n)))
})
const desktopOutputDirProgressPercentText = computed(() => `${desktopOutputDirProgressPercent.value}%`)
const desktopOutputDirProgressText = computed(() => {
const text = String(desktopOutputDirProgress.value?.message || '').trim()
return text || '正在迁移 output 目录'
})
const desktopOutputDirProgressIndeterminate = computed(() => {
const stage = String(desktopOutputDirProgress.value?.stage || '').trim()
return stage === 'preparing' || stage === 'scanning' || stage === 'rolling-back' || stage === 'restarting'
})
const desktopOutputDirProgressBarWidth = computed(() => {
if (!desktopOutputDirProgress.value) return '0%'
if (desktopOutputDirProgressIndeterminate.value) return '28%'
return `${Math.max(6, desktopOutputDirProgressPercent.value)}%`
})
const desktopOutputDirProgressDetail = computed(() => {
const progress = desktopOutputDirProgress.value
if (!progress) return ''
const parts = []
const bytesTotal = Number(progress.bytesTotal || 0)
const bytesTransferred = Number(progress.bytesTransferred || 0)
const itemsTotal = Number(progress.itemsTotal || 0)
const itemsTransferred = Number(progress.itemsTransferred || 0)
if (bytesTotal > 0) {
parts.push(`${formatBytes(bytesTransferred)} / ${formatBytes(bytesTotal)}`)
} else if (itemsTotal > 0) {
parts.push(`${Math.min(itemsTransferred, itemsTotal)} / ${itemsTotal}`)
}
const currentFile = String(progress.currentFile || '').trim()
if (currentFile) {
parts.push(currentFile)
}
return parts.join(' · ')
})
const desktopOutputDirControlsDisabled = computed(() => (
!isDesktopEnv.value || !desktopOutputDirCanChange.value || desktopOutputDirLoading.value || desktopOutputDirApplying.value
))
@@ -442,6 +502,37 @@ const switchTrackClass = (enabled, disabled = false) => {
return enabled ? 'bg-[#07b75b] hover:brightness-95' : 'bg-[#d0d0d0] hover:brightness-95'
}
const formatBytes = (value) => {
const n = Number(value || 0)
if (!Number.isFinite(n) || n <= 0) return '0 B'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let next = n
let unitIndex = 0
while (next >= 1024 && unitIndex < units.length - 1) {
next /= 1024
unitIndex += 1
}
const digits = next >= 100 || unitIndex === 0 ? 0 : next >= 10 ? 1 : 2
return `${next.toFixed(digits)} ${units[unitIndex]}`
}
const applyDesktopOutputDirProgress = (progress) => {
if (!progress || progress.active === false) {
desktopOutputDirProgress.value = null
return
}
desktopOutputDirProgress.value = { ...progress }
}
const refreshDesktopOutputDirProgress = async () => {
if (!process.client || typeof window === 'undefined') return
if (!window.wechatDesktop?.getOutputDirChangeProgress) return
try {
const progress = await window.wechatDesktop.getOutputDirChangeProgress()
applyDesktopOutputDirProgress(progress)
} catch {}
}
const sectionElements = computed(() => [
{ key: 'desktop', el: desktopSectionRef.value },
{ key: 'startup', el: startupSectionRef.value },
@@ -676,12 +767,13 @@ const applyDesktopOutputDir = async (nextDir) => {
return
}
if (!desktopOutputDirCanChange.value) {
desktopOutputDirError.value = desktopOutputDirUnavailableReason.value || '开发模式不支持界面修改 output 目录'
desktopOutputDirError.value = desktopOutputDirUnavailableReason.value || '当前环境不支持修改 output 目录'
return
}
desktopOutputDirApplying.value = true
desktopOutputDirError.value = ''
desktopOutputDirMessage.value = ''
desktopOutputDirProgress.value = null
try {
const res = await window.wechatDesktop.setOutputDir(String(nextDir ?? '').trim())
if (res?.success === false) {
@@ -856,6 +948,7 @@ watch(() => props.open, async (isOpen) => {
await refreshBackendLogFileInfo()
if (isDesktopEnv.value) {
await refreshDesktopOutputDir()
await refreshDesktopOutputDirProgress()
}
}, { immediate: true })
@@ -864,6 +957,11 @@ onMounted(async () => {
const isElectron = /electron/i.test(String(navigator.userAgent || ''))
isDesktopEnv.value = isElectron && !!window.wechatDesktop
window.addEventListener('keydown', onEscKeydown)
if (window.wechatDesktop?.onOutputDirChangeProgress) {
removeDesktopOutputDirProgressListener = window.wechatDesktop.onOutputDirChangeProgress((progress) => {
applyDesktopOutputDirProgress(progress)
})
}
}
desktopAutoRealtime.value = readLocalBoolSetting(DESKTOP_SETTING_AUTO_REALTIME_KEY, false)
@@ -876,6 +974,7 @@ onMounted(async () => {
await refreshDesktopAutoLaunch()
await refreshDesktopCloseBehavior()
await refreshDesktopOutputDir()
await refreshDesktopOutputDirProgress()
}
await nextTick()
@@ -885,6 +984,10 @@ onMounted(async () => {
onBeforeUnmount(() => {
if (!process.client || typeof window === 'undefined') return
window.removeEventListener('keydown', onEscKeydown)
if (typeof removeDesktopOutputDirProgressListener === 'function') {
removeDesktopOutputDirProgressListener()
removeDesktopOutputDirProgressListener = null
}
})
</script>
+8 -29
View File
@@ -357,30 +357,6 @@ export const useApi = () => {
return await request(url)
}
// 朋友圈图片本地缓存候选(用于错图时手动选择)
const listSnsMediaCandidates = async (params = {}) => {
const query = new URLSearchParams()
if (params && params.account) query.set('account', params.account)
if (params && params.create_time != null) query.set('create_time', String(params.create_time))
if (params && params.width != null) query.set('width', String(params.width))
if (params && params.height != null) query.set('height', String(params.height))
if (params && params.limit != null) query.set('limit', String(params.limit))
if (params && params.offset != null) query.set('offset', String(params.offset))
const url = '/sns/media_candidates' + (query.toString() ? `?${query.toString()}` : '')
return await request(url)
}
// 保存朋友圈图片手动匹配结果(本机)
const saveSnsMediaPicks = async (data = {}) => {
return await request('/sns/media_picks', {
method: 'POST',
body: {
account: data.account || null,
picks: (data && data.picks && typeof data.picks === 'object' && !Array.isArray(data.picks)) ? data.picks : {}
}
})
}
const openChatMediaFolder = async (params = {}) => {
const query = new URLSearchParams()
if (params && params.account) query.set('account', params.account)
@@ -421,6 +397,7 @@ export const useApi = () => {
const getSavedKeys = async (params = {}) => {
const query = new URLSearchParams()
if (params && params.account) query.set('account', params.account)
if (params && params.db_storage_path) query.set('db_storage_path', params.db_storage_path)
const url = '/keys' + (query.toString() ? `?${query.toString()}` : '')
return await request(url)
}
@@ -477,7 +454,7 @@ export const useApi = () => {
return await request(`/chat/exports/${encodeURIComponent(String(exportId))}`, { method: 'DELETE' })
}
// 朋友圈导出(离线 HTML zip
// 朋友圈导出(离线 ZIP,支持 HTML / JSON / TXT
const createSnsExport = async (data = {}) => {
return await request('/sns/exports', {
method: 'POST',
@@ -485,6 +462,7 @@ export const useApi = () => {
account: data.account || null,
scope: data.scope || 'selected',
usernames: Array.isArray(data.usernames) ? data.usernames : [],
format: data.format || 'html',
use_cache: data.use_cache == null ? true : !!data.use_cache,
output_dir: data.output_dir == null ? null : String(data.output_dir || '').trim(),
file_name: data.file_name || null
@@ -570,8 +548,11 @@ export const useApi = () => {
}
// 获取数据库密钥
const getKeys = async () => {
return await request('/get_keys')
const getKeys = async (params = {}) => {
const query = new URLSearchParams()
if (params && params.wechat_install_path) query.set('wechat_install_path', params.wechat_install_path)
const url = '/get_keys' + (query.toString() ? `?${query.toString()}` : '')
return await request(url)
}
// 获取图片密钥
@@ -667,8 +648,6 @@ export const useApi = () => {
resolveAppMsg,
listSnsTimeline,
listSnsUsers,
listSnsMediaCandidates,
saveSnsMediaPicks,
openChatMediaFolder,
downloadChatEmoji,
saveMediaKeys,
+24
View File
@@ -0,0 +1,24 @@
export const WECHAT_INSTALL_PATH_STORAGE_KEY = 'decrypt.wechatInstallPath'
export const normalizeWechatInstallPath = (value) => String(value || '').trim()
export const readStoredWechatInstallPath = () => {
if (!process.client || typeof window === 'undefined') return ''
try {
return normalizeWechatInstallPath(window.localStorage.getItem(WECHAT_INSTALL_PATH_STORAGE_KEY) || '')
} catch {
return ''
}
}
export const writeStoredWechatInstallPath = (value) => {
if (!process.client || typeof window === 'undefined') return
try {
const normalized = normalizeWechatInstallPath(value)
if (normalized) {
window.localStorage.setItem(WECHAT_INSTALL_PATH_STORAGE_KEY, normalized)
} else {
window.localStorage.removeItem(WECHAT_INSTALL_PATH_STORAGE_KEY)
}
} catch {}
}
+22 -3
View File
@@ -73,6 +73,12 @@
</svg>
点击按钮将自动获取数据库解密密钥您也可以手动输入已知的64位密钥
</p>
<p v-if="formData.wechat_install_path" class="mt-2 text-xs text-[#7F7F7F] flex items-start">
<svg class="w-4 h-4 mr-1 mt-0.5 text-[#10AEEF]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<span>当前将使用第一步检测时保存的微信安装目录<span class="font-mono break-all">{{ formData.wechat_install_path }}</span></span>
</p>
</div>
<!-- 数据库路径输入 -->
@@ -655,6 +661,7 @@
<script setup>
import { ref, reactive, computed, onMounted, onBeforeUnmount } from 'vue'
import { useApi } from '~/composables/useApi'
import { normalizeWechatInstallPath, readStoredWechatInstallPath } from '~/lib/wechat-install-path'
const { decryptDatabase, saveMediaKeys, getSavedKeys, getKeys, getImageKey, getWxStatus } = useApi()
@@ -677,7 +684,8 @@ const steps = [
// 表单数据
const formData = reactive({
key: '',
db_storage_path: ''
db_storage_path: '',
wechat_install_path: ''
})
// 表单错误
@@ -764,7 +772,10 @@ const prefillKeysForAccount = async (account) => {
if (!acc) return
logDecryptDebug('prefill:start', { account: acc })
try {
const resp = await getSavedKeys({ account: acc })
const resp = await getSavedKeys({
account: acc,
db_storage_path: String(formData.db_storage_path || '').trim()
})
if (!resp || resp.status !== 'success') return
const keys = resp.keys || {}
@@ -786,6 +797,9 @@ const prefillKeysForAccount = async (account) => {
request_account: acc,
response_account: String(resp.account || '').trim(),
db_key_present: !!dbKey,
db_key_store_account: String(keys.db_key_store_account || '').trim(),
db_key_source_wxid_dir: String(keys.db_key_source_wxid_dir || '').trim(),
db_key_blocked_reason: String(keys.db_key_blocked_reason || '').trim(),
...summarizeKeyStateForLog(
String(keys.image_xor_key || '').trim(),
String(keys.image_aes_key || '').trim()
@@ -873,6 +887,8 @@ const handleGetDbKey = async () => {
formErrors.key = ''
try {
const wechatInstallPath = normalizeWechatInstallPath(formData.wechat_install_path || readStoredWechatInstallPath())
formData.wechat_install_path = wechatInstallPath
const statusRes = await getWxStatus()
const wxStatus = statusRes?.wx_status
@@ -883,7 +899,9 @@ const handleGetDbKey = async () => {
warning.value = '正在启动微信,请确保微信未开启“自动登录”,并在弹窗中正常登录。'
const res = await getKeys()
const res = await getKeys({
wechat_install_path: wechatInstallPath
})
if (res && res.status === 0) {
if (res.data?.db_key) {
@@ -1617,6 +1635,7 @@ const skipToChat = async () => {
// 页面加载时检查是否有选中的账户
onMounted(async () => {
if (process.client && typeof window !== 'undefined') {
formData.wechat_install_path = readStoredWechatInstallPath()
const selectedAccount = sessionStorage.getItem('selectedAccount')
logDecryptDebug('mounted:selected-account-raw', { raw: selectedAccount || '' })
if (selectedAccount) {
+233 -125
View File
@@ -1,5 +1,5 @@
<template>
<div class="detection-result-page min-h-screen relative overflow-hidden flex items-center">
<div class="detection-result-page min-h-screen relative overflow-hidden">
<!-- 网格背景 -->
<div class="absolute inset-0 bg-grid-pattern opacity-5 pointer-events-none"></div>
@@ -9,14 +9,16 @@
<div class="absolute -bottom-8 left-40 w-80 h-80 bg-[#91D300] opacity-5 rounded-full blur-3xl pointer-events-none"></div>
<!-- 主要内容 -->
<div class="relative z-10 w-full max-w-6xl mx-auto px-4 py-8 animate-fade-in">
<div class="relative z-10 w-full max-w-5xl mx-auto px-4 sm:px-5 py-6 sm:py-8 animate-fade-in">
<!-- 顶部操作栏 -->
<div class="flex items-center justify-between mb-6">
<h2 class="text-2xl font-bold">
<span class="bg-gradient-to-r from-[#07C160] to-[#10AEEF] bg-clip-text text-transparent">检测结果</span>
</h2>
<div class="flex items-center justify-between mb-4">
<div>
<h2 class="text-[22px] font-bold leading-none">
<span class="bg-gradient-to-r from-[#07C160] to-[#10AEEF] bg-clip-text text-transparent">检测结果</span>
</h2>
</div>
<NuxtLink to="/"
class="inline-flex items-center px-3 py-1.5 text-sm text-[#07C160] hover:text-[#06AD56] font-medium transition-colors">
class="inline-flex items-center px-3 py-1.5 rounded-lg text-xs text-[#07C160] hover:text-[#06AD56] hover:bg-white/80 font-medium transition-colors">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
</svg>
@@ -24,66 +26,167 @@
</NuxtLink>
</div>
<!-- 兜底唤起原生目录选择器再次检测 -->
<div class="bg-white rounded-xl p-4 md:p-5 border border-[#EDEDED] mb-6 flex flex-col md:flex-row md:items-center justify-between gap-4 shadow-sm hover:shadow transition-shadow duration-200">
<div>
<h3 class="text-sm font-bold text-[#000000e6] flex items-center">
未找到想要的账号
<div
v-if="detectionResult && !loading && !detectionResult.error"
class="grid grid-cols-1 sm:grid-cols-3 gap-2.5 mb-4"
>
<div class="bg-white/90 backdrop-blur rounded-xl px-4 py-3 border border-[#EDEDED]">
<div class="flex items-center justify-between gap-3">
<div class="min-w-0">
<p class="text-[11px] tracking-[0.08em] uppercase text-[#7F7F7F]">微信版本</p>
<p class="mt-1 text-lg font-semibold text-[#000000e6] truncate">{{ detectionResult.data?.wechat_version || '未知' }}</p>
</div>
<div class="w-9 h-9 shrink-0 bg-[#07C160]/10 rounded-lg flex items-center justify-center">
<svg class="w-[18px] h-[18px] text-[#07C160]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
</div>
</div>
</div>
<div class="bg-white/90 backdrop-blur rounded-xl px-4 py-3 border border-[#EDEDED]">
<div class="flex items-center justify-between gap-3">
<div class="min-w-0">
<p class="text-[11px] tracking-[0.08em] uppercase text-[#7F7F7F]">检测账号</p>
<p class="mt-1 text-lg font-semibold text-[#000000e6]">{{ detectionResult.data?.total_accounts || 0 }} </p>
</div>
<div class="w-9 h-9 shrink-0 bg-[#10AEEF]/10 rounded-lg flex items-center justify-center">
<svg class="w-[18px] h-[18px] text-[#10AEEF]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283-.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
</svg>
</div>
</div>
</div>
<div class="bg-white/90 backdrop-blur rounded-xl px-4 py-3 border border-[#EDEDED]">
<div class="flex items-center justify-between gap-3">
<div class="min-w-0">
<p class="text-[11px] tracking-[0.08em] uppercase text-[#7F7F7F]">数据库文件</p>
<p class="mt-1 text-lg font-semibold text-[#000000e6]">{{ detectionResult.data?.total_databases || 0 }} </p>
</div>
<div class="w-9 h-9 shrink-0 bg-[#91D300]/10 rounded-lg flex items-center justify-center">
<svg class="w-[18px] h-[18px] text-[#91D300]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"/>
</svg>
</div>
</div>
</div>
</div>
<!-- 第一步检测数据目录与微信安装目录都在这里设置 -->
<div v-if="!loading" class="bg-white/90 backdrop-blur rounded-xl p-3.5 md:p-4 border border-[#EDEDED] mb-4 space-y-3">
<div class="flex flex-col md:flex-row md:items-center justify-between gap-3">
<div>
<h3 class="text-[13px] font-semibold text-[#000000e6] flex items-center">
未找到想要的账号
<!-- <span class="ml-2 px-2 py-0.5 bg-gray-100 text-gray-500 rounded text-xs font-normal">深度检测兜底</span>-->
</h3>
<p class="text-xs text-[#7F7F7F] mt-1.5">
<span v-if="customPath">当前指定检测路径:<span class="font-mono bg-gray-50 px-1 rounded text-[#000000e6]">{{ customPath }}</span></span>
<span v-else>如果自动检测漏了您可以手动指定微信数据根目录 (通常名为 xwechat_files) 让系统重新扫描</span>
</h3>
<p class="text-[11px] text-[#7F7F7F] mt-1">
<span v-if="customPath">当前指定检测路径:<span class="font-mono bg-gray-50 px-1 rounded text-[#000000e6]">{{ customPath }}</span></span>
<span v-else>如果自动检测漏了您可以手动指定微信数据根目录 (通常名为 xwechat_files) 让系统重新扫描</span>
</p>
</div>
<button @click="handlePickDirectory" :disabled="loading"
class="shrink-0 px-4 py-2.5 bg-[#07C160] text-white rounded-xl text-xs font-medium hover:bg-[#06AD56] focus:ring-2 focus:ring-[#07C160] focus:ring-offset-1 disabled:opacity-50 transition-all duration-200 flex items-center justify-center">
<svg v-if="!loading" class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/>
</svg>
<svg v-else class="w-4 h-4 mr-2 animate-spin" fill="none" viewBox="0 0 48 48" aria-hidden="true">
<circle class="opacity-20" cx="24" cy="24" r="18" stroke="currentColor" stroke-width="6"></circle>
<circle
cx="24"
cy="24"
r="18"
stroke="currentColor"
stroke-width="6"
stroke-linecap="round"
stroke-dasharray="28 72"
pathLength="100"
transform="rotate(-90 24 24)"
></circle>
</svg>
{{ loading ? '检测中...' : '手动选择目录检测' }}
</button>
</div>
<div class="pt-3 border-t border-[#F3F3F3]">
<label for="wechatInstallPath" class="block text-[13px] font-medium text-[#000000e6] mb-2">
微信安装目录第一步先填这里
</label>
<div class="flex flex-col lg:flex-row gap-3">
<input
id="wechatInstallPath"
v-model="wechatInstallPath"
type="text"
placeholder="例如: D:\Program Files\Tencent\WeChat 或 D:\Program Files\Tencent\WeChat\Weixin.exe"
class="flex-1 px-4 py-2.5 bg-white border border-[#EDEDED] rounded-lg font-mono text-[13px] focus:outline-none focus:ring-2 focus:ring-[#07C160] focus:border-transparent transition-all duration-200"
@blur="persistWechatInstallPath"
/>
<button
type="button"
@click="pickWechatInstallDirectory"
:disabled="isPickingWechatInstallPath"
class="shrink-0 px-4 py-2.5 bg-white border border-[#EDEDED] text-[#000000e6] rounded-xl text-xs font-medium hover:bg-gray-50 disabled:opacity-50 disabled:cursor-wait transition-all duration-200"
>
{{ isPickingWechatInstallPath ? '选择中...' : '选择微信目录' }}
</button>
</div>
<p class="text-[11px] text-[#7F7F7F] mt-2">
一键获取数据库密钥会优先使用这里填写的路径支持填写安装目录也支持直接填写 <span class="font-mono">Weixin.exe</span> / <span class="font-mono">WeChat.exe</span> 路径
</p>
</div>
<button @click="handlePickDirectory" :disabled="loading"
class="shrink-0 px-5 py-2.5 bg-[#07C160] text-white rounded-xl text-sm font-medium hover:bg-[#06AD56] focus:ring-2 focus:ring-[#07C160] focus:ring-offset-1 disabled:opacity-50 transition-all duration-200 flex items-center justify-center">
<svg v-if="!loading" class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/>
</svg>
<svg v-else class="w-4 h-4 mr-2 animate-spin" fill="none" viewBox="0 0 48 48" aria-hidden="true">
<circle class="opacity-20" cx="24" cy="24" r="18" stroke="currentColor" stroke-width="6"></circle>
<circle
cx="24"
cy="24"
r="18"
stroke="currentColor"
stroke-width="6"
stroke-linecap="round"
stroke-dasharray="28 72"
pathLength="100"
transform="rotate(-90 24 24)"
></circle>
</svg>
{{ loading ? '检测中...' : '手动选择目录检测' }}
</button>
</div>
<!-- 主内容区域 -->
<div>
<div :class="loading ? 'flex min-h-[52vh] items-center justify-center' : ''">
<!-- 检测中状态 -->
<div v-if="loading" class="absolute inset-0 bg-white/80 backdrop-blur-sm z-20 rounded-2xl flex flex-col items-center justify-center border border-[#EDEDED]">
<svg class="w-16 h-16 animate-spin text-[#07C160]" fill="none" viewBox="0 0 48 48" aria-hidden="true">
<circle class="opacity-20" cx="24" cy="24" r="18" stroke="currentColor" stroke-width="6"></circle>
<circle
cx="24"
cy="24"
r="18"
stroke="currentColor"
stroke-width="6"
stroke-linecap="round"
stroke-dasharray="28 72"
pathLength="100"
transform="rotate(-90 24 24)"
></circle>
</svg>
<p class="mt-4 text-lg text-[#7F7F7F]">正在检测微信数据...</p>
<div v-if="loading" class="w-full max-w-3xl rounded-[24px] border border-[#EDEDED] bg-white/92 px-5 py-6 sm:px-8 sm:py-7">
<div class="flex flex-col items-center text-center">
<div class="relative flex h-12 w-12 items-center justify-center rounded-2xl bg-[#07C160]/10">
<span class="absolute inset-0 rounded-2xl border border-[#07C160]/10"></span>
<svg class="h-5 w-5 animate-spin text-[#07C160]" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<circle cx="12" cy="12" r="9" stroke="currentColor" stroke-width="2.5" class="opacity-20"></circle>
<path d="M21 12a9 9 0 0 0-9-9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"></path>
</svg>
</div>
<div class="mt-4 flex flex-wrap items-center justify-center gap-2">
<span class="inline-flex items-center rounded-full bg-[#07C160]/10 px-2.5 py-1 text-[11px] font-medium text-[#07C160]">
检测中
</span>
<span class="text-[11px] text-[#7F7F7F]">正在自动读取本机微信环境</span>
</div>
<h3 class="mt-3 text-[20px] font-semibold text-[#000000e6] leading-tight">正在检查账号与数据库文件</h3>
<p class="mt-2 max-w-[560px] text-[13px] leading-6 text-[#7F7F7F]">
会依次确认微信安装信息最近登录账号以及可用数据库通常几秒内完成
</p>
<div class="mt-5 h-1.5 w-full max-w-[620px] overflow-hidden rounded-full bg-[#F3F4F6]">
<div class="h-full w-2/5 rounded-full bg-gradient-to-r from-[#07C160] via-[#34D17A] to-[#8CE0AF] animate-pulse"></div>
</div>
<div class="mt-5 flex flex-wrap items-center justify-center gap-2.5">
<div class="inline-flex items-center gap-2 rounded-full border border-[#EDEDED] bg-[#FAFAFA] px-3 py-2 text-[12px] text-[#000000d9]">
<span class="h-2 w-2 rounded-full bg-[#07C160] animate-pulse"></span>
<span>安装信息</span>
</div>
<div class="inline-flex items-center gap-2 rounded-full border border-[#EDEDED] bg-[#FAFAFA] px-3 py-2 text-[12px] text-[#000000d9]">
<span class="h-2 w-2 rounded-full bg-[#07C160] animate-pulse"></span>
<span>账号匹配</span>
</div>
<div class="inline-flex items-center gap-2 rounded-full border border-[#EDEDED] bg-[#FAFAFA] px-3 py-2 text-[12px] text-[#000000d9]">
<span class="h-2 w-2 rounded-full bg-[#07C160] animate-pulse"></span>
<span>数据库汇总</span>
</div>
</div>
</div>
</div>
<!-- 检测结果内容 -->
<!-- detection result content -->
<div v-if="detectionResult && !loading">
<!-- 错误信息 -->
<div v-if="detectionResult.error" class="bg-red-50 rounded-2xl border border-red-100 p-8">
<div v-if="detectionResult.error" class="bg-red-50 rounded-2xl border border-red-100 p-6">
<div class="flex items-center">
<svg class="w-8 h-8 text-red-500 mr-3 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
@@ -96,95 +199,49 @@
</div>
<!-- 成功结果 -->
<div v-else class="space-y-4">
<!-- 概览卡片 -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-3">
<div class="bg-white rounded-xl p-4 border border-[#EDEDED]">
<div class="flex items-center justify-between">
<div>
<p class="text-sm text-[#7F7F7F]">微信版本</p>
<p class="text-xl font-bold text-[#000000e6] mt-1">{{ detectionResult.data?.wechat_version || '未知' }}</p>
</div>
<div class="w-12 h-12 bg-[#07C160]/10 rounded-lg flex items-center justify-center">
<svg class="w-6 h-6 text-[#07C160]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
</div>
</div>
</div>
<div class="bg-white rounded-xl p-4 border border-[#EDEDED]">
<div class="flex items-center justify-between">
<div>
<p class="text-sm text-[#7F7F7F]">检测到的账户</p>
<p class="text-xl font-bold text-[#000000e6] mt-1">{{ detectionResult.data?.total_accounts || 0 }} </p>
</div>
<div class="w-12 h-12 bg-[#10AEEF]/10 rounded-lg flex items-center justify-center">
<svg class="w-6 h-6 text-[#10AEEF]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283-.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
</svg>
</div>
</div>
</div>
<div class="bg-white rounded-xl p-4 border border-[#EDEDED]">
<div class="flex items-center justify-between">
<div>
<p class="text-sm text-[#7F7F7F]">数据库文件</p>
<p class="text-xl font-bold text-[#000000e6] mt-1">{{ detectionResult.data?.total_databases || 0 }} </p>
</div>
<div class="w-12 h-12 bg-[#91D300]/10 rounded-lg flex items-center justify-center">
<svg class="w-6 h-6 text-[#91D300]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"/>
</svg>
</div>
</div>
</div>
</div>
<div v-else class="space-y-3">
<!-- 账户列表 -->
<div v-if="detectionResult.data?.accounts && detectionResult.data.accounts.length > 0"
class="bg-white rounded-2xl border border-[#EDEDED] overflow-hidden shadow-sm">
<div class="p-4 border-b border-[#EDEDED] bg-gray-50 flex items-center justify-between">
<h3 class="text-base font-bold text-[#000000e6]">可操作的微信账户</h3>
<span class="text-xs text-gray-500">点击解密即可提取数据</span>
class="bg-white/92 backdrop-blur rounded-2xl border border-[#EDEDED] overflow-hidden">
<div class="px-4 py-3 border-b border-[#EDEDED] bg-[#fafafa] flex items-center justify-between">
<h3 class="text-[15px] font-semibold text-[#000000e6]">可操作的微信账户</h3>
<span class="text-[11px] text-gray-500">点击解密即可提取数据</span>
</div>
<div class="divide-y divide-[#EDEDED] max-h-96 overflow-y-auto">
<div class="divide-y divide-[#EDEDED] max-h-[420px] overflow-y-auto">
<div v-for="(account, index) in sortedAccounts" :key="index"
:class="['p-5 transition-all duration-200 relative overflow-hidden', isCurrentAccount(account.account_name) ? 'bg-[#07C160]/5 border border-[#07C160]/20' : 'hover:bg-[#F9F9F9]']">
:class="['px-4 py-3.5 transition-all duration-200 relative overflow-hidden', isCurrentAccount(account.account_name) ? 'bg-[#07C160]/5 border border-[#07C160]/20' : 'hover:bg-[#F9F9F9]']">
<div v-if="isCurrentAccount(account.account_name)" class="absolute top-0 right-0 bg-gradient-to-l from-[#07C160]/20 to-transparent px-4 py-1 rounded-bl-xl flex items-center">
<div v-if="isCurrentAccount(account.account_name)" class="absolute top-0 right-0 bg-gradient-to-l from-[#07C160]/20 to-transparent px-3 py-1 rounded-bl-xl flex items-center">
<span class="text-xs text-[#07C160] font-bold flex items-center">
<svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
最近登录账户
</span>
</div>
<div class="flex items-center justify-between mt-1">
<div class="flex items-center justify-between gap-3 mt-1">
<div class="flex-1">
<div class="flex items-center">
<template v-if="isCurrentAccount(account.account_name) && currentAccountInfo?.avatar">
<img :src="currentAccountInfo.avatar" class="w-14 h-14 rounded-xl border-2 border-[#07C160]/30 mr-4 shadow-sm object-cover bg-white" alt=""/>
<img :src="currentAccountInfo.avatar" class="w-12 h-12 rounded-xl border-2 border-[#07C160]/30 mr-3 object-cover bg-white" alt=""/>
</template>
<template v-else>
<div class="w-14 h-14 bg-gradient-to-br from-[#07C160]/10 to-[#91D300]/10 rounded-xl flex items-center justify-center mr-4 shadow-inner">
<span class="text-[#07C160] font-bold text-xl">{{ account.account_name?.charAt(0)?.toUpperCase() || 'U' }}</span>
<div class="w-12 h-12 bg-gradient-to-br from-[#07C160]/10 to-[#91D300]/10 rounded-xl flex items-center justify-center mr-3">
<span class="text-[#07C160] font-bold text-lg">{{ account.account_name?.charAt(0)?.toUpperCase() || 'U' }}</span>
</div>
</template>
<div>
<div class="flex flex-col">
<template v-if="isCurrentAccount(account.account_name) && currentAccountInfo?.nickname">
<p class="text-xl font-extrabold text-[#000000e6] leading-tight">{{ currentAccountInfo.nickname }}</p>
<p class="text-xs text-[#7F7F7F] mt-1 font-mono">wxid: {{ account.account_name }}</p>
<p class="text-lg font-bold text-[#000000e6] leading-tight">{{ currentAccountInfo.nickname }}</p>
<p class="text-[11px] text-[#7F7F7F] mt-0.5 font-mono">wxid: {{ account.account_name }}</p>
</template>
<template v-else>
<p class="text-lg font-bold text-[#000000e6]">{{ account.account_name || '未知账户' }}</p>
<p class="text-[15px] font-semibold text-[#000000e6]">{{ account.account_name || '未知账户' }}</p>
</template>
</div>
<div class="flex items-center mt-2 space-x-4 text-sm text-[#7F7F7F]">
<div class="flex items-center mt-1.5 space-x-3 text-[12px] text-[#7F7F7F]">
<span class="flex items-center">
<svg class="w-4 h-4 mr-1 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4"/>
@@ -203,7 +260,7 @@
</div>
<button @click="goToDecrypt(account)"
class="inline-flex items-center px-6 py-2.5 bg-[#07C160] text-white rounded-xl font-bold hover:bg-[#06AD56] hover:shadow-md hover:-translate-y-0.5 transition-all duration-200 text-sm shrink-0 z-10">
class="inline-flex items-center px-4 py-2 bg-[#07C160] text-white rounded-lg font-semibold hover:bg-[#06AD56] hover:-translate-y-0.5 transition-all duration-200 text-xs shrink-0 z-10">
解密提取
<svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
@@ -211,8 +268,8 @@
</button>
</div>
<div class="mt-4 pt-3 border-t border-dashed border-gray-200 text-sm text-gray-400">
<p v-if="account.data_dir" class="font-mono text-xs truncate" title="复制路径">
<div class="mt-3 pt-2.5 border-t border-dashed border-gray-200 text-sm text-gray-400">
<p v-if="account.data_dir" class="font-mono text-[11px] truncate" title="复制路径">
📂 {{ account.data_dir }}
</p>
</div>
@@ -221,11 +278,11 @@
</div>
<!-- 无账户提示 -->
<div v-else class="bg-white rounded-2xl p-12 text-center border border-[#EDEDED]">
<svg class="w-16 h-16 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<div v-else class="bg-white rounded-2xl p-8 text-center border border-[#EDEDED]">
<svg class="w-12 h-12 mx-auto text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.172 16.172a4 4 0 015.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<p class="text-lg text-[#000000e6] font-medium">没有在这台设备上发现微信数据</p>
<p class="text-base text-[#000000e6] font-medium">没有在这台设备上发现微信数据</p>
<p class="text-sm text-gray-500 mt-2">您可以尝试通过上方的按钮手动指定 "xwechat_files" 文件夹路径</p>
</div>
</div>
@@ -238,6 +295,7 @@
<script setup>
import {computed, onMounted, ref} from 'vue'
import {useApi} from '~/composables/useApi'
import {normalizeWechatInstallPath, readStoredWechatInstallPath, writeStoredWechatInstallPath} from '~/lib/wechat-install-path'
import {useAppStore} from '~/stores/app'
const { detectWechat, pickSystemDirectory } = useApi()
@@ -245,6 +303,8 @@ const appStore = useAppStore()
const loading = ref(false)
const detectionResult = ref(null)
const customPath = ref('')
const wechatInstallPath = ref('')
const isPickingWechatInstallPath = ref(false)
const STORAGE_KEY = 'wechat_data_root_path'
const isDesktopShell = () => {
@@ -288,6 +348,51 @@ const handlePickDirectory = async () => {
}
}
const persistWechatInstallPath = () => {
const normalized = normalizeWechatInstallPath(wechatInstallPath.value)
wechatInstallPath.value = normalized
writeStoredWechatInstallPath(normalized)
}
const pickWechatInstallDirectory = async () => {
if (isPickingWechatInstallPath.value) return
isPickingWechatInstallPath.value = true
try {
let path = ''
if (isDesktopShell()) {
const res = await window.wechatDesktop.chooseDirectory({
title: '请选择微信安装目录'
})
if (!res || res.canceled || !res.filePaths?.length) return
path = res.filePaths[0]
} else {
try {
const res = await pickSystemDirectory({
title: '请选择微信安装目录',
initial_dir: normalizeWechatInstallPath(wechatInstallPath.value)
})
if (!res || !res.path) return
path = res.path
} catch (e) {
console.error('通过API唤起微信安装目录选择器失败:', e)
path = window.prompt('无法直接唤起窗口,请输入微信安装目录或 Weixin.exe / WeChat.exe 的绝对路径:')
if (!path) return
}
}
const normalized = normalizeWechatInstallPath(path)
if (!normalized) return
wechatInstallPath.value = normalized
persistWechatInstallPath()
} catch (e) {
console.error('选择微信安装目录失败:', e)
} finally {
isPickingWechatInstallPath.value = false
}
}
// 计算属性:将当前登录账号排在第一位
const sortedAccounts = computed(() => {
if (!detectionResult.value?.data?.accounts) return []
@@ -384,6 +489,8 @@ const startDetection = async () => {
// 跳转到解密页面并传递账户信息
const goToDecrypt = (account) => {
persistWechatInstallPath()
if (process.client && typeof window !== 'undefined') {
sessionStorage.setItem('selectedAccount', JSON.stringify({
account_name: account.account_name,
@@ -412,6 +519,7 @@ onMounted(() => {
const saved = String(localStorage.getItem(STORAGE_KEY) || '').trim()
if (saved) customPath.value = saved
} catch {}
wechatInstallPath.value = readStoredWechatInstallPath()
}
startDetection()
})
+217 -139
View File
@@ -1,134 +1,186 @@
<template>
<div class="import-page min-h-screen flex items-center justify-center py-8">
<div class="max-w-2xl mx-auto px-6 w-full">
<div class="bg-white rounded-3xl border border-[#EDEDED] shadow-sm overflow-hidden">
<div class="p-8 md:p-12">
<!-- 标题部分 -->
<div class="flex items-center mb-8">
<div class="w-14 h-14 bg-[#91D300] rounded-2xl flex items-center justify-center mr-5 shadow-sm">
<svg class="w-8 h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
</svg>
</div>
<div>
<h2 class="text-2xl font-bold text-[#000000e6]">数据导入</h2>
<p class="text-[#7F7F7F] mt-1">导入已解密的数据库备份目录</p>
</div>
</div>
<div class="import-page min-h-screen relative overflow-hidden">
<div class="absolute inset-0 bg-grid-pattern opacity-5 pointer-events-none"></div>
<div class="bg-blue-50 border border-blue-100 rounded-2xl p-6 mb-8">
<h3 class="text-blue-800 font-bold mb-3 flex items-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
标准目录结构要求
</h3>
<ul class="text-sm text-blue-700 space-y-2 list-disc list-inside opacity-90">
<li><strong>预期目标</strong>请选择形如 <strong>/output/wxid_xxxxx/</strong> 这一级目录</li>
<li><strong>databases/</strong> 目录存放扁平化的 .db 文件</li>
<li><strong>account.json</strong> 文件系统会自动生成</li>
</ul>
</div>
<!-- 初始状态选择目录 -->
<div v-if="!importPreview && !importError && !importing" class="flex flex-col items-center justify-center py-12 border-2 border-dashed border-[#EDEDED] rounded-3xl hover:border-[#91D300] transition-colors cursor-pointer group" @click="handlePickDirectory">
<div class="w-20 h-20 bg-gray-50 rounded-full flex items-center justify-center mb-6 group-hover:scale-110 transition-transform duration-300">
<svg class="w-10 h-10 text-gray-400 group-hover:text-[#91D300]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/>
</svg>
</div>
<div class="text-[#000000e6] font-medium text-lg">点击选择备份目录</div>
<p class="text-[#7F7F7F] text-sm mt-2">支持原生目录选择器</p>
</div>
<!-- 导入进度状态 -->
<div v-if="importing" class="animate-fade-in py-12">
<div class="flex flex-col items-center">
<div class="relative w-32 h-32 mb-8">
<svg class="w-full h-full" viewBox="0 0 100 100">
<circle class="text-gray-100" stroke-width="8" stroke="currentColor" fill="transparent" r="42" cx="50" cy="50"/>
<circle class="text-[#91D300] transition-all duration-500" stroke-width="8" :stroke-dasharray="263.89" :stroke-dashoffset="263.89 * (1 - importProgress / 100)" stroke-linecap="round" stroke="currentColor" fill="transparent" r="42" cx="50" cy="50" transform="rotate(-90 50 50)"/>
<div class="relative z-10 mx-auto flex min-h-screen w-full max-w-3xl items-center justify-center px-4 py-6 sm:px-6 sm:py-8">
<div class="w-full rounded-[28px] border border-[#EDEDED] bg-white/92 backdrop-blur-sm">
<div class="px-5 py-5 sm:px-7 sm:py-7">
<div class="mb-5 flex items-start justify-between gap-3">
<div class="flex min-w-0 items-center gap-3">
<div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-2xl bg-[#07C160]/10 text-[#07C160]">
<svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
</svg>
<div class="absolute inset-0 flex items-center justify-center">
<span class="text-2xl font-bold text-gray-900">{{ importProgress }}%</span>
</div>
</div>
<h3 class="text-lg font-bold text-gray-900 mb-2">{{ importMessage }}</h3>
<p class="text-sm text-gray-500">正在为您处理数据请稍候...</p>
<div class="w-full max-w-xs bg-gray-100 h-1.5 rounded-full mt-8 overflow-hidden">
<div class="bg-[#91D300] h-full transition-all duration-500" :style="{ width: importProgress + '%' }"></div>
</div>
</div>
</div>
<!-- 预览状态显示账号信息 -->
<div v-if="importPreview && !importing" class="animate-fade-in">
<div class="flex flex-col items-center py-8 bg-[#FBFBFB] rounded-3xl border border-[#EDEDED] mb-8">
<div class="w-28 h-28 rounded-full overflow-hidden border-4 border-white shadow-md mb-5">
<img :src="importPreview.avatar_url || '/Contact.png'" class="w-full h-full object-cover" alt="头像">
</div>
<div class="text-center">
<div class="text-xl font-bold text-gray-900">{{ importPreview.nick }}</div>
<div class="text-sm text-gray-500 font-mono mt-1 bg-white px-3 py-1 rounded-full border border-[#EDEDED] inline-block">{{ importPreview.username }}</div>
</div>
<div class="mt-8 flex gap-6">
<div class="flex items-center text-sm text-gray-600">
<div class="w-2 h-2 rounded-full bg-[#07C160] mr-2"></div>
数据库已就绪
</div>
<div class="flex items-center text-sm text-gray-600">
<div class="w-2 h-2 rounded-full mr-2" :class="importPreview.has_resource ? 'bg-[#07C160]' : 'bg-gray-300'"></div>
资源文件{{ importPreview.has_resource ? '已发现' : '未发现' }}
</div>
<div class="min-w-0">
<p class="text-[11px] uppercase tracking-[0.12em] text-[#7F7F7F]">Import backup</p>
<h1 class="mt-1 text-[24px] font-semibold leading-none text-[#000000e6]">数据导入</h1>
<p class="mt-2 text-sm text-[#7F7F7F]">导入已解密的微信备份目录确认账号后即可写入当前工具</p>
</div>
</div>
<div class="flex gap-4">
<button @click="resetImport"
class="flex-1 px-8 py-4 border border-[#EDEDED] text-gray-600 rounded-2xl font-bold hover:bg-gray-50 transition-all">
重新选择
</button>
<button @click="confirmImport" :disabled="importing"
class="flex-[2] px-8 py-4 bg-[#91D300] text-white rounded-2xl font-bold hover:bg-[#82BD00] shadow-lg shadow-[#91D300]/20 disabled:opacity-50 transition-all flex items-center justify-center transform hover:scale-[1.02] active:scale-[0.98]">
<span v-if="!importing">确认导入此账号</span>
<span v-else>正在导入数据...</span>
</button>
</div>
</div>
<!-- 错误状态 -->
<div v-if="importError && !importing" class="animate-fade-in">
<div class="p-6 bg-red-50 border border-red-100 rounded-2xl flex items-start mb-8">
<svg class="w-6 h-6 text-red-500 mr-3 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<div>
<p class="font-bold text-red-800 mb-1">导入失败</p>
<p class="text-sm text-red-600">{{ importError }}</p>
</div>
</div>
<button @click="resetImport"
class="w-full px-8 py-4 bg-white border border-[#EDEDED] text-[#07C160] rounded-2xl font-bold hover:bg-gray-50 transition-all flex items-center justify-center">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
重新选择目录
</button>
</div>
<!-- 返回首页 -->
<div class="mt-12 text-center">
<NuxtLink to="/" class="text-[#7F7F7F] hover:text-[#07C160] text-sm transition-colors inline-flex items-center">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
<NuxtLink
to="/"
class="inline-flex shrink-0 items-center rounded-lg px-3 py-1.5 text-xs font-medium text-[#07C160] transition-colors hover:bg-[#F3FBF6] hover:text-[#06AD56]"
>
<svg class="mr-1 h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
</svg>
返回首页
</NuxtLink>
</div>
<div class="mb-5 rounded-[22px] border border-[#E8EFE8] bg-[#F8FBF8] px-4 py-4">
<div class="flex items-center gap-2 text-[13px] font-semibold text-[#000000d9]">
<svg class="h-4 w-4 text-[#07C160]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span>目录要求</span>
</div>
<div class="mt-3 grid gap-2 sm:grid-cols-3">
<div class="rounded-2xl border border-white bg-white/80 px-3 py-3">
<p class="text-[11px] uppercase tracking-[0.08em] text-[#7F7F7F]">Target</p>
<p class="mt-1 text-sm leading-6 text-[#000000d9]">请选择 `output / wxid_xxxxx` 这一层目录</p>
</div>
<div class="rounded-2xl border border-white bg-white/80 px-3 py-3">
<p class="text-[11px] uppercase tracking-[0.08em] text-[#7F7F7F]">Database</p>
<p class="mt-1 text-sm leading-6 text-[#000000d9]">目录内需要包含 `databases/`用于存放 `.db` 文件</p>
</div>
<div class="rounded-2xl border border-white bg-white/80 px-3 py-3">
<p class="text-[11px] uppercase tracking-[0.08em] text-[#7F7F7F]">Account</p>
<p class="mt-1 text-sm leading-6 text-[#000000d9]">`account.json` 会作为账号识别与信息校验依据</p>
</div>
</div>
</div>
<div v-if="!importPreview && !importError && !importing" class="animate-fade-in">
<div
class="group cursor-pointer rounded-[24px] border border-dashed border-[#D8E5DA] bg-[#FCFDFC] px-6 py-10 text-center transition-colors duration-200 hover:border-[#07C160] hover:bg-white"
@click="handlePickDirectory"
>
<div class="mx-auto flex h-14 w-14 items-center justify-center rounded-2xl bg-white text-[#07C160] ring-1 ring-[#EDEDED]">
<svg class="h-7 w-7" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
</svg>
</div>
<h3 class="mt-4 text-lg font-semibold text-[#000000e6]">选择解密备份目录</h3>
<p class="mt-2 text-sm text-[#7F7F7F]">建议直接选择 `wxid_xxxxx` 层级减少后续校验失败</p>
<div class="mt-5 inline-flex items-center rounded-full bg-[#07C160] px-4 py-2 text-sm font-medium text-white transition-colors duration-200 group-hover:bg-[#06AD56]">
点击开始选择
</div>
<p class="mt-4 text-xs text-[#A3A3A3]">桌面端优先使用系统目录选择器异常时会自动回退到手动输入</p>
</div>
</div>
<div v-if="importing" class="animate-fade-in">
<div class="rounded-[24px] border border-[#EDEDED] bg-[#FCFDFC] px-5 py-8 sm:px-6">
<div class="mx-auto flex w-fit items-center gap-2 rounded-full bg-[#07C160]/10 px-3 py-1 text-xs font-medium text-[#07C160]">
<span class="inline-flex h-2 w-2 rounded-full bg-current animate-pulse"></span>
正在导入
</div>
<div class="mt-5 text-center">
<p class="text-xl font-semibold text-[#000000e6]">{{ importMessage }}</p>
<p class="mt-2 text-sm text-[#7F7F7F]">请保持程序运行导入完成后会自动进入聊天页面</p>
</div>
<div class="mt-6 overflow-hidden rounded-full bg-[#EDF3EE]">
<div
class="h-2 rounded-full bg-[#07C160] transition-all duration-500"
:style="{ width: `${Math.min(Math.max(importProgress, 0), 100)}%` }"
></div>
</div>
<div class="mt-3 flex items-center justify-between text-xs text-[#7F7F7F]">
<span>已连接导入任务</span>
<span>{{ importProgress }}%</span>
</div>
</div>
</div>
<div v-if="importPreview && !importing" class="animate-fade-in space-y-4">
<div class="rounded-[24px] border border-[#EDEDED] bg-[#FCFDFC] px-5 py-5 sm:px-6">
<div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div class="flex min-w-0 items-center gap-4">
<div class="h-16 w-16 shrink-0 overflow-hidden rounded-2xl border border-[#EDEDED] bg-white">
<img :src="importPreview.avatar_url || '/Contact.png'" class="h-full w-full object-cover" alt="Avatar" />
</div>
<div class="min-w-0">
<p class="text-[11px] uppercase tracking-[0.12em] text-[#7F7F7F]">Detected account</p>
<div class="mt-1 truncate text-xl font-semibold text-[#000000e6]">{{ importPreview.nick || '未命名账号' }}</div>
<div class="mt-2 inline-flex max-w-full items-center rounded-full border border-[#EDEDED] bg-white px-3 py-1 text-xs font-mono text-[#7F7F7F]">
<span class="truncate">{{ importPreview.username }}</span>
</div>
</div>
</div>
<div class="inline-flex w-fit items-center rounded-full bg-[#07C160]/10 px-3 py-1 text-xs font-medium text-[#07C160]">
可导入
</div>
</div>
<div v-if="selectedImportPath" class="mt-4 rounded-[18px] border border-[#EDEDED] bg-white px-3 py-3">
<p class="text-[11px] uppercase tracking-[0.12em] text-[#7F7F7F]">Import path</p>
<p class="mt-1 break-all text-sm text-[#000000d9]">{{ selectedImportPath }}</p>
</div>
<div class="mt-4 flex flex-wrap gap-2">
<span class="inline-flex items-center gap-2 rounded-full border border-[#EDEDED] bg-white px-3 py-1.5 text-xs text-[#4A4A4A]">
<span class="h-2 w-2 rounded-full bg-[#07C160]"></span>
数据库已就绪
</span>
<span class="inline-flex items-center gap-2 rounded-full border border-[#EDEDED] bg-white px-3 py-1.5 text-xs text-[#4A4A4A]">
<span class="h-2 w-2 rounded-full" :class="importPreview.has_resource ? 'bg-[#07C160]' : 'bg-[#C9D2CB]'"></span>
资源文件{{ importPreview.has_resource ? '已发现' : '未发现' }}
</span>
</div>
</div>
<div class="grid gap-3 sm:grid-cols-[minmax(0,1fr)_minmax(0,1.35fr)]">
<button
class="inline-flex items-center justify-center rounded-2xl border border-[#E2E2E2] bg-white px-4 py-3 text-sm font-medium text-[#4A4A4A] transition-colors hover:bg-[#F8F8F8]"
@click="handlePickDirectory"
>
重新选择目录
</button>
<button
:disabled="importing"
class="inline-flex items-center justify-center rounded-2xl bg-[#07C160] px-4 py-3 text-sm font-medium text-white transition-colors hover:bg-[#06AD56] disabled:cursor-not-allowed disabled:bg-[#8FD9AE]"
@click="confirmImport"
>
确认导入此账号
</button>
</div>
</div>
<div v-if="importError && !importing" class="animate-fade-in space-y-4">
<div class="rounded-[22px] border border-[#F4D6D6] bg-[#FFF7F7] px-5 py-5">
<div class="flex items-start gap-3">
<div class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-white text-[#E05A5A] ring-1 ring-[#F0D7D7]">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</div>
<div class="min-w-0">
<p class="text-base font-semibold text-[#B64545]">导入失败</p>
<p class="mt-1 text-sm leading-6 text-[#9C5F5F]">{{ importError }}</p>
</div>
</div>
<div v-if="selectedImportPath" class="mt-4 rounded-[18px] border border-[#F1E3E3] bg-white/80 px-3 py-3">
<p class="text-[11px] uppercase tracking-[0.12em] text-[#B26B6B]">Current path</p>
<p class="mt-1 break-all text-sm text-[#7A4B4B]">{{ selectedImportPath }}</p>
</div>
</div>
<button
class="inline-flex w-full items-center justify-center rounded-2xl border border-[#E2E2E2] bg-white px-4 py-3 text-sm font-medium text-[#4A4A4A] transition-colors hover:bg-[#F8F8F8]"
@click="retryPickDirectory"
>
重新选择目录
</button>
</div>
</div>
</div>
</div>
@@ -136,9 +188,9 @@
</template>
<script setup>
import {ref, onUnmounted} from 'vue'
import {useApi} from '~/composables/useApi'
import {useApiBase} from '~/composables/useApiBase'
import { onUnmounted, ref } from 'vue'
import { useApi } from '~/composables/useApi'
import { useApiBase } from '~/composables/useApiBase'
const importing = ref(false)
const importProgress = ref(0)
@@ -149,10 +201,15 @@ const selectedImportPath = ref('')
let eventSource = null
onUnmounted(() => {
const closeEventSource = () => {
if (eventSource) {
eventSource.close()
eventSource = null
}
}
onUnmounted(() => {
closeEventSource()
})
const isDesktopShell = () => {
@@ -161,6 +218,7 @@ const isDesktopShell = () => {
}
const resetImport = () => {
closeEventSource()
importPreview.value = null
importError.value = ''
selectedImportPath.value = ''
@@ -199,7 +257,10 @@ const handlePickDirectory = async () => {
}
if (path && !path.includes('wxid_')) {
const isOk = window.confirm(`你选择的目录为:\n${path}\n\n该目录似乎不符合 "wxid_xxxxx" 的格式。确定要继续吗?`)
const isOk = window.confirm(`你选择的目录为:
${path}
该目录似乎不符合 "wxid_xxxxx" 的格式。确定要继续吗?`)
if (!isOk) return
}
@@ -208,15 +269,20 @@ const handlePickDirectory = async () => {
importPreview.value = null
try {
importPreview.value = await importDecryptedPreview({import_path: path})
importPreview.value = await importDecryptedPreview({ import_path: path })
} catch (e) {
importError.value = e.message || '目录格式不正确,请确保包含 databases 目录和 account.json'
}
}
const retryPickDirectory = async () => {
resetImport()
await handlePickDirectory()
}
const confirmImport = async () => {
if (!selectedImportPath.value) return
importing.value = true
importError.value = ''
importProgress.value = 0
@@ -225,30 +291,28 @@ const confirmImport = async () => {
const url = new URL(`${apiBase.replace(/\/$/, '')}/import_decrypted`, window.location.origin)
url.searchParams.set('import_path', selectedImportPath.value)
if (eventSource) eventSource.close()
closeEventSource()
eventSource = new EventSource(url.toString())
eventSource.onmessage = async (event) => {
try {
const data = JSON.parse(event.data)
if (data.type === 'progress') {
importProgress.value = data.percent || 0
importMessage.value = data.message || '正在处理...'
} else if (data.type === 'complete') {
importProgress.value = 100
importMessage.value = '导入完成!'
eventSource.close()
// 延迟跳转,让用户看到 100%
closeEventSource()
setTimeout(async () => {
await navigateTo('/chat')
}, 1000)
} else if (data.type === 'error') {
importError.value = data.message || '导入失败'
importing.value = false
eventSource.close()
closeEventSource()
}
} catch (e) {
console.error('解析 SSE 数据失败:', e)
@@ -259,18 +323,32 @@ const confirmImport = async () => {
console.error('EventSource 错误:', e)
importError.value = '与服务器连接断开或发生错误'
importing.value = false
eventSource.close()
closeEventSource()
}
}
</script>
<style scoped>
.animate-fade-in {
animation: fadeIn 0.5s ease-out;
animation: fadeIn 0.35s ease-out;
}
.bg-grid-pattern {
background-image:
linear-gradient(rgba(7, 193, 96, 0.08) 1px, transparent 1px),
linear-gradient(90deg, rgba(7, 193, 96, 0.08) 1px, transparent 1px);
background-size: 32px 32px;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
-7
View File
@@ -34,13 +34,6 @@
<span>开始检测</span>
</button>
<NuxtLink to="/decrypt"
class="group inline-flex items-center px-12 py-4 bg-white text-[#07C160] border border-[#07C160] rounded-lg text-lg font-medium hover:bg-[#F7F7F7] transform hover:scale-105 transition-all duration-200">
<svg class="w-6 h-6 mr-3 group-hover:-rotate-12 transition-transform duration-200" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"/>
</svg>
<span>直接解密</span>
</NuxtLink>
<NuxtLink to="/import"
class="group inline-flex items-center px-12 py-4 bg-white text-[#91D300] border border-[#91D300] rounded-lg text-lg font-medium hover:bg-[#F7F7F7] transform hover:scale-105 transition-all duration-200">
+816 -443
View File
File diff suppressed because it is too large Load Diff
+4 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "wechat-decrypt-tool"
version = "1.3.0"
version = "1.7.12"
description = "Modern WeChat database decryption tool with React frontend"
readme = "README.md"
requires-python = ">=3.11"
@@ -43,6 +43,9 @@ include = [
"src/wechat_decrypt_tool/native/VoipEngine.dll",
"src/wechat_decrypt_tool/native/wcdb_api.dll",
"src/wechat_decrypt_tool/native/WCDB.dll",
"src/wechat_decrypt_tool/native/weflow_wasm/weflow_wasm_keystream.js",
"src/wechat_decrypt_tool/native/weflow_wasm/wasm_video_decode.js",
"src/wechat_decrypt_tool/native/weflow_wasm/wasm_video_decode.wasm",
]
[tool.uv]
+1 -1
View File
@@ -1,5 +1,5 @@
"""微信数据库解密工具
"""
__version__ = "1.3.0"
__version__ = "1.7.12"
__author__ = "WeChat Decrypt Tool"
-23
View File
@@ -35,7 +35,6 @@ from .routers.sns_export import router as _sns_export_router
from .routers.wechat_detection import router as _wechat_detection_router
from .routers.wrapped import router as _wrapped_router
from .request_logging import log_server_errors_middleware
from .sns_stage_timing import add_sns_stage_timing_headers
from .wcdb_realtime import WCDB_REALTIME, shutdown as _wcdb_shutdown
from .routers.biz import router as _biz_router
from .routers.system import router as _system_router
@@ -56,31 +55,9 @@ app.add_middleware(
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["X-SNS-Source", "X-SNS-Hit-Type", "X-SNS-X-Enc"],
)
@app.middleware("http")
async def _add_sns_stage_timing_headers(request: Request, call_next):
"""Expose SNS stage metadata to the frontend without extra requests.
`<img>` elements can't read response headers, but browsers can surface `Server-Timing`
via `performance.getEntriesByName(...).serverTiming` when `Timing-Allow-Origin` is set.
"""
response = await call_next(request)
try:
add_sns_stage_timing_headers(
response.headers,
source=str(response.headers.get("X-SNS-Source") or ""),
hit_type=str(response.headers.get("X-SNS-Hit-Type") or ""),
x_enc=str(response.headers.get("X-SNS-X-Enc") or ""),
)
except Exception:
pass
return response
@app.middleware("http")
async def _log_server_errors(request: Request, call_next):
return await log_server_errors_middleware(request_logger, request, call_next)
+3 -3
View File
@@ -10,9 +10,9 @@ This module provides a pure-Python ISAAC-64 implementation so the backend can
still attempt to generate a keystream when the WASM helper is unavailable.
Notes:
- Moments *image* decryption is handled via `wcdb_api.dll` (`wcdb_decrypt_sns_image`)
because "ISAAC-64 full-file XOR" is not reliably reproducible for images across
different versions/samples.
- Production Moments image/video decryption should prefer the vendored
WxIsaac64/WASM path. This pure-Python implementation is only a fallback when
Node/WASM is unavailable.
- This ISAAC-64 implementation may not perfectly match WxIsaac64; treat it as
best-effort.
"""
+84 -13
View File
@@ -29,6 +29,8 @@ from .media_helpers import _resolve_account_dir, _resolve_account_wxid_dir
logger = logging.getLogger(__name__)
WECHAT_EXECUTABLE_NAMES = ("Weixin.exe", "WeChat.exe")
def _summarize_aes_key(value: Any) -> str:
raw = str(value or "").strip()
@@ -109,19 +111,72 @@ def _resolve_wxid_dir_for_image_key(
raise FileNotFoundError("无法定位该账号的 wxid_dir,请传入有效的 db_storage_path 或先完成数据库解密")
def _normalize_user_path(value: Any) -> str:
raw = str(value or "").strip().strip('"').strip("'")
if not raw:
return ""
try:
return os.path.normpath(os.path.expandvars(raw))
except Exception:
return raw
def _read_wechat_version_from_exe(exe_path: str) -> str:
normalized = _normalize_user_path(exe_path)
if not normalized:
return ""
try:
import win32api
version_info = win32api.GetFileVersionInfo(normalized, "\\")
return (
f"{version_info['FileVersionMS'] >> 16}."
f"{version_info['FileVersionMS'] & 0xFFFF}."
f"{version_info['FileVersionLS'] >> 16}."
f"{version_info['FileVersionLS'] & 0xFFFF}"
)
except Exception:
return ""
def _resolve_manual_wechat_exe_path(wechat_install_path: Optional[str] = None) -> str:
normalized = _normalize_user_path(wechat_install_path)
if not normalized:
return ""
candidate = Path(normalized).expanduser()
executable_names = {name.lower() for name in WECHAT_EXECUTABLE_NAMES}
if candidate.is_file():
if candidate.name.lower() not in executable_names:
raise RuntimeError("手动路径必须指向微信安装目录,或直接指向 Weixin.exe / WeChat.exe")
return str(candidate)
if candidate.is_dir():
for exe_name in WECHAT_EXECUTABLE_NAMES:
exe_path = candidate / exe_name
if exe_path.is_file():
return str(exe_path)
raise RuntimeError("手动指定的微信安装目录中未找到 Weixin.exe 或 WeChat.exe")
raise RuntimeError(f"手动指定的微信安装目录不存在: {candidate}")
# ====================== 以下是hook逻辑 ======================================
class WeChatKeyFetcher:
def __init__(self):
self.process_name = "Weixin.exe"
self.process_names = {name.lower() for name in WECHAT_EXECUTABLE_NAMES}
self.timeout_seconds = 60
def _is_wechat_process(self, name: Any) -> bool:
return str(name or "").strip().lower() in self.process_names
def kill_wechat(self):
"""检测并查杀微信进程"""
killed = False
for proc in psutil.process_iter(['pid', 'name']):
try:
if proc.info['name'] == self.process_name:
if self._is_wechat_process(proc.info['name']):
logger.info(f"Killing WeChat process: {proc.info['pid']}")
proc.terminate()
killed = True
@@ -134,11 +189,14 @@ class WeChatKeyFetcher:
def launch_wechat(self, exe_path: str) -> int:
"""启动微信并返回 PID"""
try:
process = subprocess.Popen(exe_path)
normalized_exe_path = _normalize_user_path(exe_path)
process = subprocess.Popen(normalized_exe_path)
time.sleep(2)
candidates = []
target_process_name = Path(normalized_exe_path).name.lower()
for proc in psutil.process_iter(['pid', 'name', 'create_time']):
if proc.info['name'] == self.process_name:
proc_name = str(proc.info.get('name') or "").strip().lower()
if proc_name == target_process_name or self._is_wechat_process(proc_name):
candidates.append(proc)
if candidates:
@@ -152,19 +210,32 @@ class WeChatKeyFetcher:
logger.error(f"启动微信失败: {e}")
raise RuntimeError(f"无法启动微信: {e}")
def fetch_db_key(self) -> dict:
def fetch_db_key(self, wechat_install_path: Optional[str] = None) -> dict:
"""调用 wx_key 仅获取数据库密钥 (Hook 模式)"""
if wx_key is None:
raise RuntimeError("wx_key 模块未安装或加载失败")
install_info = detect_wechat_installation()
exe_path = install_info.get('wechat_exe_path')
version = install_info.get('wechat_version')
manual_path = _normalize_user_path(wechat_install_path)
if manual_path:
exe_path = _resolve_manual_wechat_exe_path(manual_path)
version = _read_wechat_version_from_exe(exe_path)
logger.info(
"[db_key] 使用手动指定的微信安装路径: input=%s exe_path=%s version=%s",
manual_path,
exe_path,
version or "unknown",
)
else:
install_info = detect_wechat_installation()
exe_path = _normalize_user_path(install_info.get('wechat_exe_path'))
version = str(install_info.get('wechat_version') or "").strip()
if not exe_path or not version:
raise RuntimeError("无法自动定位微信安装路径或版本")
if not exe_path:
raise RuntimeError("无法自动定位微信安装路径,请手动填写微信安装目录")
if not Path(exe_path).is_file():
raise RuntimeError(f"微信可执行文件不存在: {exe_path}")
logger.info(f"Detect WeChat: {version} at {exe_path}")
logger.info(f"Detect WeChat: {version or 'unknown'} at {exe_path}")
self.kill_wechat()
pid = self.launch_wechat(exe_path)
@@ -204,9 +275,9 @@ class WeChatKeyFetcher:
"db_key": found_db_key
}
def get_db_key_workflow():
def get_db_key_workflow(wechat_install_path: Optional[str] = None):
fetcher = WeChatKeyFetcher()
return fetcher.fetch_db_key()
return fetcher.fetch_db_key(wechat_install_path=wechat_install_path)
# ============================== 以下是图片密钥逻辑 =====================================
+44 -5
View File
@@ -1,13 +1,41 @@
import datetime
import json
from pathlib import Path
from typing import Any, Optional
from typing import Any, Iterable, Optional
from .app_paths import get_account_keys_path
_KEY_STORE_PATH = get_account_keys_path()
def normalize_key_store_path(path_value: Optional[str]) -> str:
raw = str(path_value or "").strip()
if not raw:
return ""
try:
return str(Path(raw).expanduser().resolve())
except Exception:
try:
return str(Path(raw).expanduser())
except Exception:
return raw
def _normalize_account_aliases(*values: Optional[str], aliases: Optional[Iterable[str]] = None) -> list[str]:
out: list[str] = []
seen: set[str] = set()
for value in [*values, *(list(aliases or []))]:
key = str(value or "").strip()
if (not key) or (key in seen):
continue
seen.add(key)
out.append(key)
return out
def _atomic_write_json(path: Path, payload: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
@@ -40,25 +68,36 @@ def upsert_account_keys_in_store(
db_key: Optional[str] = None,
image_xor_key: Optional[str] = None,
image_aes_key: Optional[str] = None,
aliases: Optional[Iterable[str]] = None,
db_key_source_wxid_dir: Optional[str] = None,
db_key_source_db_storage_path: Optional[str] = None,
) -> dict[str, Any]:
account = str(account or "").strip()
if not account:
return {}
store = load_account_keys_store()
item = store.get(account, {})
if not isinstance(item, dict):
item = {}
target_accounts = _normalize_account_aliases(account, aliases=aliases)
item: dict[str, Any] = {}
for target_account in target_accounts:
existing = store.get(target_account, {})
if isinstance(existing, dict) and existing:
item = dict(existing)
break
if db_key is not None:
item["db_key"] = str(db_key)
item["db_key_source_wxid_dir"] = normalize_key_store_path(db_key_source_wxid_dir)
item["db_key_source_db_storage_path"] = normalize_key_store_path(db_key_source_db_storage_path)
if image_xor_key is not None:
item["image_xor_key"] = str(image_xor_key)
if image_aes_key is not None:
item["image_aes_key"] = str(image_aes_key)
item["updated_at"] = datetime.datetime.now().isoformat(timespec="seconds")
store[account] = item
for target_account in target_accounts:
store[target_account] = dict(item)
try:
_atomic_write_json(_KEY_STORE_PATH, store)
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,122 @@
// Generate WeChat/WeFlow WxIsaac64 keystream via the vendored WASM module.
//
// Usage:
// node weflow_wasm_keystream.js <key> <size>
//
// Prints a base64-encoded keystream to stdout (no extra logs).
const fs = require('fs')
const path = require('path')
const vm = require('vm')
function usageAndExit() {
process.stderr.write('Usage: node weflow_wasm_keystream.js <key> <size>\\n')
process.exit(2)
}
const key = String(process.argv[2] || '').trim()
const size = Number(process.argv[3] || 0)
if (!key || !Number.isFinite(size) || size <= 0) usageAndExit()
const basePath = __dirname
const wasmPath = path.join(basePath, 'wasm_video_decode.wasm')
const jsPath = path.join(basePath, 'wasm_video_decode.js')
if (!fs.existsSync(wasmPath) || !fs.existsSync(jsPath)) {
process.stderr.write(`Vendored WASM assets not found: ${basePath}\\n`)
process.exit(1)
}
const wasmBinary = fs.readFileSync(wasmPath)
const jsContent = fs.readFileSync(jsPath, 'utf8')
let capturedKeystream = null
let resolveInit
let rejectInit
const initPromise = new Promise((res, rej) => {
resolveInit = res
rejectInit = rej
})
const mockGlobal = {
console: { log: () => {}, error: () => {} },
Buffer,
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array,
Array,
Object,
Function,
String,
Number,
Boolean,
Error,
Promise,
require,
process,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
}
mockGlobal.Module = {
onRuntimeInitialized: () => resolveInit(),
wasmBinary,
print: () => {},
printErr: () => {},
}
mockGlobal.self = mockGlobal
mockGlobal.self.location = { href: jsPath }
mockGlobal.WorkerGlobalScope = function () {}
mockGlobal.VTS_WASM_URL = `file://${wasmPath}`
mockGlobal.wasm_isaac_generate = (ptr, n) => {
const buf = new Uint8Array(mockGlobal.Module.HEAPU8.buffer, ptr, n)
capturedKeystream = new Uint8Array(buf)
}
try {
const context = vm.createContext(mockGlobal)
new vm.Script(jsContent, { filename: jsPath }).runInContext(context)
} catch (e) {
rejectInit(e)
}
;(async () => {
try {
await initPromise
if (!mockGlobal.Module.WxIsaac64 && mockGlobal.Module.asm && mockGlobal.Module.asm.WxIsaac64) {
mockGlobal.Module.WxIsaac64 = mockGlobal.Module.asm.WxIsaac64
}
if (!mockGlobal.Module.WxIsaac64) {
throw new Error('WxIsaac64 not found in WASM module')
}
const alignedSize = Math.ceil(size / 8) * 8
capturedKeystream = null
const isaac = new mockGlobal.Module.WxIsaac64(key)
isaac.generate(alignedSize)
if (isaac.delete) isaac.delete()
if (!capturedKeystream) throw new Error('Failed to capture keystream')
const out = Buffer.from(capturedKeystream)
out.reverse()
process.stdout.write(out.subarray(0, size).toString('base64'))
} catch (e) {
process.stderr.write(String(e && e.stack ? e.stack : e) + '\\n')
process.exit(1)
}
})()
+38 -4
View File
@@ -118,6 +118,38 @@ def _acquire_decrypt_account_guards(accounts: Any, *, reason: str) -> list[tuple
return guards
def _save_db_key_for_account(account: str, key: str, account_result: dict[str, Any] | None) -> None:
payload = dict(account_result or {})
success_count = int(payload.get("success") or 0)
if success_count <= 0:
logger.info("[decrypt] skip saving db key for failed account=%s success=%s", account, success_count)
return
source_wxid_dir = str(payload.get("source_wxid_dir") or "").strip()
source_db_storage_path = str(payload.get("source_db_storage_path") or "").strip()
aliases: list[str] = []
if source_wxid_dir:
wxid_dir_name = str(Path(source_wxid_dir).name or "").strip()
if wxid_dir_name and wxid_dir_name != str(account or "").strip():
aliases.append(wxid_dir_name)
upsert_account_keys_in_store(
str(account),
db_key=key,
aliases=aliases,
db_key_source_wxid_dir=source_wxid_dir or None,
db_key_source_db_storage_path=source_db_storage_path or None,
)
logger.info(
"[decrypt] saved db key account=%s aliases=%s source_wxid_dir=%s source_db_storage_path=%s",
str(account),
aliases,
source_wxid_dir,
source_db_storage_path,
)
class DecryptRequest(BaseModel):
"""解密请求模型"""
@@ -170,8 +202,8 @@ async def decrypt_databases(request: DecryptRequest):
# 成功解密后,按账号保存数据库密钥(用于前端自动回填)
try:
for account_name in (results.get("account_results") or {}).keys():
upsert_account_keys_in_store(str(account_name), db_key=request.key)
for account_name, account_result in (results.get("account_results") or {}).items():
_save_db_key_for_account(str(account_name), request.key, account_result)
except Exception:
pass
@@ -417,6 +449,8 @@ async def decrypt_databases_stream(
"success": account_success,
"failed": len(dbs) - account_success,
"output_dir": str(account_output_dir),
"source_db_storage_path": str(source_db_storage_path),
"source_wxid_dir": str(wxid_dir),
"processed_files": account_processed,
"failed_files": account_failed,
"db_diagnostics": account_db_diagnostics,
@@ -481,8 +515,8 @@ async def decrypt_databases_stream(
# Save db key for frontend autofill.
try:
for account in (account_results or {}).keys():
upsert_account_keys_in_store(str(account), db_key=k)
for account, account_result in (account_results or {}).items():
_save_db_key_for_account(str(account), k, account_result)
except Exception:
pass
+158 -8
View File
@@ -1,9 +1,10 @@
from pathlib import Path
from typing import Optional
from fastapi import APIRouter
from ..logging_config import get_logger
from ..key_store import get_account_keys_from_store
from ..key_store import get_account_keys_from_store, normalize_key_store_path
from ..key_service import get_db_key_workflow, get_image_key_integrated_workflow
from ..media_helpers import _load_media_keys, _resolve_account_dir
from ..path_fix import PathFixRoute
@@ -21,8 +22,106 @@ def _summarize_aes_key(value: str) -> str:
return f"{raw[:4]}...{raw[-4:]}(len={len(raw)})"
def _resolve_requested_wxid_dir(*, db_storage_path: Optional[str] = None, wxid_dir: Optional[str] = None) -> str:
explicit_wxid_dir = str(wxid_dir or "").strip()
if explicit_wxid_dir:
return normalize_key_store_path(explicit_wxid_dir)
raw_db_storage_path = str(db_storage_path or "").strip()
if not raw_db_storage_path:
return ""
candidate = Path(raw_db_storage_path).expanduser()
try:
if str(candidate.name or "").lower() == "db_storage":
return normalize_key_store_path(str(candidate.parent))
except Exception:
pass
try:
if str((candidate / "db_storage").name or "").lower() == "db_storage":
return normalize_key_store_path(str(candidate))
except Exception:
pass
return ""
def _build_saved_key_candidates(account_name: Optional[str], request_account: Optional[str], request_wxid_dir: str) -> list[str]:
out: list[str] = []
seen: set[str] = set()
for value in [
Path(request_wxid_dir).name if request_wxid_dir else "",
str(account_name or "").strip(),
str(request_account or "").strip(),
]:
key = str(value or "").strip()
if (not key) or (key in seen):
continue
seen.add(key)
out.append(key)
return out
def _evaluate_db_key_candidate(
*,
store_account: str,
keys: dict,
account_name: Optional[str],
request_wxid_dir: str,
request_db_storage_path: str,
) -> tuple[bool, int, str]:
db_key = str(keys.get("db_key") or "").strip()
if not db_key:
return False, -1, ""
source_wxid_dir = normalize_key_store_path(keys.get("db_key_source_wxid_dir"))
source_db_storage_path = normalize_key_store_path(keys.get("db_key_source_db_storage_path"))
request_wxid_dir_name = Path(request_wxid_dir).name if request_wxid_dir else ""
source_wxid_dir_name = Path(source_wxid_dir).name if source_wxid_dir else ""
if request_db_storage_path and source_db_storage_path:
if source_db_storage_path == request_db_storage_path:
return True, 400, ""
return (
False,
0,
f"Saved db key source does not match current db_storage_path. request={request_db_storage_path} stored={source_db_storage_path}",
)
if request_wxid_dir and source_wxid_dir:
if (source_wxid_dir == request_wxid_dir) or (
source_wxid_dir_name and source_wxid_dir_name == request_wxid_dir_name
):
return True, 300, ""
return (
False,
0,
f"Saved db key source does not match current wxid_dir. request={request_wxid_dir_name} stored={source_wxid_dir_name or source_wxid_dir}",
)
if request_wxid_dir_name:
if store_account == request_wxid_dir_name:
return True, 200, ""
if account_name and request_wxid_dir_name == str(account_name or "").strip():
return True, 100, ""
return (
False,
0,
f"Legacy saved db key is ambiguous for current wxid_dir={request_wxid_dir_name}. Please fetch a fresh db key.",
)
return True, 50, ""
@router.get("/api/keys", summary="获取账号已保存的密钥")
async def get_saved_keys(account: Optional[str] = None):
async def get_saved_keys(
account: Optional[str] = None,
db_storage_path: Optional[str] = None,
wxid_dir: Optional[str] = None,
):
"""获取账号的数据库密钥与图片密钥(用于前端自动回填)"""
account_name: Optional[str] = None
account_dir = None
@@ -34,16 +133,56 @@ async def get_saved_keys(account: Optional[str] = None):
# 账号可能尚未解密;仍允许从全局 store 读取(如果传入了 account
account_name = str(account or "").strip() or None
request_db_storage_path = normalize_key_store_path(db_storage_path)
request_wxid_dir = _resolve_requested_wxid_dir(db_storage_path=db_storage_path, wxid_dir=wxid_dir)
candidate_accounts = _build_saved_key_candidates(account_name, account, request_wxid_dir)
logger.info(
"[keys] get_saved_keys start: request_account=%s resolved_account=%s account_dir=%s",
"[keys] get_saved_keys start: request_account=%s resolved_account=%s account_dir=%s db_storage_path=%s wxid_dir=%s candidates=%s",
str(account or "").strip(),
str(account_name or ""),
str(account_dir) if account_dir else "",
request_db_storage_path,
request_wxid_dir,
candidate_accounts,
)
keys: dict = {}
if account_name:
keys = get_account_keys_from_store(account_name)
selected_db_key_account = ""
selected_db_key_score = -1
db_key_blocked_reason = ""
db_key_source_wxid_dir = ""
db_key_source_db_storage_path = ""
for candidate_account in candidate_accounts:
candidate_keys = get_account_keys_from_store(candidate_account)
if not isinstance(candidate_keys, dict) or not candidate_keys:
continue
if not str(keys.get("image_xor_key") or "").strip():
keys["image_xor_key"] = str(candidate_keys.get("image_xor_key") or "").strip()
if not str(keys.get("image_aes_key") or "").strip():
keys["image_aes_key"] = str(candidate_keys.get("image_aes_key") or "").strip()
if not str(keys.get("updated_at") or "").strip():
keys["updated_at"] = str(candidate_keys.get("updated_at") or "").strip()
ok, score, blocked_reason = _evaluate_db_key_candidate(
store_account=candidate_account,
keys=candidate_keys,
account_name=account_name,
request_wxid_dir=request_wxid_dir,
request_db_storage_path=request_db_storage_path,
)
if ok and score > selected_db_key_score:
selected_db_key_score = score
selected_db_key_account = candidate_account
keys["db_key"] = str(candidate_keys.get("db_key") or "").strip()
db_key_source_wxid_dir = normalize_key_store_path(candidate_keys.get("db_key_source_wxid_dir"))
db_key_source_db_storage_path = normalize_key_store_path(candidate_keys.get("db_key_source_db_storage_path"))
if str(candidate_keys.get("updated_at") or "").strip():
keys["updated_at"] = str(candidate_keys.get("updated_at") or "").strip()
elif (not ok) and blocked_reason and (not db_key_blocked_reason):
db_key_blocked_reason = blocked_reason
# 兼容:如果 store 里没有图片密钥,尝试从账号目录的 _media_keys.json 读取
if account_dir and isinstance(keys, dict):
@@ -62,11 +201,18 @@ async def get_saved_keys(account: Optional[str] = None):
"image_xor_key": str(keys.get("image_xor_key") or "").strip(),
"image_aes_key": str(keys.get("image_aes_key") or "").strip(),
"updated_at": str(keys.get("updated_at") or "").strip(),
"db_key_source_wxid_dir": db_key_source_wxid_dir,
"db_key_source_db_storage_path": db_key_source_db_storage_path,
"db_key_store_account": selected_db_key_account,
"db_key_blocked_reason": db_key_blocked_reason,
}
logger.info(
"[keys] get_saved_keys done: account=%s db_key_present=%s xor_key=%s aes_key=%s updated_at=%s",
"[keys] get_saved_keys done: account=%s db_key_present=%s db_key_store_account=%s db_key_source_wxid_dir=%s blocked_reason=%s xor_key=%s aes_key=%s updated_at=%s",
str(account_name or ""),
bool(result["db_key"]),
result["db_key_store_account"],
result["db_key_source_wxid_dir"],
result["db_key_blocked_reason"],
result["image_xor_key"],
_summarize_aes_key(result["image_aes_key"]),
result["updated_at"],
@@ -80,7 +226,7 @@ async def get_saved_keys(account: Optional[str] = None):
@router.get("/api/get_keys", summary="自动获取微信数据库与图片密钥")
async def get_wechat_db_key():
async def get_wechat_db_key(wechat_install_path: Optional[str] = None):
"""
自动流程
1. 结束微信进程
@@ -89,7 +235,11 @@ async def get_wechat_db_key():
4. 抓取 DB 图片密钥(AES + XOR)并返回
"""
try:
keys_data = get_db_key_workflow()
logger.info(
"[keys] get_wechat_db_key start: wechat_install_path=%s",
str(wechat_install_path or "").strip(),
)
keys_data = get_db_key_workflow(wechat_install_path=wechat_install_path)
return {
"status": 0,
+5 -729
View File
@@ -1,4 +1,3 @@
from bisect import bisect_left, bisect_right
from functools import lru_cache
from pathlib import Path
import os
@@ -20,7 +19,6 @@ from starlette.background import BackgroundTask
from fastapi import APIRouter, HTTPException
from fastapi.responses import Response, FileResponse # 返回视频文件
from pydantic import BaseModel, Field
from ..chat_helpers import _load_contact_rows, _pick_display_name, _resolve_account_dir
from ..logging_config import get_logger
@@ -44,8 +42,6 @@ logger = get_logger(__name__)
router = APIRouter(route_class=PathFixRoute)
SNS_MEDIA_PICKS_FILE = "_sns_media_picks.json"
_SNS_VIDEO_KEY_RE = re.compile(r'<enc\s+key="(\d+)"', flags=re.IGNORECASE)
_MP_BIZ_RE = re.compile(r"__biz=([A-Za-z0-9_=+-]+)")
_ZSTD_MAGIC = b"\x28\xb5\x2f\xfd"
@@ -860,233 +856,6 @@ def _parse_timeline_xml(xml_text: str, fallback_username: str) -> dict[str, Any]
return out
def _image_size_from_bytes(data: bytes, media_type: str) -> tuple[int, int]:
mt = str(media_type or "").lower()
if mt == "image/png":
# PNG IHDR width/height are stored at byte offsets 16..24
if len(data) >= 24 and data.startswith(b"\x89PNG\r\n\x1a\n"):
try:
w = int.from_bytes(data[16:20], "big")
h = int.from_bytes(data[20:24], "big")
return w, h
except Exception:
return 0, 0
return 0, 0
if mt in {"image/jpeg", "image/jpg"}:
# Minimal JPEG SOF parser.
if len(data) < 4 or (not data.startswith(b"\xFF\xD8")):
return 0, 0
i = 2
while i + 3 < len(data):
if data[i] != 0xFF:
i += 1
continue
# Skip padding 0xFF bytes.
while i < len(data) and data[i] == 0xFF:
i += 1
if i >= len(data):
break
marker = data[i]
i += 1
# Markers without a segment length.
if marker in (0xD8, 0xD9):
continue
if marker == 0xDA: # Start of scan.
break
if i + 1 >= len(data):
break
seg_len = (data[i] << 8) + data[i + 1]
i += 2
if seg_len < 2:
break
# SOF markers which contain width/height.
if marker in {
0xC0,
0xC1,
0xC2,
0xC3,
0xC5,
0xC6,
0xC7,
0xC9,
0xCA,
0xCB,
0xCD,
0xCE,
0xCF,
}:
# segment: [precision(1), height(2), width(2), ...]
if i + 4 < len(data):
try:
h = (data[i + 1] << 8) + data[i + 2]
w = (data[i + 3] << 8) + data[i + 4]
return w, h
except Exception:
return 0, 0
i += seg_len - 2
return 0, 0
return 0, 0
@lru_cache(maxsize=16)
def _sns_img_time_index(wxid_dir_str: str) -> tuple[list[float], list[str]]:
"""Build a (mtime_sorted, path_sorted) index for local Moments cache images.
WeChat stores encrypted SNS cache images under:
`{wxid_dir}/cache/YYYY-MM/Sns/Img/<2hex>/<30hex>`
"""
wxid_dir = Path(str(wxid_dir_str or "").strip())
out: list[tuple[float, str]] = []
cache_root = wxid_dir / "cache"
try:
month_dirs = [p for p in cache_root.iterdir() if p.is_dir()]
except Exception:
month_dirs = []
for mdir in month_dirs:
img_root = mdir / "Sns" / "Img"
try:
if not (img_root.exists() and img_root.is_dir()):
continue
except Exception:
continue
# The Img dir uses a 2-level layout; keep this tight (no global rglob).
try:
for sub in img_root.iterdir():
if not sub.is_dir():
continue
for f in sub.iterdir():
try:
if not f.is_file():
continue
st = f.stat()
out.append((float(st.st_mtime), str(f)))
except Exception:
continue
except Exception:
continue
out.sort(key=lambda x: x[0])
mtimes = [m for m, _p in out]
paths = [_p for _m, _p in out]
return mtimes, paths
def _normalize_hex32(value: Optional[str]) -> str:
"""Return the first 32 hex chars from value, or '' if not present."""
s = str(value or "").strip().lower()
if not s:
return ""
# Keep only hex chars. Some attrs may contain separators or be wrapped.
s = re.sub(r"[^0-9a-f]", "", s)
if len(s) < 32:
return ""
return s[:32]
def _sns_media_picks_path(account_dir: Path) -> Path:
return account_dir / SNS_MEDIA_PICKS_FILE
def _sns_post_id_from_media_key(media_key: str) -> str:
# Frontend stores picks under `${postId}:${idx}`.
s = str(media_key or "").strip()
if not s:
return ""
return s.split(":", 1)[0].strip()
@lru_cache(maxsize=32)
def _load_sns_media_picks_cached(path_str: str, mtime: float) -> dict[str, str]:
p = Path(str(path_str or "").strip())
try:
raw = p.read_text(encoding="utf-8")
except Exception:
return {}
try:
obj = json.loads(raw)
except Exception:
return {}
picks_obj = obj.get("picks") if isinstance(obj, dict) else None
if not isinstance(picks_obj, dict):
return {}
out: dict[str, str] = {}
for k, v in picks_obj.items():
mk = str(k or "").strip()
if not mk:
continue
ck = _normalize_hex32(str(v or ""))
if not ck:
continue
out[mk] = ck
return out
def _load_sns_media_picks(account_dir: Path) -> dict[str, str]:
p = _sns_media_picks_path(account_dir)
try:
st = p.stat()
mtime = float(st.st_mtime)
except Exception:
mtime = 0.0
return _load_sns_media_picks_cached(str(p), mtime)
def _save_sns_media_picks(account_dir: Path, picks: dict[str, str]) -> int:
# Normalize + keep it stable for easier diff/debugging.
out: dict[str, str] = {}
for k, v in (picks or {}).items():
mk = str(k or "").strip()
if not mk:
continue
ck = _normalize_hex32(str(v or ""))
if not ck:
continue
out[mk] = ck
try:
payload = {"updated_at": int(time.time()), "picks": dict(sorted(out.items(), key=lambda x: x[0]))}
_sns_media_picks_path(account_dir).write_text(
json.dumps(payload, ensure_ascii=False, indent=2),
encoding="utf-8",
)
except Exception:
pass
try:
_load_sns_media_picks_cached.cache_clear()
except Exception:
pass
return len(out)
@lru_cache(maxsize=16)
def _sns_img_roots(wxid_dir_str: str) -> tuple[str, ...]:
"""List all month cache roots that contain `Sns/Img`."""
wxid_dir = Path(str(wxid_dir_str or "").strip())
cache_root = wxid_dir / "cache"
try:
month_dirs = [p for p in cache_root.iterdir() if p.is_dir()]
except Exception:
month_dirs = []
roots: list[str] = []
for mdir in month_dirs:
img_root = mdir / "Sns" / "Img"
try:
if img_root.exists() and img_root.is_dir():
roots.append(str(img_root))
except Exception:
continue
# Keep it stable (helps debugging and caching predictability).
roots.sort()
return tuple(roots)
@lru_cache(maxsize=16)
def _sns_video_roots(wxid_dir_str: str) -> tuple[str, ...]:
"""List all month cache roots that contain `Sns/Video`."""
@@ -1139,268 +908,6 @@ def _resolve_sns_cached_video_path(
return None
def _resolve_sns_cached_image_path_by_md5(
*,
wxid_dir: Path,
md5: str,
create_time: int,
) -> Optional[str]:
"""Try to resolve SNS cache image by md5-based cache path layout."""
md5_32 = _normalize_hex32(md5)
if not md5_32:
return None
sub = md5_32[:2]
rest = md5_32[2:]
roots = _sns_img_roots(str(wxid_dir))
if not roots:
return None
best: tuple[float, str] | None = None
for root_str in roots:
try:
p = Path(root_str) / sub / rest
if not (p.exists() and p.is_file()):
continue
# Prefer the cache file closest to the post create_time (if provided),
# otherwise pick the newest one.
st = p.stat()
if create_time > 0:
score = abs(float(st.st_mtime) - float(create_time))
else:
score = -float(st.st_mtime)
if best is None or score < best[0]:
best = (score, str(p))
except Exception:
continue
return best[1] if best else None
def _sns_cache_key_from_path(p: Path) -> str:
"""Return the 32-hex cache key for a SNS cache file path, or ''."""
try:
# cache/.../Sns/Img/<2hex>/<30hex>
key = f"{p.parent.name}{p.name}"
except Exception:
return ""
return _normalize_hex32(key)
def _generate_sns_cache_key(tid: str, media_id: str, media_type: int = 2) -> str:
"""
公式: md5(tid_mediaId_type)
Example: 14852422213384352392_14852422213963625090_2 -> 6d479249ca5a090fab5c42c79bc56b89
"""
if not tid or not media_id:
return ""
raw_key = f"{tid}_{media_id}_{media_type}"
try:
return hashlib.md5(raw_key.encode("utf-8")).hexdigest()
except Exception:
return ""
def _resolve_sns_cached_image_path_by_cache_key(
*,
wxid_dir: Path,
cache_key: str,
create_time: int,
) -> Optional[str]:
"""Resolve SNS cache image by `<2hex>/<30hex>` cache key."""
key32 = _normalize_hex32(cache_key)
if not key32:
return None
sub = key32[:2]
rest = key32[2:]
roots = _sns_img_roots(str(wxid_dir))
if not roots:
return None
best: tuple[float, str] | None = None
for root_str in roots:
try:
p = Path(root_str) / sub / rest
if not (p.exists() and p.is_file()):
continue
st = p.stat()
if create_time > 0:
score = abs(float(st.st_mtime) - float(create_time))
else:
score = -float(st.st_mtime)
if best is None or score < best[0]:
best = (score, str(p))
except Exception:
continue
return best[1] if best else None
@lru_cache(maxsize=4096)
def _resolve_sns_cached_image_path(
*,
account_dir_str: str,
create_time: int,
width: int,
height: int,
idx: int,
total_size: int = 0,
) -> Optional[str]:
"""Best-effort resolve a local cached SNS image for a post+media meta."""
total_size_i = int(total_size or 0)
must_match_size = width > 0 and height > 0
# Without size/total_size, time-only matching is too error-prone and can easily mix images.
if (not must_match_size) and total_size_i <= 0:
return None
account_dir = Path(str(account_dir_str or "").strip())
if not account_dir.exists():
return None
wxid_dir = _resolve_account_wxid_dir(account_dir)
if not wxid_dir:
return None
mtimes, paths = _sns_img_time_index(str(wxid_dir))
if not mtimes:
return None
create_time_i = int(create_time or 0)
if create_time_i > 0:
# We don't know when the image was cached (could be close to create_time, could be hours later).
# Use a generous window but keep it bounded for performance.
window = 72 * 3600 # 72h
lo = create_time_i - window
hi = create_time_i + window
l = bisect_left(mtimes, lo)
r = bisect_right(mtimes, hi)
if l >= r:
# Fallback: search the newest N files if time window has no hits.
l = max(0, len(mtimes) - 800)
r = len(mtimes)
else:
# Missing createTime: only probe the newest cache entries.
l = max(0, len(mtimes) - 800)
r = len(mtimes)
# Rank by time proximity to create_time (or by recency when createTime is missing).
candidates: list[tuple[float, str]] = []
for j in range(l, r):
try:
if create_time_i > 0:
candidates.append((abs(mtimes[j] - float(create_time_i)), paths[j]))
else:
candidates.append((-mtimes[j], paths[j]))
except Exception:
continue
candidates.sort(key=lambda x: x[0])
matched: list[tuple[int, float, str]] = []
# Limit the work per request.
max_probe = 2000 if (r - l) <= 2000 else 2000
for _diff, pstr in candidates[:max_probe]:
try:
p = Path(pstr)
payload, media_type = _read_and_maybe_decrypt_media(p, account_dir)
if not payload or not str(media_type or "").startswith("image/"):
continue
if must_match_size:
w0, h0 = _image_size_from_bytes(payload, str(media_type or ""))
if (w0, h0) != (width, height):
continue
size_diff = abs(len(payload) - total_size_i) if total_size_i > 0 else 0
# When totalSize is available, it tends to be a stronger discriminator than mtime.
matched.append((int(size_diff), float(_diff), pstr))
except Exception:
continue
if not matched:
return None
if must_match_size:
matched.sort(key=lambda x: (x[0], x[1], x[2]))
# If we have totalSize, treat it as a strong discriminator and always take the best match.
if total_size_i > 0:
return matched[0][2]
idx0 = max(0, int(idx or 0))
return matched[idx0][2] if idx0 < len(matched) else None
# No size: only return a best-effort match when totalSize is available.
if total_size_i > 0:
matched.sort(key=lambda x: (x[0], x[1], x[2]))
return matched[0][2]
return None
@lru_cache(maxsize=2048)
def _list_sns_cached_image_candidate_keys(
*,
account_dir_str: str,
create_time: int,
width: int,
height: int,
) -> tuple[str, ...]:
"""List local SNS cache candidates (as 32-hex cache keys) for a media item.
The ordering matches `_resolve_sns_cached_image_path()`'s scan order, so `idx`
is stable within the same (account, create_time, width, height) input.
"""
if create_time <= 0 or width <= 0 or height <= 0:
return tuple()
account_dir = Path(str(account_dir_str or "").strip())
if not account_dir.exists():
return tuple()
wxid_dir = _resolve_account_wxid_dir(account_dir)
if not wxid_dir:
return tuple()
mtimes, paths = _sns_img_time_index(str(wxid_dir))
if not mtimes:
return tuple()
window = 72 * 3600 # 72h
lo = create_time - window
hi = create_time + window
l = bisect_left(mtimes, lo)
r = bisect_right(mtimes, hi)
if l >= r:
l = max(0, len(mtimes) - 800)
r = len(mtimes)
candidates: list[tuple[float, str]] = []
for j in range(l, r):
try:
candidates.append((abs(mtimes[j] - float(create_time)), paths[j]))
except Exception:
continue
candidates.sort(key=lambda x: x[0])
max_probe = 2000 if (r - l) <= 2000 else 2000
out: list[str] = []
seen: set[str] = set()
for _diff, pstr in candidates[:max_probe]:
try:
p = Path(pstr)
payload, media_type = _read_and_maybe_decrypt_media(p, account_dir)
if not payload or not str(media_type or "").startswith("image/"):
continue
w0, h0 = _image_size_from_bytes(payload, str(media_type or ""))
if (w0, h0) != (width, height):
continue
key = _sns_cache_key_from_path(p)
if not key or key in seen:
continue
seen.add(key)
out.append(key)
except Exception:
continue
return tuple(out)
def _get_sns_covers(account_dir: Path, target_wxid: str, limit: int = 20) -> list[dict[str, Any]]:
"""无论多古老,强行揪出用户的朋友圈封面历史 (type=7)。
@@ -2560,6 +2067,8 @@ def list_sns_users(
post_count = int(r["postCount"] or 0)
except Exception:
post_count = 0
if post_count <= 0:
continue
row = contact_rows.get(uname)
display = _clean_name(_pick_display_name(row, uname)) or uname
@@ -2575,47 +2084,6 @@ def list_sns_users(
return {"items": items, "count": len(items), "limit": lim}
class SnsMediaPicksSaveRequest(BaseModel):
account: Optional[str] = Field(None, description="账号目录名(可选,默认使用第一个)")
picks: dict[str, str] = Field(default_factory=dict, description="手动匹配表:`${postId}:${idx}` -> 32hex cacheKey")
@router.post("/api/sns/media_picks", summary="保存朋友圈图片手动匹配结果(本机)")
async def save_sns_media_picks(request: SnsMediaPicksSaveRequest):
account_dir = _resolve_account_dir(request.account)
count = _save_sns_media_picks(account_dir, request.picks or {})
return {"status": "success", "count": int(count)}
@router.get("/api/sns/media_candidates", summary="获取朋友圈图片本地缓存候选")
def list_sns_media_candidates(
account: Optional[str] = None,
create_time: int = 0,
width: int = 0,
height: int = 0,
limit: int = 24,
offset: int = 0,
):
if limit <= 0:
raise HTTPException(status_code=400, detail="Invalid limit.")
if limit > 200:
limit = 200
if offset < 0:
offset = 0
account_dir = _resolve_account_dir(account)
keys = _list_sns_cached_image_candidate_keys(
account_dir_str=str(account_dir),
create_time=int(create_time or 0),
width=int(width or 0),
height=int(height or 0),
)
total = len(keys)
end = min(total, offset + limit)
items = [{"idx": i, "key": keys[i]} for i in range(offset, end)]
return {"count": total, "items": items, "hasMore": end < total, "limit": limit, "offset": offset}
def _is_allowed_sns_media_host(host: str) -> bool:
return _sns_media.is_allowed_sns_media_host(host)
@@ -2902,10 +2370,7 @@ async def _try_fetch_and_decrypt_sns_remote(
token: str,
use_cache: bool,
) -> Optional[Response]:
"""Try remote download+decrypt first (accurate when keys are present).
Returns a Response on success, or None on failure so caller can fall back to local cache matching.
"""
"""Try remote download+decrypt first (accurate when keys are present)."""
res = await _sns_media.try_fetch_and_decrypt_sns_image_remote(
account_dir=account_dir,
url=str(url or ""),
@@ -2918,34 +2383,18 @@ async def _try_fetch_and_decrypt_sns_remote(
resp = Response(content=res.payload, media_type=res.media_type)
resp.headers["Cache-Control"] = "public, max-age=86400" if use_cache else "no-store"
resp.headers["X-SNS-Source"] = str(res.source or "remote")
if res.x_enc:
resp.headers["X-SNS-X-Enc"] = str(res.x_enc)
return resp
@router.get("/api/sns/media", summary="获取朋友圈图片(下载解密优先)")
async def get_sns_media(
account: Optional[str] = None,
create_time: int = 0,
width: int = 0,
height: int = 0,
total_size: int = 0,
idx: int = 0,
avoid_picked: int = 0,
post_id: Optional[str] = None,
media_id: Optional[str] = None,
post_type: int = 1,
media_type: int = 2,
pick: Optional[str] = None,
md5: Optional[str] = None,
token: Optional[str] = None,
key: Optional[str] = None,
use_cache: int = 1,
url: Optional[str] = None,
):
account_dir = _resolve_account_dir(account)
wxid_dir = _resolve_account_wxid_dir(account_dir)
try:
use_cache_flag = bool(int(use_cache or 1))
@@ -2963,179 +2412,7 @@ async def get_sns_media(
if remote_resp is not None:
return remote_resp
# Cache disabled: do not fall back to local cache heuristics.
if not use_cache_flag:
raise HTTPException(status_code=404, detail="SNS media not found (cache disabled).")
if wxid_dir and post_id and media_id:
if int(post_type) == 7:
raw_key = f"{post_id}_{media_id}_4" # 硬编码
md5_str = hashlib.md5(raw_key.encode("utf-8")).hexdigest()
bkg_path = wxid_dir / "business" / "sns" / "bkg" / md5_str[:2] / md5_str
if bkg_path.exists() and bkg_path.is_file():
print(f"===== Hit Bkg Cover ======= {bkg_path}")
return FileResponse(bkg_path, media_type="image/jpeg",
headers={"Cache-Control": "public, max-age=31536000", "X-SNS-Source": "bkg-cover"})
exact_match_path = None
hit_type = ""
# 尝试 1: 使用 post_type 计算 MD5
key_post = _generate_sns_cache_key(post_id, media_id, post_type)
exact_match_path = _resolve_sns_cached_image_path_by_cache_key(
wxid_dir=wxid_dir,
cache_key=key_post,
create_time=0
)
if exact_match_path:
hit_type = "post_type"
# 尝试 2: 如果没找到,并且 media_type 和 post_type 不一样,再试一次
if not exact_match_path and post_type != media_type:
key_media = _generate_sns_cache_key(post_id, media_id, media_type)
exact_match_path = _resolve_sns_cached_image_path_by_cache_key(
wxid_dir=wxid_dir,
cache_key=key_media,
create_time=0
)
if exact_match_path:
hit_type = "media_type"
# 如果通过这两种精确定位找到了文件,直接返回
if exact_match_path:
print(f"=====exact_match_path======={exact_match_path}============= (Hit: {hit_type})")
try:
payload, mtype = _read_and_maybe_decrypt_media(Path(exact_match_path), account_dir)
if payload and str(mtype or "").startswith("image/"):
resp = Response(content=payload, media_type=str(mtype or "image/jpeg"))
resp.headers["Cache-Control"] = "public, max-age=31536000"
resp.headers["X-SNS-Source"] = "deterministic-hash"
# 在 Header 里塞入到底是哪个 type 命中的,方便 F12 调试
resp.headers["X-SNS-Hit-Type"] = hit_type
return resp
except Exception:
pass
print("no exact match path, falling back...")
# 0) User-picked cache key override (stable across candidate ordering).
pick_key = _normalize_hex32(pick)
if pick_key:
wxid_dir = _resolve_account_wxid_dir(account_dir)
if wxid_dir:
local = _resolve_sns_cached_image_path_by_cache_key(
wxid_dir=wxid_dir,
cache_key=pick_key,
create_time=int(create_time or 0),
)
if local:
try:
payload, media_type = _read_and_maybe_decrypt_media(Path(local), account_dir)
if payload and str(media_type or "").startswith("image/"):
resp = Response(content=payload, media_type=str(media_type or "image/jpeg"))
resp.headers["Cache-Control"] = "public, max-age=86400"
resp.headers["X-SNS-Source"] = "manual-pick"
return resp
except Exception:
pass
# Optional: avoid using a cache image that was manually pinned to another post.
# Only applies when frontend enables this setting and the current media has no explicit `pick`.
try:
avoid_flag = bool(int(avoid_picked or 0))
except Exception:
avoid_flag = False
cur_post_id = str(post_id or "").strip()
reserved_other: set[str] = set()
if avoid_flag and (not pick_key) and cur_post_id:
picks_map = _load_sns_media_picks(account_dir)
for mk, ck in (picks_map or {}).items():
pid = _sns_post_id_from_media_key(mk)
if not pid or pid == cur_post_id:
continue
if ck:
reserved_other.add(str(ck))
# 1) Try local decrypted cache first (works for old posts where CDN URLs return placeholders).
local = _resolve_sns_cached_image_path(
account_dir_str=str(account_dir),
create_time=int(create_time or 0),
width=int(width or 0),
height=int(height or 0),
idx=max(0, int(idx or 0)),
total_size=int(total_size or 0),
)
if local and reserved_other:
try:
ck0 = _sns_cache_key_from_path(Path(local))
if ck0 and ck0 in reserved_other:
local = None
except Exception:
pass
if local:
try:
payload, media_type = _read_and_maybe_decrypt_media(Path(local), account_dir)
if payload and str(media_type or "").startswith("image/"):
resp = Response(content=payload, media_type=str(media_type or "image/jpeg"))
resp.headers["Cache-Control"] = "public, max-age=86400"
resp.headers["X-SNS-Source"] = "local-heuristic"
return resp
except Exception:
pass
# 1.5) If enabled, and the default match was skipped (or not found), pick the next candidate
# that is not reserved by a manual pick on another post.
if reserved_other and int(create_time or 0) > 0 and int(width or 0) > 0 and int(height or 0) > 0:
wxid_dir = _resolve_account_wxid_dir(account_dir)
if wxid_dir:
keys = _list_sns_cached_image_candidate_keys(
account_dir_str=str(account_dir),
create_time=int(create_time or 0),
width=int(width or 0),
height=int(height or 0),
)
base_idx = max(0, int(idx or 0))
for ck in keys[base_idx:]:
if not ck or ck in reserved_other:
continue
local2 = _resolve_sns_cached_image_path_by_cache_key(
wxid_dir=wxid_dir,
cache_key=str(ck),
create_time=int(create_time or 0),
)
if not local2:
continue
try:
payload, media_type = _read_and_maybe_decrypt_media(Path(local2), account_dir)
if payload and str(media_type or "").startswith("image/"):
resp = Response(content=payload, media_type=str(media_type or "image/jpeg"))
resp.headers["Cache-Control"] = "public, max-age=86400"
resp.headers["X-SNS-Source"] = "local-heuristic-next"
return resp
except Exception:
continue
# 2) Fallback to the remote URL (may still return a Tencent placeholder image).
u = str(url or "").strip()
if not u:
raise HTTPException(status_code=404, detail="SNS media not found.")
# Delay-import to avoid pulling requests machinery during normal timeline listing.
from .chat_media import proxy_image # pylint: disable=import-outside-toplevel
try:
resp0 = await proxy_image(u)
try:
resp0.headers["X-SNS-Source"] = "proxy"
except Exception:
pass
return resp0
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=502, detail=f"Fetch sns media failed: {e}")
raise HTTPException(status_code=404, detail="SNS media not found.")
@router.get("/api/sns/article_thumb", summary="提取公众号文章封面图")
@@ -3197,8 +2474,7 @@ async def get_sns_video_remote(
if path is None:
raise HTTPException(status_code=404, detail="SNS remote video not found.")
headers = {"X-SNS-Source": "remote-video-cache" if use_cache_flag else "remote-video"}
headers["Cache-Control"] = "public, max-age=86400" if use_cache_flag else "no-store"
headers = {"Cache-Control": "public, max-age=86400" if use_cache_flag else "no-store"}
if use_cache_flag:
return FileResponse(str(path), media_type="video/mp4", headers=headers)
@@ -13,23 +13,26 @@ from ..sns_export_service import SNS_EXPORT_MANAGER
router = APIRouter(route_class=PathFixRoute)
ExportScope = Literal["selected", "all"]
ExportFormat = Literal["html", "json", "txt"]
class SnsExportCreateRequest(BaseModel):
account: Optional[str] = Field(None, description="账号目录名(可选,默认使用第一个)")
scope: ExportScope = Field("selected", description="导出范围:selected=指定联系人;all=全部联系人")
usernames: list[str] = Field(default_factory=list, description="朋友圈 username 列表(scope=selected 时使用)")
format: ExportFormat = Field("html", description="导出格式:html/json/txt")
use_cache: bool = Field(True, description="是否复用导出过程中的本地缓存(默认开启)")
output_dir: Optional[str] = Field(None, description="导出目录绝对路径(可选;不填时使用默认目录)")
file_name: Optional[str] = Field(None, description="导出 zip 文件名(可选,不含/含 .zip 都可)")
@router.post("/api/sns/exports", summary="创建朋友圈导出任务(离线 HTML zip")
@router.post("/api/sns/exports", summary="创建朋友圈导出任务(离线 ZIP,支持 HTML/JSON/TXT")
async def create_sns_export(req: SnsExportCreateRequest):
job = SNS_EXPORT_MANAGER.create_job(
account=req.account,
scope=req.scope,
usernames=req.usernames,
export_format=req.format,
use_cache=bool(req.use_cache),
output_dir=req.output_dir,
file_name=req.file_name,
@@ -111,4 +114,3 @@ async def cancel_sns_export(export_id: str):
if not ok:
raise HTTPException(status_code=404, detail="Export not found.")
return {"status": "success"}
File diff suppressed because it is too large Load Diff
+35 -10
View File
@@ -8,8 +8,8 @@ so it can be reused by:
- Offline export (`sns_export_service.py`)
Important notes (empirical, matches current repo behavior):
- SNS images: prefer `wcdb_api.dll` export `wcdb_decrypt_sns_image` (black-box). Pure ISAAC64
keystream XOR is NOT reliable for images across versions.
- SNS images: match WeFlow's Electron implementation by generating the WxIsaac64
keystream from WASM and XORing the full payload in-memory.
- SNS videos: encrypted only for the first 128KB; decrypt via WeFlow's WxIsaac64 (WASM keystream)
and XOR in-place.
"""
@@ -31,9 +31,11 @@ import httpx
from fastapi import HTTPException
from .logging_config import get_logger
from .wcdb_realtime import decrypt_sns_image as _wcdb_decrypt_sns_image
logger = get_logger(__name__)
_PACKAGE_DIR = Path(__file__).resolve().parent
_NATIVE_DIR = _PACKAGE_DIR / "native"
_WEFLOW_WASM_DIR = _NATIVE_DIR / "weflow_wasm"
def is_allowed_sns_media_host(host: str) -> bool:
@@ -96,11 +98,16 @@ def _detect_mp4_ftyp(head: bytes) -> bool:
@lru_cache(maxsize=1)
def _weflow_wxisaac64_script_path() -> str:
"""Locate the Node helper that wraps WeFlow's wasm_video_decode.* assets."""
repo_root = Path(__file__).resolve().parents[2]
script = repo_root / "tools" / "weflow_wasm_keystream.js"
if script.exists() and script.is_file():
return str(script)
"""Locate the bundled Node helper that wraps the vendored wasm_video_decode.* assets."""
bundled = _WEFLOW_WASM_DIR / "weflow_wasm_keystream.js"
if bundled.exists() and bundled.is_file():
return str(bundled)
# Development fallback: allow the repo-level helper to proxy into the vendored assets.
repo_root = _PACKAGE_DIR.parents[1]
legacy = repo_root / "tools" / "weflow_wasm_keystream.js"
if legacy.exists() and legacy.is_file():
return str(legacy)
return ""
@@ -416,6 +423,24 @@ def detect_image_mime(data: bytes) -> str:
return ""
def weflow_decrypt_sns_image_bytes(payload: bytes, key: str) -> bytes:
"""Decrypt a Moments image with the same full-file XOR flow that WeFlow uses."""
raw = bytes(payload or b"")
key_text = str(key or "").strip()
if not raw or not key_text:
return raw
ks = weflow_wxisaac64_keystream(key_text, len(raw))
if not ks:
return raw
out = bytearray(raw)
n = min(len(out), len(ks))
for i in range(n):
out[i] ^= ks[i]
return bytes(out)
_SNS_REMOTE_CACHE_EXTS = [
".jpg",
".jpeg",
@@ -558,7 +583,7 @@ async def try_fetch_and_decrypt_sns_image_remote(
token: str,
use_cache: bool,
) -> Optional[SnsRemoteImageResult]:
"""Try WeFlow-style: download from CDN -> decrypt via wcdb_decrypt_sns_image -> return bytes.
"""Try WeFlow-style: download from CDN -> WxIsaac64 full-file XOR -> return bytes.
Returns a SnsRemoteImageResult on success, or None on failure so caller can fall back to
local cache matching logic.
@@ -652,7 +677,7 @@ async def try_fetch_and_decrypt_sns_image_remote(
if need_decrypt:
try:
decoded2 = _wcdb_decrypt_sns_image(raw, k)
decoded2 = weflow_decrypt_sns_image_bytes(raw, k)
mt2 = detect_image_mime(decoded2)
if mt2:
decoded = decoded2
@@ -1,63 +0,0 @@
import re
from collections.abc import MutableMapping
def add_sns_stage_timing_headers(
headers: MutableMapping[str, str],
*,
source: str,
hit_type: str = "",
x_enc: str = "",
) -> None:
"""Inject `Server-Timing` + `Timing-Allow-Origin` for SNS media stage inspection.
The frontend can't read `<img>` response headers, but browsers expose `Server-Timing` metrics
via `performance.getEntriesByName(...).serverTiming` when `Timing-Allow-Origin` allows it.
This helper is intentionally side-effect free beyond mutating `headers`.
"""
src = str(source or "").strip()
if not src:
return
ht = str(hit_type or "").strip()
xe = str(x_enc or "").strip()
if "Timing-Allow-Origin" not in headers:
headers["Timing-Allow-Origin"] = "*"
def _esc(v: str) -> str:
return v.replace("\\", "\\\\").replace('"', '\\"')
def _token(v: str) -> str:
raw = str(v or "").strip()
if not raw:
return ""
raw = raw.replace(" ", "_")
safe = re.sub(r"[^0-9A-Za-z_.-]+", "_", raw).strip("_")
if not safe:
return ""
return safe[:64]
parts: list[str] = []
src_tok = _token(src) or "unknown"
parts.append(f'sns_source_{src_tok};dur=0;desc="{_esc(src)}"')
if ht:
ht_tok = _token(ht)
if ht_tok:
parts.append(f'sns_hit_{ht_tok};dur=0;desc="{_esc(ht)}"')
if xe:
xe_tok = _token(xe)
if xe_tok:
parts.append(f'sns_xenc_{xe_tok};dur=0;desc="{_esc(xe)}"')
existing = str(headers.get("Server-Timing") or "").strip()
# Some responses may already have upstream `Server-Timing` metrics. Always append ours so
# the frontend can consistently read `sns_source_*` via ResourceTiming.serverTiming.
if existing and re.search(r"(^|,\\s*)sns_source(_|;)", existing):
return
combined = ", ".join(parts)
headers["Server-Timing"] = f"{existing}, {combined}" if existing else combined
@@ -731,6 +731,8 @@ def decrypt_wechat_databases(db_storage_path: str = None, key: str = None) -> di
"success": account_success,
"failed": len(databases) - account_success,
"output_dir": str(account_output_dir),
"source_db_storage_path": str(source_db_storage_path),
"source_wxid_dir": str(wxid_dir),
"processed_files": account_processed,
"failed_files": account_failed,
"db_diagnostics": account_db_diagnostics,
+53 -42
View File
@@ -7,6 +7,7 @@ import unittest
import importlib
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
@@ -64,35 +65,43 @@ class TestDecryptStreamSSE(unittest.TestCase):
client = TestClient(app)
events: list[dict] = []
with client.stream(
"GET",
"/api/decrypt_stream",
params={"key": "00" * 32, "db_storage_path": str(db_storage)},
) as resp:
self.assertEqual(resp.status_code, 200)
self.assertIn("text/event-stream", resp.headers.get("content-type", ""))
with mock.patch.object(decrypt_router, "upsert_account_keys_in_store") as upsert_mock:
with client.stream(
"GET",
"/api/decrypt_stream",
params={"key": "00" * 32, "db_storage_path": str(db_storage)},
) as resp:
self.assertEqual(resp.status_code, 200)
self.assertIn("text/event-stream", resp.headers.get("content-type", ""))
for line in resp.iter_lines():
if not line:
continue
if isinstance(line, bytes):
line = line.decode("utf-8", errors="ignore")
line = str(line)
for line in resp.iter_lines():
if not line:
continue
if isinstance(line, bytes):
line = line.decode("utf-8", errors="ignore")
line = str(line)
if line.startswith(":"):
continue
if not line.startswith("data: "):
continue
payload = json.loads(line[len("data: ") :])
events.append(payload)
if payload.get("type") in {"complete", "error"}:
break
if line.startswith(":"):
continue
if not line.startswith("data: "):
continue
payload = json.loads(line[len("data: ") :])
events.append(payload)
if payload.get("type") in {"complete", "error"}:
break
types = {e.get("type") for e in events}
self.assertIn("start", types)
self.assertIn("progress", types)
self.assertEqual(events[-1].get("type"), "complete")
self.assertEqual(events[-1].get("status"), "completed")
upsert_mock.assert_called_once_with(
"wxid_foo",
db_key="00" * 32,
aliases=["wxid_foo_bar"],
db_key_source_wxid_dir=str(db_storage.parent),
db_key_source_db_storage_path=str(db_storage),
)
out = root / "output" / "databases" / "wxid_foo" / "MSG0.db"
self.assertTrue(out.exists())
@@ -135,35 +144,37 @@ class TestDecryptStreamSSE(unittest.TestCase):
client = TestClient(app)
events: list[dict] = []
with client.stream(
"GET",
"/api/decrypt_stream",
params={"key": "00" * 32, "db_storage_path": str(db_storage)},
) as resp:
self.assertEqual(resp.status_code, 200)
self.assertIn("text/event-stream", resp.headers.get("content-type", ""))
with mock.patch.object(decrypt_router, "upsert_account_keys_in_store") as upsert_mock:
with client.stream(
"GET",
"/api/decrypt_stream",
params={"key": "00" * 32, "db_storage_path": str(db_storage)},
) as resp:
self.assertEqual(resp.status_code, 200)
self.assertIn("text/event-stream", resp.headers.get("content-type", ""))
for line in resp.iter_lines():
if not line:
continue
if isinstance(line, bytes):
line = line.decode("utf-8", errors="ignore")
line = str(line)
for line in resp.iter_lines():
if not line:
continue
if isinstance(line, bytes):
line = line.decode("utf-8", errors="ignore")
line = str(line)
if line.startswith(":"):
continue
if not line.startswith("data: "):
continue
payload = json.loads(line[len("data: ") :])
events.append(payload)
if payload.get("type") in {"complete", "error"}:
break
if line.startswith(":"):
continue
if not line.startswith("data: "):
continue
payload = json.loads(line[len("data: ") :])
events.append(payload)
if payload.get("type") in {"complete", "error"}:
break
self.assertEqual(events[-1].get("type"), "complete")
self.assertEqual(events[-1].get("status"), "failed")
self.assertEqual(events[-1].get("success_count"), 0)
self.assertEqual(events[-1].get("failure_count"), 1)
self.assertIn("密钥可能不匹配", str(events[-1].get("message") or ""))
upsert_mock.assert_not_called()
out = root / "output" / "databases" / "wxid_bad" / "MSG0.db"
self.assertFalse(out.exists())
@@ -0,0 +1,115 @@
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
import wechat_decrypt_tool.key_service as key_service
class _FakeWxKey:
def __init__(self, key: str) -> None:
self.key = key
self.initialize_calls: list[int] = []
self.cleanup_calls = 0
def initialize_hook(self, pid: int) -> bool:
self.initialize_calls.append(pid)
return True
def get_last_error_msg(self) -> str:
return ""
def poll_key_data(self):
return {"key": self.key}
def get_status_message(self):
return None, None
def cleanup_hook(self) -> None:
self.cleanup_calls += 1
class TestKeyServiceManualWechatInstallPath(unittest.TestCase):
def test_get_db_key_workflow_can_use_manual_install_directory(self) -> None:
fake_wx_key = _FakeWxKey("a" * 64)
with TemporaryDirectory() as temp_dir:
install_dir = Path(temp_dir)
exe_path = install_dir / "WeChat.exe"
exe_path.write_bytes(b"")
with mock.patch.object(
key_service,
"wx_key",
fake_wx_key,
), mock.patch.object(
key_service,
"detect_wechat_installation",
side_effect=AssertionError("should not auto-detect when manual path is provided"),
), mock.patch.object(
key_service,
"_read_wechat_version_from_exe",
return_value="",
), mock.patch.object(
key_service.WeChatKeyFetcher,
"kill_wechat",
autospec=True,
) as kill_mock, mock.patch.object(
key_service.WeChatKeyFetcher,
"launch_wechat",
autospec=True,
return_value=4321,
) as launch_mock:
result = key_service.get_db_key_workflow(wechat_install_path=str(install_dir))
self.assertEqual(result["db_key"], "a" * 64)
kill_mock.assert_called_once()
launch_mock.assert_called_once()
_, used_exe_path = launch_mock.call_args.args
self.assertEqual(used_exe_path, str(exe_path))
self.assertEqual(fake_wx_key.initialize_calls, [4321])
self.assertEqual(fake_wx_key.cleanup_calls, 1)
def test_get_db_key_workflow_does_not_require_detected_version(self) -> None:
fake_wx_key = _FakeWxKey("b" * 64)
with TemporaryDirectory() as temp_dir:
exe_path = Path(temp_dir) / "Weixin.exe"
exe_path.write_bytes(b"")
with mock.patch.object(
key_service,
"wx_key",
fake_wx_key,
), mock.patch.object(
key_service,
"detect_wechat_installation",
return_value={
"wechat_exe_path": str(exe_path),
"wechat_version": "",
},
), mock.patch.object(
key_service.WeChatKeyFetcher,
"kill_wechat",
autospec=True,
), mock.patch.object(
key_service.WeChatKeyFetcher,
"launch_wechat",
autospec=True,
return_value=2468,
):
result = key_service.get_db_key_workflow()
self.assertEqual(result["db_key"], "b" * 64)
self.assertEqual(fake_wx_key.initialize_calls, [2468])
self.assertEqual(fake_wx_key.cleanup_calls, 1)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,107 @@
import asyncio
import importlib
import logging
import os
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
def _close_logging_handlers() -> None:
for logger_name in ("", "uvicorn", "uvicorn.access", "uvicorn.error", "fastapi"):
lg = logging.getLogger(logger_name)
for handler in lg.handlers[:]:
try:
handler.close()
except Exception:
pass
try:
lg.removeHandler(handler)
except Exception:
pass
class TestSavedDbKeySourceValidation(unittest.TestCase):
def test_get_saved_keys_blocks_legacy_db_key_for_suffixed_wxid_dir(self) -> None:
with TemporaryDirectory() as td:
root = Path(td)
db_storage = root / "xwechat_files" / "wxid_demo_abcd" / "db_storage"
db_storage.mkdir(parents=True, exist_ok=True)
prev_data_dir = os.environ.get("WECHAT_TOOL_DATA_DIR")
try:
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
import wechat_decrypt_tool.app_paths as app_paths
import wechat_decrypt_tool.key_store as key_store
import wechat_decrypt_tool.routers.keys as keys_router
importlib.reload(app_paths)
importlib.reload(key_store)
importlib.reload(keys_router)
key_store.upsert_account_keys_in_store("wxid_demo", db_key="A" * 64)
result = asyncio.run(
keys_router.get_saved_keys(account="wxid_demo", db_storage_path=str(db_storage))
)
self.assertEqual(result["status"], "success")
self.assertEqual(result["keys"]["db_key"], "")
self.assertIn("Legacy saved db key is ambiguous", result["keys"]["db_key_blocked_reason"])
finally:
_close_logging_handlers()
if prev_data_dir is None:
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
else:
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data_dir
def test_get_saved_keys_accepts_source_matched_db_key(self) -> None:
with TemporaryDirectory() as td:
root = Path(td)
db_storage = root / "xwechat_files" / "wxid_demo_abcd" / "db_storage"
db_storage.mkdir(parents=True, exist_ok=True)
prev_data_dir = os.environ.get("WECHAT_TOOL_DATA_DIR")
try:
os.environ["WECHAT_TOOL_DATA_DIR"] = str(root)
import wechat_decrypt_tool.app_paths as app_paths
import wechat_decrypt_tool.key_store as key_store
import wechat_decrypt_tool.routers.keys as keys_router
importlib.reload(app_paths)
importlib.reload(key_store)
importlib.reload(keys_router)
key_store.upsert_account_keys_in_store(
"wxid_demo",
db_key="B" * 64,
aliases=["wxid_demo_abcd"],
db_key_source_wxid_dir=str(db_storage.parent),
db_key_source_db_storage_path=str(db_storage),
)
result = asyncio.run(
keys_router.get_saved_keys(account="wxid_demo", db_storage_path=str(db_storage))
)
self.assertEqual(result["status"], "success")
self.assertEqual(result["keys"]["db_key"], "B" * 64)
self.assertEqual(result["keys"]["db_key_store_account"], "wxid_demo_abcd")
self.assertEqual(result["keys"]["db_key_source_wxid_dir"], str(db_storage.parent))
self.assertEqual(result["keys"]["db_key_source_db_storage_path"], str(db_storage))
self.assertEqual(result["keys"]["db_key_blocked_reason"], "")
finally:
_close_logging_handlers()
if prev_data_dir is None:
os.environ.pop("WECHAT_TOOL_DATA_DIR", None)
else:
os.environ["WECHAT_TOOL_DATA_DIR"] = prev_data_dir
if __name__ == "__main__":
unittest.main()
+16 -3
View File
@@ -15,6 +15,20 @@ from wechat_decrypt_tool import sns_media # noqa: E402 pylint: disable=wrong-i
class TestSnsMedia(unittest.TestCase):
def test_weflow_wxisaac64_script_path_uses_bundled_helper(self):
sns_media._weflow_wxisaac64_script_path.cache_clear()
script = sns_media._weflow_wxisaac64_script_path()
self.assertTrue(script)
script_path = Path(script)
normalized = script.replace("\\", "/")
self.assertTrue(script_path.exists())
self.assertEqual(script_path.name, "weflow_wasm_keystream.js")
self.assertIn("/src/wechat_decrypt_tool/native/weflow_wasm/", normalized)
self.assertNotIn("/WeFlow/", normalized)
self.assertTrue((script_path.parent / "wasm_video_decode.js").exists())
self.assertTrue((script_path.parent / "wasm_video_decode.wasm").exists())
def test_fix_sns_cdn_url_image_rewrites_150_and_appends_token(self):
u = "http://mmsns.qpic.cn/sns/abc/150"
out = sns_media.fix_sns_cdn_url(u, token="tkn", is_video=False)
@@ -131,7 +145,7 @@ class TestSnsMedia(unittest.TestCase):
account_dir.mkdir(parents=True, exist_ok=True)
with mock.patch("wechat_decrypt_tool.sns_media._download_sns_remote_bytes", side_effect=fake_download):
with mock.patch("wechat_decrypt_tool.sns_media._wcdb_decrypt_sns_image", return_value=decoded):
with mock.patch("wechat_decrypt_tool.sns_media.weflow_decrypt_sns_image_bytes", return_value=decoded):
res = asyncio.run(
sns_media.try_fetch_and_decrypt_sns_image_remote(
account_dir=account_dir,
@@ -161,7 +175,7 @@ class TestSnsMedia(unittest.TestCase):
account_dir.mkdir(parents=True, exist_ok=True)
with mock.patch("wechat_decrypt_tool.sns_media._download_sns_remote_bytes", side_effect=fake_download):
with mock.patch("wechat_decrypt_tool.sns_media._wcdb_decrypt_sns_image", return_value=decoded_bad):
with mock.patch("wechat_decrypt_tool.sns_media.weflow_decrypt_sns_image_bytes", return_value=decoded_bad):
res = asyncio.run(
sns_media.try_fetch_and_decrypt_sns_image_remote(
account_dir=account_dir,
@@ -177,4 +191,3 @@ class TestSnsMedia(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,39 @@
import asyncio
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from wechat_decrypt_tool.routers import sns # noqa: E402 pylint: disable=wrong-import-position
class TestSnsMediaRouteWeFlowDefault(unittest.TestCase):
def test_route_stops_after_remote_miss_by_default(self):
with TemporaryDirectory() as td:
account_dir = Path(td) / "acc"
account_dir.mkdir(parents=True, exist_ok=True)
with mock.patch("wechat_decrypt_tool.routers.sns._resolve_account_dir", return_value=account_dir):
with mock.patch("wechat_decrypt_tool.routers.sns._try_fetch_and_decrypt_sns_remote", return_value=None):
with self.assertRaises(sns.HTTPException) as ctx:
asyncio.run(
sns.get_sns_media(
account="acc",
url="https://mmsns.qpic.cn/sns/test/0",
key="123",
token="tkn",
use_cache=1,
)
)
self.assertEqual(ctx.exception.status_code, 404)
if __name__ == "__main__":
unittest.main()
-40
View File
@@ -1,40 +0,0 @@
import sys
import unittest
from pathlib import Path
from starlette.responses import Response
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from wechat_decrypt_tool.sns_stage_timing import add_sns_stage_timing_headers # noqa: E402 pylint: disable=wrong-import-position
class TestSnsStageServerTiming(unittest.TestCase):
def test_injects_server_timing_when_missing(self):
resp = Response(content=b"ok")
add_sns_stage_timing_headers(resp.headers, source="proxy")
st = str(resp.headers.get("Server-Timing") or "")
self.assertIn("sns_source_", st)
self.assertIn("proxy", st)
def test_appends_when_upstream_server_timing_exists(self):
resp = Response(content=b"ok")
resp.headers["Server-Timing"] = "edge;dur=1"
add_sns_stage_timing_headers(resp.headers, source="proxy")
st = str(resp.headers.get("Server-Timing") or "")
self.assertIn("edge;dur=1", st)
self.assertIn("sns_source_", st)
def test_does_not_duplicate_existing_sns_source_metric(self):
resp = Response(content=b"ok")
resp.headers["Server-Timing"] = 'sns_source_proxy;dur=0;desc="proxy"'
add_sns_stage_timing_headers(resp.headers, source="proxy")
st = str(resp.headers.get("Server-Timing") or "")
self.assertEqual(st.count("sns_source_"), 1)
if __name__ == "__main__":
unittest.main()
+1 -121
View File
@@ -1,122 +1,2 @@
// Generate WeChat/WeFlow WxIsaac64 keystream via WeFlow's WASM module.
//
// Usage:
// node tools/weflow_wasm_keystream.js <key> <size>
//
// Prints a base64-encoded keystream to stdout (no extra logs).
const fs = require('fs')
const path = require('path')
const vm = require('vm')
function usageAndExit() {
process.stderr.write('Usage: node tools/weflow_wasm_keystream.js <key> <size>\\n')
process.exit(2)
}
const key = String(process.argv[2] || '').trim()
const size = Number(process.argv[3] || 0)
if (!key || !Number.isFinite(size) || size <= 0) usageAndExit()
const basePath = path.join(__dirname, '..', 'WeFlow', 'electron', 'assets', 'wasm')
const wasmPath = path.join(basePath, 'wasm_video_decode.wasm')
const jsPath = path.join(basePath, 'wasm_video_decode.js')
if (!fs.existsSync(wasmPath) || !fs.existsSync(jsPath)) {
process.stderr.write(`WeFlow WASM assets not found: ${basePath}\\n`)
process.exit(1)
}
const wasmBinary = fs.readFileSync(wasmPath)
const jsContent = fs.readFileSync(jsPath, 'utf8')
let capturedKeystream = null
let resolveInit
let rejectInit
const initPromise = new Promise((res, rej) => {
resolveInit = res
rejectInit = rej
})
const mockGlobal = {
console: { log: () => {}, error: () => {} }, // keep stdout clean
Buffer,
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array,
Array,
Object,
Function,
String,
Number,
Boolean,
Error,
Promise,
require,
process,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
}
mockGlobal.Module = {
onRuntimeInitialized: () => resolveInit(),
wasmBinary,
print: () => {},
printErr: () => {},
}
mockGlobal.self = mockGlobal
mockGlobal.self.location = { href: jsPath }
mockGlobal.WorkerGlobalScope = function () {}
mockGlobal.VTS_WASM_URL = `file://${wasmPath}`
mockGlobal.wasm_isaac_generate = (ptr, n) => {
const buf = new Uint8Array(mockGlobal.Module.HEAPU8.buffer, ptr, n)
capturedKeystream = new Uint8Array(buf) // copy view
}
try {
const context = vm.createContext(mockGlobal)
new vm.Script(jsContent, { filename: jsPath }).runInContext(context)
} catch (e) {
rejectInit(e)
}
;(async () => {
try {
await initPromise
if (!mockGlobal.Module.WxIsaac64 && mockGlobal.Module.asm && mockGlobal.Module.asm.WxIsaac64) {
mockGlobal.Module.WxIsaac64 = mockGlobal.Module.asm.WxIsaac64
}
if (!mockGlobal.Module.WxIsaac64) {
throw new Error('WxIsaac64 not found in WASM module')
}
capturedKeystream = null
const isaac = new mockGlobal.Module.WxIsaac64(key)
isaac.generate(size)
if (isaac.delete) isaac.delete()
if (!capturedKeystream) throw new Error('Failed to capture keystream')
const out = Buffer.from(capturedKeystream)
// Match WeFlow worker logic: reverse the captured Uint8Array.
out.reverse()
process.stdout.write(out.toString('base64'))
} catch (e) {
process.stderr.write(String(e && e.stack ? e.stack : e) + '\\n')
process.exit(1)
}
})()
require(path.join(__dirname, '..', 'src', 'wechat_decrypt_tool', 'native', 'weflow_wasm', 'weflow_wasm_keystream.js'))
Generated
+1 -1
View File
@@ -872,7 +872,7 @@ wheels = [
[[package]]
name = "wechat-decrypt-tool"
version = "1.3.0"
version = "1.7.12"
source = { editable = "." }
dependencies = [
{ name = "aiofiles" },