diff --git a/packages/core/tests/actions/friend.test.ts b/packages/core/tests/actions/friend.test.ts index ccebec0b..cd2bf732 100644 --- a/packages/core/tests/actions/friend.test.ts +++ b/packages/core/tests/actions/friend.test.ts @@ -1,6 +1,7 @@ import { describe, it, expect, vi } from 'vitest'; import { subscribeLogs, type LogEntry } from '@snowluma/common/logger'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { pb, pb_repeated, uint_32, uint_64 } from '@snowluma/proton'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; import type { OidbDeleteFriend, @@ -117,7 +118,7 @@ describe('apis/friend', () => { gotResponse: true, errorCode: 0, errorMessage: '', - responseData: encodeDoubtList([{ nick: 'Alice', uin: 12345n, reqTime: 1700000000n }]), + responseData: encodeNumericDoubtList([{ nick: 'Alice', uin: 12345n, reqTime: 1700000000n }]), })), }); const list = await new FriendApi(bridge as any).getDoubtRequests(10); @@ -132,13 +133,14 @@ describe('apis/friend', () => { it('getDoubtRequests keeps a wire uid and does not resolve', async () => { const bridge = mockBridge({ + identity: { findUinByUid: vi.fn(() => 12345) } as any, sendRawPacket: vi.fn(async () => ({ success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: encodeDoubtList([ - { uid: 'u_alice', nick: 'Alice', uin: 12345n, reqTime: 1700000000n }, + { uid: 'u_alice', nick: 'Alice', reqTime: 1700000000n }, ]), })), }); @@ -156,7 +158,7 @@ describe('apis/friend', () => { gotResponse: true, errorCode: 0, errorMessage: '', - responseData: encodeDoubtList([{ nick: 'Alice', uin: 12345n }]), + responseData: encodeNumericDoubtList([{ nick: 'Alice', uin: 12345n }]), })), }); const captured: LogEntry[] = []; @@ -214,3 +216,24 @@ function encodeDoubtList( body: { status: 1, body: { list } }, })); } + +interface NumericDoubtItem { + uin?: pb<1, uint_64>; + nick?: pb<2, string>; + reqTime?: pb<8, uint_64>; +} +interface NumericDoubtBody { + list?: pb_repeated<1, NumericDoubtItem>; +} +interface NumericDoubtResponse { + status?: pb<1, uint_32>; + body?: pb<2, NumericDoubtBody>; +} + +function encodeNumericDoubtList(list: NumericDoubtItem[]): Buffer { + return Buffer.from(protobuf_encode>({ + command: 0xD69, + subCommand: 0, + body: { status: 1, body: { list } }, + })); +} diff --git a/packages/proto-defs/src/oidb-actions/doubt-buddy.ts b/packages/proto-defs/src/oidb-actions/doubt-buddy.ts index 70187e36..d5f84344 100644 --- a/packages/proto-defs/src/oidb-actions/doubt-buddy.ts +++ b/packages/proto-defs/src/oidb-actions/doubt-buddy.ts @@ -5,8 +5,11 @@ // GET request (EncodeRequest sub_3F3DC20): {1:const 1, 2:{1:num, 2:uk}}. // reqId is NOT serialized (kernel uses it only for JS callback correlation). // GET response: status (==1 ok) + body{1:repeated item, 2:reason}. -// Per-item: uid(1) and reqTime(9,u64) are HIGH confidence (read verbatim -// from the codec). The string tags' semantic NAMES (nick/source/msg) come +// Linux 3.2.32 sends the account number at tag 1 (varint), request time at +// tag 8, and source group at tag 9; it carries no string uid. Some builds +// send a string uid at tag 1 instead, so the service decodes both wire +// types and keeps whichever is present. +// The string tags' semantic NAMES (nick/source/msg) come // from the GENERIC buddy serializer registry, not a doubt-specific table, // so they are MEDIUM confidence — but this is a READ, so a mislabel is // cosmetic, never a malformed-packet/ban risk. We model the ones NapCat @@ -34,10 +37,9 @@ export interface OidbDoubtItem { msg?: pb<5, string>; source?: pb<6, string>; reason?: pb<7, string>; - uin?: pb<8, uint_64>; - reqTime?: pb<9, uint_64>; + reqTime?: pb<8, uint_64>; + groupCode?: pb<9, uint_64>; commFriendNum?: pb<10, uint_32>; - groupCode?: pb<11, string>; } export interface OidbDoubtGetRespBody { list?: pb_repeated<1, OidbDoubtItem>; diff --git a/packages/protocol/src/oidb-services/friend/get-doubt-buddy-req.ts b/packages/protocol/src/oidb-services/friend/get-doubt-buddy-req.ts index d8faac92..888a5273 100644 --- a/packages/protocol/src/oidb-services/friend/get-doubt-buddy-req.ts +++ b/packages/protocol/src/oidb-services/friend/get-doubt-buddy-req.ts @@ -1,17 +1,37 @@ // 0xd69_0 — getDoubtBuddyReq: list the "doubtful" friend-add requests // (可能认识的人 / 被过滤的好友申请). RE'd from QQNT doubt_codec.cc. // Request {1:1, 2:{1:num, 2:uk}} (reqId is NOT on the wire). Response body -// holds a repeated item list. DecodePullDoubtReq maps uid as a string and -// the account number as a separate integer field; do not reread the uid -// field as a number. String names (nick/source/msg) are MEDIUM confidence. +// holds a repeated item list. Two wire shapes exist for the per-item id: +// Linux 3.2.32 sends the account number as a tag-1 varint (no string uid), +// while some builds send a string uid at tag 1. The item is decoded twice so +// the numeric and string shapes merge; reqTime(8) and groupCode(9) are +// numeric on Linux. String names (nick/source/msg) are MEDIUM confidence. import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { pb, pb_repeated, uint_32, uint_64 } from '@snowluma/proton'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; import type { OidbDoubtGetReq, OidbDoubtGetResp, } from '@snowluma/proto-defs/oidb-actions/doubt-buddy'; import { invokeOidb, type OidbSender } from '../../oidb-service'; +interface OidbDoubtItemAccountOnTag1 { + uin?: pb<1, uint_64>; +} +interface OidbDoubtGetRespAccountOnTag1Body { + list?: pb_repeated<1, OidbDoubtItemAccountOnTag1>; +} +interface OidbDoubtGetRespAccountOnTag1 { + status?: pb<1, uint_32>; + body?: pb<2, OidbDoubtGetRespAccountOnTag1Body>; +} +type DecodedDoubtItem = NonNullable['list']>[number] & { + uin?: bigint; +}; +interface DecodedDoubtGetResp extends Omit { + body?: Omit, 'list'> & { list?: DecodedDoubtItem[] }; +} + export interface DoubtBuddyRequest { [key: string]: import('@snowluma/common/json').JsonValue; /** Opaque uid — pass back as `flag` to set_doubt_friends_add_request. */ @@ -38,7 +58,7 @@ export namespace GetDoubtBuddyReq { inner: { num: p.count, uk: p.cookie ?? '' }, }); - export const deserialize = (_ctx: Deps, body: OidbDoubtGetResp): DoubtBuddyRequest[] => + export const deserialize = (_ctx: Deps, body: DecodedDoubtGetResp): DoubtBuddyRequest[] => (body.body?.list ?? []).map((it) => ({ uid: it.uid ?? '', user_id: Number(it.uin ?? 0), @@ -46,15 +66,23 @@ export namespace GetDoubtBuddyReq { source: it.source ?? '', reason: it.reason ?? '', msg: it.msg ?? '', - group_code: it.groupCode ?? '', + group_code: it.groupCode ? String(it.groupCode) : '', reqTime: Number(it.reqTime ?? 0), })); export const encode = (env: OidbBase): Uint8Array => protobuf_encode>(env); - export const decode = (bytes: Uint8Array): OidbBase => - protobuf_decode>(bytes); + export const decode = (bytes: Uint8Array): OidbBase => { + const decoded = protobuf_decode>(bytes); + const numeric = protobuf_decode>(bytes); + const items = decoded.body?.body?.list ?? []; + const numericItems = numeric.body?.body?.list ?? []; + for (let i = 0; i < items.length; i++) { + (items[i] as DecodedDoubtItem).uin = numericItems[i]?.uin; + } + return decoded as OidbBase; + }; export const invoke = (deps: Deps, params: Params): Promise => invokeOidb(deps, GetDoubtBuddyReq, params); diff --git a/packages/protocol/tests/oidb-services/friend/fixtures/d69-linux-3.2.32.hex b/packages/protocol/tests/oidb-services/friend/fixtures/d69-linux-3.2.32.hex new file mode 100644 index 00000000..e22111c5 --- /dev/null +++ b/packages/protocol/tests/oidb-services/friend/fixtures/d69-linux-3.2.32.hex @@ -0,0 +1 @@ +08e91a22a60c080112a10c0a7e0880c2d72f12066e69636b3030180020fe012a056d73673030320851512067726f75703a1766696c746572656420667269656e6420726571756573744080c3bbc2064880cee4cd02500062006a30abc8a360ea075bcf8bbfa1e1e0e010beb1ac5228138eb82586704b41691e9efea640392a2a65e9dd50de82f154e0201b0a7d08c7aa9b3012066e69636b3031180020012a056d73673031320851512067726f75703a1766696c746572656420667269656e6420726571756573744080e6c0c206488dcee4cd02500162006a30ee591d017a9bb6807f7db7c8d3665e34923c7db6f6252d672a8f1f8986c7c9e3d21415ea86987a1b689c8d675d2a26800a7d088e93df3012066e69636b3032180020012a056d73673032320851512067726f75703a1766696c746572656420667269656e642072657175657374408089c6c206489acee4cd02500062006a305fe9e2cce8e6bc04c4f76fe8872347907e73a73b9184c3a7cbbd9c85a1d319c252129d950d06920d50732964df4afcf20a810108d5fba23112066e69636b3033180020ffffffff0f2a056d73673033320851512067726f75703a1766696c746572656420667269656e6420726571756573744080accbc20648a7cee4cd02500062006a3006b881debd01f92c98ef3c1726406ebeb88cae9d12d89e9fa36c20a77495dd0a3eb9fbbe7b14f65d657e979adfda5c600a7d089ce4e63112066e69636b3034180020012a056d73673034320851512067726f75703a1766696c746572656420667269656e6420726571756573744080cfd0c20648b4cee4cd02500062006a30316f76c654c33c8f6f3689a9475d8b871f74654789edb72d3f1a87bb1ba11cf07286b8d748f4d08d6fbe5afee8208e010a870108e3ccaa3212066e69636b3035185920ffffffff0f2a056d73673035320851512067726f75703a1766696c746572656420667269656e6420726571756573744080f2d5c20648c1cee4cd0250006206e4b89ce4baac6a30a3038a382c30ee6a8686410089e1c618f0b11bf92fdf2726da2910d2082a147a5569d04815e7c42287b424ef45b00ff70a860108aab5ee3212066e69636b3036180120002a056d73673036320851512067726f75703a1766696c746572656420667269656e642072657175657374408095dbc20648cecee4cd0250006209e7a59ee5a588e5b79d6a3097d6cc1f21ca290ab424147c3181e68490c49de47eda96eb422ec861eefdc8f580578655ac4f874ab7e8467cfc0e9fd30a810108f19db23312066e69636b3037180020ffffffff0f2a056d73673037320851512067726f75703a1766696c746572656420667269656e6420726571756573744080b8e0c20648dbcee4cd02500062006a303c1421acb295e3a5a4c914625ef1dbee99b552716ed28046cfa3b9171518dab5c247e0ddd3a24cb5f318a1cb60d6c9b30a810108b886f63312066e69636b3038180020ffffffff0f2a056d73673038320851512067726f75703a1766696c746572656420667269656e6420726571756573744080dbe5c20648e8cee4cd02500162006a3020ac3ddb705e3246f8fe7d266becea5bf92ec584a2cc12d4a83197d782047ca61d0ed90c5c4419e0866940f22a3eb1420a7d08ffeeb93412066e69636b3039180020012a056d73673039320851512067726f75703a1766696c746572656420667269656e6420726571756573744080feeac20648f5cee4cd02500162006a3047da9cf4f576624c5edd21b6bcfda8898c91143711f0bd251e00dfb1c273738290fa190cf6dc231c41dc31ad2086c6050a7d08c6d7fd3412066e69636b3130181420002a056d73673130320851512067726f75703a1766696c746572656420667269656e6420726571756573744080a1f0c2064882cfe4cd02500062006a300ab4598cfe36f3f340ec5237766213a06ddd99d900c3b3ac4d18530d4308f17943a22ef6b85d4d3f08086dcaf36465550a8401088dc0c13512066e69636b3131181220fe012a056d73673131320851512067726f75703a1766696c746572656420667269656e6420726571756573744080c4f5c206488fcfe4cd0250016206e697a0e994a16a3006e504c8f85b64fa8d5cb196d4b8cebc8098c752d879880bd8ab0108a829a1b29f952ff4e4b3807af4d54f70fb0dbf50 diff --git a/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts b/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts index 738d4c1d..3440792d 100644 --- a/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts +++ b/packages/protocol/tests/oidb-services/friend/get-doubt-buddy-req.test.ts @@ -1,10 +1,12 @@ import { describe, expect, it, vi } from 'vitest'; +import { readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; import { protobuf_encode } from '@snowluma/proton'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; import type { OidbDoubtGetResp } from '@snowluma/proto-defs/oidb-actions/doubt-buddy'; import type { SendPacketResult } from '@snowluma/common/packet-sender'; import { GetDoubtBuddyReq } from '../../../src/oidb-services/friend/get-doubt-buddy-req'; -import { env, v, m } from '../_pb-oracle'; +import { env, v, s, m } from '../_pb-oracle'; function makeSender(): { sendRawPacket: ReturnType } { const respEnv: OidbBase = { @@ -12,7 +14,7 @@ function makeSender(): { sendRawPacket: ReturnType } { body: { status: 1, body: { list: [ { uid: 'u_alice', nick: 'Alice', source: '可能认识', reason: '', - msg: 'hi', uin: 12345n, groupCode: '', reqTime: 1700000000n, + msg: 'hi', reqTime: 1700000000n, }, ], reason: '' } }, }; @@ -46,11 +48,11 @@ describe('GetDoubtBuddyReq namespace', () => { expect(Buffer.from(bytes).toString('hex')).toBe(env(0xD69, 0, body, true)); }); - it('deserializes the item list to the OneBot shape (uid + reqTime high-confidence)', async () => { + it('deserializes the string uid item to the OneBot shape', async () => { const list = await GetDoubtBuddyReq.invoke(makeSender(), { count: 10 }); expect(list).toEqual([ { - uid: 'u_alice', user_id: 12345, nick: 'Alice', source: '可能认识', + uid: 'u_alice', user_id: 0, nick: 'Alice', source: '可能认识', reason: '', msg: 'hi', group_code: '', reqTime: 1700000000, }, ]); @@ -73,16 +75,20 @@ describe('GetDoubtBuddyReq namespace', () => { }); it('keeps the uid string and does not reread it as the account number', async () => { - const respEnv: OidbBase = { - command: 0xD69, subCommand: 0, - body: { status: 1, body: { list: [ - { uid: 'u_alice', nick: 'Alice', msg: 'hi', source: 'QQ号查找', reason: 'caution' }, - ] } }, - }; + const item = [ + ...s(1, 'u_alice'), + ...s(2, 'Alice'), + ...s(5, 'hi'), + ...s(6, 'QQ号查找'), + ...s(7, 'caution'), + ]; const sender = { sendRawPacket: vi.fn(async () => ({ success: true, gotResponse: true, errorCode: 0, errorMessage: '', - responseData: Buffer.from(protobuf_encode>(respEnv)), + responseData: Buffer.from(env(0xD69, 0, [ + ...v(1, 1), + ...m(2, m(1, item)), + ], false), 'hex'), })), }; const list = await GetDoubtBuddyReq.invoke(sender, { count: 10 }); @@ -93,4 +99,44 @@ describe('GetDoubtBuddyReq namespace', () => { }, ]); }); + + // Live capture from QQ 3.2.32-52194 on Linux. Locks the real wire shape: + // tag1 is the applicant's account number (not a string uid), tag8 is the + // request unix time, and tag9 is the source group. Before this, a tag8-as-uin + // read made the uin->uid fallback query the request time as an account. + it('decodes a captured Linux reply: account at tag1, time at tag8, group at tag9', () => { + const hex = readFileSync( + fileURLToPath(new URL('./fixtures/d69-linux-3.2.32.hex', import.meta.url)), + 'utf8', + ).trim(); + const list = GetDoubtBuddyReq.deserialize( + {} as any, + GetDoubtBuddyReq.decode(Buffer.from(hex, 'hex')).body!, + ); + + expect(list).toHaveLength(12); + expect(list[0]).toMatchObject({ + uid: '', + user_id: 100000000, + nick: 'nick00', + source: 'QQ group', + reason: 'filtered friend request', + msg: 'msg00', + group_code: '700000000', + reqTime: 1750000000, + }); + expect(list[1]).toMatchObject({ + uid: '', + user_id: 101111111, + group_code: '700000013', + reqTime: 1750086400, + }); + // Account numbers stay in the QQ range; request times land in 2025-2026. + for (const item of list) { + expect(item.user_id).toBeGreaterThan(100000); + expect(item.user_id).toBeLessThan(4294967296); + expect(item.reqTime).toBeGreaterThan(1700000000); + expect(item.reqTime).toBeLessThan(2000000000); + } + }); });