Him188 6 anni fa
parent
commit
d4bfbb8088

+ 95 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/packet/OutgoingPacketHelperAndroid.kt

@@ -0,0 +1,95 @@
+package net.mamoe.mirai.qqandroid.network.packet
+
+import kotlinx.atomicfu.AtomicInt
+import kotlinx.atomicfu.atomic
+import kotlinx.io.core.BytePacketBuilder
+
+
+/*
+private open fun writeHead(
+    always_8001: Int,
+    command: Int,
+    uin: Long,
+    encryptType: Int,
+    const8_always_0: Int,
+    appClientVersion: Int,
+    constp_always_0: Int,
+    bodyLength: Int
+) {
+    val j: Int = this.j + 1
+    this.j = j
+    this.pos = 0
+    util.int8_to_buf(this.buffer, this.pos, 2)
+    ++this.pos
+    util.int16_to_buf(this.buffer, this.pos, this.d + 2 + bodyLength)
+    this.pos += 2
+    util.int16_to_buf(this.buffer, this.pos, always_8001)
+    this.pos += 2
+    util.int16_to_buf(this.buffer, this.pos, command)
+    this.pos += 2
+    util.int16_to_buf(this.buffer, this.pos, j)
+    this.pos += 2
+    util.int32_to_buf(this.buffer, this.pos, uin.toInt())
+    this.pos += 4
+    util.int8_to_buf(this.buffer, this.pos, 3)
+    ++this.pos
+    util.int8_to_buf(this.buffer, this.pos, encryptType)
+    ++this.pos
+    util.int8_to_buf(this.buffer, this.pos, const8_always_0)
+    ++this.pos
+    util.int32_to_buf(this.buffer, this.pos, 2)
+    this.pos += 4
+    util.int32_to_buf(this.buffer, this.pos, appClientVersion)
+    this.pos += 4
+    util.int32_to_buf(this.buffer, this.pos, constp_always_0)
+    this.pos += 4
+}
+*/
+
+@UseExperimental(ExperimentalUnsignedTypes::class)
+private fun BytePacketBuilder.writeHead(
+    always_8001: Short = 8001,
+    command: Short,
+    uin: Long,
+    encryptType: Int, //
+    sequenceId: Int = SequenceIdCounter.nextSequenceId(),
+    const8_always_0: Byte = 0,
+    appClientVersion: Int,
+    constp_always_0: Int = 0,
+    bodyLength: Int
+) {
+    writeByte(2)
+    writeShort((27 + 2 + bodyLength).toShort())
+    writeShort(always_8001)
+    writeShort(command)
+    writeShort(sequenceId.toShort())
+    writeInt(uin.toInt())
+    writeByte(3)
+    writeByte(encryptType.toByte())
+    writeByte(const8_always_0)
+    writeInt(2)
+    writeInt(appClientVersion)
+    writeInt(constp_always_0)
+}
+
+fun buildOutgoingPacket(
+    command: Short
+    ///uin: Long,
+) {
+
+}
+
+//private b
+
+private object SequenceIdCounter {
+    private val sequenceId: AtomicInt = atomic(0)
+
+    fun nextSequenceId(): Int {
+        val id = sequenceId.getAndAdd(1)
+        if (id > Short.MAX_VALUE.toInt() * 2) {
+            sequenceId.value = 0
+            return nextSequenceId()
+        }
+        return id
+    }
+}

+ 25 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/packet/TouchPacket.kt

@@ -0,0 +1,25 @@
+package net.mamoe.mirai.qqandroid.network.packet
+
+import kotlinx.io.core.ByteReadPacket
+import net.mamoe.mirai.data.Packet
+import net.mamoe.mirai.network.BotNetworkHandler
+import net.mamoe.mirai.network.packet.DecrypterByteArray
+import net.mamoe.mirai.network.packet.DecrypterType
+import net.mamoe.mirai.network.packet.PacketFactory
+import net.mamoe.mirai.network.packet.PacketId
+
+
+object TouchKey : DecrypterByteArray, DecrypterType<TouchKey> {
+    override val value: ByteArray
+        get() = TODO("not implemented")
+}
+
+object TouchPacket : PacketFactory<TouchPacketResponse, TouchKey>(TouchKey) {
+    @UseExperimental(ExperimentalUnsignedTypes::class)
+    override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler): TouchPacketResponse {
+        TODO("not implemented")
+    }
+}
+
+
+interface TouchPacketResponse : Packet

+ 158 - 0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/packet/tlv/Tlv.kt

@@ -0,0 +1,158 @@
+package net.mamoe.mirai.qqandroid.network.packet.tlv
+
+import kotlinx.io.core.BytePacketBuilder
+import kotlinx.io.core.buildPacket
+import kotlinx.io.core.readBytes
+import kotlinx.io.core.writeFully
+import net.mamoe.mirai.utils.io.*
+import net.mamoe.mirai.utils.md5
+import kotlin.random.Random
+
+object Tlv {
+    fun BytePacketBuilder.t1(qq: Long, ip: ByteArray) {
+        require(ip.size == 4)
+        writeShort(0x0001)
+        writeShortLVPacket {
+            writeShort(1) // ip_ver
+            writeInt(Random.nextInt())
+            writeInt(qq.toInt())
+            writeTime()
+            writeFully(ip)
+            writeShort(0)
+        }
+    }
+
+    fun BytePacketBuilder.t2(captchaCode: String, captchaToken: ByteArray, sigVer: Short = 0) {
+        writeShort(0x0002)
+        writeShortLVPacket {
+            writeShort(sigVer)
+            writeShortLVString(captchaCode)
+            writeShortLVByteArray(captchaToken)
+        }
+    }
+
+    fun BytePacketBuilder.t8() {
+        writeShort(0x0008)
+        writeShortLVPacket {
+            writeShort(0)
+            writeInt(2052) // localId
+            writeShort(0)
+        }
+    }
+
+    fun BytePacketBuilder.t18(appId: Long, appClientVersion: Int, uin: Long, constant1_always_0: Int) {
+        writeShort(0x18)
+        writeShortLVPacket {
+            writeShort(1) //ping_version
+            writeInt(1536) //sso_version
+            writeInt(appId.toInt())
+            writeInt(appClientVersion)
+            writeInt(uin.toInt())
+            writeShort(constant1_always_0.toShort())
+            writeShort(0)
+        }
+    }
+
+    fun BytePacketBuilder.t106(
+        appId: Long,
+        subAppId: Long,
+        appClientVersion: Int,
+        uin: Long,
+        ipAddress: ByteArray,
+        n5_always_1: Int = 1,
+        temp_pwd: ByteArray,
+        salt: Long,
+        uinAccount: ByteArray,
+        tgtgtKey: ByteArray,
+        n7: Int,
+        array_6_may_be_null: ByteArray?,
+        n8: Int = 1
+    ) {
+        writeShort(0x106)
+
+        writeShortLVPacket {
+            encryptAndWrite(
+                if (salt == 0L) {
+                    md5(buildPacket { writeFully(temp_pwd); writeInt(uin.toInt()) }.readBytes())
+                } else {
+                    md5(buildPacket { writeFully(temp_pwd); writeInt(salt.toInt()) }.readBytes())
+                }
+            ) {
+                writeShort(4)//TGTGTVer
+                writeInt(Random.nextInt())
+                writeInt(5)//ssoVer
+                writeInt(appId.toInt())
+                writeInt(appClientVersion)
+
+                if (uin == 0L) {
+                    writeLong(salt)
+                } else {
+                    writeLong(uin)
+                }
+
+                writeTime()
+                writeFully(ipAddress)
+                writeByte(n5_always_1.toByte())
+                writeFully(temp_pwd)
+                writeFully(tgtgtKey)
+                writeInt(0)
+                writeByte(n7.toByte())
+                if (array_6_may_be_null == null) {
+                    repeat(4) {
+                        writeInt(Random.nextInt())
+                    }
+                } else {
+                    writeFully(array_6_may_be_null)
+                }
+                writeInt(subAppId.toInt())
+                writeInt(n8)
+                writeShortLVByteArray(uinAccount)
+            }
+        }
+    }
+
+    fun BytePacketBuilder.t100(
+        appId: Long,
+        subAppId: Long,
+        appClientVersion: Int,
+        mainSigMap: Int
+    ) {
+        writeShort(0x100)
+        writeShortLVPacket {
+            writeShort(1)//db_buf_ver
+            writeInt(5)//sso_ver
+            writeInt(appId.toInt())
+            writeInt(subAppId.toInt())
+            writeInt(appClientVersion)
+            writeInt(mainSigMap)
+        } shouldEqualsTo 22
+    }
+
+    fun BytePacketBuilder.t107(
+        picType: Int,
+        const1_always_0: Int = 0,
+        const2_always_0: Int = 0,
+        const3_always_1: Int = 1
+    ) {
+        writeShort(0x107)
+        writeShortLVPacket {
+            writeShort(picType.toShort())
+            writeByte(const1_always_0.toByte())
+            writeShort(const2_always_0.toShort())
+            writeByte(const3_always_1.toByte())
+        } shouldEqualsTo 6
+    }
+}
+
+
+private infix fun Int.shouldEqualsTo(int: Int) = require(this == int)
+
+fun randomAndroidId(): String = buildString(15) {
+    repeat(15) { append(Random.nextInt(10)) }
+}
+
+fun generateGuid(androidId: String, macAddress: String): ByteArray {
+    return md5(androidId + macAddress)
+}
+
+fun getMacAddr(): String = "02:00:00:00:00:00"