refactor: await native operations across the bridge lifecycle
This commit is contained in:
1 parent
1faa6c58fa
commit
9b165a8971
24 files changed
+241
-177
No files matched your search
@@ -129,13 +129,7 @@ export class HookManager {
|
||||
|
||||
this.injector = deps.injector ?? {
|
||||
inject: injectHookProcess,
|
||||
unload: (pid, handle) => {
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
|
||||
unloadHookProcess(pid, handle);
|
||||
},
|
||||
unload: unloadHookProcess,
|
||||
};
|
||||
this.makeClient = deps.makeClient ?? ((pid: number) => new QqHookClient(pid));
|
||||
this.detectAccounts = deps.detectAccounts ?? detectAccountLoginsForPids;
|
||||
|
||||
@@ -10,7 +10,7 @@ import type { PacketSink } from '@snowluma/common/protocol-types';
|
||||
import { isRealUin } from '@snowluma/common/uin';
|
||||
import { EventEmitter } from 'events';
|
||||
import { HookPacketClient } from './hook-packet-client';
|
||||
import type { HookInjectResult } from './injector';
|
||||
import type { HookInjectResult, ManualMapHandle } from './injector';
|
||||
import { detectUinForPid } from './qq-account';
|
||||
import type { QqHookClient, QqHookLoginState, QqHookPacket } from './qq-hook-client';
|
||||
import { statusFor } from './hook-status';
|
||||
@@ -33,8 +33,8 @@ const RECEIVE_STALE_CONFIRM_MS = 15_000;
|
||||
|
||||
export type HookSessionDeps = {
|
||||
injector: {
|
||||
inject: (pid: number) => HookInjectResult | Promise<HookInjectResult>;
|
||||
unload: (pid: number, handle: HookInjectResult['handle']) => void;
|
||||
inject: (pid: number) => Promise<HookInjectResult>;
|
||||
unload: (pid: number, handle: ManualMapHandle) => Promise<void>;
|
||||
};
|
||||
makeClient: (pid: number) => QqHookClient;
|
||||
/** Account QQ's local data reports as signed in on `pid`, or null. Drives the
|
||||
@@ -440,7 +440,7 @@ export class HookSession extends EventEmitter {
|
||||
const handle = this.injectResult?.handle;
|
||||
|
||||
if (this.injected && handle) {
|
||||
this.injector.unload(this.pid, handle);
|
||||
await this.injector.unload(this.pid, handle);
|
||||
this.log.info('SnowLuma unloaded from PID=%d', this.pid);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ export interface ManualMapHandle {
|
||||
}
|
||||
|
||||
interface NativeHookAddon {
|
||||
getRuntimeIntegrity?(): unknown;
|
||||
getRuntimeIntegrityAsync(): Promise<unknown>;
|
||||
getAllMainProcessAsync(): Promise<number[]>;
|
||||
probeDbLock?(dbPath: string): { pid: number; name: string }[];
|
||||
loadModuleManual(pid: number, dylibPath: string): ManualMapHandle | Promise<ManualMapHandle>;
|
||||
unloadModuleManual(pid: number, handle: ManualMapHandle): void;
|
||||
getFileHoldersAsync(path: string): Promise<{ pid: number; name: string }[]>;
|
||||
loadModuleManualAsync(pid: number, dylibPath: string): Promise<ManualMapHandle>;
|
||||
unloadModuleManualAsync(pid: number, handle: ManualMapHandle): Promise<void>;
|
||||
}
|
||||
|
||||
export interface HookProcessBaseInfo {
|
||||
@@ -26,7 +26,7 @@ export interface HookProcessBaseInfo {
|
||||
|
||||
export interface HookInjectResult {
|
||||
method: 'loadModuleManual';
|
||||
handle?: ManualMapHandle;
|
||||
handle: ManualMapHandle;
|
||||
}
|
||||
|
||||
let nativeAddon: NativeHookAddon | null = null;
|
||||
@@ -123,15 +123,15 @@ export async function injectHookProcess(pid: number): Promise<HookInjectResult>
|
||||
throw new Error(`No hook ${injectableExt} found for ${process.platform}-${process.arch}`);
|
||||
}
|
||||
|
||||
return { method: 'loadModuleManual', handle: await addon.loadModuleManual(pid, dllPath) };
|
||||
return { method: 'loadModuleManual', handle: await addon.loadModuleManualAsync(pid, dllPath) };
|
||||
}
|
||||
|
||||
export function unloadHookProcess(pid: number, handle: ManualMapHandle): void {
|
||||
export async function unloadHookProcess(pid: number, handle: ManualMapHandle): Promise<void> {
|
||||
const addon = getNativeHookAddon();
|
||||
|
||||
if (!addon) {
|
||||
throw new Error(getNativeHookLoadError() ?? 'hook native addon is not available');
|
||||
}
|
||||
|
||||
addon.unloadModuleManual(pid, handle);
|
||||
await addon.unloadModuleManualAsync(pid, handle);
|
||||
}
|
||||
+28
-19
@@ -1,28 +1,27 @@
|
||||
import fs from 'node:fs';
|
||||
import fs from 'node:fs/promises';
|
||||
import { getNativeHookAddon } from '../injector';
|
||||
|
||||
export interface DbLockHolder {
|
||||
export interface DatabaseHolder {
|
||||
pid: number;
|
||||
|
||||
name: string;
|
||||
}
|
||||
|
||||
export function probeDbLock(dbPath: string): DbLockHolder[] {
|
||||
export async function getDatabaseHolders(dbPath: string): Promise<DatabaseHolder[]> {
|
||||
if (process.platform === 'win32') {
|
||||
return probeWindowsHolders(dbPath);
|
||||
return getWindowsHolders(dbPath);
|
||||
}
|
||||
|
||||
return process.platform === 'linux' ? probeLinuxLocks(dbPath) : [];
|
||||
return process.platform === 'linux' ? getLinuxHolders(dbPath) : [];
|
||||
}
|
||||
|
||||
function probeWindowsHolders(dbPath: string): DbLockHolder[] {
|
||||
async function getWindowsHolders(dbPath: string): Promise<DatabaseHolder[]> {
|
||||
const addon = getNativeHookAddon();
|
||||
|
||||
if (!addon?.probeDbLock) {
|
||||
throw new Error('Windows account detection requires a hook addon with probeDbLock');
|
||||
if (!addon?.getFileHoldersAsync) {
|
||||
throw new Error('Windows account detection requires a hook addon with getFileHoldersAsync');
|
||||
}
|
||||
|
||||
const holders = addon.probeDbLock(dbPath);
|
||||
const holders = await addon.getFileHoldersAsync(dbPath);
|
||||
|
||||
if (
|
||||
!Array.isArray(holders) ||
|
||||
@@ -50,16 +49,22 @@ export function isQqProcessName(name: string): boolean {
|
||||
}
|
||||
|
||||
/** Match POSIX locks by device and inode; the table does not contain file paths. */
|
||||
function probeLinuxLocks(dbPath: string): DbLockHolder[] {
|
||||
const stat = fs.statSync(dbPath, { throwIfNoEntry: false });
|
||||
async function getLinuxHolders(dbPath: string): Promise<DatabaseHolder[]> {
|
||||
const stat = await fs.stat(dbPath).catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === 'ENOENT') {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
|
||||
if (!stat) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const key = deviceInodeKey(stat.dev, stat.ino);
|
||||
const table = fs.readFileSync('/proc/locks', 'utf8');
|
||||
const holders: DbLockHolder[] = [];
|
||||
const table = await fs.readFile('/proc/locks', 'utf8');
|
||||
const holders: DatabaseHolder[] = [];
|
||||
const seen = new Set<number>();
|
||||
|
||||
for (const line of table.split('\n')) {
|
||||
@@ -77,7 +82,7 @@ function probeLinuxLocks(dbPath: string): DbLockHolder[] {
|
||||
}
|
||||
|
||||
seen.add(pid);
|
||||
holders.push({ pid, name: readProcessName(pid) });
|
||||
holders.push({ pid, name: await readProcessName(pid) });
|
||||
}
|
||||
|
||||
return holders;
|
||||
@@ -89,10 +94,14 @@ function deviceInodeKey(device: number, inode: number): string {
|
||||
return `${major.toString(16).padStart(2, '0')}:${minor.toString(16).padStart(2, '0')}:${inode}`;
|
||||
}
|
||||
|
||||
function readProcessName(pid: number): string {
|
||||
async function readProcessName(pid: number): Promise<string> {
|
||||
try {
|
||||
return fs.readFileSync(`/proc/${pid}/comm`, 'utf8').trim();
|
||||
} catch {
|
||||
return '';
|
||||
return (await fs.readFile(`/proc/${pid}/comm`, 'utf8')).trim();
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
||||
return '';
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { isQqProcessName, probeDbLock } from './db-lock';
|
||||
import { isQqProcessName, getDatabaseHolders } from './database-holders';
|
||||
import { readLocalAccounts, type LocalAccount } from './login-db';
|
||||
import { loginDbPaths, ntMsgDbPath, qqDataRoots, qqDataRootsForPid } from './paths';
|
||||
|
||||
@@ -39,8 +39,8 @@ export async function detectAccountLogins(
|
||||
}
|
||||
|
||||
for (const account of report.accounts) {
|
||||
const holder = probeDbLock(ntMsgDbPath(root, account)).find((candidate) =>
|
||||
isQqProcessName(candidate.name),
|
||||
const holder = (await getDatabaseHolders(ntMsgDbPath(root, account))).find(
|
||||
(candidate) => isQqProcessName(candidate.name),
|
||||
);
|
||||
states.push({ ...account, pid: holder?.pid ?? null });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { isQqProcessName, probeDbLock, type DbLockHolder } from './db-lock';
|
||||
export { isQqProcessName, getDatabaseHolders, type DatabaseHolder } from './database-holders';
|
||||
export {
|
||||
detectAccountLogins,
|
||||
detectAccountLoginsForPids,
|
||||
|
||||
@@ -76,18 +76,18 @@ function isNativeRuntimeIntegrity(value: unknown): value is NativeRuntimeIntegri
|
||||
);
|
||||
}
|
||||
|
||||
let cached: RuntimeIntegrity | undefined;
|
||||
let cached: Promise<RuntimeIntegrity> | undefined;
|
||||
|
||||
/** One startup snapshot, shared by the startup log and the WebUI warning. */
|
||||
export function getRuntimeIntegrity(): RuntimeIntegrity {
|
||||
export function getRuntimeIntegrity(): Promise<RuntimeIntegrity> {
|
||||
return (cached ??= readRuntimeIntegrity());
|
||||
}
|
||||
|
||||
async function readRuntimeIntegrity(): Promise<RuntimeIntegrity> {
|
||||
if (process.platform !== 'win32') {
|
||||
return { status: 'unsupported' };
|
||||
}
|
||||
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
try {
|
||||
const addon = getNativeHookAddon();
|
||||
|
||||
@@ -95,17 +95,17 @@ export function getRuntimeIntegrity(): RuntimeIntegrity {
|
||||
throw new Error(getNativeHookLoadError() ?? '原生组件不可用');
|
||||
}
|
||||
|
||||
if (typeof addon.getRuntimeIntegrity !== 'function') {
|
||||
if (typeof addon.getRuntimeIntegrityAsync !== 'function') {
|
||||
throw new Error('当前原生组件不支持完整性诊断,请更新原生组件');
|
||||
}
|
||||
|
||||
const result = addon.getRuntimeIntegrity();
|
||||
const result = await addon.getRuntimeIntegrityAsync();
|
||||
|
||||
if (!isNativeRuntimeIntegrity(result)) {
|
||||
throw new Error('原生完整性诊断返回了无效数据');
|
||||
}
|
||||
|
||||
cached = {
|
||||
return {
|
||||
status: 'available',
|
||||
pid: result.pid,
|
||||
executablePath: result.executablePath,
|
||||
@@ -113,11 +113,9 @@ export function getRuntimeIntegrity(): RuntimeIntegrity {
|
||||
file: result.file,
|
||||
};
|
||||
} catch (error) {
|
||||
cached = {
|
||||
return {
|
||||
status: 'unavailable',
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
|
||||
return cached;
|
||||
}
|
||||
Reference in new issue
Block a user