From 0e42c19d3bc9a88846afef17b97e38bad77c8a8d Mon Sep 17 00:00:00 2001 From: hicccc77 <98377878+hicccc77@users.noreply.github.com> Date: Thu, 12 Mar 2026 20:46:10 +0800 Subject: [PATCH] feat(mac): implement WeChat PID detection and pass to helper --- electron/services/keyServiceMac.ts | 57 +++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/electron/services/keyServiceMac.ts b/electron/services/keyServiceMac.ts index 764337f..56280b6 100644 --- a/electron/services/keyServiceMac.ts +++ b/electron/services/keyServiceMac.ts @@ -169,14 +169,69 @@ export class KeyServiceMac { } } + private async getWeChatPid(): Promise { + try { + const { stdout } = await execFileAsync('ps', ['-A', '-o', 'pid,comm,command']) + const lines = stdout.split('\n').slice(1) + + const candidates: Array<{ pid: number; comm: string; command: string }> = [] + + for (const line of lines) { + const match = line.trim().match(/^(\d+)\s+(\S+)\s+(.*)$/) + if (!match) continue + + const pid = parseInt(match[1]) + const comm = match[2] + const command = match[3] + + const nameMatch = comm === 'WeChat' || comm === '微信' + const pathMatch = command.includes('WeChat.app') || command.includes('/Contents/MacOS/WeChat') + + if (nameMatch && pathMatch) { + candidates.push({ pid, comm, command }) + } + } + + if (candidates.length === 0) { + throw new Error('WeChat process not found') + } + + const filtered = candidates.filter(p => { + const cmd = p.command + return !cmd.includes('WeChatAppEx.app/') && + !cmd.includes('/WeChatAppEx') && + !cmd.includes(' WeChatAppEx') && + !cmd.includes('crashpad_handler') && + !cmd.includes('Helper') + }) + + if (filtered.length === 0) { + throw new Error('No valid WeChat main process found') + } + + const preferredMain = filtered.filter(p => + p.command.includes('/Contents/MacOS/WeChat') + ) + + const selectedPool = preferredMain.length > 0 ? preferredMain : filtered + const selected = selectedPool.reduce((max, p) => p.pid > max.pid ? p : max) + + return selected.pid + } catch (e: any) { + throw new Error('Failed to get WeChat PID: ' + e.message) + } + } + private async getDbKeyByHelper( timeoutMs: number, onStatus?: (message: string, level: number) => void ): Promise { const helperPath = this.getHelperPath() const waitMs = Math.max(timeoutMs, 30_000) + const pid = await this.getWeChatPid() + return await new Promise((resolve, reject) => { - const child = spawn(helperPath, [String(waitMs)], { stdio: ['ignore', 'pipe', 'pipe'] }) + const child = spawn(helperPath, [String(pid), String(waitMs)], { stdio: ['ignore', 'pipe', 'pipe'] }) let stdout = '' let stderr = '' let stdoutBuf = ''