Parcourir la source

Remove `httpVerify`, `httpBind` dsl

ryoii il y a 2 ans
Parent
commit
cd9ad236c0

+ 0 - 21
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt

@@ -24,9 +24,7 @@ import net.mamoe.mirai.api.http.adapter.http.plugin.session
 import net.mamoe.mirai.api.http.adapter.http.support.receiveParameter
 import net.mamoe.mirai.api.http.adapter.internal.consts.Paths
 import net.mamoe.mirai.api.http.adapter.internal.dto.AuthedDTO
-import net.mamoe.mirai.api.http.adapter.internal.dto.BindDTO
 import net.mamoe.mirai.api.http.adapter.internal.dto.DTO
-import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyDTO
 import net.mamoe.mirai.api.http.context.MahContext
 import net.mamoe.mirai.api.http.context.MahContextHolder
 import net.mamoe.mirai.api.http.context.session.Session
@@ -63,25 +61,6 @@ internal inline fun <reified T, reified R : DTO> respondDTOStrategy(crossinline
 internal inline fun Route.routeWithHandle(path: String, method: HttpMethod, crossinline blk: Strategy<Unit>) =
     route(Paths.httpPath(path), method) { handle { blk(Unit) } }
 
-/**
- * Auth,处理http server的验证
- * 为闭包传入一个AuthDTO对象
- */
-@KtorDsl
-internal inline fun Route.httpVerify(path: String, crossinline body: Strategy<VerifyDTO>) =
-    routeWithHandle(path, HttpMethod.Post) {
-        val dto = context.receive<VerifyDTO>()
-        this.body(dto)
-    }
-
-
-@KtorDsl
-internal inline fun Route.httpBind(path: String, crossinline body: Strategy<BindDTO>) =
-    routeWithHandle(path, HttpMethod.Post) {
-        val dto = context.receive<BindDTO>()
-        body(dto)
-    }
-
 
 /**
  * Verify,用于处理bot的行为请求

+ 29 - 22
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/verify.kt

@@ -10,6 +10,7 @@
 package net.mamoe.mirai.api.http.adapter.http.router
 
 import io.ktor.server.application.*
+import io.ktor.server.request.*
 import io.ktor.server.response.*
 import io.ktor.server.routing.*
 import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException
@@ -17,6 +18,8 @@ import net.mamoe.mirai.api.http.adapter.common.StateCode
 import net.mamoe.mirai.api.http.adapter.http.HttpAdapterSetting
 import net.mamoe.mirai.api.http.adapter.http.session.asHttpSession
 import net.mamoe.mirai.api.http.adapter.http.session.unloadHttpSession
+import net.mamoe.mirai.api.http.adapter.internal.dto.BindDTO
+import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyDTO
 import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyRetDTO
 import net.mamoe.mirai.api.http.context.MahContextHolder
 import net.mamoe.mirai.api.http.util.getBotOrThrow
@@ -29,40 +32,42 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
     /**
      * 进行认证
      */
-    httpVerify("/verify") {
-        if (!MahContextHolder.enableVerify
-            || it.verifyKey == MahContextHolder.sessionManager.verifyKey
-        ) {
-            val session = if (MahContextHolder.singleMode) {
-                MahContextHolder.createSingleSession(verified = true)
-                    .asHttpSession(setting.unreadQueueMaxSize)
-            } else {
-                MahContextHolder.sessionManager.createTempSession()
-            }
+    post("/verify") {
+        val verifyDTO = call.receive<VerifyDTO>()
+        if (MahContextHolder.enableVerify && verifyDTO.verifyKey != MahContextHolder.sessionManager.verifyKey) {
+            call.respond(StateCode.AuthKeyFail)
+            return@post
+        }
 
-            call.respond(VerifyRetDTO(0, session.key))
-            return@httpVerify
+        val session = if (MahContextHolder.singleMode) {
+            MahContextHolder.createSingleSession(verified = true)
+                .asHttpSession(setting.unreadQueueMaxSize)
+        } else {
+            MahContextHolder.sessionManager.createTempSession()
         }
 
-        call.respond(StateCode.AuthKeyFail)
+        call.respond(VerifyRetDTO(0, session.key))
     }
 
     /**
      * 验证并分配session
      */
-    httpBind("/bind") {
+    post("/bind") {
         if (MahContextHolder.singleMode) {
             call.respond(StateCode.NoOperateSupport)
-            return@httpBind
+            return@post
         }
-        val session = MahContextHolder[it.sessionKey] ?: kotlin.run {
+
+        val bindDTO = call.receive<BindDTO>()
+
+        val session = MahContextHolder[bindDTO.sessionKey] ?: kotlin.run {
             call.respond(StateCode.IllegalSession)
-            return@httpBind
+            return@post
         }
 
         if (!session.isAuthed) {
-            val bot = getBotOrThrow(it.qq)
-            MahContextHolder.sessionManager.authSession(bot, it.sessionKey)
+            val bot = getBotOrThrow(bindDTO.qq)
+            MahContextHolder.sessionManager.authSession(bot, bindDTO.sessionKey)
                 .asHttpSession(setting.unreadQueueMaxSize)
         }
         call.respond(StateCode.Success)
@@ -71,9 +76,11 @@ internal fun Application.authRouter(setting: HttpAdapterSetting) = routing {
     /**
      * 释放session
      */
-    httpBind("/release") {
-        val bot = getBotOrThrow(it.qq)
-        val session = MahContextHolder[it.sessionKey] ?: throw IllegalSessionException
+    post("/release") {
+        val bindDTO = call.receive<BindDTO>()
+
+        val bot = getBotOrThrow(bindDTO.qq)
+        val session = MahContextHolder[bindDTO.sessionKey] ?: throw IllegalSessionException
         if (bot.id == session.bot.id) {
             session.apply {
                 unloadHttpSession()