注入的竞态窗口里,native hook 偶发上报 13 位垃圾 UIN(时间戳型,读到 QQ 尚未落定
的内存)。TS 层防御缺失,导致两个连锁问题:
1) 幽灵账号:isRealUin 只查「纯数字且 ≥5 位」、无上限,垃圾值通过后建会话并把
config/onebot_<垃圾>.json 落盘 → OneBot 节点配置里出现随机幽灵账号(跨重启残留)。
2) 进程崩溃:logger 彩色路径 ' '.repeat(SLOT - tagLen),UIN tag(含方括号)超宽 →
负数 → RangeError: Invalid count value → uncaughtException。
跨平台(Win DLL / Linux .so 同逻辑)+「更新 QQ 后消失」都印证是 native 读到那块内存
的偶发垃圾;根修在 SnowLumaNative,此处补齐 TS 防御:
- 抽出唯一的 @snowluma/common/uin::isRealUin = /^\d{5,10}$/(真实 QQ ≤ uint32 →
10 位)。删除 bridge/core 两处重复副本(重复正是这次漏加上限的温床)。等价旧逻辑,
仅新增拒 11+ 位。
- logger.ts 加 Math.max(0, …),日志永不崩进程(独立健壮化)。
- 收紧 WebUI UIN_REGEX /^\d{5,12}$/ → /^\d{5,10}$/ 保持一致。
- 启动时 cleanupInvalidPerUinConfigs() 清掉已落盘的幽灵 onebot_<uin>.json(只删
UIN 不合法的,绝不碰合法账号/全局配置)。
测试:common +8(uin 6 / logger 2)、onebot +3(cleanup)。typecheck 12 包干净;
common 58 / onebot 519 / core 471 / bridge 73 全绿。对抗性 Agent 逐输入核验等价性、
三层 gate 堵死幽灵入口、cleanup 安全时序,无回归。
113 lines
4.6 KiB
TypeScript
113 lines
4.6 KiB
TypeScript
import { HookManager } from '@snowluma/bridge';
|
|
import { closeLogger, createLogger } from '@snowluma/common/logger';
|
|
import { loadRuntimeConfig } from '@snowluma/common/runtime';
|
|
import { OneBotManager } from '@snowluma/onebot/manager';
|
|
import { migrateGlobalSettings } from '@snowluma/onebot/global-config';
|
|
import { cleanupInvalidPerUinConfigs } from '@snowluma/onebot/config';
|
|
import { BridgeManager } from './bridge/manager';
|
|
import { createNotificationManager } from './notifications/manager';
|
|
import { createStateWiring } from './webui/state-wiring';
|
|
|
|
const runtimeConfig = loadRuntimeConfig();
|
|
const log = createLogger('App');
|
|
|
|
// Last-resort process guards. This is a long-running daemon: a hooked QQ pipe
|
|
// can drop at any moment, and a stray rejection on a detached callback (or a
|
|
// throw that escapes one) must NOT take the whole process down — otherwise
|
|
// Docker restarts it (dropping every session) and a bare Windows run just dies
|
|
// ("connection pipe closed", bot offline). Log and keep serving; the bridge's
|
|
// own watcher reconnects the dropped pipe. Targeted handlers still cover the
|
|
// known paths (e.g. the hook-client send deferreds) — these only catch what
|
|
// slips through, and surface its stack so the real source can be fixed.
|
|
process.on('unhandledRejection', (reason) => {
|
|
log.error('unhandledRejection (kept alive): %s',
|
|
reason instanceof Error ? (reason.stack ?? reason.message) : String(reason));
|
|
});
|
|
process.on('uncaughtException', (error) => {
|
|
log.error('uncaughtException (kept alive): %s', error.stack ?? error.message);
|
|
});
|
|
|
|
async function main() {
|
|
log.info('SnowLuma starting');
|
|
|
|
// One-shot: lift a legacy per-UIN musicSignUrl into the global store before
|
|
// any session (and thus any per-UIN config rewrite) can drop it.
|
|
migrateGlobalSettings();
|
|
|
|
// One-shot: purge any persisted phantom accounts (onebot_<garbage>.json) the
|
|
// native hook may have created with a timestamp-shaped UIN (issue #162).
|
|
cleanupInvalidPerUinConfigs();
|
|
|
|
const bridgeManager = new BridgeManager();
|
|
const oneBotManager = new OneBotManager();
|
|
const autoLoadOnDiscovery = resolveAutoLoad(runtimeConfig.hookAutoLoad);
|
|
|
|
// WebUI state-push wiring: HookManager + BridgeManager edges publish
|
|
// `processes` / `qq-list` / `connections` invalidations into a bus the
|
|
// /api/state/stream SSE handler subscribes to (no REST polling required).
|
|
const stateWiring = createStateWiring();
|
|
const hookManager = new HookManager({
|
|
bridgeManager,
|
|
autoLoadOnDiscovery,
|
|
onSessionsChanged: stateWiring.onSessionsChanged,
|
|
});
|
|
if (autoLoadOnDiscovery) {
|
|
log.info('hook auto-load enabled: every discovered QQ process will be injected');
|
|
}
|
|
|
|
oneBotManager.bind(bridgeManager);
|
|
stateWiring.bindBridgeManager(bridgeManager);
|
|
|
|
// Global notification subsystem (account up/down → webhook). Bound AFTER
|
|
// OneBotManager so the nickname fallback is already populated when it observes.
|
|
const notificationManager = createNotificationManager();
|
|
notificationManager.bind(bridgeManager);
|
|
|
|
if (
|
|
(typeof __BUILD_WEBUI__ !== 'undefined' && __BUILD_WEBUI__) ||
|
|
process.env.SNOWLUMA_DEV_WEBUI === '1'
|
|
) {
|
|
try {
|
|
const { initWebUI } = await import('./webui/server');
|
|
await initWebUI(runtimeConfig.webuiPort || 5099, oneBotManager, hookManager, notificationManager, {
|
|
host: runtimeConfig.webuiHost,
|
|
tlsEnabled: runtimeConfig.webuiTls?.enabled,
|
|
trustProxy: runtimeConfig.trustProxy,
|
|
stateBus: stateWiring.bus,
|
|
});
|
|
} catch (err) {
|
|
log.error('Failed to start WebUI: ', err);
|
|
}
|
|
}
|
|
|
|
// Graceful shutdown: dispose managers, await log flush, then exit.
|
|
// SIGINT (Ctrl-C) and SIGTERM (Docker/systemd) take the same path.
|
|
const shutdown = (signal: string) => async () => {
|
|
log.warn(`Shutting down (${signal})...`);
|
|
oneBotManager.dispose();
|
|
notificationManager.dispose();
|
|
hookManager.dispose();
|
|
stateWiring.dispose();
|
|
await closeLogger();
|
|
process.exit(0);
|
|
};
|
|
process.on('SIGINT', shutdown('SIGINT'));
|
|
process.on('SIGTERM', shutdown('SIGTERM'));
|
|
}
|
|
|
|
function resolveAutoLoad(fromConfig: boolean | undefined): boolean {
|
|
const envRaw = process.env.SNOWLUMA_HOOK_AUTOLOAD;
|
|
if (typeof envRaw === 'string' && envRaw.trim()) {
|
|
const v = envRaw.trim().toLowerCase();
|
|
if (v === '1' || v === 'true' || v === 'yes' || v === 'on') return true;
|
|
if (v === '0' || v === 'false' || v === 'no' || v === 'off') return false;
|
|
}
|
|
return fromConfig === true;
|
|
}
|
|
|
|
main().catch(async (error) => {
|
|
log.error(error instanceof Error ? (error.stack ?? error.message) : String(error));
|
|
await closeLogger();
|
|
process.exit(1);
|
|
});
|