fix(core): finalize c2c file upload so the recipient can download (#157)
Private (c2c) file sends uploaded the bytes (apply 0xE37_1700 + Highway) and PbSendMsg returned ok, but the receiver saw "文件传输失败": the message carried only FileExtra.file (metadata) and not the server-issued download routing. Group files use a different element, hence c2c-only. Port NapCat's upload→finalize→send sequence: - add 0xE37_800 (FinalizeOfflineFile) OIDB service, run after the Highway PUT to fetch the download metadata (field30) - extend FileExtra with field6 (PrivateFileExtra.field2) and populate it from that metadata in sendC2cFile; covers both the url-upload and the file_id-resend send paths (both funnel through sendC2cFile) Tests: byte-oracle locks the 0xE37_800 request + FileExtra.field6 tags; core routing test now asserts finalize runs and field6 is populated. Needs real-machine verification (send a private file, confirm download).
This commit is contained in:
7 files changed
+325
-3
No files matched your search
@@ -9,6 +9,7 @@ import type {
|
||||
SsoReadedReportReq,
|
||||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||||
import { buildSendElems } from '@snowluma/protocol/element-builder';
|
||||
import { FinalizeOfflineFile } from '@snowluma/protocol/oidb-services/group-file/finalize-offline-file';
|
||||
import type { MessageElement, QQEventVariant } from '@snowluma/protocol/events';
|
||||
import { fetchC2cMessageRange, fetchGroupMessageRange } from '@snowluma/protocol/msg-push';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
@@ -238,6 +239,11 @@ export class MessageApi {
|
||||
* - `subcmd: 1` — c2c file send command code
|
||||
* - `dangerEvel: 0` — virus-scan severity, always 0 client-side
|
||||
* - `expireTime` — 7 days from now (Lagrange convention)
|
||||
*
|
||||
* Beyond NotOnlineFile (`FileExtra.file`), the message MUST also carry
|
||||
* `FileExtra.field6` — the server-issued download routing. We obtain it by
|
||||
* running the 0xE37_800 finalize first; without it the recipient sees
|
||||
* "文件传输失败" even though PbSendMsg returns ok (issue #157).
|
||||
*/
|
||||
async sendC2cFile(
|
||||
userUin: number,
|
||||
@@ -247,6 +253,28 @@ export class MessageApi {
|
||||
const random = this.ctx.nextMessageRandom();
|
||||
const clientSeq = this.ctx.nextClientSequence();
|
||||
|
||||
// Resolve our own uid — needed both for the 0xE37_800 finalize and the
|
||||
// `field6` download-routing the receiver reads.
|
||||
let selfUid = this.ctx.identity.selfUid;
|
||||
if (!selfUid) {
|
||||
const selfUin = Number.parseInt(this.ctx.identity.uin ?? '', 10);
|
||||
if (Number.isFinite(selfUin) && selfUin > 0) selfUid = await this.ctx.resolveUserUid(selfUin);
|
||||
}
|
||||
if (!selfUid) throw new Error('self uid unavailable for c2c file send');
|
||||
|
||||
// Finalize the offline file (0xE37_800) to obtain the server-issued
|
||||
// download routing. The apply + Highway upload alone leave the file
|
||||
// un-downloadable; `field6` below carries the credential the recipient
|
||||
// needs (see finalize-offline-file.ts). Mirrors NapCat's upload→finalize
|
||||
// →send sequence (issue #157).
|
||||
const fileHash = info.fileHash ?? '';
|
||||
const meta = await FinalizeOfflineFile.invoke(this.ctx, {
|
||||
senderUid: selfUid,
|
||||
receiverUid: userUid,
|
||||
fileUuid: info.fileId,
|
||||
fileHash,
|
||||
});
|
||||
|
||||
const nowSec = Math.floor(Date.now() / 1000);
|
||||
const sevenDaysSec = 7 * 24 * 60 * 60;
|
||||
const fileExtraBytes = protobuf_encode<FileExtra>({
|
||||
@@ -259,7 +287,21 @@ export class MessageApi {
|
||||
subcmd: 1,
|
||||
dangerEvel: 0,
|
||||
expireTime: nowSec + sevenDaysSec,
|
||||
fileHash: info.fileHash ?? '',
|
||||
fileHash,
|
||||
},
|
||||
field6: {
|
||||
field2: {
|
||||
field1: meta.field110,
|
||||
fileUuid: info.fileId,
|
||||
fileName: info.fileName,
|
||||
field6: meta.field3,
|
||||
field7: meta.field101,
|
||||
field8: meta.field100,
|
||||
timestamp1: meta.timestamp1,
|
||||
fileHash,
|
||||
selfUid,
|
||||
destUid: userUid,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import type { SendMessageRequest, SendMessageResponse } from '@snowluma/proto-defs/action';
|
||||
import type { FileExtra } from '@snowluma/proto-defs/message';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbOfflineFileFinalizeResp } from '@snowluma/proto-defs/oidb-actions/media';
|
||||
|
||||
vi.mock('@snowluma/protocol/element-builder', () => ({
|
||||
buildSendElems: vi.fn(async () => [{ text: { str: 'stub media elem' } }]),
|
||||
@@ -64,7 +66,23 @@ describe('Bridge private media routing', () => {
|
||||
|
||||
class TestBridge extends Bridge {
|
||||
capturedBody: Uint8Array | null = null;
|
||||
override async sendRawPacket(_cmd: string, body: Uint8Array): Promise<SendPacketResult> {
|
||||
finalizeCmd: string | null = null;
|
||||
|
||||
override async resolveUserUid(uin: number): Promise<string> {
|
||||
return uin === 10000 ? 'u_self' : `u_${uin}`;
|
||||
}
|
||||
|
||||
override async sendRawPacket(cmd: string, body: Uint8Array): Promise<SendPacketResult> {
|
||||
// The c2c file send first runs the 0xE37_800 finalize, then PbSendMsg.
|
||||
if (cmd.includes('0xe37_800')) {
|
||||
this.finalizeCmd = cmd;
|
||||
return {
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
responseData: Buffer.from(protobuf_encode<OidbBase<OidbOfflineFileFinalizeResp>>({
|
||||
body: { body: { metadata: { field3: 11, field100: new Uint8Array([1]), field101: new Uint8Array([2]), field110: 22, timestamp1: 1710000123 } } },
|
||||
})),
|
||||
};
|
||||
}
|
||||
this.capturedBody = body;
|
||||
return {
|
||||
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
|
||||
@@ -118,6 +136,22 @@ describe('Bridge private media routing', () => {
|
||||
// expireTime is now+7d; just sanity-check it landed (non-zero, plausible)
|
||||
expect(fileExtra?.file?.expireTime).toBeGreaterThan(Math.floor(Date.now() / 1000));
|
||||
|
||||
// issue #157: the finalize (0xE37_800) must run, and its download routing
|
||||
// must ride along in field6 — without it the recipient can't download.
|
||||
expect(bridge.finalizeCmd).toBe('OidbSvcTrpcTcp.0xe37_800');
|
||||
expect(fileExtra?.field6?.field2).toMatchObject({
|
||||
fileUuid: 'uuid-abc-123',
|
||||
fileName: 'doc.txt',
|
||||
fileHash: 'hash-xyz',
|
||||
selfUid: 'u_self',
|
||||
destUid: 'u_peer_xyz',
|
||||
field1: 22, // ← finalize metadata.field110
|
||||
field6: 11, // ← finalize metadata.field3
|
||||
timestamp1: 1710000123,
|
||||
});
|
||||
expect(fileExtra?.field6?.field2?.field7).toEqual(new Uint8Array([2])); // ← metadata.field101
|
||||
expect(fileExtra?.field6?.field2?.field8).toEqual(new Uint8Array([1])); // ← metadata.field100
|
||||
|
||||
// c2cCmd is left at 0 / undefined — the old `c2cCmd=11` was a
|
||||
// stale go-cqhttp value the QQ-NT server doesn't recognise on the
|
||||
// c2c-file path.
|
||||
|
||||
@@ -85,8 +85,29 @@ export interface MessageBody {
|
||||
|
||||
// C2C 文件附加信息
|
||||
// 统一使用与线上数据对齐的 NotOnlineFile 结构,修复收发双向的解析问题。
|
||||
//
|
||||
// `field6` 携带服务器签发的下载路由(来自 0xE37_800 finalize 响应的
|
||||
// metadata)。发送 c2c 文件时缺了它,接收方点开会"文件传输失败"——
|
||||
// `file` 只是元数据,真正的下载凭证在这里。字段 tag 对照 NapCat
|
||||
// `message/component.ts: PrivateFileExtra / PrivateFileExtraField2`。
|
||||
export interface PrivateFileExtraField2 {
|
||||
field1?: pb<1, uint_32>; // ← finalize metadata.field110
|
||||
fileUuid?: pb<4, string>;
|
||||
fileName?: pb<5, string>;
|
||||
field6?: pb<6, uint_32>; // ← finalize metadata.field3
|
||||
field7?: pb<7, bytes>; // ← finalize metadata.field101
|
||||
field8?: pb<8, bytes>; // ← finalize metadata.field100
|
||||
timestamp1?: pb<9, uint_32>; // ← finalize metadata.timestamp1
|
||||
fileHash?: pb<14, string>;
|
||||
selfUid?: pb<15, string>;
|
||||
destUid?: pb<16, string>;
|
||||
}
|
||||
export interface PrivateFileExtra {
|
||||
field2?: pb<2, PrivateFileExtraField2>;
|
||||
}
|
||||
export interface FileExtra {
|
||||
file?: pb<1, NotOnlineFile>;
|
||||
file?: pb<1, NotOnlineFile>;
|
||||
field6?: pb<6, PrivateFileExtra>;
|
||||
}
|
||||
|
||||
// PushMsgBody
|
||||
|
||||
@@ -75,6 +75,47 @@ export interface OidbPrivateFileUploadRespBody {
|
||||
export interface OidbPrivateFileUploadResp {
|
||||
upload?: pb<19, OidbPrivateFileUploadRespBody>;
|
||||
}
|
||||
// 0xE37_800 — c2c offline-file finalize / download-credential fetch.
|
||||
// Called AFTER the Highway upload completes (NapCat runs it even when the
|
||||
// file already exists server-side). The response metadata (`field30`)
|
||||
// supplies the download-routing fields the receiver needs; those ride
|
||||
// along in `FileExtra.field6` on the PbSendMsg. Without this step the file
|
||||
// uploads + PbSendMsg returns ok but the recipient sees "文件传输失败".
|
||||
// Ported byte-for-byte from NapCat `Oidb.0XE37_800` + `Oidb.0xE37_1200`.
|
||||
export interface OidbOfflineFileFinalizeReqBody {
|
||||
senderUid?: pb<10, string>;
|
||||
receiverUid?: pb<20, string>;
|
||||
fileUuid?: pb<30, string>;
|
||||
fileHash?: pb<40, string>;
|
||||
}
|
||||
export interface OidbOfflineFileFinalizeReq {
|
||||
subCommand?: pb<1, uint_32>;
|
||||
field2?: pb<2, int_32>;
|
||||
body?: pb<10, OidbOfflineFileFinalizeReqBody>;
|
||||
field101?: pb<101, int_32>;
|
||||
field102?: pb<102, int_32>;
|
||||
field200?: pb<200, int_32>;
|
||||
}
|
||||
// Server-issued download routing for the just-uploaded c2c file. Only the
|
||||
// fields consumed by `FileExtra.field6` are modelled (NapCat names them
|
||||
// field3/field100/field101/field110/timestamp1; tags verified).
|
||||
export interface OidbOfflineFileMetadata {
|
||||
field3?: pb<3, uint_32>;
|
||||
field100?: pb<100, bytes>;
|
||||
field101?: pb<101, bytes>;
|
||||
field110?: pb<110, uint_32>;
|
||||
timestamp1?: pb<130, uint_32>;
|
||||
}
|
||||
export interface OidbOfflineFileFinalizeRespBody {
|
||||
field10?: pb<10, uint_32>;
|
||||
metadata?: pb<30, OidbOfflineFileMetadata>;
|
||||
}
|
||||
export interface OidbOfflineFileFinalizeResp {
|
||||
command?: pb<1, uint_32>;
|
||||
subCommand?: pb<2, uint_32>;
|
||||
body?: pb<10, OidbOfflineFileFinalizeRespBody>;
|
||||
field50?: pb<50, uint_32>;
|
||||
}
|
||||
export interface NTV2CommonHead {
|
||||
requestId?: pb<1, uint_32>;
|
||||
command?: pb<2, uint_32>;
|
||||
|
||||
@@ -344,6 +344,10 @@
|
||||
"types": "./src/oidb-services/group-file/upload-private-file-request.ts",
|
||||
"default": "./src/oidb-services/group-file/upload-private-file-request.ts"
|
||||
},
|
||||
"./oidb-services/group-file/finalize-offline-file": {
|
||||
"types": "./src/oidb-services/group-file/finalize-offline-file.ts",
|
||||
"default": "./src/oidb-services/group-file/finalize-offline-file.ts"
|
||||
},
|
||||
"./oidb-services/group-file/list-group-files-page": {
|
||||
"types": "./src/oidb-services/group-file/list-group-files-page.ts",
|
||||
"default": "./src/oidb-services/group-file/list-group-files-page.ts"
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// 0xE37_800 — c2c (private) offline-file finalize.
|
||||
//
|
||||
// Runs AFTER the Highway PUT for a private file. The apply (0xE37_1700)
|
||||
// + Highway upload get the bytes onto the server, but the recipient
|
||||
// can't download the file until this call returns the server-issued
|
||||
// download routing (metadata). Those fields ride along in the outgoing
|
||||
// PbSendMsg as `FileExtra.field6` — without them the receiver's client
|
||||
// shows "文件传输失败" even though PbSendMsg returned ok.
|
||||
//
|
||||
// NapCat runs this unconditionally (even when the apply reports the file
|
||||
// already exists). Ported from NapCat `highway/DownloadOfflineFile`.
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type {
|
||||
OidbOfflineFileFinalizeReq,
|
||||
OidbOfflineFileFinalizeResp,
|
||||
OidbOfflineFileMetadata,
|
||||
} from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import { invokeOidb, type OidbSender } from '../../oidb-service';
|
||||
|
||||
export namespace FinalizeOfflineFile {
|
||||
export const command = 0xE37;
|
||||
export const subCommand = 800;
|
||||
|
||||
export interface Params {
|
||||
senderUid: string;
|
||||
receiverUid: string;
|
||||
fileUuid: string;
|
||||
fileHash: string;
|
||||
}
|
||||
|
||||
export type Deps = OidbSender;
|
||||
|
||||
export const serialize = (_ctx: Deps, p: Params): OidbOfflineFileFinalizeReq => ({
|
||||
subCommand: 800,
|
||||
field2: 0,
|
||||
body: {
|
||||
senderUid: p.senderUid,
|
||||
receiverUid: p.receiverUid,
|
||||
fileUuid: p.fileUuid,
|
||||
fileHash: p.fileHash,
|
||||
},
|
||||
field101: 3,
|
||||
field102: 1,
|
||||
field200: 1,
|
||||
});
|
||||
|
||||
export const deserialize = (_ctx: Deps, body: OidbOfflineFileFinalizeResp): OidbOfflineFileMetadata =>
|
||||
body.body?.metadata ?? {};
|
||||
|
||||
export const encode = (env: OidbBase<OidbOfflineFileFinalizeReq>): Uint8Array =>
|
||||
protobuf_encode<OidbBase<OidbOfflineFileFinalizeReq>>(env);
|
||||
|
||||
export const decode = (bytes: Uint8Array): OidbBase<OidbOfflineFileFinalizeResp> =>
|
||||
protobuf_decode<OidbBase<OidbOfflineFileFinalizeResp>>(bytes);
|
||||
|
||||
export const invoke = (deps: Deps, params: Params): Promise<OidbOfflineFileMetadata> =>
|
||||
invokeOidb(deps, FinalizeOfflineFile, params);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
||||
import type { OidbOfflineFileFinalizeResp } from '@snowluma/proto-defs/oidb-actions/media';
|
||||
import type { FileExtra } from '@snowluma/proto-defs/message';
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
|
||||
import { FinalizeOfflineFile } from '../../../src/oidb-services/group-file/finalize-offline-file';
|
||||
import { env, m, s, v } from '../_pb-oracle';
|
||||
|
||||
// `bytes` fields are wire type 2 — identical framing to a sub-message, so the
|
||||
// oracle's `m()` (tag + length + payload) doubles as the bytes emitter.
|
||||
const b = m;
|
||||
|
||||
function makeDeps(resp?: OidbOfflineFileFinalizeResp) {
|
||||
const responseData = resp !== undefined
|
||||
? Buffer.from(protobuf_encode<OidbBase<OidbOfflineFileFinalizeResp>>({ body: resp }))
|
||||
: Buffer.alloc(0);
|
||||
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
|
||||
return { sendRawPacket: vi.fn(async () => r) };
|
||||
}
|
||||
|
||||
describe('FinalizeOfflineFile (0xE37_800)', () => {
|
||||
it('declares 0xE37_800', () => {
|
||||
expect(FinalizeOfflineFile.command).toBe(0xE37);
|
||||
expect(FinalizeOfflineFile.subCommand).toBe(800);
|
||||
});
|
||||
|
||||
it('serializes the request body byte-for-byte (oracle locks every tag)', async () => {
|
||||
const deps = makeDeps({});
|
||||
await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
const [wire, bytes] = deps.sendRawPacket.mock.calls[0]!;
|
||||
expect(wire).toBe('OidbSvcTrpcTcp.0xe37_800');
|
||||
|
||||
// body(10) { senderUid(10) receiverUid(20) fileUuid(30) fileHash(40) }
|
||||
const innerBody = [...s(10, 'su'), ...s(20, 'ru'), ...s(30, 'uuid'), ...s(40, 'hh')];
|
||||
// req: subCommand(1)=800, field2(2)=0 omitted, body(10), field101(101)=3,
|
||||
// field102(102)=1, field200(200)=1
|
||||
const req = [...v(1, 800), ...m(10, innerBody), ...v(101, 3), ...v(102, 1), ...v(200, 1)];
|
||||
const expected = env(0xE37, 800, req, false);
|
||||
expect(Buffer.from(bytes).toString('hex')).toBe(expected);
|
||||
});
|
||||
|
||||
it('extracts the download-routing metadata from field30 of the response', async () => {
|
||||
const deps = makeDeps({
|
||||
body: { metadata: { field3: 5, field100: new Uint8Array([9, 9]), field101: new Uint8Array([8]), field110: 7, timestamp1: 42 } },
|
||||
});
|
||||
const meta = await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
expect(meta.field3).toBe(5);
|
||||
expect(meta.field110).toBe(7);
|
||||
expect(meta.timestamp1).toBe(42);
|
||||
expect(Array.from(meta.field100 ?? [])).toEqual([9, 9]);
|
||||
expect(Array.from(meta.field101 ?? [])).toEqual([8]);
|
||||
});
|
||||
|
||||
it('returns {} when the response carries no metadata', async () => {
|
||||
const deps = makeDeps({});
|
||||
const meta = await FinalizeOfflineFile.invoke(deps, {
|
||||
senderUid: 'su', receiverUid: 'ru', fileUuid: 'uuid', fileHash: 'hh',
|
||||
});
|
||||
expect(meta).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('FileExtra.field6 (c2c download routing — issue #157)', () => {
|
||||
it('encodes field6.field2 with the exact NapCat tag layout', () => {
|
||||
const extra: FileExtra = {
|
||||
field6: {
|
||||
field2: {
|
||||
field1: 7,
|
||||
fileUuid: 'uu',
|
||||
fileName: 'n.bin',
|
||||
field6: 3,
|
||||
field7: new Uint8Array([1, 2]),
|
||||
field8: new Uint8Array([3]),
|
||||
timestamp1: 99,
|
||||
fileHash: 'hh',
|
||||
selfUid: 'su',
|
||||
destUid: 'du',
|
||||
},
|
||||
},
|
||||
};
|
||||
const hex = Buffer.from(protobuf_encode<FileExtra>(extra)).toString('hex');
|
||||
|
||||
// PrivateFileExtraField2 in ascending tag order:
|
||||
// field1(1) fileUuid(4) fileName(5) field6(6) field7(7,bytes)
|
||||
// field8(8,bytes) timestamp1(9) fileHash(14) selfUid(15) destUid(16)
|
||||
const f2 = [
|
||||
...v(1, 7),
|
||||
...s(4, 'uu'),
|
||||
...s(5, 'n.bin'),
|
||||
...v(6, 3),
|
||||
...b(7, [1, 2]),
|
||||
...b(8, [3]),
|
||||
...v(9, 99),
|
||||
...s(14, 'hh'),
|
||||
...s(15, 'su'),
|
||||
...s(16, 'du'),
|
||||
];
|
||||
// PrivateFileExtra { field2(2) } → FileExtra { field6(6) }
|
||||
const expected = Buffer.from(m(6, m(2, f2))).toString('hex');
|
||||
expect(hex).toBe(expected);
|
||||
});
|
||||
|
||||
it('round-trips field6 through encode/decode', () => {
|
||||
const extra: FileExtra = {
|
||||
file: { fileUuid: 'u', fileName: 'f', subcmd: 1 },
|
||||
field6: { field2: { field1: 1, fileUuid: 'u', fileName: 'f', timestamp1: 5, selfUid: 's', destUid: 'd' } },
|
||||
};
|
||||
const back = protobuf_decode<FileExtra>(protobuf_encode<FileExtra>(extra));
|
||||
expect(back.field6?.field2).toMatchObject({
|
||||
field1: 1, fileUuid: 'u', fileName: 'f', timestamp1: 5, selfUid: 's', destUid: 'd',
|
||||
});
|
||||
expect(back.file?.subcmd).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user