Him188moe 6 ani în urmă
părinte
comite
3c0c8f18cf

+ 1 - 1
LICENSE

@@ -508,7 +508,7 @@ license to downstream recipients.  "Knowingly relying" means you have
 actual knowledge that, but for the patent license, your conveying the
 covered work in a country, or your recipient's use of the covered work
 in a country, would infringe one or more identifiable patents in that
-country that you have reason to believe are valid.
+country that you have state to believe are valid.
 
   If, pursuant to or in connection with a single transaction or
 arrangement, you convey, or propagate by procuring conveyance of, a

+ 46 - 20
mirai-core/src/main/java/net/mamoe/mirai/network/Robot.kt

@@ -15,7 +15,11 @@ import net.mamoe.mirai.network.packet.client.login.ClientPasswordSubmissionPacke
 import net.mamoe.mirai.network.packet.client.login.ClientServerRedirectionPacket
 import net.mamoe.mirai.network.packet.client.writeHex
 import net.mamoe.mirai.network.packet.server.ServerPacket
-import net.mamoe.mirai.network.packet.server.ServerTouchResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginFailedResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginResendResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginSucceedResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginVerificationCodeResponsePacket
+import net.mamoe.mirai.network.packet.server.touch.ServerTouchResponsePacket
 import net.mamoe.mirai.utils.MiraiLogger
 import java.net.DatagramPacket
 import java.net.InetSocketAddress
@@ -26,37 +30,59 @@ import java.net.InetSocketAddress
  * @author Him188moe @ Mirai Project
  */
 class Robot(val number: Int, private val password: String) {
-    private lateinit var channel: Channel
+    private var channel: Channel? = null
 
 
     @ExperimentalUnsignedTypes
     internal fun onPacketReceived(packet: ServerPacket) {
         packet.decode()
-        if (packet is ServerTouchResponsePacket) {
-            if (packet.serverIP != null) {//redirection
-                connect(packet.serverIP!!)
-                sendPacket(ClientServerRedirectionPacket(
-                        serverIP = packet.serverIP!!,
-                        qq = number
-                ))
-            } else {//password submission
-                sendPacket(ClientPasswordSubmissionPacket(
-                        qq = this.number,
-                        password = this.password,
-                        loginTime = packet.loginTime,
-                        loginIP = packet.loginIP,
-                        token0825 = packet.token,
-                        tgtgtKey = packet.tgtgtKey
-                ))
+        when (packet) {
+            is ServerTouchResponsePacket -> {
+                if (packet.serverIP != null) {//redirection
+                    connect(packet.serverIP!!)
+                    sendPacket(ClientServerRedirectionPacket(
+                            serverIP = packet.serverIP!!,
+                            qq = number
+                    ))
+                } else {//password submission
+                    sendPacket(ClientPasswordSubmissionPacket(
+                            qq = this.number,
+                            password = this.password,
+                            loginTime = packet.loginTime,
+                            loginIP = packet.loginIP,
+                            token0825 = packet.token,
+                            tgtgtKey = packet.tgtgtKey
+                    ))
+                }
             }
+
+            is ServerLoginFailedResponsePacket -> {
+                channel = null
+                println("Login failed: " + packet.state.toString())
+            }
+
+            is ServerLoginVerificationCodeResponsePacket -> {
+
+            }
+
+            is ServerLoginSucceedResponsePacket -> {
+
+            }
+
+            is ServerLoginResendResponsePacket -> {
+
+            }
+
+            else -> throw IllegalStateException()
         }
+
     }
 
     @ExperimentalUnsignedTypes
     private fun sendPacket(packet: ClientPacket) {
         packet.encode()
         packet.writeHex(Protocol.tail);
-        channel.writeAndFlush(DatagramPacket(packet.toByteArray()))
+        channel!!.writeAndFlush(DatagramPacket(packet.toByteArray()))
     }
 
     companion object {
@@ -106,7 +132,7 @@ class Robot(val number: Int, private val password: String) {
                     })
 
             channel = b.connect().sync().channel();
-            channel.closeFuture().sync()
+            channel!!.closeFuture().sync()
         } finally {
             group.shutdownGracefully().sync()
         }

+ 5 - 1
mirai-core/src/main/java/net/mamoe/mirai/network/packet/client/login/ClientPasswordSubmissionPacket.kt

@@ -51,7 +51,11 @@ class ClientPasswordSubmissionPacket(private val qq: Int, private val password:
                 this.writeHex("03 0F");//tag
                 this.writeShort(hostName.length / 2);//todo check that
                 this.writeShort(hostName.length);
-                this.writeBytes(hostName)
+                this.writeBytes(hostName)//todo 这个对吗?
+                /*易语言源码: PCName就是HostName
+                PCName = BytesToStr (Ansi转Utf8 (取主机名 ()))
+                PCName = 取文本左边 (PCName, 取文本长度 (PCName) - 3)*/
+
                 this.writeHex("00 05 00 06 00 02")
                 this.writeQQ(qq)
                 this.writeHex("00 06")//tag

+ 1 - 1
mirai-core/src/main/java/net/mamoe/mirai/network/packet/client/login/ClientHeartbeatPacket.kt → mirai-core/src/main/java/net/mamoe/mirai/network/packet/client/touch/ClientHeartbeatPacket.kt

@@ -1,4 +1,4 @@
-package net.mamoe.mirai.network.packet.client.login
+package net.mamoe.mirai.network.packet.client.touch
 
 import net.mamoe.mirai.network.Protocol
 import net.mamoe.mirai.network.packet.PacketId

+ 1 - 1
mirai-core/src/main/java/net/mamoe/mirai/network/packet/client/login/ClientTouchPacket.kt → mirai-core/src/main/java/net/mamoe/mirai/network/packet/client/touch/ClientTouchPacket.kt

@@ -1,4 +1,4 @@
-package net.mamoe.mirai.network.packet.client.login
+package net.mamoe.mirai.network.packet.client.touch
 
 import net.mamoe.mirai.network.Protocol
 import net.mamoe.mirai.network.packet.PacketId

+ 0 - 11
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/ServerLoginSucceedPacket.kt

@@ -1,11 +0,0 @@
-package net.mamoe.mirai.network.packet.server
-
-import java.io.DataInputStream
-
-/**
- * @author Him188moe @ Mirai Project
- */
-class ServerLoginSucceedPacket(inputStream: DataInputStream) : ServerPacket(inputStream) {
-    override fun decode() {
-    }
-}

+ 28 - 3
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/ServerPacket.kt

@@ -1,8 +1,12 @@
 package net.mamoe.mirai.network.packet.server
 
 import net.mamoe.mirai.network.packet.Packet
+import net.mamoe.mirai.network.packet.server.login.ServerLoginFailedResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginResendResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginSucceedResponsePacket
+import net.mamoe.mirai.network.packet.server.login.ServerLoginVerificationCodeResponsePacket
+import net.mamoe.mirai.network.packet.server.touch.ServerTouchResponsePacket
 import net.mamoe.mirai.util.toHexString
-
 import java.io.DataInputStream
 
 /**
@@ -24,13 +28,34 @@ abstract class ServerPacket(val input: DataInputStream) : Packet {
             return when (idBytes.joinToString("") { it.toString(16) }) {
                 "08 25 31 01" -> ServerTouchResponsePacket(ServerTouchResponsePacket.Type.TYPE_08_25_31_01, stream)
                 "08 25 31 02" -> ServerTouchResponsePacket(ServerTouchResponsePacket.Type.TYPE_08_25_31_02, stream)
+                "08 36 31 03", "08 36 31 04", "08 36 31 05", "08 36 31 06" -> {
+                    when (bytes.size) {
+                        271, 207 -> return ServerLoginResendResponsePacket(stream)
+                        871 -> return ServerLoginVerificationCodeResponsePacket(stream)
+                    }
+
+                    if (bytes.size > 700) {
+                        return ServerLoginSucceedResponsePacket(stream)
+                    }
 
-                else -> throw UnsupportedOperationException()
+                    return ServerLoginFailedResponsePacket(when (bytes.size) {
+                        319 -> ServerLoginFailedResponsePacket.State.WRONG_PASSWORD
+                        135 -> ServerLoginFailedResponsePacket.State.RETYPE_PASSWORD
+                        279 -> ServerLoginFailedResponsePacket.State.BLOCKED
+                        263 -> ServerLoginFailedResponsePacket.State.UNKNOWN_QQ_NUMBER
+                        551, 487 -> ServerLoginFailedResponsePacket.State.DEVICE_LOCK
+                        359 -> ServerLoginFailedResponsePacket.State.TAKEN_BACK
+                        else -> throw IllegalStateException()
+                    }, stream)
+                }
+
+                else -> throw IllegalStateException()
             }
         }
     }
 }
 
+
 fun DataInputStream.skipUntil(byte: Byte) {
     while (readByte() != byte);
 }
@@ -52,7 +77,7 @@ fun DataInputStream.readIP(): String {
     for (i in 0..3) {
         val byte = readByte();
         buff += (byte.toUByte().toString())
-        if(i !=3)buff+="."
+        if (i != 3) buff += "."
         println(byte.toHexString())
     }
     return buff

+ 24 - 0
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/login/ServerLoginFailedResponsePacket.kt

@@ -0,0 +1,24 @@
+package net.mamoe.mirai.network.packet.server.login
+
+import net.mamoe.mirai.network.packet.server.ServerPacket
+import java.io.DataInputStream
+
+/**
+ * @author Him188moe @ Mirai Project
+ */
+class ServerLoginFailedResponsePacket(val state: State, input: DataInputStream) : ServerPacket(input) {
+    enum class State {
+        WRONG_PASSWORD,
+        // UNKNOWN,//? 要再次发送某数据包
+        RETYPE_PASSWORD,//similar to [WRONG_PASSWORD]
+        BLOCKED,//你的帐号存在被盗风险,已进入保护模式
+        UNKNOWN_QQ_NUMBER,//你输入的帐号不存在
+        DEVICE_LOCK,//设备锁
+        TAKEN_BACK,//被回收
+        // VERIFICATION_CODE,//需要验证码
+        // SUCCEED,
+    }
+
+    override fun decode() {
+    }
+}

+ 15 - 0
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/login/ServerLoginResendResponsePacket.kt

@@ -0,0 +1,15 @@
+package net.mamoe.mirai.network.packet.server.login
+
+import net.mamoe.mirai.network.packet.server.ServerPacket
+import java.io.DataInputStream
+
+/**
+ * @author Him188moe @ Mirai Project
+ */
+class ServerLoginResendResponsePacket(input: DataInputStream) : ServerPacket(input) {
+    
+
+    override fun decode() {
+        TODO()
+    }
+}

+ 15 - 0
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/login/ServerLoginSucceedResponsePacket.kt

@@ -0,0 +1,15 @@
+package net.mamoe.mirai.network.packet.server.login
+
+import net.mamoe.mirai.network.packet.server.ServerPacket
+import java.io.DataInputStream
+
+/**
+ * @author Him188moe @ Mirai Project
+ */
+class ServerLoginSucceedResponsePacket(input: DataInputStream) : ServerPacket(input) {
+
+
+    override fun decode() {
+        TODO()
+    }
+}

+ 19 - 0
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/login/ServerLoginVerificationCodeResponsePacket.kt

@@ -0,0 +1,19 @@
+package net.mamoe.mirai.network.packet.server.login
+
+import net.mamoe.mirai.network.packet.server.ServerPacket
+import java.io.DataInputStream
+
+/**
+ * @author Him188moe @ Mirai Project
+ */
+class ServerLoginVerificationCodeResponsePacket(input: DataInputStream) : ServerPacket(input) {
+    private var verifyCodeLength: Int = 0
+    private lateinit var verifyCode: String
+    private lateinit var token00BA: ByteArray
+
+
+    override fun decode() {
+
+        TODO()
+    }
+}

+ 3 - 1
mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/ServerTouchResponsePacket.kt → mirai-core/src/main/java/net/mamoe/mirai/network/packet/server/touch/ServerTouchResponsePacket.kt

@@ -1,5 +1,7 @@
-package net.mamoe.mirai.network.packet.server
+package net.mamoe.mirai.network.packet.server.touch
 
+import net.mamoe.mirai.network.packet.server.ServerPacket
+import net.mamoe.mirai.network.packet.server.readIP
 import net.mamoe.mirai.util.getRandomKey
 import java.io.DataInputStream
 

+ 1 - 1
mirai-core/src/test/java/PacketTest.kt

@@ -1,5 +1,5 @@
 import net.mamoe.mirai.network.packet.client.toHexString
-import net.mamoe.mirai.network.packet.server.ServerTouchResponsePacket
+import net.mamoe.mirai.network.packet.server.touch.ServerTouchResponsePacket
 import net.mamoe.mirai.util.hexToBytes
 import net.mamoe.mirai.util.toHexString
 import java.io.DataInputStream