Probe and reconnect to existing hooks

Add a lightweight pipe liveness probe and use it to detect/reconnect to existing SnowLuma hooks instead of always re-injecting. Introduces PROBE_TIMEOUT_MS (400ms) and QqHookClient.probePipe(pid, timeout) which quickly checks if the control pipe accepts a connection (no hello exchange). HookManager now parallel-probes freshly-enumerated processes (probeAndSeed) and fast-checks before injecting in loadProcess; positive hits mark the state as injected with method="reconnect" and avoid redundant injection. Also deduplicate login notifications in handleLoginState (skip if already logged in with the same UIN) and adjust the loginState emission ordering in QqHookClient.
This commit is contained in:
手瓜一十雪 committed 2026-05-03 16:49:06 +08:00
1 parent 82c1721466
commit f04ec3ec36
2 files changed
+49 -6

No files matched your search

+25 -4
View File
@@ -47,6 +47,16 @@ export class HookManager {
state.path = proc.path;
}
// Probe every freshly-enumerated PID in parallel to detect hooks left over
// from a previous SnowLuma run. Positive hits are reconnected immediately
// so the login-state snapshot can update the UI without a manual load.
await Promise.all(
processes
.map(proc => this.states.get(proc.pid)!)
.filter(state => !state.injected && !this.loadTasks.has(state.pid))
.map(state => this.probeAndSeed(state)),
);
const seen = new Set(processes.map(proc => proc.pid));
const result: HookProcessInfo[] = [];
for (const proc of processes) {
@@ -109,10 +119,18 @@ export class HookManager {
try {
if (!state.injected) {
state.injectResult = injectHookProcess(pid);
state.injected = true;
state.method = state.injectResult.method;
log.info('SnowLuma loaded into PID=%d via %s', pid, state.method);
// Fast path: if QQ.exe still hosts a hook DLL from a previous SnowLuma
// run, the control pipe is already listed. Skip re-injection on a hit.
if (await QqHookClient.probePipe(pid)) {
state.injected = true;
state.method = 'reconnect';
log.info('SnowLuma reconnected to existing hook in PID=%d', pid);
} else {
state.injectResult = injectHookProcess(pid);
state.injected = true;
state.method = state.injectResult.method;
log.info('SnowLuma loaded into PID=%d via %s', pid, state.method);
}
}
state.status = state.connected ? (state.loggedIn ? 'online' : 'loaded') : 'loaded';
this.startClient(state);
@@ -248,11 +266,14 @@ export class HookManager {
}
private handleLoginState(state: HookProcessState, loginState: QqHookLoginState): void {
const wasLoggedIn = state.loggedIn;
const previousUin = state.uin;
state.uin = loginState.uin || loginState.uinNumber.toString();
state.loggedIn = loginState.loggedIn && isRealUin(state.uin);
state.status = state.loggedIn ? 'online' : (state.connected ? 'loaded' : state.status);
if (!state.loggedIn || !state.sender) return;
if (wasLoggedIn && previousUin === state.uin) return;
this.bridgeManager.onHookLogin(state.pid, state.uin, state.sender);
log.success('login detected: PID=%d UIN=%s', state.pid, state.uin);
+24 -2
View File
@@ -1,6 +1,7 @@
import net from 'net';
import os from 'os';
import path from 'path';
import { promises as fs } from 'fs';
import { EventEmitter } from 'events';
export const PIPE_MAGIC = 0x31504851;
@@ -98,7 +99,7 @@ function mojoPipeName(pid: number, suffix: string): string {
if (process.platform === 'win32') {
return `\\\\.\\pipe\\mojo.${pid}.${suffix}`;
}
return path.join(linuxRuntimeDir(), `mojo.${pid}.${suffix}`);
return path.join(linuxRuntimeDir(), `mojo.${pid}.${suffix}.sock`);
}
function toBuffer(body: Buffer | Uint8Array | string | null | undefined): Buffer {
@@ -282,6 +283,27 @@ export class QqHookClient extends EventEmitter {
return mojoPipeName(pid, 'recv');
}
/**
* Lightweight liveness check for the control pipe. Used to detect whether an
* existing SnowLuma hook is still hosted inside the QQ.exe process without
* opening the pipe, so probing cannot disturb the first real client connect.
*/
static async probePipe(pid: number): Promise<boolean> {
try {
const pipeFileName = process.platform === 'win32'
? `mojo.${pid}.control`
: `mojo.${pid}.control.sock`;
if (process.platform === 'win32') {
const names = await fs.readdir('\\\\.\\pipe\\');
return names.some(name => name.toLowerCase() === pipeFileName.toLowerCase());
}
const names = await fs.readdir(linuxRuntimeDir());
return names.includes(pipeFileName);
} catch {
return false;
}
}
async connect(): Promise<QqHookHello> {
if (this.closed) {
throw new Error('qq_hook client is closed');
@@ -592,8 +614,8 @@ export class QqHookClient extends EventEmitter {
const previous = this.loginState;
const next = { loggedIn, uin, uinNumber };
this.loginState = next;
this.emit('loginState', next);
if (previous.loggedIn !== next.loggedIn || previous.uin !== next.uin) {
this.emit('loginState', next);
if (next.loggedIn) {
const waiters = this.loginWaiters;
this.loginWaiters = [];