diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt index c3279fa2b..965cd1c35 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt @@ -8,9 +8,9 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.BotSocketAdapter import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.LoginHandler import net.mamoe.mirai.network.protocol.tim.handler.* -import net.mamoe.mirai.network.protocol.tim.packet.HeartbeatPacket import net.mamoe.mirai.network.protocol.tim.packet.OutgoingPacket import net.mamoe.mirai.network.protocol.tim.packet.Packet +import net.mamoe.mirai.network.protocol.tim.packet.login.HeartbeatPacket import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket import net.mamoe.mirai.utils.BotConfiguration diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt index 505211106..605c98d35 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt @@ -255,7 +255,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot if (factory is SessionPacketFactory<*>) { with(factory as SessionPacketFactory) { - processPacket(packet) + handlePacket(packet) } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/OutgoingPacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/OutgoingPacket.kt index f0e766664..da66ca18b 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/OutgoingPacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/OutgoingPacket.kt @@ -37,7 +37,7 @@ abstract class SessionPacketFactory : PacketFactory.processPacket(packet: TPacket) {} + open suspend fun BotNetworkHandler<*>.handlePacket(packet: TPacket) {} } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketFactory.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketFactory.kt index 23c77e9b0..891a49fe2 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketFactory.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketFactory.kt @@ -52,10 +52,10 @@ abstract class PacketFactory(val d } object UnknownPacketFactory : SessionPacketFactory() { - override suspend fun BotNetworkHandler<*>.processPacket(packet: UnknownPacket) { + override suspend fun BotNetworkHandler<*>.handlePacket(packet: UnknownPacket) { ByteArrayPool.useInstance { packet.body.readAvailable(it) - bot.logger.debug("Unknown packet(${packet.id.value}) body = " + it.toUHexString()) + bot.logger.debug("UnknownPacket(${packet.id.value.toUHexString()}) = " + it.toUHexString()) } packet.body.close() } @@ -66,7 +66,7 @@ object UnknownPacketFactory : SessionPacketFactory() { } object IgnoredPacketFactory : SessionPacketFactory() { - override suspend fun BotNetworkHandler<*>.processPacket(packet: IgnoredPacket) { + override suspend fun BotNetworkHandler<*>.handlePacket(packet: IgnoredPacket) { } override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler<*>): IgnoredPacket = IgnoredPacket diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketId.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketId.kt index c3099b644..6fb467737 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketId.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/PacketId.kt @@ -82,5 +82,5 @@ enum class KnownPacketId(override inline val value: UShort, override inline val ; - override fun toString(): String = factory::class.simpleName ?: this.name + override fun toString(): String = (factory::class.simpleName ?: this.name) + "(${value.toUHexString()})" } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt index 6abffb523..b80197498 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt @@ -162,7 +162,7 @@ object AddFriendPacket : SessionPacketFactory } } - override suspend fun BotNetworkHandler<*>.processPacket(packet: AddFriendResponse) { + override suspend fun BotNetworkHandler<*>.handlePacket(packet: AddFriendResponse) { } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt index 2d1f2588d..f3b83a898 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt @@ -22,8 +22,7 @@ data class UnknownEventPacket( class UnknownEventParserAndHandler(override val id: UShort) : EventParserAndHandler { override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): UnknownEventPacket { - MiraiLogger.debug("UnknownEventPacket type = ${id.toUHexString()}") - MiraiLogger.debug("UnknownEventPacket data = ${readBytes().toUHexString()}") + MiraiLogger.debug("UnknownEventPacket(${id.toUHexString()}) = ${readBytes().toUHexString()}") return UnknownEventPacket(id, this) //TODO the cause is that `this` reference. } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt index c37a572a3..b8293c056 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt @@ -41,9 +41,12 @@ fun UShort.toByteArray(): ByteArray = with(toUInt()) { } /** - * 转 [ByteArray] 后再转 hex + * 转 [ByteArray] 后再转 hex. + * + * @return 2 位 Unsigned Hex */ -fun UShort.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator) +fun UShort.toUHexString(separator: String = " "): String = + (this.toInt().shr(8).toUShort() and 255u).toByte().toUHexString() + separator + (this and 255u).toByte().toUHexString() /** * 255u -> 00 00 00 FF @@ -108,7 +111,8 @@ fun getRandomString(length: Int, charRange: CharRange): String = String(CharArra /** * 根据所给 [charRanges] 随机生成长度为 [length] 的 [String]. */ -fun getRandomString(length: Int, vararg charRanges: CharRange): String = String(CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() }) +fun getRandomString(length: Int, vararg charRanges: CharRange): String = + String(CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() }) /** * 将 [this] 前 4 个 [Byte] 的 bits 合并为一个 [Int] @@ -118,7 +122,8 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String = String( * 一个 [Int] 有 32 bits * 本函数将 4 个 [Byte] 的 bits 连接得到 [Int] */ -fun ByteArray.toUInt(): UInt = this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0) +fun ByteArray.toUInt(): UInt = + this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0) /** * 从 [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入. diff --git a/mirai-debug/src/main/kotlin/PacketDebuger.kt b/mirai-debug/src/main/kotlin/PacketDebuger.kt index a59cde836..0d2fc3fc6 100644 --- a/mirai-debug/src/main/kotlin/PacketDebuger.kt +++ b/mirai-debug/src/main/kotlin/PacketDebuger.kt @@ -93,30 +93,20 @@ object Main { //println("raw = " + data.toUHexString()) data.read { discardExact(3) - val idHex = readInt().toUHexString(" ") - if (idHex.startsWith("00 81")) { + val id = matchPacketId(readUShort()) + val sequenceId = readUShort() + if (id == KnownPacketId.HEARTBEAT || readUInt() != qq) return@read - } - if (readUInt() != qq) { - return@read - } println("--------------") println("接收数据包") discardExact(3)//0x00 0x00 0x00. 但更可能是应该 discard 8 - println("id=$idHex") + println("id=$id, sequence=${sequenceId.toUHexString()}") val remaining = this.readRemainingBytes().cutTail(1) try { val decrypted = remaining.decryptBy(sessionKey) println("解密body=${decrypted.toUHexString()}") - discardExact(3) - - val id = matchPacketId(readUShort()) - val sequenceId = readUShort() - - discardExact(7)//4 for qq number, 3 for 0x00 0x00 0x00 - val packet = use { with(id.factory) { provideDecrypter(id.factory)