Fix fucking length

This commit is contained in:
Him188 2020-02-02 21:01:30 +08:00
parent f3ec93071f
commit 36ef9c2890
10 changed files with 104 additions and 102 deletions

View File

@ -259,13 +259,6 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
if (cache == null) {
// 没有缓存
var length: Int = rawInput.readInt() - 4
if (length and 0xFFFF != length) {
cachedPacket.value = rawInput
expectingRemainingLength = length.toLong() and 0xFFFF
// 丢包了. 后半部分包提前到达
PacketLogger.error { "丢包了." }
return
}
if (rawInput.remaining == length.toLong()) {
// 捷径: 当包长度正好, 直接传递剩余数据.
cachedPacketTimeoutJob?.cancel()
@ -296,13 +289,6 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} else {
// 有缓存
val expectingLength = expectingRemainingLength
if (expectingLength and 0xFFFF != expectingLength) {
processPacket(buildPacket {
writePacket(rawInput)
writeInt(expectingLength.toInt())
writePacket(cache)
})
}
if (rawInput.remaining >= expectingLength) {
// 剩余长度够, 连接上去, 处理这个包.
parsePacketAsync(buildPacket {

View File

@ -195,6 +195,7 @@ internal object KnownPacketFactories {
return
}
it.data.withUse {
when (flag2) {
1 ->//it.data.parseUniResponse(bot, it.packetFactory, it.sequenceId, consumer)
when (it.packetFactory) {
@ -216,6 +217,7 @@ internal object KnownPacketFactories {
2 -> it.data.parseOicqResponse(bot, it.packetFactory as OutgoingPacketFactory<T>, it.sequenceId, consumer)
else -> error("unknown flag2: $flag2. Body to be parsed for inner packet=${it.data.readBytes().toUHexString()}")
}
}
} ?: inline {
// 无法解析
PacketLogger.error("任何key都无法解密: ${data.take(size).toUHexString()}")
@ -261,12 +263,29 @@ internal object KnownPacketFactories {
}
val packet = when (dataCompressed) {
0 -> input
0 -> {
val size = input.readInt().toLong() and 0xffffffff
if (size == input.remaining || size == input.remaining + 4) {
input
} else {
buildPacket {
writeInt(size.toInt())
writePacket(input)
}
}
}
1 -> {
input.discardExact(4)
input.useBytes { data, length ->
data.unzip(length = length)
}.toReadPacket()
data.unzip(length = length).let {
val size = it.toInt()
if (size == it.size || size == it.size + 4) {
it.toReadPacket(offset = 4)
} else {
it.toReadPacket()
}
}
}
}
else -> error("unknown dataCompressed flag: $dataCompressed")
}
@ -284,7 +303,6 @@ internal object KnownPacketFactories {
ssoSequenceId: Int,
consumer: PacketConsumer<T>
) {
readPacket(readInt() - 4).withUse {
check(readByte().toInt() == 2)
this.discardExact(2) // 27 + 2 + body.size
this.discardExact(2) // const, =8001
@ -327,7 +345,6 @@ internal object KnownPacketFactories {
consumer(packetFactory, packet, packetFactory.commandName, ssoSequenceId)
}
}
}

View File

@ -18,7 +18,7 @@ internal object ImageUpPacket : OutgoingPacketFactory<ImageUpPacket.ImageUpPacke
operator fun invoke(client: QQAndroidClient, req: UploadImgReq): OutgoingPacket {
// TODO: 2020/1/24 测试: bodyType, subAppId
return buildLoginOutgoingPacket(client, key = client.wLoginSigInfo.d2Key, bodyType = 1) {
writeSsoPacket(client, subAppId = 0, commandName = ImageDownPacket.commandName, sequenceId = it) {
writeSsoPacket(client, subAppId = 0, commandName = commandName, sequenceId = it) {
val data = ProtoBufWithNullableSupport.dump(
Cmd0x352Packet.serializer(),
Cmd0x352Packet.createByImageRequest(req)

View File

@ -39,7 +39,7 @@ internal class MessageSvc {
*/
internal object PushNotify : IncomingPacketFactory<RequestPushNotify>("MessageSvc.PushNotify") {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): RequestPushNotify {
discardExact(8)
discardExact(4)
return decodeUniPacket(RequestPushNotify.serializer())
}
@ -89,7 +89,6 @@ internal class MessageSvc {
)
}
@UseExperimental(MiraiInternalAPI::class)
internal class GetMsgSuccess(delegate: MutableList<FriendMessage>) : Response(MsgSvc.SyncFlag.STOP, delegate)
@ -106,7 +105,6 @@ internal class MessageSvc {
@UseExperimental(MiraiInternalAPI::class)
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
// 00 00 01 0F 08 00 12 00 1A 34 08 FF C1 C4 F1 05 10 FF C1 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 8A CA 91 D1 0C 48 9B A5 BD 9B 0A 58 DE 9D 99 F8 08 60 1D 68 FF C1 C4 F1 05 70 00 20 02 2A 9D 01 08 F3 C1 C4 F1 05 10 A2 FF 8C F0 03 18 01 22 8A 01 0A 2A 08 A2 FF 8C F0 03 10 DD F1 92 B7 07 18 A6 01 20 0B 28 AE F9 01 30 F4 C1 C4 F1 05 38 A7 E3 D8 D4 84 80 80 80 01 B8 01 CD B5 01 12 08 08 01 10 00 18 00 20 00 1A 52 0A 50 0A 27 08 00 10 F4 C1 C4 F1 05 18 A7 E3 D8 D4 04 20 00 28 0C 30 00 38 86 01 40 22 4A 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 12 08 0A 06 0A 04 4E 4D 53 4C 12 15 AA 02 12 9A 01 0F 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 12 04 4A 02 08 00 30 01 2A 15 08 97 A2 C1 F1 05 10 95 A6 F5 E5 0C 18 01 30 01 40 01 48 81 01 2A 10 08 D3 F7 B5 F1 05 10 DD F1 92 B7 07 18 01 30 01 38 00 42 00 48 00
discardExact(4)
val resp = readProtoBuf(MsgSvc.PbGetMsgResp.serializer())
if (resp.result != 0) {
@ -162,7 +160,6 @@ internal class MessageSvc {
*/
internal object PushForceOffline : OutgoingPacketFactory<ForceOfflineEvent>("MessageSvc.PushForceOffline") {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): ForceOfflineEvent {
discardExact(4)
val struct = this.decodeUniPacket(RequestPushForceOffline.serializer())
return ForceOfflineEvent(bot, title = struct.title ?: "", tips = struct.tips ?: "")
}
@ -240,8 +237,6 @@ internal class MessageSvc {
}
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
discardExact(4)
val response = readProtoBuf(MsgSvc.PbSendMsgResp.serializer())
return if (response.result == 0) {
Response.SUCCESS

View File

@ -3,7 +3,6 @@
package net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.discardExact
import kotlinx.io.core.readBytes
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.message.GroupMessage
@ -23,7 +22,6 @@ internal class OnlinePush {
@UseExperimental(ExperimentalStdlibApi::class)
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): GroupMessage {
// 00 00 02 E4 0A D5 05 0A 4F 08 A2 FF 8C F0 03 10 DD F1 92 B7 07 18 52 20 00 28 BC 3D 30 8C 82 AB F1 05 38 D2 80 E0 8C 80 80 80 80 02 4A 21 08 E7 C1 AD B8 02 10 01 18 BA 05 22 09 48 69 6D 31 38 38 6D 6F 65 30 06 38 02 42 05 4D 69 72 61 69 50 01 58 01 60 00 88 01 08 12 06 08 01 10 00 18 00 1A F9 04 0A F6 04 0A 26 08 00 10 87 82 AB F1 05 18 B7 B4 BF 30 20 00 28 0C 30 00 38 86 01 40 22 4A 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 12 E6 03 42 E3 03 12 2A 7B 34 45 31 38 35 38 32 32 2D 30 45 37 42 2D 46 38 30 46 2D 43 35 42 31 2D 33 34 34 38 38 33 37 34 44 33 39 43 7D 2E 6A 70 67 22 00 2A 04 03 00 00 00 32 60 15 36 20 39 36 6B 45 31 41 38 35 32 32 39 64 63 36 39 38 34 37 39 37 37 62 20 20 20 20 20 20 35 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7B 34 45 31 38 35 38 32 32 2D 30 45 37 42 2D 46 38 30 46 2D 43 35 42 31 2D 33 34 34 38 38 33 37 34 44 33 39 43 7D 2E 6A 70 67 31 32 31 32 41 38 C6 BB 8A A9 08 40 FB AE 9E C2 09 48 50 50 41 5A 00 60 01 6A 10 4E 18 58 22 0E 7B F8 0F C5 B1 34 48 83 74 D3 9C 72 59 2F 67 63 68 61 74 70 69 63 5F 6E 65 77 2F 31 30 34 30 34 30 30 32 39 30 2F 36 35 35 30 35 37 31 32 37 2D 32 32 33 33 36 33 38 33 34 32 2D 34 45 31 38 35 38 32 32 30 45 37 42 46 38 30 46 43 35 42 31 33 34 34 38 38 33 37 34 44 33 39 43 2F 31 39 38 3F 74 65 72 6D 3D 32 82 01 57 2F 67 63 68 61 74 70 69 63 5F 6E 65 77 2F 31 30 34 30 34 30 30 32 39 30 2F 36 35 35 30 35 37 31 32 37 2D 32 32 33 33 36 33 38 33 34 32 2D 34 45 31 38 35 38 32 32 30 45 37 42 46 38 30 46 43 35 42 31 33 34 34 38 38 33 37 34 44 33 39 43 2F 30 3F 74 65 72 6D 3D 32 B0 01 4D B8 01 2E C8 01 FF 05 D8 01 4D E0 01 2E FA 01 59 2F 67 63 68 61 74 70 69 63 5F 6E 65 77 2F 31 30 34 30 34 30 30 32 39 30 2F 36 35 35 30 35 37 31 32 37 2D 32 32 33 33 36 33 38 33 34 32 2D 34 45 31 38 35 38 32 32 30 45 37 42 46 38 30 46 43 35 42 31 33 34 34 38 38 33 37 34 44 33 39 43 2F 34 30 30 3F 74 65 72 6D 3D 32 80 02 4D 88 02 2E 12 45 AA 02 42 50 03 60 00 68 00 9A 01 39 08 09 20 BF 50 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 98 03 00 A0 03 20 B0 03 00 C0 03 00 D0 03 00 E8 03 00 8A 04 04 08 02 08 01 90 04 80 80 80 10 B8 04 00 C0 04 00 12 06 4A 04 08 00 40 01 12 14 82 01 11 0A 09 48 69 6D 31 38 38 6D 6F 65 18 06 20 08 28 03 10 8A CA 9D A1 07 1A 00
discardExact(4)
val pbPushMsg = ProtoBufWithNullableSupport.load(MsgOnlinePush.PbPushMsg.serializer(), readBytes())
val extraInfo: ImMsgBody.ExtraInfo? = pbPushMsg.msg.msgBody.richText.elems.firstOrNull { it.extraInfo != null }?.extraInfo

View File

@ -16,7 +16,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacketFactory
import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket
import net.mamoe.mirai.utils.io.debugIfFail
internal class FriendList {
@ -66,7 +65,7 @@ internal class FriendList {
internal object GetTroopListSimplify :
OutgoingPacketFactory<GetTroopListSimplify.Response>("friendlist.GetTroopListReqV2") {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): GetTroopListSimplify.Response {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
val res = this.decodeUniPacket(GetTroopListRespV2.serializer())
return Response(res.vecTroopList.orEmpty())
}
@ -120,8 +119,7 @@ internal class FriendList {
}
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
//this.discardExact(4)
val res = this.debugIfFail { this.decodeUniPacket(GetFriendListResp.serializer()) }
val res = this.decodeUniPacket(GetFriendListResp.serializer())
return Response(
res.totoalFriendCount,
res.vecFriendInfo.orEmpty()

View File

@ -1,7 +1,6 @@
package net.mamoe.mirai.qqandroid.network.protocol.packet.login
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.discardExact
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.io.serialization.JceCharset
import net.mamoe.mirai.qqandroid.io.serialization.decodeUniPacket
@ -21,7 +20,6 @@ internal class ConfigPushSvc {
responseCommandName = "ConfigPushSvc.PushResp"
) {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): PushReqJceStruct {
discardExact(4)
return decodeUniPacket(PushReqJceStruct.serializer())
}

View File

@ -10,6 +10,7 @@ import kotlinx.io.streams.writePacket
import net.mamoe.mirai.utils.MiraiInternalAPI
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.IOException
import java.net.Socket
/**
@ -41,8 +42,12 @@ actual class PlatformSocket : Closeable {
*/
actual suspend inline fun send(packet: ByteReadPacket) {
withContext(Dispatchers.IO) {
try {
writeChannel.writePacket(packet)
writeChannel.flush()
} catch (e: IOException) {
throw SendPacketInternalException(e)
}
}
}
@ -51,7 +56,11 @@ actual class PlatformSocket : Closeable {
*/
actual suspend inline fun read(): ByteReadPacket {
return withContext(Dispatchers.IO) {
try {
readChannel.readPacketAtMost(Long.MAX_VALUE)
} catch (e: IOException) {
throw ReadPacketInternalException(e)
}
}
}

View File

@ -10,6 +10,7 @@ import kotlinx.io.streams.writePacket
import net.mamoe.mirai.utils.MiraiInternalAPI
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.IOException
import java.net.Socket
/**
@ -41,8 +42,12 @@ actual class PlatformSocket : Closeable {
*/
actual suspend inline fun send(packet: ByteReadPacket) {
withContext(Dispatchers.IO) {
try {
writeChannel.writePacket(packet)
writeChannel.flush()
} catch (e: IOException) {
throw SendPacketInternalException(e)
}
}
}
@ -51,7 +56,11 @@ actual class PlatformSocket : Closeable {
*/
actual suspend inline fun read(): ByteReadPacket {
return withContext(Dispatchers.IO) {
try {
readChannel.readPacketAtMost(Long.MAX_VALUE)
} catch (e: IOException) {
throw ReadPacketInternalException(e)
}
}
}