ryoii 4 vuotta sitten
vanhempi
sitoutus
c8c4cd73e6

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

@@ -26,15 +26,20 @@ internal fun Application.authRouter() = routing {
      * 进行认证
      */
     httpVerify("/verify") {
-        if (!MahContextHolder.mahContext.enableVerify) {
-            call.respondStateCode(StateCode.NoOperateSupport)
+        if (!MahContextHolder.mahContext.enableVerify
+            || it.verifyKey == MahContextHolder.mahContext.sessionManager.verifyKey
+        ) {
+            val session = if (MahContextHolder.mahContext.singleMode) {
+                MahContextHolder.mahContext.createSingleSession(verified = true)
+            } else {
+                MahContextHolder.sessionManager.createTempSession()
+            }
+
+            call.respondDTO(VerifyRetDTO(0, session.key))
             return@httpVerify
         }
-        if (it.verifyKey != MahContextHolder.mahContext.sessionManager.verifyKey) {
-            call.respondStateCode(StateCode.AuthKeyFail)
-        } else {
-            call.respondDTO(VerifyRetDTO(0, MahContextHolder.sessionManager.createTempSession().key))
-        }
+
+        call.respondStateCode(StateCode.AuthKeyFail)
     }
 
     /**
@@ -46,12 +51,8 @@ internal fun Application.authRouter() = routing {
             return@httpBind
         }
         val session = MahContextHolder[it.sessionKey] ?: kotlin.run {
-            if (MahContextHolder.mahContext.enableVerify) {
-                call.respondStateCode(StateCode.IllegalSession)
-                return@httpBind
-            } else {
-                null
-            }
+            call.respondStateCode(StateCode.IllegalSession)
+            return@httpBind
         }
 
         if (session !is AuthedSession) {

+ 1 - 1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/about.kt

@@ -30,5 +30,5 @@ internal fun onAbout(): Map<String, String> {
  * 获取 session 信息
  */
 internal fun onGetSessionInfo(dto: EmptyAuthedDTO): SessionDTO {
-    return SessionDTO(dto.sessionKey, QQDTO(dto.session.bot.asFriend))
+    return SessionDTO(dto.session.key, QQDTO(dto.session.bot.asFriend))
 }

+ 2 - 2
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/ws/router/utils.kt

@@ -42,7 +42,7 @@ internal inline fun Route.miraiWebsocket(
 
         // single 模式
         if (MahContextHolder.mahContext.singleMode) {
-            body(MahContextHolder[MahContext.SINGLE_SESSION_KEY] as AuthedSession)
+            body(MahContextHolder.mahContext.createSingleSession(true) as AuthedSession)
             return@webSocket
         }
 
@@ -62,7 +62,7 @@ internal inline fun Route.miraiWebsocket(
             return@webSocket
         }
 
-        // 非 single 模式校验 session key
+        // 非 single 模式校验已有 session
         if (sessionKey == null) {
             closeWithCode(StateCode.InvalidParameter)
             return@webSocket

+ 40 - 20
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/MahContext.kt

@@ -15,8 +15,8 @@ import net.mamoe.mirai.api.http.adapter.MahAdapter
 import net.mamoe.mirai.api.http.adapter.common.NoSuchBotException
 import net.mamoe.mirai.api.http.context.cache.MessageSourceCache
 import net.mamoe.mirai.api.http.context.session.AuthedSession
-import net.mamoe.mirai.api.http.context.session.SampleAuthedSession
 import net.mamoe.mirai.api.http.context.session.Session
+import net.mamoe.mirai.api.http.context.session.TempSession
 import net.mamoe.mirai.api.http.context.session.manager.SessionManager
 import net.mamoe.mirai.api.http.setting.MainSetting
 import net.mamoe.mirai.event.Listener
@@ -74,6 +74,11 @@ open class MahContext internal constructor() {
      */
     var singleMode = false
 
+    /**
+     * 调试日志
+     */
+    val debugLog by lazy { MiraiLogger.Factory.create(this::class, "Mah Debug").withSwitch(debug) }
+
     /**
      * 添加一个 adapter
      */
@@ -81,10 +86,38 @@ open class MahContext internal constructor() {
         adapters.add(adapter)
     }
 
-    /**
-     * 调试日志
-     */
-    val debugLog by lazy { MiraiLogger.Factory.create(this::class, "Mah Debug").withSwitch(debug) }
+    // 生成 SingleMode Session
+    fun createSingleSession(verified: Boolean = false): Session {
+        var session = sessionManager[SINGLE_SESSION_KEY]
+
+        // double check lock
+        if (session == null) {
+            synchronized(this) {
+                if (session == null) {
+                    val singleTempSession = TempSession(SINGLE_SESSION_KEY, EmptyCoroutineContext)
+                    sessionManager[SINGLE_SESSION_KEY] = singleTempSession
+                    session = singleTempSession
+                }
+            }
+        }
+
+        val autoVerify = !enableVerify
+        if (verified || autoVerify){
+            session = authSingleSession()
+        }
+
+        return session!!
+    }
+
+    private fun authSingleSession(): AuthedSession {
+        val bot = Bot.instances.firstOrNull() ?: throw NoSuchBotException
+        val session = sessionManager[SINGLE_SESSION_KEY]
+        if (session is TempSession) {
+            sessionManager.authSession(bot, session)
+            MahContextHolder.listen(bot, SINGLE_SESSION_KEY)
+        }
+        return sessionManager[SINGLE_SESSION_KEY] as AuthedSession
+    }
 }
 
 
@@ -97,21 +130,8 @@ object MahContextHolder {
 
     operator fun get(sessionKey: String): Session? {
         if (mahContext.singleMode) {
-            var session = sessionManager[MahContext.SINGLE_SESSION_KEY]
-
-            // double check lock
-            if (session == null) {
-                synchronized(MahContextHolder) {
-                    if (session == null) {
-                        val bot = Bot.instances.firstOrNull() ?: throw NoSuchBotException
-                        val singleAuthedSession = SampleAuthedSession(bot, MahContext.SINGLE_SESSION_KEY, EmptyCoroutineContext)
-                        listen(bot, MahContext.SINGLE_SESSION_KEY)
-                        sessionManager[MahContext.SINGLE_SESSION_KEY] = singleAuthedSession
-                        session =  singleAuthedSession
-                    }
-                }
-            }
-            return session
+            return sessionManager[MahContext.SINGLE_SESSION_KEY]
+                ?: mahContext.createSingleSession()
         }
 
         return sessionManager[sessionKey]