注入的竞态窗口里,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 安全时序,无回归。
140 lines
4.5 KiB
TypeScript
140 lines
4.5 KiB
TypeScript
import { createLogger } from '@snowluma/common/logger';
|
|
import type { PacketSender } from '@snowluma/common/packet-sender';
|
|
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
|
import { isRealUin } from '@snowluma/common/uin';
|
|
import { IdentityService } from '@snowluma/protocol/identity-service';
|
|
import { Bridge } from './bridge';
|
|
|
|
export type SessionStartedCallback = (uin: string, bridge: Bridge) => void;
|
|
export type SessionClosedCallback = (uin: string, bridge: Bridge) => void;
|
|
|
|
interface QQSession {
|
|
bridge: Bridge;
|
|
}
|
|
|
|
const log = createLogger('Bridge');
|
|
|
|
export class BridgeManager {
|
|
private sessions_ = new Map<string, QQSession>();
|
|
private pidToUin_ = new Map<number, string>();
|
|
private pidPacketClients_ = new Map<number, PacketSender>();
|
|
|
|
private sessionStartedListeners_: SessionStartedCallback[] = [];
|
|
private sessionClosedListeners_: SessionClosedCallback[] = [];
|
|
|
|
/** Additive subscription: every observer (OneBotManager, NotificationManager,
|
|
* …) receives every session edge. (Was a single `set*Callback` setter —
|
|
* converted to N listeners so a second observer can't clobber the first.)
|
|
* The close edge now carries the bridge too, since callers read the last
|
|
* nickname before it is disposed. */
|
|
addSessionStartedListener(cb: SessionStartedCallback): void {
|
|
this.sessionStartedListeners_.push(cb);
|
|
}
|
|
addSessionClosedListener(cb: SessionClosedCallback): void {
|
|
this.sessionClosedListeners_.push(cb);
|
|
}
|
|
|
|
private fireSessionStarted(uin: string, bridge: Bridge): void {
|
|
for (const cb of this.sessionStartedListeners_) {
|
|
try {
|
|
cb(uin, bridge);
|
|
} catch (err) {
|
|
log.warn('session-started listener threw: %s', err instanceof Error ? err.message : String(err));
|
|
}
|
|
}
|
|
}
|
|
|
|
private fireSessionClosed(uin: string, bridge: Bridge): void {
|
|
for (const cb of this.sessionClosedListeners_) {
|
|
try {
|
|
cb(uin, bridge);
|
|
} catch (err) {
|
|
log.warn('session-closed listener threw: %s', err instanceof Error ? err.message : String(err));
|
|
}
|
|
}
|
|
}
|
|
|
|
onPidDisconnected(pid: number): void {
|
|
this.pidPacketClients_.delete(pid);
|
|
const uin = this.pidToUin_.get(pid);
|
|
if (!uin) return;
|
|
|
|
this.pidToUin_.delete(pid);
|
|
const session = this.sessions_.get(uin);
|
|
if (!session) return;
|
|
|
|
session.bridge.detachPid(pid);
|
|
if (session.bridge.empty) {
|
|
this.sessions_.delete(uin);
|
|
log.debug('session closed: UIN=%s', uin);
|
|
// Fire before dispose() so listeners can still read bridge.identity.
|
|
this.fireSessionClosed(uin, session.bridge);
|
|
session.bridge.dispose();
|
|
}
|
|
}
|
|
|
|
onHookLogin(pid: number, uin: string, packetClient: PacketSender): void {
|
|
if (!isRealUin(uin)) return;
|
|
|
|
this.pidPacketClients_.set(pid, packetClient);
|
|
|
|
const { session, created } = this.ensureSession(uin);
|
|
session.bridge.attachPid(pid);
|
|
session.bridge.setPacketClient(packetClient);
|
|
this.pidToUin_.set(pid, uin);
|
|
|
|
if (created) {
|
|
log.debug('session started: UIN=%s', uin);
|
|
this.fireSessionStarted(uin, session.bridge);
|
|
}
|
|
}
|
|
|
|
onPacket(pkt: PacketInfo): void {
|
|
if (!pkt.uin || !isRealUin(pkt.uin)) return;
|
|
const uin = pkt.uin;
|
|
|
|
// Ensure session exists
|
|
const { session, created } = this.ensureSession(uin);
|
|
|
|
// Attach PID if known
|
|
if (pkt.pid && this.pidPacketClients_.has(pkt.pid)) {
|
|
session.bridge.attachPid(pkt.pid);
|
|
this.pidToUin_.set(pkt.pid, uin);
|
|
|
|
const client = this.pidPacketClients_.get(pkt.pid);
|
|
if (client) session.bridge.setPacketClient(client);
|
|
}
|
|
|
|
// Notify session started on first real packet
|
|
if (created) {
|
|
log.debug('session started: UIN=%s', uin);
|
|
this.fireSessionStarted(uin, session.bridge);
|
|
}
|
|
|
|
// Dispatch packet to bridge
|
|
session.bridge.onPacket(pkt);
|
|
}
|
|
|
|
private ensureSession(uin: string): { session: QQSession; created: boolean } {
|
|
let session = this.sessions_.get(uin);
|
|
if (session) return { session, created: false };
|
|
|
|
const bridge = new Bridge(IdentityService.openForUin(uin));
|
|
session = { bridge };
|
|
this.sessions_.set(uin, session);
|
|
|
|
// Each downstream consumer (e.g. OneBotInstance) subscribes to
|
|
// `bridge.events` directly via the per-kind bus — there is no longer a
|
|
// generic firehose to wire here.
|
|
return { session, created: true };
|
|
}
|
|
|
|
getSession(uin: string): QQSession | null {
|
|
return this.sessions_.get(uin) ?? null;
|
|
}
|
|
|
|
get sessions(): Map<string, { bridge: Bridge }> {
|
|
return this.sessions_;
|
|
}
|
|
}
|