From 221a5c60ab510a40663890172f8ef16e51baddba Mon Sep 17 00:00:00 2001 From: ryoii Date: Mon, 12 Dec 2022 12:19:26 +0800 Subject: [PATCH] Support `about`, `botList` commands for unauthorized reverse websocket connects (#610) --- .../api/http/context/session/manager/api.kt | 2 ++ .../mirai/api/http/adapter/reverse/action.kt | 18 ++++++++++++------ .../http/context/session/manager/default.kt | 5 +++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/mirai-api-http-spi/src/main/java/net/mamoe/mirai/api/http/context/session/manager/api.kt b/mirai-api-http-spi/src/main/java/net/mamoe/mirai/api/http/context/session/manager/api.kt index 9b08b2bb..69f69a34 100644 --- a/mirai-api-http-spi/src/main/java/net/mamoe/mirai/api/http/context/session/manager/api.kt +++ b/mirai-api-http-spi/src/main/java/net/mamoe/mirai/api/http/context/session/manager/api.kt @@ -24,6 +24,8 @@ interface SessionManager { */ val verifyKey: String + fun getEmptySession(): Session + fun createOneTimeSession(bot: Bot): Session /** diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/reverse/action.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/reverse/action.kt index 30ebcdcf..c9b80b2a 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/reverse/action.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/reverse/action.kt @@ -14,6 +14,7 @@ import io.ktor.websocket.* import kotlinx.serialization.Serializable import net.mamoe.mirai.Bot import net.mamoe.mirai.api.http.adapter.common.StateCode +import net.mamoe.mirai.api.http.adapter.internal.consts.Paths import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyRetDTO import net.mamoe.mirai.api.http.adapter.internal.serializer.jsonElementParseOrNull import net.mamoe.mirai.api.http.adapter.internal.serializer.jsonParseOrNull @@ -36,7 +37,17 @@ internal suspend fun DefaultClientWebSocketSession.handleReverseWs(client: WsCli sessionKey = kotlin.runCatching { - handleVerify(command)?.key + when(command.command) { + "verify" -> handleVerify(command)?.key + Paths.about, Paths.botList -> { + outgoing.handleWsAction(MahContextHolder.sessionManager.getEmptySession(), String(frame.data)) + null + } + else -> { + sendWithCode(StateCode.AuthKeyFail) + null + } + } }.onFailure { outgoing.send(Frame.Text(it.localizedMessage ?: "")) @@ -70,11 +81,6 @@ internal suspend fun DefaultClientWebSocketSession.handleReverseWs(client: WsCli } private suspend fun DefaultClientWebSocketSession.handleVerify(commandWrapper: WsIncoming): Session? { - if (commandWrapper.command != "verify") { - sendWithCode(StateCode.AuthKeyFail) - return null - } - val dto = commandWrapper.content?.jsonElementParseOrNull() if (dto == null) { diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/session/manager/default.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/session/manager/default.kt index 522963e7..3857689d 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/session/manager/default.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/session/manager/default.kt @@ -28,6 +28,11 @@ class DefaultSessionManager(override val verifyKey: String, val context: MahCont private val sessionMap: MutableMap = mutableMapOf() private val cacheMap: MutableMap = mutableMapOf() + private val emptySession: Session by lazy { StandardSession("empty", this) } + override fun getEmptySession(): Session { + return emptySession + } + override fun createOneTimeSession(bot: Bot) = StandardSession("", manager = this).also { oneTimeSession -> oneTimeSession.authWith(bot, getCache(bot))