Skip to content

Commit

Permalink
Support about, botList commands for unauthorized reverse websocke…
Browse files Browse the repository at this point in the history
…t connects (#610)
  • Loading branch information
ryoii committed Dec 12, 2022
1 parent eaea0ea commit 221a5c6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface SessionManager {
*/
val verifyKey: String

fun getEmptySession(): Session

fun createOneTimeSession(bot: Bot): Session

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ?: ""))
Expand Down Expand Up @@ -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<ReverseAuthDTO>()

if (dto == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class DefaultSessionManager(override val verifyKey: String, val context: MahCont
private val sessionMap: MutableMap<String, Session> = mutableMapOf()
private val cacheMap: MutableMap<Long, Persistence> = 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))
Expand Down

0 comments on commit 221a5c6

Please sign in to comment.