From f9252fb26dd2dcc1b73311202bc3c6a33a880006 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 31 May 2026 22:33:22 +0800 Subject: [PATCH] fix(bridge): type Ptlogin probe + de-magic process threshold (PR #73 follow-up) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #73's login-state probe shipped with issues: - `fetchPtlogin` returned `Promise`, tripping the repo's `@typescript-eslint/no-explicit-any` (error) and breaking `pnpm lint`. Modelled the read fields as a `PtloginUin` interface and cast the parsed JSONP to it. - `tryPtloginMethod` had a dead `if` whose two branches both returned 'fallback'; collapsed to a single return with a comment. - The `< 6` QQ-process-count heuristic was a bare magic number (one copy annotated only with "13579 5个端口全部用光"). Extracted to `LOGGED_OUT_PROCESS_COUNT_MAX` with an explanation and reused in all three sites (incl. the error fallback). Behaviour-preserving: the probing logic (Ptlogin parse, deep-link fallback, PID port discovery) is unchanged — only typing and the magic constant. --- packages/bridge/src/qq-port-probe.ts | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/bridge/src/qq-port-probe.ts b/packages/bridge/src/qq-port-probe.ts index d9071f7a..3ab83e5b 100644 --- a/packages/bridge/src/qq-port-probe.ts +++ b/packages/bridge/src/qq-port-probe.ts @@ -9,6 +9,13 @@ const PORT_RANGE_START = 9210; const PORT_RANGE_END = 9219; const PROBE_TIMEOUT_MS = 1000; const CONNECTION_TIMEOUT_MS = 500; +// QQ's Ptlogin quick-login ports plus its main process mean a single +// logged-in client surfaces roughly this many processes. When no usable +// probe port is found, a count BELOW this implies the target PID is still at +// the login screen; AT/ABOVE it the environment is ambiguous (multiple or +// unrelated `qq` processes), so we fall through to deep-link probing rather +// than guess "logged out". +const LOGGED_OUT_PROCESS_COUNT_MAX = 6; export interface QqPortLoginInfo { port: number; @@ -111,7 +118,15 @@ async function probePort(port: number): Promise { } -async function fetchPtlogin(port: number): Promise { +/** One entry of the Ptlogin `pt_get_uins` JSONP array. Only the fields the + * probe reads are modelled; QQ sends more but they're irrelevant here. */ +interface PtloginUin { + uin?: string | number; + account?: string | number; + nickname?: string; +} + +async function fetchPtlogin(port: number): Promise { return new Promise((resolve) => { const url = `https://127.0.0.1:${port}/pt_get_uins?callback=ptui_getuins_CB&pt_local_tk=0`; @@ -133,7 +148,7 @@ async function fetchPtlogin(port: number): Promise { try { // 100% 复刻 Python 切片逻辑:获取两个方括号中间的字符串,再包装成数组 const inner = text.split('[')[1].split(']')[0]; - const data = JSON.parse('[' + inner + ']'); + const data = JSON.parse('[' + inner + ']') as PtloginUin[]; resolve(data); } catch { resolve([]); @@ -167,10 +182,8 @@ async function tryPtloginMethod(port: number): Promise { return parseInt(stdout.trim(), 10) || 0; } } catch { - return 6; + // On failure report the ambiguous threshold so we never falsely + // conclude "logged out". + return LOGGED_OUT_PROCESS_COUNT_MAX; } } @@ -230,7 +245,7 @@ export async function probeQqLoginInfo(pid: number): Promise