feat(protocol): decode online device snapshots

Decode Event 0x210 subtype 349 into typed online-device snapshots, including QQ-compatible low-byte client type normalization and one-entry-per-device-category projection.

Keep replacement snapshots outside system-message deduplication, reject malformed device identities observably, and add wire-level regressions for complete, packed, repeated, and invalid payloads. Live verification covered mobile account switching without requiring native changes.
This commit is contained in:
DrSmoothl committed 2026-07-19 19:19:58 +08:00
1 parent e6a69b05a7
commit b92c8ad881
8 files changed
+279 -3

No files matched your search

@@ -4,6 +4,8 @@ import type {
FriendAddEvent,
FriendPokeEvent,
FriendRequestEvent,
OnlineDeviceInfo,
OnlineDeviceKind,
QQEventVariant,
} from '../../events';
import type {
@@ -12,6 +14,7 @@ import type {
GeneralGrayTipInfo,
InputStatusNotify,
NewFriend,
OnlineDeviceNotify,
ProfileLikeTip,
} from '@snowluma/proto-defs/notify';
import type { PttTransPush } from '@snowluma/proto-defs/ptt-trans';
@@ -42,6 +45,7 @@ export const decodeEvent0x210: MsgPushDecoder = (ctx) => {
return decodeFriendRecall(ctx);
case Event0x210SubType.FriendPokeNotice: return decodeFriendPoke(ctx);
case Event0x210SubType.InputStatusNotice: return decodeInputStatus(ctx);
case Event0x210SubType.OnlineDevicesNotice: return decodeOnlineDevices(ctx);
case Event0x210SubType.ProfileLikeNotice: return decodeProfileLike(ctx);
// 179 + 226 both carry the NewFriend payload — see enum comment.
case Event0x210SubType.NewFriendNotice:
@@ -66,6 +70,63 @@ export const decodeEvent0x210: MsgPushDecoder = (ctx) => {
return [];
};
const COMPUTER_CLIENT_TYPES = new Set([1, 5, 15]);
const PHONE_CLIENT_TYPES = new Set([2, 3, 4, 6, 7, 12]);
const PAD_CLIENT_TYPES = new Set([8, 9, 10, 11, 13]);
function onlineDeviceKind(clientType: number): OnlineDeviceKind {
if (COMPUTER_CLIENT_TYPES.has(clientType)) return 'computer';
if (PHONE_CLIENT_TYPES.has(clientType)) return 'phone';
if (PAD_CLIENT_TYPES.has(clientType)) return 'pad';
unknownLog.warn('online-device snapshot contains unknown clientType=%d', clientType);
return 'unknown';
}
function onlineDeviceClientType(rawClientType: number): number {
// QQ's OnRecvSysMsg path stores this wire field as an unsigned byte before
// it reaches DecodeOnLineDev. Higher bits carry terminal metadata and must
// not participate in device classification (live examples: 0x14702 -> 2,
// 0x10107 -> 7).
return rawClientType & 0xff;
}
function decodeOnlineDevices(ctx: MsgPushContext): QQEventVariant[] {
const notify = protobuf_decode<OnlineDeviceNotify>(ctx.content);
if (!notify) return [];
const devices: OnlineDeviceInfo[] = [];
const seenDeviceKinds = new Set<OnlineDeviceKind>();
for (const [index, item] of (notify.devices ?? []).entries()) {
const appId = item.appId ?? 0;
const rawClientType = item.clientType ?? 0;
if (appId <= 0 || rawClientType <= 0) {
throw new Error(
`online-device entry ${index} is missing appId or clientType `
+ `(appIdPresent=${item.appId != null} clientTypePresent=${item.clientType != null})`,
);
}
const clientType = onlineDeviceClientType(rawClientType);
const deviceKind = onlineDeviceKind(clientType);
// QQ's DecodeOnLineDevList exposes one entry per derived devUid
// (computer/pad/phone), retaining the first item in each category.
if (seenDeviceKinds.has(deviceKind)) continue;
seenDeviceKinds.add(deviceKind);
devices.push({
appId,
instanceId: item.instanceId ?? 0,
clientType,
platform: item.platform ?? 0,
deviceName: item.deviceName ?? '',
deviceKind,
});
}
return [{
kind: 'online_devices_changed',
time: ctx.head.timestamp,
selfUin: ctx.selfUin,
devices,
}];
}
// Voice-to-text async result push. Wire shape (live-verified):
// { f1: uint, f2: { f1 = msgId (echoes the request), f8 = text, ... } }
// `msgId` is the correlation key a pending fetch_ptt_text waits on; we surface
+6
View File
@@ -84,6 +84,12 @@ export enum Event0x210SubType {
* `onInputStatusPush`.
*/
InputStatusNotice = 277,
/**
* QQ NT online-device cache update. The payload is a snapshot with repeated
* device entries; the kernel's getOnLineDev call only replays this cache and
* does not issue a separate network request.
*/
OnlineDevicesNotice = 349,
/**
* Group-app state push (troop shortcut bar / discussion app).
*
+13 -1
View File
@@ -57,6 +57,13 @@ 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 +
@@ -127,7 +134,12 @@ export function parseMsgPush(
// 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))) {
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);