diff --git a/packages/core/src/bridge/actions/group-file.ts b/packages/core/src/bridge/actions/group-file.ts index ef12dbd3..6131ef10 100644 --- a/packages/core/src/bridge/actions/group-file.ts +++ b/packages/core/src/bridge/actions/group-file.ts @@ -83,6 +83,19 @@ function bytesToHexUpper(data: unknown): string { return toHexUpper(data); } +// Reverses acidify's `Int.toIpString()`: the 32-bit IP arrives +// little-endian-packed (byte0 = first dotted octet) and we unpack it the +// same way. Force-unsigned the shift to keep negative ints (high bit set) +// rendering correctly — JS `>>` is arithmetic and would turn 0xFF000000 +// into a negative number. +function int32ToIpv4Dotted(value: number): string { + const b1 = value & 0xFF; + const b2 = (value >>> 8) & 0xFF; + const b3 = (value >>> 16) & 0xFF; + const b4 = (value >>> 24) & 0xFF; + return `${b1}.${b2}.${b3}.${b4}`; +} + function normalizeUploadFileName(name: string, fallback: string): string { const trimmed = name.trim(); if (trimmed) return trimmed; @@ -440,36 +453,64 @@ export async function uploadPrivateFile( if (!fileId) throw new Error('private file upload response missing file_id'); if (!upload.boolFileExist && uploadFile) { - // The server normally returns the highway host in `uploadIp` (field - // 60). LagrangeGo / Lagrange.Core both read that field directly with - // no fallback. We've seen real captures where the server picks one - // of the parallel host fields (uploadDomain / uploadIpList[0] / - // uploadHttpsDomain / uploadDns) instead — likely a rollout - // difference. Walk them in order rather than die with "upload host - // is invalid" when the server gives us a perfectly usable host - // through a different slot. The highway client uses HTTP PUT so we - // can pair any of these with the regular `uploadPort`; we only fall - // back to `uploadHttpsPort` if we picked the HTTPS-flavored host. + // Host selection. + // + // Current QQ-NT server rollout has stopped populating the legacy + // `uploadIp` (field 60) entirely. The host now arrives as the first + // entry of `rtpMediaPlatformUploadAddress` (field 210, repeated + // IPv4 message) — same place acidify reads it from since their + // 2026-04 protobuf refactor. Each IPv4 has paired `inIP`/`inPort` + // (LAN, same DC as the OIDB endpoint) and `outIP`/`outPort` (WAN); + // acidify uses `inIP`/`inPort` exclusively and so do we, because + // that's the address the highway PUT actually needs to reach. + // + // The 32-bit IPs are little-endian-packed (byte0 = first octet) + // per acidify's `Int.toIpString()`. Cross-checked the byte order + // by inspecting their highway flow — there's no separate htonl + // step, so the integer is already in network-octet-first order. + // + // Older server versions still populate the legacy string fields + // (uploadIp / uploadDomain / uploadIpList[0] / uploadHttpsDomain / + // uploadDns), so we fall through to those after rtpMediaPlatform. + // Pair an HTTPS-flavored host with `uploadHttpsPort` if that's + // what we picked. + const rtpFirst = (Array.isArray(upload.rtpMediaPlatformUploadAddress) + && upload.rtpMediaPlatformUploadAddress[0]) + ? upload.rtpMediaPlatformUploadAddress[0] : null; + const rtpInIP = rtpFirst && typeof rtpFirst.inIP === 'number' && rtpFirst.inIP !== 0 + ? int32ToIpv4Dotted(rtpFirst.inIP) : ''; + const rtpInPort = rtpFirst && typeof rtpFirst.inPort === 'number' + ? rtpFirst.inPort : 0; const ipListFirst = (Array.isArray(upload.uploadIpList) && upload.uploadIpList[0]) ? upload.uploadIpList[0] : ''; - const uploadHost = (typeof upload.uploadIp === 'string' && upload.uploadIp) + const uploadHost = (rtpInIP) + || (typeof upload.uploadIp === 'string' && upload.uploadIp) || (typeof upload.uploadDomain === 'string' && upload.uploadDomain) || (ipListFirst) || (typeof upload.uploadHttpsDomain === 'string' && upload.uploadHttpsDomain) || (typeof upload.uploadDns === 'string' && upload.uploadDns) || ''; - const httpsHostUsed = !upload.uploadIp && !upload.uploadDomain && !ipListFirst + const httpsHostUsed = !rtpInIP && !upload.uploadIp && !upload.uploadDomain && !ipListFirst && typeof upload.uploadHttpsDomain === 'string' && !!upload.uploadHttpsDomain; - const uploadPort = httpsHostUsed && toInt(upload.uploadHttpsPort) > 0 - ? toInt(upload.uploadHttpsPort) - : toInt(upload.uploadPort); + const uploadPort = rtpInIP && rtpInPort > 0 + ? rtpInPort + : httpsHostUsed && toInt(upload.uploadHttpsPort) > 0 + ? toInt(upload.uploadHttpsPort) + : toInt(upload.uploadPort); if (!uploadHost || uploadPort <= 0) { // Surface every host-bearing field we know about so a user // hitting this can show us exactly which slot the server filled // (or whether it returned nothing at all — which would point at // a request mismatch rather than a missing decoder). + const rtpDump = Array.isArray(upload.rtpMediaPlatformUploadAddress) + ? JSON.stringify(upload.rtpMediaPlatformUploadAddress.map((e) => ({ + outIP: e.outIP, outPort: e.outPort, inIP: e.inIP, inPort: e.inPort, + iPType: e.iPType, + }))) + : '[]'; log.warn( - 'private file upload host missing — ip=%s domain=%s ipList=%s httpsDomain=%s dns=%s lanip=%s port=%s httpsPort=%s', + 'private file upload host missing — rtp=%s ip=%s domain=%s ipList=%s httpsDomain=%s dns=%s lanip=%s port=%s httpsPort=%s', + rtpDump, upload.uploadIp ?? '', upload.uploadDomain ?? '', JSON.stringify(upload.uploadIpList ?? []), upload.uploadHttpsDomain ?? '', upload.uploadDns ?? '', diff --git a/packages/core/src/bridge/proto/proton/oidb-action.ts b/packages/core/src/bridge/proto/proton/oidb-action.ts index 19ea7e4b..bb051888 100644 --- a/packages/core/src/bridge/proto/proton/oidb-action.ts +++ b/packages/core/src/bridge/proto/proton/oidb-action.ts @@ -643,6 +643,21 @@ export interface OidbPrivateFileUploadReq { flagSupportMediaPlatform?: pb<200, int_32>; } +// IPv4 wire message used inside ApplyUploadRespV3.rtpMediaPlatformUploadAddress +// (field 210). Sourced from acidify's `IPv4` class — outIP/inIP are +// little-endian-packed 32-bit IPv4 addresses (byte order: low byte = first +// octet), decoded via the standard `(ip & 0xFF).(ip>>8 & 0xFF)....` recipe. +// outPort/inPort are the matching port for each leg (out = WAN, in = LAN). +// iPType is the address family hint (1 = IPv4, etc — acidify treats it +// purely as metadata, no behavior). +export interface IPv4 { + outIP?: pb<1, int_32>; + outPort?: pb<2, int_32>; + inIP?: pb<3, int_32>; + inPort?: pb<4, int_32>; + iPType?: pb<5, int_32>; +} + export interface OidbPrivateFileUploadRespBody { retCode?: pb<10, int_32>; retMsg?: pb<20, string>; @@ -651,12 +666,13 @@ export interface OidbPrivateFileUploadRespBody { // quota numbers don't drive any code path. uploadIp?: pb<60, string>; // Cross-checked against LagrangeGo `OidbSvcTrpcTcp0xE37_800.proto` - // (fields 60-170 enumerated below) + NapCat `Oidb.0XE37_800.ts`. - // Real server has been observed to leave `uploadIp` empty while - // populating `uploadDomain` / `uploadIpList[0]` instead (likely a - // server-side rollout difference). Decoding all of these lets us - // fall through in the consumer rather than die with "upload host - // is invalid" when the server picks a different field. + // (fields 60-170 enumerated below) + NapCat `Oidb.0XE37_800.ts` + + // acidify (most recent NT-protocol fork, 2026-04). Real server in + // 2026 has been observed to leave the legacy host fields empty and + // put the host into the new field 210 (`rtpMediaPlatformUploadAddress`, + // repeated IPv4 messages) instead — the consumer walks them in that + // order. Decoding the legacy fields too means we keep working against + // older server rollouts. uploadDomain?: pb<70, string>; uploadPort?: pb<80, uint_32>; uuid?: pb<90, string>; @@ -667,7 +683,18 @@ export interface OidbPrivateFileUploadRespBody { uploadHttpsDomain?: pb<150, string>; uploadDns?: pb<160, string>; uploadLanip?: pb<170, string>; + // Field 200 — acidify renamed this to `fileIdCrc` in their 2026-04 + // refactor; we keep `fileAddon` because the wire bytes are identical + // and our callers already read `fileAddon` as the file-hash blob (it's + // a hex string in practice). Worth renaming if a consumer ever needs + // the new semantic. fileAddon?: pb<200, string>; + // Field 210 — the new "host carrier" the NT server actually populates + // in current rollouts. Each IPv4 entry has `inIP`/`inPort` (LAN) + + // `outIP`/`outPort` (WAN). acidify reads `inIP`+`inPort` exclusively; + // we follow suit because that's what the highway HTTP PUT needs to + // reach (same datacenter as the OIDB endpoint that returned this). + rtpMediaPlatformUploadAddress?: pb_repeated<210, IPv4>; mediaPlatformUploadKey?: pb<220, bytes>; } diff --git a/packages/core/tests/actions/group-file.test.ts b/packages/core/tests/actions/group-file.test.ts index a5148277..73bd9ae8 100644 --- a/packages/core/tests/actions/group-file.test.ts +++ b/packages/core/tests/actions/group-file.test.ts @@ -242,6 +242,35 @@ describe('actions/group-file', () => { expect(bridge.sendC2cFileMessage).not.toHaveBeenCalled(); }); + it('uploadPrivateFile reads host from rtpMediaPlatformUploadAddress[0].inIP when populated', async () => { + // The 2026 QQ-NT server rollout moved the upload host out of the + // legacy `uploadIp` (field 60) string into the new + // `rtpMediaPlatformUploadAddress` (field 210, repeated IPv4 message). + // Mirrors acidify's `UploadPrivateFile.kt` consumer logic — read + // `inIP` (LAN, same DC as the OIDB endpoint) and pair with `inPort`. + // The int is little-endian-packed: 0x0101A8C0 → 192.168.1.1. + const bridge = mockBridge(); + vi.mocked(bridge.resolveUserUid).mockResolvedValueOnce('target-uid'); + vi.mocked(oidb.runOidb).mockResolvedValueOnce( + protobuf_encode>({ + body: { + upload: { + uuid: 'pfid', + fileAddon: 'phash', + // boolFileExist defaults to false — forces highway path + // 192.168.1.1 little-endian packed = (1 << 24) | (1 << 16) | (168 << 8) | 192 = 16885952 + rtpMediaPlatformUploadAddress: [ + { outIP: 0, outPort: 0, inIP: 16885952, inPort: 8080, iPType: 1 }, + ], + mediaPlatformUploadKey: new Uint8Array([1, 2, 3]), + } as any, + }, + }), + ); + await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file.bin'); + expect(highwayClient.uploadHighwayHttp).toHaveBeenCalledOnce(); + }); + it('uploadPrivateFile falls back to uploadDomain when uploadIp is empty', async () => { // Regression: the server has been observed in the wild returning the // highway host in `uploadDomain` (field 70) instead of `uploadIp`