From e6d75cdc98c60316f1536500c0280c1b62351377 Mon Sep 17 00:00:00 2001 From: f1ynng8 Date: Thu, 7 May 2026 11:50:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=20issues-26=EF=BC=8C=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E6=96=87=E6=9C=AC=E5=9C=A8=E7=89=B9=E5=AE=9A?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E8=8C=83=E5=9B=B4=E5=86=85=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E5=9C=A8=E5=BC=80=E5=A4=B4=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?ASCII=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- onebot/script.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/onebot/script.js b/onebot/script.js index 2e338b3..a74859b 100644 --- a/onebot/script.js +++ b/onebot/script.js @@ -129,7 +129,7 @@ function getProtobufRawBytes(pBuffer, scanSize) { if (targetTag === 0x42) { finalResults.push(rawData); } else { - finalResults.push(getCleanString(rawData)); + finalResults.push(decodeProtobufString(rawData)); } i += length; } else { @@ -218,6 +218,110 @@ function getCleanString(uint8Array) { return out; } +function readProtobufVarint(uint8Array, offset) { + let value = 0; + let shift = 0; + + for (let i = offset; i < uint8Array.length && i - offset < 10; i++) { + const b = uint8Array[i]; + value += (b & 0x7F) * Math.pow(2, shift); + if ((b & 0x80) === 0) { + return {value: value, nextOffset: i + 1}; + } + shift += 7; + } + + return null; +} + +function decodeProtobufString(uint8Array, depth) { + if (depth === undefined) { + depth = 0; + } + + if (depth < 2) { + const nested = tryDecodeNestedProtobufString(uint8Array, depth); + if (nested !== null) { + return nested; + } + } + + return getCleanString(uint8Array); +} + +function tryDecodeNestedProtobufString(uint8Array, depth) { + if (!uint8Array || uint8Array.length < 2) { + return null; + } + + let offset = 0; + const candidates = []; + + while (offset < uint8Array.length) { + const tagInfo = readProtobufVarint(uint8Array, offset); + if (!tagInfo) { + return null; + } + + const fieldNumber = Math.floor(tagInfo.value / 8); + const wireType = tagInfo.value & 0x07; + if (fieldNumber <= 0) { + return null; + } + + offset = tagInfo.nextOffset; + if (wireType === 2) { + const lenInfo = readProtobufVarint(uint8Array, offset); + if (!lenInfo) { + return null; + } + + const valueStart = lenInfo.nextOffset; + const valueEnd = valueStart + lenInfo.value; + if (valueEnd > uint8Array.length) { + return null; + } + + const value = uint8Array.slice(valueStart, valueEnd); + const text = decodeProtobufString(value, depth + 1); + if (text) { + candidates.push({fieldNumber: fieldNumber, text: text}); + } + offset = valueEnd; + } else if (wireType === 0) { + const valueInfo = readProtobufVarint(uint8Array, offset); + if (!valueInfo) { + return null; + } + offset = valueInfo.nextOffset; + } else if (wireType === 1) { + offset += 8; + if (offset > uint8Array.length) { + return null; + } + } else if (wireType === 5) { + offset += 4; + if (offset > uint8Array.length) { + return null; + } + } else { + return null; + } + } + + if (candidates.length === 0) { + return null; + } + + for (const candidate of candidates) { + if (candidate.fieldNumber === 2) { + return candidate.text; + } + } + + return candidates.map(candidate => candidate.text).join(""); +} + function protobufVarintToNumberString(uint8Array) { let result = BigInt(0); let shift = BigInt(0); @@ -503,7 +607,7 @@ function triggerSendTextMessage(taskId, receiver, content, atUser) { processNextTextMessage(); } - return "ok"; + return "1"; } function processNextTextMessage() {