diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt index ad455e8a0..b70028999 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt @@ -225,7 +225,7 @@ fun MessageChain.toPacket(): ByteReadPacket = buildPacket { */ writeShortLVPacket { writeByte(0x02) - //"46 52 25 46 60 30 59 4F 4A 5A 51 48 31 46 4A 53 4C 51 4C 4A 33 46 31 2E 6A 70 67".hexToBytes().stringOfWitch() + //"46 52 25 46 60 30 59 4F 4A 5A 51 48 31 46 4A 53 4C 51 4C 4A 33 46 31 2E 6A 70 67".hexToBytes().encodeToString() // writeShortLVString(filename)//图片文件名 FR%F`0YOJZQH1FJSLQLJ3F1.jpg writeShortLVString(id.value.substring(1..24) + ".gif")// 图片文件名. 后缀不影响. 但无后缀会导致 PC QQ 无法显示这个图片 writeHex("03 00 04 00 00 02 A2 04") diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMProtocol.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMProtocol.kt index 96c6288a4..deeb94717 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMProtocol.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMProtocol.kt @@ -2,6 +2,7 @@ package net.mamoe.mirai.network.protocol.tim +import net.mamoe.mirai.utils.io.hexToBytes import net.mamoe.mirai.utils.solveIpAddress object TIMProtocol { @@ -9,32 +10,32 @@ object TIMProtocol { //add("183.60.56.29") val list = mutableListOf() arrayOf( - "sz3.tencent.com", - "sz4.tencent.com", - "sz5.tencent.com", - "sz6.tencent.com", - "sz8.tencent.com", - "sz9.tencent.com", - "sz2.tencent.com" + "sz3.tencent.com", + "sz4.tencent.com", + "sz5.tencent.com", + "sz6.tencent.com", + "sz8.tencent.com", + "sz9.tencent.com", + "sz2.tencent.com" ).forEach { list.add(solveIpAddress(it)) } // 需 IPv4 地址 list.toList() }()//不使用lazy, 在初始化时就加载. - const val head = "02" - const val ver = "37 13"// TIM 最新版中这个有时候是 38 03 - const val fixVer = "03 00 00 00 01 2E 01 00 00 68 52 00 00 00 00" - const val tail = "03" + val head = "02".hexToBytes() + val ver = "37 13".hexToBytes()// TIM 最新版中这个有时候是 38 03 + val fixVer = "03 00 00 00 01 2E 01 00 00 68 52 00 00 00 00".hexToBytes() + val tail = "03".hexToBytes() /** * _fixVer */ - const val fixVer2 = "02 00 00 00 01 01 01 00 00 68 20" + val fixVer2 = "02 00 00 00 01 01 01 00 00 68 20".hexToBytes() // 02 38 03 00 CD 48 68 3E 03 3F A2 02 00 00 00 - const val version0x02 = "02 00 00 00 01 2E 01 00 00 69 35" - const val version0x04 = "04 00 00 00 01 2E 01 00 00 69 35 00 00 00 00 00 00 00 00" + val version0x02 = "02 00 00 00 01 2E 01 00 00 69 35".hexToBytes() + val version0x04 = "04 00 00 00 01 2E 01 00 00 69 35 00 00 00 00 00 00 00 00".hexToBytes() - const val constantData1 = "00 18 00 16 00 01 " - const val constantData2 = "00 00 04 53 00 00 00 01 00 00 15 85 " + val constantData1 = "00 18 00 16 00 01 ".hexToBytes() + val constantData2 = "00 00 04 53 00 00 00 01 00 00 15 85 ".hexToBytes() //todo 使用 byte array @@ -42,7 +43,7 @@ object TIMProtocol { /** * Touch 发出时写入, 并用于加密, 接受 sendTouch response 时解密. */ - const val touchKey = "A4 F1 91 88 C9 82 14 99 0C 9E 56 55 91 23 C8 3D"//16 + val touchKey = "A4 F1 91 88 C9 82 14 99 0C 9E 56 55 91 23 C8 3D".hexToBytes()//16 //统一替换为了 touchKey @@ -50,37 +51,38 @@ object TIMProtocol { // * Redirection 发出时写入, 并用于加密, 接受 Redirection response 时解密. // * 这个 key 似乎是可以任意的. // */ - //const val redirectionKey = "A8 F2 14 5F 58 12 60 AF 07 63 97 D6 76 B2 1A 3B"//16 + //val redirectionKey = "A8 F2 14 5F 58 12 60 AF 07 63 97 D6 76 B2 1A 3B"//16 /** * 并非常量. 设置为常量是为了让 [shareKey] 为常量 */ - const val publicKey = "02 6D 28 41 D2 A5 6F D2 FC 3E 2A 1F 03 75 DE 6E 28 8F A8 19 3E 5F 16 49 D3"//25 + val publicKey = "02 6D 28 41 D2 A5 6F D2 FC 3E 2A 1F 03 75 DE 6E 28 8F A8 19 3E 5F 16 49 D3".hexToBytes()//25 /** * 并非常量. 设置为常量是为了让 [shareKey] 为常量 * * LoginResend 和 PasswordSubmission 时写入, 但随后都使用 shareKey 加密, 收到回复也是用的 share key */ - const val key0836 = "EF 4A 36 6A 16 A8 E6 3D 2E EA BD 1F 98 C1 3C DA"//16 + val key0836 = "EF 4A 36 6A 16 A8 E6 3D 2E EA BD 1F 98 C1 3C DA".hexToBytes()//16 /** * 并非常量. 是 publicKey 与 key0836 的算法计算结果 */ - //const val shareKey = "5B 6C 91 55 D9 92 F5 A7 99 85 37 76 3D 0F 08 B7"//16 - const val shareKey = "1A E9 7F 7D C9 73 75 98 AC 02 E0 80 5F A9 C6 AF"//16//original + //val shareKey = "5B 6C 91 55 D9 92 F5 A7 99 85 37 76 3D 0F 08 B7"//16 + val shareKey = "1A E9 7F 7D C9 73 75 98 AC 02 E0 80 5F A9 C6 AF".hexToBytes()//16//original - const val key00BA = "C1 9C B8 C8 7B 8C 81 BA 9E 9E 7A 89 E1 7A EC 94" - const val key00BAFix = "69 20 D1 14 74 F5 B3 93 E4 D5 02 B3 71 1A CD 2A" + val key00BA = "C1 9C B8 C8 7B 8C 81 BA 9E 9E 7A 89 E1 7A EC 94".hexToBytes() + val key00BAFix = "69 20 D1 14 74 F5 B3 93 E4 D5 02 B3 71 1A CD 2A".hexToBytes() /** * 0836_622_fix2 */ - const val passwordSubmissionTLV2 = "00 15 00 30 00 01 01 27 9B C7 F5 00 10 65 03 FD 8B 00 00 00 00 00 00 00 00 00 00 00 00 02 90 49 55 33 00 10 15 74 C4 89 85 7A 19 F5 5E A9 C9 A3 5E 8A 5A 9B" + val passwordSubmissionTLV2 = + "00 15 00 30 00 01 01 27 9B C7 F5 00 10 65 03 FD 8B 00 00 00 00 00 00 00 00 00 00 00 00 02 90 49 55 33 00 10 15 74 C4 89 85 7A 19 F5 5E A9 C9 A3 5E 8A 5A 9B".hexToBytes() /** * 0836_622_fix1 */ - const val passwordSubmissionTLV1 = "03 00 00 00 01 01 01 00 00 68 20 00 00 00 00 00 01 01 03"//19 + val passwordSubmissionTLV1 = "03 00 00 00 01 01 01 00 00 68 20 00 00 00 00 00 01 01 03".hexToBytes()//19 // 最新版 03 00 00 00 01 2E 01 00 00 69 35 00 00 00 00 00 02 01 03 // 第一版 1.0.2 03 00 00 00 01 2E 01 00 00 68 13 00 00 00 00 00 02 01 03 // 1.0.4 03 00 00 00 01 2E 01 00 00 68 27 00 00 00 00 00 02 01 03 @@ -91,7 +93,7 @@ object TIMProtocol { * 发送/接受消息中的一个const (?) * length=15 */ - const val messageConst1 = "00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91" - const val messageConstNewest = "22 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91" + val messageConst1 = "00 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91".hexToBytes() + val messageConstNewest = "22 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91".hexToBytes() // TIM最新 22 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 } 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 da66ca18b..4f380189b 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 @@ -2,14 +2,10 @@ package net.mamoe.mirai.network.protocol.tim.packet -import kotlinx.io.core.BytePacketBuilder -import kotlinx.io.core.ByteReadPacket -import kotlinx.io.core.use -import kotlinx.io.core.writeUShort +import kotlinx.io.core.* import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol import net.mamoe.mirai.utils.io.encryptAndWrite -import net.mamoe.mirai.utils.io.writeHex import net.mamoe.mirai.utils.io.writeQQ import kotlin.jvm.JvmOverloads @@ -55,12 +51,12 @@ fun PacketFactory<*, *>.buildOutgoingPacket( ): OutgoingPacket { BytePacketBuilder(headerSizeHint).use { with(it) { - writeHex(TIMProtocol.head) - writeHex(TIMProtocol.ver) + writeFully(TIMProtocol.head) + writeFully(TIMProtocol.ver) writeUShort(id.value) writeUShort(sequenceId) block(this) - writeHex(TIMProtocol.tail) + writeFully(TIMProtocol.tail) } return OutgoingPacket(name, id, sequenceId, it.build()) } @@ -80,10 +76,11 @@ fun PacketFactory<*, *>.buildSessionPacket( id: PacketId = this.id, sequenceId: UShort = PacketFactory.atomicNextSequenceId(), headerSizeHint: Int = 0, + version: ByteArray = TIMProtocol.version0x02, block: BytePacketBuilder.() -> Unit ): OutgoingPacket = buildOutgoingPacket(name, id, sequenceId, headerSizeHint) { writeQQ(bot) - writeHex(TIMProtocol.version0x02) + writeFully(version) encryptAndWrite(sessionKey) { block() } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt index 56199342d..4b9e4d1f1 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt @@ -62,9 +62,9 @@ object RequestProfileDetailsPacket : SessionPacketFactory Gender.SECRET //error("Cannot determine gender, entry 0x4E29u not found") 0x02u -> Gender.FEMALE @@ -73,11 +73,11 @@ object RequestProfileDetailsPacket : SessionPacketFactory error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}") }, birthday = map[0x4E3Fu]?.let { Date(it.toUInt().toInt()) }, - personalStatus = map[0x4E33u]?.stringOfWitch(), - homepage = map[0x4E2Du]?.stringOfWitch(), - company = map[0x5DC8u]?.stringOfWitch(), - school = map[0x4E35u]?.stringOfWitch(), - email = map[0x4E2Bu]?.stringOfWitch() + personalStatus = map[0x4E33u]?.encodeToString(), + homepage = map[0x4E2Du]?.encodeToString(), + company = map[0x5DC8u]?.encodeToString(), + school = map[0x4E35u]?.encodeToString(), + email = map[0x4E2Bu]?.encodeToString() ) map.clear() diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/SendFriendMessagePacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/SendFriendMessagePacket.kt index 9a2ce6456..45290cc94 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/SendFriendMessagePacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/SendFriendMessagePacket.kt @@ -49,7 +49,7 @@ object SendFriendMessagePacket : SessionPacketFactory(SessionKey) { identity: EventPacketIdentity ): OutgoingPacket = buildOutgoingPacket(name = "EventPacket", id = id, sequenceId = sequenceId) { writeQQ(bot) - writeHex(TIMProtocol.fixVer2) + writeFully(TIMProtocol.fixVer2) encryptAndWrite(sessionKey) { writeEventPacketIdentity(identity) } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Captcha.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Captcha.kt index af7a9a771..b87d799da 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Captcha.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Captcha.kt @@ -6,10 +6,13 @@ import kotlinx.io.core.* import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol import net.mamoe.mirai.network.protocol.tim.packet.* -import net.mamoe.mirai.utils.io.* +import net.mamoe.mirai.utils.io.encryptAndWrite +import net.mamoe.mirai.utils.io.readIoBuffer +import net.mamoe.mirai.utils.io.writeHex +import net.mamoe.mirai.utils.io.writeQQ object CaptchaKey : DecrypterByteArray, DecrypterType { - override val value: ByteArray = TIMProtocol.key00BA.hexToBytes(withCache = false) + override val value: ByteArray = TIMProtocol.key00BA } @AnnotatedId(KnownPacketId.CAPTCHA) @@ -24,21 +27,21 @@ object CaptchaPacket : PacketFactory( token00BA: ByteArray ): OutgoingPacket = buildOutgoingPacket { writeQQ(bot) - writeHex(TIMProtocol.fixVer) - writeHex(TIMProtocol.key00BA) + writeFully(TIMProtocol.fixVer) + writeFully(TIMProtocol.key00BA) encryptAndWrite(TIMProtocol.key00BA) { writeHex("00 02 00 00 08 04 01 E0") - writeHex(TIMProtocol.constantData2) + writeFully(TIMProtocol.constantData2) writeHex("00 00 38") writeFully(token0825) writeHex("01 03 00 19") - writeHex(TIMProtocol.publicKey) + writeFully(TIMProtocol.publicKey) writeHex("13 00 05 00 00 00 00") writeUByte(captchaSequence.toUByte()) writeHex("00 28") writeFully(token00BA) writeHex("00 10") - writeHex(TIMProtocol.key00BAFix) + writeFully(TIMProtocol.key00BAFix) } } @@ -50,17 +53,17 @@ object CaptchaPacket : PacketFactory( token0825: ByteArray ): OutgoingPacket = buildOutgoingPacket { writeQQ(bot) - writeHex(TIMProtocol.fixVer) - writeHex(TIMProtocol.key00BA) + writeFully(TIMProtocol.fixVer) + writeFully(TIMProtocol.key00BA) encryptAndWrite(TIMProtocol.key00BA) { writeHex("00 02 00 00 08 04 01 E0") - writeHex(TIMProtocol.constantData2) + writeFully(TIMProtocol.constantData2) writeHex("00 00 38") writeFully(token0825) writeHex("01 03 00 19") - writeHex(TIMProtocol.publicKey) + writeFully(TIMProtocol.publicKey) writeHex("13 00 05 00 00 00 00 00 00 00 00 10") - writeHex(TIMProtocol.key00BAFix) + writeFully(TIMProtocol.key00BAFix) } } @@ -75,17 +78,17 @@ object CaptchaPacket : PacketFactory( ): OutgoingPacket = buildOutgoingPacket { require(captcha.length == 4) { "captcha.length must == 4" } writeQQ(bot) - writeHex(TIMProtocol.fixVer) - writeHex(TIMProtocol.key00BA) + writeFully(TIMProtocol.fixVer) + writeFully(TIMProtocol.key00BA) encryptAndWrite(TIMProtocol.key00BA) { writeHex("00 02 00 00 08 04 01 E0") - writeHex(TIMProtocol.constantData2) + writeFully(TIMProtocol.constantData2) writeHex("01 00 38") writeFully(token0825) writeHex("01 03") writeShort(25) - writeHex(TIMProtocol.publicKey)//25 + writeFully(TIMProtocol.publicKey)//25 writeHex("14 00 05 00 00 00 00 00 04") writeStringUtf8(captcha.toUpperCase()) @@ -93,7 +96,7 @@ object CaptchaPacket : PacketFactory( writeFully(captchaToken) writeShort(16) - writeHex(TIMProtocol.key00BAFix)//16 + writeFully(TIMProtocol.key00BAFix)//16 } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/ChangeOnlineStatusPacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/ChangeOnlineStatusPacket.kt index 97d924a97..a553546c6 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/ChangeOnlineStatusPacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/ChangeOnlineStatusPacket.kt @@ -3,6 +3,7 @@ package net.mamoe.mirai.network.protocol.tim.packet.login import kotlinx.io.core.ByteReadPacket +import kotlinx.io.core.writeFully import kotlinx.io.core.writeUByte import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol @@ -23,7 +24,7 @@ object ChangeOnlineStatusPacket : PacketFactory() { sessionKey: SessionKey ): OutgoingPacket = buildOutgoingPacket { writeQQ(bot) - writeHex(TIMProtocol.fixVer) + writeFully(TIMProtocol.fixVer) encryptAndWrite(sessionKey) { writeHex("00 01 00 01") } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/PasswordSubmission.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/PasswordSubmission.kt index 966c4795a..fb00cd76f 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/PasswordSubmission.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/PasswordSubmission.kt @@ -14,7 +14,7 @@ import net.mamoe.mirai.utils.io.* import net.mamoe.mirai.utils.writeCRC32 object ShareKey : DecrypterByteArray, DecrypterType { - override val value: ByteArray = TIMProtocol.shareKey.hexToBytes(withCache = false) + override val value: ByteArray = TIMProtocol.shareKey } inline class PrivateKey(override val value: ByteArray) : DecrypterByteArray { @@ -58,10 +58,10 @@ object SubmitPasswordPacket : PacketFactory() { sessionKey: SessionKey ): OutgoingPacket = buildOutgoingPacket { writeQQ(bot) - writeHex(TIMProtocol.fixVer2) + writeFully(TIMProtocol.fixVer2) encryptAndWrite(sessionKey) { writeHex("33 00 05 00 08 74 2E 71 71 2E 63 6F 6D 00 0A 71 75 6E 2E 71 71 2E 63 6F 6D 00 0C 71 7A 6F 6E 65 2E 71 71 2E 63 6F 6D 00 0C 6A 75 62 61 6F 2E 71 71 2E 63 6F 6D 00 09 6B 65 2E 71 71 2E 63 6F 6D") } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Session.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Session.kt index 0f11af402..5c98eb1dc 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Session.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Session.kt @@ -31,8 +31,8 @@ object RequestSessionPacket : PacketFactory { - override val value: ByteArray = TIMProtocol.touchKey.hexToBytes(withCache = false) + override val value: ByteArray = TIMProtocol.touchKey } /** @@ -27,12 +28,12 @@ object TouchPacket : PacketFactory(TouchKey isRedirect: Boolean ): OutgoingPacket = buildOutgoingPacket { writeQQ(bot) - writeHex(TIMProtocol.fixVer) - writeHex(TIMProtocol.touchKey) + writeFully(TIMProtocol.fixVer) + writeFully(TIMProtocol.touchKey) encryptAndWrite(TIMProtocol.touchKey) { - writeHex(TIMProtocol.constantData1) - writeHex(TIMProtocol.constantData2) + writeFully(TIMProtocol.constantData1) + writeFully(TIMProtocol.constantData2) writeQQ(bot) writeHex(if (isRedirect) "00 01 00 00 03 09 00 0C 00 01" else "00 00 00 00 03 09 00 08 00 01") writeIP(serverIp) @@ -40,7 +41,7 @@ object TouchPacket : PacketFactory(TouchKey if (isRedirect) "01 6F A1 58 22 01 00 36 00 12 00 02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 14 00 1D 01 03 00 19" else "00 02 00 36 00 12 00 02 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 14 00 1D 01 02 00 19" ) - writeHex(TIMProtocol.publicKey) + writeFully(TIMProtocol.publicKey) } }