Adds the missing macOS branch so HookManager + PipeWatcher discover QQ
processes when the dylib is injected via DYLD_INSERT_LIBRARIES (the
in-process injection model used by apps/qq/macos/qq_hook_dylib.cpp in
the nnphook private repo, vs Linux's out-of-process ptrace + N-API
addon model and Windows's manual-PE-loader injection).
Two small changes:
1. `linuxRuntimeDir()` (qq-hook-client.ts) — name kept for callsite
stability; the function now applies to every non-win32 platform.
New darwin branch prefers `$TMPDIR/snowluma-hook/` to match the
dylib's `runtime_dir()` (apps/qq/macos/qq_hook_dylib.cpp). The
Linux `/tmp/snowluma-<uid>` fallback stays as the last resort.
$TMPDIR is per-user under /var/folders/ on stock macOS, which is
the right place — /tmp/snowluma-<uid> doesn't exist by default.
2. `listSnowlumaPipePidsSync()` (qq-hook-client.ts) + darwin branch
in `listHookProcesses()` (injector.ts) — on macOS there is no
native enumerate-QQ-processes addon (DYLD_INSERT obviates the
need; the dylib is already mapped in QQ when SnowLuma launches).
The watcher's `listProcesses` dep is sync, so we provide a sync
directory scan that returns the pids encoded in mojo.<pid>.control.sock
filenames. The subsequent async `listLivePipes()` still gates the
pipe-up emit via a real connect probe — a dangling socket file
from a force-killed previous run can't drive a false connect.
Without these two changes, `pipe-watcher.tickOnce()` filtered out our
QQ pid at line 170 (`if (!newKnownPids.has(pid)) continue;`) because
`listProcessesNative()` returned []. The pipe stayed live (verified
by our nnphook recv hook firing), but the bridge never emitted
'pipe-up', HookManager never auto-loaded the session, and the WebUI
showed an empty process list.
Verified end-to-end against the resigned QQ 6.9.96-49738 + nnphook
gpt54 branch dylib on macOS 26.5.1 (Tahoe) arm64:
* Direct HookManager harness picks up PID=16032 in <2.5s,
fast-paths to 'reconnect' (the live-pipe-skip-injection branch
in hook-session.ts:200), pipe connected, status=loaded
* `pnpm dev` (SNOWLUMA_DEV_NO_NATIVE=1 for the websocket stub):
/api/processes returns the live QQ entry with method=reconnect,
connected=true, awaiting login
The websocket darwin-arm64.node native addon is still missing — set
SNOWLUMA_DEV_NO_NATIVE=1 to dev-mode boot with the existing stub
fallback. That's a separate runtime artifact and not in scope here.
`shouldAutoLoadPid()` returns true on non-linux without further checks
(no Electron-child-process scanning needed on darwin), and the
`hook-session.ts` "pipe already live → reconnect" fast path means
the bridge never tries to invoke the non-existent darwin native
injector. Auto-load on a freshly discovered macOS QQ is therefore
safe with or without `SNOWLUMA_HOOK_AUTOLOAD=1`.
Tests: bridge typecheck + 62 unit tests still pass.
142 lines
4.7 KiB
TypeScript
142 lines
4.7 KiB
TypeScript
import { existsSync } from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { listSnowlumaPipePidsSync } from './qq-hook-client';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
export interface ManualMapHandle {
|
|
base: bigint;
|
|
entry: bigint;
|
|
exceptionTable: bigint;
|
|
size: number;
|
|
}
|
|
|
|
interface NativeHookAddon {
|
|
getAllMainProcess(): number[];
|
|
loadModuleManual(pid: number, dylibPath: string): ManualMapHandle;
|
|
unloadModuleManual(pid: number, handle: ManualMapHandle): void;
|
|
}
|
|
|
|
export interface HookProcessBaseInfo {
|
|
pid: number;
|
|
name: string;
|
|
path: string;
|
|
}
|
|
|
|
export interface HookInjectResult {
|
|
method: 'loadModuleManual';
|
|
handle?: ManualMapHandle;
|
|
}
|
|
|
|
let nativeAddon: NativeHookAddon | null = null;
|
|
let nativeLoadError: string | null = null;
|
|
|
|
function loadNativeAddon(addonPath: string): NativeHookAddon {
|
|
const mod = { exports: {} as Record<string, unknown> };
|
|
process.dlopen(mod, addonPath);
|
|
return mod.exports as unknown as NativeHookAddon;
|
|
}
|
|
|
|
type HookBinaryExt = 'node' | 'dll' | 'so';
|
|
|
|
function platformBinaryName(ext: HookBinaryExt): string {
|
|
if (process.platform === 'win32' && process.arch === 'x64') return `snowluma-win32-x64.${ext}`;
|
|
return `snowluma-${process.platform}-${process.arch}.${ext}`;
|
|
}
|
|
|
|
function platformInjectableExt(): 'dll' | 'so' {
|
|
return process.platform === 'win32' ? 'dll' : 'so';
|
|
}
|
|
|
|
function defaultProcessName(): string {
|
|
return process.platform === 'win32' ? 'QQ.exe' : 'qq';
|
|
}
|
|
|
|
function nativeSearchDirs(): string[] {
|
|
// Several layouts must be supported:
|
|
// 1. Released zip (flattened dist/): __dirname=<extracted>/, native at <extracted>/native
|
|
// 2. Dev via tsx (packages/bridge/src/injector.ts): __dirname=<root>/packages/bridge/src
|
|
// → walk up to <root>/packages and append runtime/native
|
|
// Same offset works for `packages/bridge/dist/index.mjs` after build.
|
|
// 3. cwd-based fallback when neither __dirname-relative path resolves.
|
|
return [
|
|
path.resolve(__dirname, 'native'),
|
|
path.resolve(__dirname, '..', '..', 'runtime', 'native'),
|
|
path.resolve(process.cwd(), 'dist', 'native'),
|
|
path.resolve(process.cwd(), 'packages', 'runtime', 'native'),
|
|
];
|
|
}
|
|
|
|
export function resolveHookNativePath(ext: HookBinaryExt): string | null {
|
|
const fileName = platformBinaryName(ext);
|
|
for (const dir of nativeSearchDirs()) {
|
|
const fullPath = path.join(dir, fileName);
|
|
if (existsSync(fullPath)) return fullPath;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function getNativeHookAddon(): NativeHookAddon | null {
|
|
if (nativeAddon) return nativeAddon;
|
|
const addonPath = resolveHookNativePath('node');
|
|
if (!addonPath) {
|
|
nativeLoadError = `No hook native addon found for ${process.platform}-${process.arch}`;
|
|
return null;
|
|
}
|
|
try {
|
|
nativeAddon = loadNativeAddon(addonPath);
|
|
nativeLoadError = null;
|
|
return nativeAddon;
|
|
} catch (error) {
|
|
nativeLoadError = error instanceof Error ? error.message : String(error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function getNativeHookLoadError(): string | null {
|
|
return nativeLoadError;
|
|
}
|
|
|
|
export function listHookProcesses(): HookProcessBaseInfo[] {
|
|
// macOS: no native enumerate-QQ addon. The DYLD_INSERT injection model
|
|
// means QQ ran with our dylib already mapped, so the dylib's listener
|
|
// socket IS the discovery signal. Treat every `mojo.<pid>.control.sock`
|
|
// in the runtime dir as a known process (the watcher's subsequent
|
|
// connectable-probe gates the pipe-up emit, so a stale dangling socket
|
|
// here doesn't drive a false connect).
|
|
if (process.platform === 'darwin') {
|
|
return [...listSnowlumaPipePidsSync()]
|
|
.sort((a, b) => a - b)
|
|
.map(pid => ({ pid, name: defaultProcessName(), path: '' }));
|
|
}
|
|
const addon = getNativeHookAddon();
|
|
if (!addon) return [];
|
|
return [...new Set(addon.getAllMainProcess())]
|
|
.filter(pid => Number.isInteger(pid) && pid > 0)
|
|
.sort((a, b) => a - b)
|
|
.map(pid => ({ pid, name: defaultProcessName(), path: '' }));
|
|
}
|
|
|
|
export function injectHookProcess(pid: number): HookInjectResult {
|
|
const addon = getNativeHookAddon();
|
|
if (!addon) {
|
|
throw new Error(getNativeHookLoadError() ?? 'hook native addon is not available');
|
|
}
|
|
const injectableExt = platformInjectableExt();
|
|
const dllPath = resolveHookNativePath(injectableExt);
|
|
if (!dllPath) {
|
|
throw new Error(`No hook ${injectableExt} found for ${process.platform}-${process.arch}`);
|
|
}
|
|
return { method: 'loadModuleManual', handle: addon.loadModuleManual(pid, dllPath) };
|
|
}
|
|
|
|
export function unloadHookProcess(pid: number, handle: ManualMapHandle): void {
|
|
const addon = getNativeHookAddon();
|
|
if (!addon) {
|
|
throw new Error(getNativeHookLoadError() ?? 'hook native addon is not available');
|
|
}
|
|
addon.unloadModuleManual(pid, handle);
|
|
}
|