Skip to content

Commit

Permalink
feat: session loss handle, close #69, close #72
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Aug 13, 2023
1 parent faefaf5 commit 5d47cd2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
32 changes: 25 additions & 7 deletions src/main/kotlin/xyz/cssxsh/mirai/tool/UnidbgFetchQsign.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import net.mamoe.mirai.*
import net.mamoe.mirai.event.*
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.internal.spi.*
import net.mamoe.mirai.utils.*
import org.asynchttpclient.*
Expand Down Expand Up @@ -80,7 +83,7 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
.addQueryParam("key", key)
.execute().get()
val body = Json.decodeFromString(DataWrapper.serializer(), response.responseBody)
check(body.code == 0) { "unidbg-fetch-qsign 服务异常, " + body.message }
body.check(uin = uin)

logger.info("Bot(${uin}) register, ${body.message}")
}
Expand All @@ -96,6 +99,21 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
logger.info("Bot(${uin}) destroy, ${body.message}")
}

private fun DataWrapper.check(uin: Long) {
if (code == 0) return
token.compareAndSet(uin, 0)
val cause = IllegalStateException("unidbg-fetch-qsign 服务异常, 请检查其日志, $message")
launch(CoroutineName(name = "Dropped(${uin})")) {
if ("Uin is not registered." != message) return@launch
@OptIn(MiraiInternalApi::class)
BotOfflineEvent.Dropped(
bot = Bot.getInstance(qq = uin),
cause = cause
).broadcast()
}
throw cause
}

override fun encryptTlv(context: EncryptServiceContext, tlvType: Int, payload: ByteArray): ByteArray? {
if (tlvType != 0x544) return null
val command = context.extraArgs[EncryptServiceContext.KEY_COMMAND_STR]
Expand All @@ -112,7 +130,7 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
.addQueryParam("data", data)
.execute().get()
val body = Json.decodeFromString(DataWrapper.serializer(), response.responseBody)
check(body.code == 0) { "unidbg-fetch-qsign 服务异常, " + body.message }
body.check(uin = uin)

logger.debug("Bot(${uin}) custom_energy ${data}, ${body.message}")

Expand All @@ -128,7 +146,7 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
if (commandName == "StatSvc.register") {
if (token.compareAndSet(0, context.id)) {
val uin = context.id
launch(CoroutineName("RequestToken")) {
launch(CoroutineName(name = "RequestToken")) {
while (isActive) {
val interval = System.getProperty(REQUEST_TOKEN_INTERVAL, "2400000").toLong()
if (interval <= 0L) break
Expand Down Expand Up @@ -167,7 +185,7 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
.addFormParam("buffer", buffer.toUHexString(""))
.execute().get()
val body = Json.decodeFromString(DataWrapper.serializer(), response.responseBody)
check(body.code == 0) { "unidbg-fetch-qsign 服务异常, " + body.message }
body.check(uin = uin)

logger.debug("Bot(${uin}) sign ${cmd}, ${body.message}")

Expand All @@ -179,7 +197,7 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
.addQueryParam("uin", uin.toString())
.execute().get()
val body = Json.decodeFromString(DataWrapper.serializer(), response.responseBody)
check(body.code == 0) { "unidbg-fetch-qsign 服务异常, " + body.message }
body.check(uin = uin)

logger.info("Bot(${uin}) request_token, ${body.message}")

Expand All @@ -194,13 +212,13 @@ public class UnidbgFetchQsign(private val server: String, private val key: Strin
.addQueryParam("buffer", buffer.toUHexString(""))
.execute().get()
val body = Json.decodeFromString(DataWrapper.serializer(), response.responseBody)
check(body.code == 0) { "unidbg-fetch-qsign 服务异常, " + body.message }
body.check(uin = uin)

logger.debug("Bot(${uin}) submit ${cmd}, ${body.message}")
}

private fun callback(uin: Long, request: List<RequestCallback>) {
launch(CoroutineName("SendMessage")) {
launch(CoroutineName(name = "SendMessage")) {
for (callback in request) {
logger.debug("Bot(${uin}) sendMessage ${callback.cmd} ")
val result = try {
Expand Down
20 changes: 17 additions & 3 deletions src/main/kotlin/xyz/cssxsh/mirai/tool/ViVo50.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import net.mamoe.mirai.*
import net.mamoe.mirai.event.*
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.internal.spi.*
import net.mamoe.mirai.internal.utils.*
import net.mamoe.mirai.utils.*
Expand Down Expand Up @@ -151,7 +154,7 @@ public class ViVo50(
}
}

launch(CoroutineName("Session(bot=${context.id})")) {
launch(CoroutineName(name = "Session(bot=${context.id})")) {
cmd.writeText(white.joinToString("\n"))
}

Expand Down Expand Up @@ -302,7 +305,7 @@ public class ViVo50(
"rpc.service.send" -> {
val uin = json["botUin"]!!.jsonPrimitive.long
val cmd = json["command"]!!.jsonPrimitive.content
launch(CoroutineName("Session(bot=${bot})")) {
launch(CoroutineName(name = "Session(bot=${bot})")) {
logger.verbose("Bot(${bot}) sendMessage <- $cmd")

val result = channel.sendMessage(
Expand Down Expand Up @@ -359,7 +362,18 @@ public class ViVo50(
return when (response.statusCode) {
204 -> websocket0
404 -> null
else -> throw IllegalStateException("Session(bot=${bot}) ${response.responseBody}")
else -> {
sessions.remove(bot, this)
val cause = IllegalStateException("Session(bot=${bot}) ${response.responseBody}")
launch(CoroutineName(name = "Dropped(bot=${bot})")) {
@OptIn(MiraiInternalApi::class)
BotOfflineEvent.Dropped(
bot = Bot.getInstance(qq = bot),
cause = cause
).broadcast()
}
throw cause
}
}
}

Expand Down

0 comments on commit 5d47cd2

Please sign in to comment.