fix(core): make c2c file 0xE37_800 finalize best-effort, not fatal (#157)

Real-machine retest showed a plain c2c file send (NotOnlineFile only, no
field6) is already downloadable — the original #157 'fix' made the 0xE37_800
finalize a hard dependency, so any finalize error would now fail a send that
would otherwise succeed (a regression, same speculative-NapCat-parity trap as
the #145 force-full).

Wrap the finalize in try/catch: on success we still attach field6 (extra
download routing that mirrors NapCat and may help some receiver clients); on
failure we log and send without it. field6 is now enrichment, not a
requirement.

Adds a regression test: a failing 0xE37_800 still produces a PbSendMsg with
the file and no field6.
This commit is contained in:
DrSmoothl committed 2026-06-29 16:08:20 +08:00
1 parent 053137669a
commit 57ff0e69d8
2 files changed
+80 -21

No files matched your search

+35 -19
View File
@@ -13,6 +13,7 @@ import { FinalizeOfflineFile } from '@snowluma/protocol/oidb-services/group-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';
import { createLogger } from '@snowluma/common/logger';
import type { BridgeContext } from '../bridge-context';
// `Bridge` is imported as a type only so we can narrow `ctx` back to
// the concrete Bridge instance when passing it to `buildSendElems`
@@ -26,6 +27,8 @@ import type { Bridge, SendMessageReceipt } from '../bridge';
const SEND_MSG_CMD = 'MessageSvc.PbSendMsg';
const log = createLogger('Bridge.Message');
type GroupMessage = Extract<QQEventVariant, { kind: 'group_message' }>;
type FriendMessage = Extract<QQEventVariant, { kind: 'friend_message' }>;
@@ -240,10 +243,11 @@ export class MessageApi {
* - `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).
* `FileExtra.file` alone makes the file downloadable (verified on a live
* account). We ALSO attach `FileExtra.field6` — extra server-issued download
* routing from a best-effort 0xE37_800 finalize that mirrors NapCat and may
* help some receiver clients. The finalize is non-fatal: if it fails we send
* without field6 rather than failing the send (issue #157).
*/
async sendC2cFile(
userUin: number,
@@ -262,22 +266,31 @@ export class MessageApi {
}
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).
// The file is already downloadable from the `NotOnlineFile` reference
// alone — verified on a live account: a plain c2c file send (no field6)
// downloads fine. `field6` is EXTRA server-issued download routing (from
// the 0xE37_800 finalize) that mirrors NapCat and may help some receiver
// clients. So fetch it BEST-EFFORT: if the finalize fails, send without
// field6 rather than failing the whole send. (issue #157 — the finalize
// was originally fatal, which could regress a send that would otherwise
// succeed.)
const fileHash = info.fileHash ?? '';
const meta = await FinalizeOfflineFile.invoke(this.ctx, {
senderUid: selfUid,
receiverUid: userUid,
fileUuid: info.fileId,
fileHash,
});
let meta: Awaited<ReturnType<typeof FinalizeOfflineFile.invoke>> | null = null;
try {
meta = await FinalizeOfflineFile.invoke(this.ctx, {
senderUid: selfUid,
receiverUid: userUid,
fileUuid: info.fileId,
fileHash,
});
} catch (err) {
log.warn('c2c file finalize (0xE37_800) failed, sending without field6: %s',
err instanceof Error ? err.message : String(err));
}
const nowSec = Math.floor(Date.now() / 1000);
const sevenDaysSec = 7 * 24 * 60 * 60;
const fileExtraBytes = protobuf_encode<FileExtra>({
const fileExtra: FileExtra = {
file: {
fileType: 0,
fileUuid: info.fileId,
@@ -289,7 +302,9 @@ export class MessageApi {
expireTime: nowSec + sevenDaysSec,
fileHash,
},
field6: {
};
if (meta) {
fileExtra.field6 = {
field2: {
field1: meta.field110,
fileUuid: info.fileId,
@@ -302,8 +317,9 @@ export class MessageApi {
selfUid,
destUid: userUid,
},
},
});
};
}
const fileExtraBytes = protobuf_encode<FileExtra>(fileExtra);
const request = protobuf_encode<SendMessageRequest>({
routingHead: {
@@ -136,8 +136,9 @@ 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.
// issue #157: when the 0xE37_800 finalize succeeds, its download routing
// rides along in field6 (best-effort enrichment on top of the always-
// present NotOnlineFile).
expect(bridge.finalizeCmd).toBe('OidbSvcTrpcTcp.0xe37_800');
expect(fileExtra?.field6?.field2).toMatchObject({
fileUuid: 'uuid-abc-123',
@@ -157,4 +158,46 @@ describe('Bridge private media routing', () => {
// c2c-file path.
expect(request?.contentHead?.c2cCmd ?? 0).toBe(0);
});
it('c2c file send survives a failing 0xE37_800 finalize — sends without field6 (#157 best-effort)', async () => {
const { Bridge } = await import('../src/bridge/bridge');
const { IdentityService } = await import('@snowluma/protocol/identity-service');
class TestBridge extends Bridge {
capturedBody: Uint8Array | 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> {
// Finalize fails (server error → OidbError). The send must NOT fail
// because of it.
if (cmd.includes('0xe37_800')) {
return { success: false, gotResponse: true, errorCode: 9999, errorMessage: 'finalize boom', responseData: Buffer.alloc(0) };
}
this.capturedBody = body;
return {
success: true, gotResponse: true, errorCode: 0, errorMessage: '',
responseData: Buffer.from(protobuf_encode<SendMessageResponse>({
result: 0, errMsg: '', privateSequence: 88, timestamp1: 1710000000,
})),
};
}
}
const bridge = new TestBridge(IdentityService.memory('10000'));
// Should resolve (not throw) despite the finalize error.
await bridge.apis.message.sendC2cFile(67890, 'u_peer_xyz', {
fileId: 'uuid-abc-123', fileName: 'doc.txt', fileSize: 1024,
fileMd5: new Uint8Array([1, 2, 3, 4]), fileHash: 'hash-xyz',
});
// PbSendMsg still went out, carrying the file but NO field6.
expect(bridge.capturedBody).toBeInstanceOf(Uint8Array);
const request = protobuf_decode<SendMessageRequest>(bridge.capturedBody as Uint8Array);
const fileExtra = protobuf_decode<FileExtra>(request!.messageBody!.msgContent as Uint8Array);
expect(fileExtra?.file?.fileUuid).toBe('uuid-abc-123');
expect(fileExtra?.field6 ?? undefined).toBeUndefined();
});
});