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:
DrSmoothl committed 2026-06-28 20:11:17 +08:00
1 parent 12b01907bc
commit 5d39cf5782
7 files changed
+325 -3

No files matched your search

+43 -1
View File
@@ -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.