|
|
@@ -2,33 +2,25 @@ package net.mamoe.mirai.api.http.adapter.ws.extension
|
|
|
|
|
|
import io.ktor.util.*
|
|
|
import io.ktor.websocket.*
|
|
|
-import net.mamoe.mirai.api.http.adapter.internal.serializer.jsonParseOrNull
|
|
|
-import net.mamoe.mirai.api.http.adapter.ws.dto.WsIncoming
|
|
|
import net.mamoe.mirai.utils.MiraiLogger
|
|
|
|
|
|
-class FrameLogExtension(configuration: Configuration) :
|
|
|
- WebSocketExtension<FrameLogExtension.Configuration> {
|
|
|
+class FrameLogExtension: WebSocketExtension<Unit> {
|
|
|
|
|
|
- private val logger = configuration.logger.value
|
|
|
- private val enable = configuration.enableAccessLog
|
|
|
+ private val logger = MiraiLogger.Factory.create(FrameLogExtension::class, "MAH Access")
|
|
|
|
|
|
override val factory = FrameLogExtension
|
|
|
override val protocols = emptyList<WebSocketExtensionHeader>()
|
|
|
|
|
|
override fun clientNegotiation(negotiatedProtocols: List<WebSocketExtensionHeader>): Boolean {
|
|
|
-
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
override fun serverNegotiation(requestedProtocols: List<WebSocketExtensionHeader>): List<WebSocketExtensionHeader> {
|
|
|
- return emptyList()
|
|
|
+ return listOf(WebSocketExtensionHeader("frame-log", emptyList()))
|
|
|
}
|
|
|
|
|
|
override fun processIncomingFrame(frame: Frame): Frame {
|
|
|
- if (enable) {
|
|
|
- val commandWrapper = String(frame.data).jsonParseOrNull<WsIncoming>() ?: return frame
|
|
|
- logger.debug("[incoming] $commandWrapper")
|
|
|
- }
|
|
|
+ logger.debug("[incoming] ${(frame as Frame.Text).readText()})")
|
|
|
return frame
|
|
|
}
|
|
|
|
|
|
@@ -36,20 +28,15 @@ class FrameLogExtension(configuration: Configuration) :
|
|
|
return frame
|
|
|
}
|
|
|
|
|
|
- class Configuration {
|
|
|
- var logger = lazy { MiraiLogger.Factory.create(FrameLogExtension::class, "MAH Access") }
|
|
|
- var enableAccessLog = false
|
|
|
- }
|
|
|
-
|
|
|
- companion object : WebSocketExtensionFactory<Configuration, FrameLogExtension> {
|
|
|
+ companion object : WebSocketExtensionFactory<Unit, FrameLogExtension> {
|
|
|
override val key = AttributeKey<FrameLogExtension>("FRAME LOG")
|
|
|
|
|
|
override val rsv1: Boolean = false
|
|
|
override val rsv2: Boolean = false
|
|
|
override val rsv3: Boolean = false
|
|
|
|
|
|
- override fun install(config: Configuration.() -> Unit): FrameLogExtension {
|
|
|
- return FrameLogExtension(Configuration().apply(config))
|
|
|
+ override fun install(config: Unit.() -> Unit): FrameLogExtension {
|
|
|
+ return FrameLogExtension()
|
|
|
}
|
|
|
}
|
|
|
|