fix(protocol): scope system dedup to native routes
Derive QQ NT's peerUid_chatType_msg_seq_random identity from its reverse-engineered static routing table before event dispatch. Unknown routes and incomplete identities fail open, while C2C and group peers use their native namespaces. Refines #266
This commit is contained in:
3 files changed
+235
-103
No files matched your search
@@ -21,9 +21,16 @@ import { decodeGroupMessage } from './decoders/group-message';
|
||||
import { decodeTempMessage } from './decoders/temp-message';
|
||||
import { PkgType } from './enums';
|
||||
import { MsgPushRegistry } from './registry';
|
||||
import { SysMsgDedup } from './sysmsg-dedup';
|
||||
import {
|
||||
deriveSysMsgDedupIdentity,
|
||||
SysMsgDedup,
|
||||
} from './sysmsg-dedup';
|
||||
|
||||
export { SysMsgDedup } from './sysmsg-dedup';
|
||||
export {
|
||||
deriveSysMsgDedupIdentity,
|
||||
SysMsgDedup,
|
||||
} from './sysmsg-dedup';
|
||||
export type { SysMsgDedupIdentity } from './sysmsg-dedup';
|
||||
|
||||
export { SSO_GET_GROUP_MSG_CMD, fetchGroupMessageRange } from './fetch-group-history';
|
||||
export {
|
||||
@@ -63,13 +70,6 @@ const MESSAGE_KINDS = new Set<QQEventVariant['kind']>([
|
||||
'friend_message', 'group_message', 'temp_message',
|
||||
]);
|
||||
|
||||
// Replacement snapshots describe the complete current state. They are
|
||||
// idempotent, but not duplicate events: a later snapshot may legitimately
|
||||
// reuse the enclosing system-message identity while carrying new state.
|
||||
const STATE_SNAPSHOT_KINDS = new Set<QQEventVariant['kind']>([
|
||||
'online_devices_changed',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Summarise a body that decoded to zero elements despite carrying content —
|
||||
* each element's field names, every `commonElem`'s serviceType/businessType +
|
||||
@@ -118,6 +118,28 @@ export function parseMsgPush(
|
||||
): QQEventVariant[] {
|
||||
const ctx = buildContext(pkt, identity);
|
||||
if (!ctx) return [];
|
||||
|
||||
// #137/#266: mirror QQ NT `sys_msg_mgr.cc::ProcessRecvSysMsg` before
|
||||
// dispatch. Its static routing table is the scope boundary: listed routes
|
||||
// use the native global key, while unlisted routes fail open. Running this
|
||||
// before registry decode also covers native system packets whose outer type
|
||||
// SnowLuma otherwise interprets as a chat message or does not decode at all.
|
||||
if (dedup) {
|
||||
const dedupIdentity = deriveSysMsgDedupIdentity(ctx);
|
||||
if (dedupIdentity && dedup.seenDuplicate(dedupIdentity)) {
|
||||
log.debug(
|
||||
'dropped duplicate system push (peer=%s chatType=%d seq=%d msgType=%d/%d msgId=%d)',
|
||||
dedupIdentity.peerUid,
|
||||
dedupIdentity.chatType,
|
||||
ctx.head.sequence,
|
||||
ctx.head.msgType,
|
||||
ctx.head.subType,
|
||||
ctx.head.msgId,
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
const events = registry.decode(ctx);
|
||||
const out = events.filter((ev) => {
|
||||
if (!MESSAGE_KINDS.has(ev.kind)) return true;
|
||||
@@ -151,25 +173,5 @@ export function parseMsgPush(
|
||||
return false;
|
||||
});
|
||||
|
||||
// #137: mirror QQ NT `sys_msg_mgr.cc::ProcessRecvSysMsg` global-key dedup.
|
||||
// The server pushes some system notices twice (e.g. inviting an official
|
||||
// robot → two `group_member_increase`); the kernel drops the duplicate by
|
||||
// (peer, seq, random) before any listener sees it, but we read the raw
|
||||
// OlPush, so we replicate the drop. Scoped to system pushes only — chat
|
||||
// messages have their own NT dedup path and the forward re-parse re-runs
|
||||
// this without a tracker. A push that decodes to a message kind, or to
|
||||
// nothing, is never deduped here.
|
||||
if (
|
||||
dedup
|
||||
&& out.length > 0
|
||||
&& out.every((ev) => !MESSAGE_KINDS.has(ev.kind))
|
||||
&& out.every((ev) => !STATE_SNAPSHOT_KINDS.has(ev.kind))
|
||||
) {
|
||||
if (dedup.seenDuplicate(ctx.head, ctx.fromUin)) {
|
||||
log.debug('dropped duplicate system push (kinds=%s seq=%d from=%d msgType=%d msgId=%d)',
|
||||
out.map((ev) => ev.kind).join(','), ctx.head.sequence, ctx.fromUin, ctx.head.msgType, ctx.head.msgId);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -1,42 +1,66 @@
|
||||
import type { MsgPushHead } from './context';
|
||||
import type { MsgPushContext } from './context';
|
||||
|
||||
export type SysMsgChatType = 1 | 2;
|
||||
|
||||
export interface SysMsgDedupIdentity {
|
||||
readonly peerUid: string;
|
||||
readonly chatType: SysMsgChatType;
|
||||
readonly sequence: number;
|
||||
readonly random: number;
|
||||
}
|
||||
|
||||
function getNativeSysMsgChatType(msgType: number, subType: number): SysMsgChatType | null {
|
||||
if (msgType === 528 && subType === 290) return 1;
|
||||
if (msgType === 166 && (subType === 75 || subType === 129 || subType === 131 || subType === 133 || subType === 135)) return 1;
|
||||
if (msgType === 167 && subType === 133) return 1;
|
||||
|
||||
if (msgType === 732 && (subType === 12 || subType === 16 || subType === 20)) return 2;
|
||||
if ((msgType === 33 || msgType === 34 || msgType === 85) && subType === 0) return 2;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirrors QQ NT's system-message dedup so SnowLuma stops double-reporting the
|
||||
* duplicate sys pushes the server emits for some events (#137: inviting an
|
||||
* official robot pushes the `group_member_increase` notice twice → two
|
||||
* `notice.group_increase`; a normal member is pushed once).
|
||||
* Derive the identity used by QQ NT's system-message dedup. Only the static
|
||||
* `(msgType, subType)` routes proven in `sys_msg_mgr.cc` participate; unknown
|
||||
* routes fail open rather than risking suppression of unrelated events.
|
||||
*/
|
||||
export function deriveSysMsgDedupIdentity(
|
||||
ctx: Pick<MsgPushContext, 'head' | 'fromUin' | 'fromUid'>,
|
||||
): SysMsgDedupIdentity | null {
|
||||
const { head } = ctx;
|
||||
const chatType = getNativeSysMsgChatType(head.msgType, head.subType);
|
||||
// Decoded zero is also the protobuf default for an absent field. Without a
|
||||
// complete server identity, prefer a duplicate event over suppressing two
|
||||
// unrelated pushes that collapse to the same default-valued key.
|
||||
if (chatType === null || head.sequence === 0 || head.msgId === 0) return null;
|
||||
|
||||
const peerUid = chatType === 1 ? ctx.fromUid : String(ctx.fromUin);
|
||||
if (!peerUid || (chatType === 2 && ctx.fromUin === 0)) return null;
|
||||
|
||||
return {
|
||||
peerUid,
|
||||
chatType,
|
||||
sequence: head.sequence,
|
||||
random: head.msgId,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirrors QQ NT's system-message dedup so SnowLuma stops double-reporting raw
|
||||
* OlPush system events that the QQ kernel would discard before notifying its
|
||||
* listeners.
|
||||
*
|
||||
* RE of `wrapper.linux.node` — `sys_msg_mgr.cc::ProcessRecvSysMsg`
|
||||
* (`sub_37BADE0`): every received system message is keyed and looked up in a
|
||||
* per-account set; an already-seen key is logged ("on recv sys msg, ignore dup
|
||||
* msg … global_key={}", sys_msg_mgr.cc:467) and the whole message is dropped
|
||||
* before the UI / JS listeners ever see it. The key (`sub_37BC4E0`) is
|
||||
* macOS QQ 6.9.98 `sys_msg_mgr.cc::ProcessRecvSysMsg` (`sub_E4B94A`) builds:
|
||||
*
|
||||
* global_key = `{peerUid}_{chatType}_{msg_seq}_{random}`
|
||||
*
|
||||
* whose per-message discriminators are `msg_seq` (attr 40003 = contentHead
|
||||
* field 5) and `random` (attr 40002 = contentHead field 4) — exactly
|
||||
* {@link MsgPushHead.sequence} and {@link MsgPushHead.msgId} here
|
||||
* (`msg_header_codec_helper.cc` sub_37C8AD0). Kernel-based bots (NapCat /
|
||||
* LLOneBot) receive events *after* this dedup; SnowLuma reads the raw OlPush
|
||||
* *before* it, so we replicate it here.
|
||||
*
|
||||
* We dedup at the push level (drop the whole duplicate, like NT) using
|
||||
* `sequence` + `msgId` and `fromUin` for peer scoping (a stand-in for peerUid —
|
||||
* `msg_seq` can be per-conversation, so the peer keeps two groups' pushes from
|
||||
* colliding; `random`/`msgId` makes the collision astronomically unlikely
|
||||
* anyway). `msgType` and `subType` are deliberately absent: they are not part
|
||||
* of QQ NT's global key, and one server system message may arrive through
|
||||
* different outer package types (#266). A push with no server identity
|
||||
* (`sequence` or `msgId` 0) is never deduped — without a real per-message id we
|
||||
* cannot distinguish a true duplicate from two distinct events, and dropping
|
||||
* then would be a regression.
|
||||
* `sub_E523C8` maps content-head fields 5 and 4 to `msg_seq` and `random`.
|
||||
* `sub_E4B30D` supplies chatType from a static `(msgType, subType)` table, and
|
||||
* `sub_E4B62A` selects response-head `fromUid` for C2C or decimal `fromUin` for
|
||||
* groups. {@link deriveSysMsgDedupIdentity} mirrors those routes exactly.
|
||||
*/
|
||||
export class SysMsgDedup {
|
||||
private readonly seen = new Set<string>();
|
||||
// Fixed-capacity ring of keys for O(1) bounded eviction (oldest-out). System
|
||||
// pushes are low-frequency, so a duplicate always lands within a few entries
|
||||
// of its original — eviction only matters far beyond the dup window.
|
||||
private readonly ring: (string | undefined)[];
|
||||
private cursor = 0;
|
||||
|
||||
@@ -44,13 +68,10 @@ export class SysMsgDedup {
|
||||
this.ring = new Array<string | undefined>(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if a system push with this identity was already seen (caller
|
||||
* should drop it); otherwise records it and returns false.
|
||||
*/
|
||||
seenDuplicate(head: Pick<MsgPushHead, 'sequence' | 'msgId'>, fromUin: number): boolean {
|
||||
if (head.sequence === 0 || head.msgId === 0) return false;
|
||||
const key = `${fromUin}:${head.sequence}:${head.msgId}`;
|
||||
/** Returns true when this native system-message identity was already seen. */
|
||||
seenDuplicate(identity: SysMsgDedupIdentity): boolean {
|
||||
if (identity.sequence === 0 || identity.random === 0 || !identity.peerUid) return false;
|
||||
const key = `${identity.peerUid}_${identity.chatType}_${identity.sequence}_${identity.random}`;
|
||||
if (this.seen.has(key)) return true;
|
||||
const evicted = this.ring[this.cursor];
|
||||
if (evicted !== undefined) this.seen.delete(evicted);
|
||||
|
||||
Reference in new issue
Block a user