diff --git a/common/src/main/kotlin/entity/DiscordGuild.kt b/common/src/main/kotlin/entity/DiscordGuild.kt index b6fb5b3c27f8..697d0a6bdb9c 100644 --- a/common/src/main/kotlin/entity/DiscordGuild.kt +++ b/common/src/main/kotlin/entity/DiscordGuild.kt @@ -448,7 +448,10 @@ public data class DiscordVoiceServerUpdateData( val token: String, @SerialName("guild_id") val guildId: Snowflake, val endpoint: String?, -) +) { + override fun toString(): String = + "DiscordVoiceServerUpdateData(token=hunter2, guildId=$guildId, endpoint=$endpoint)" +} @Serializable public data class DiscordWebhooksUpdateData( diff --git a/core/src/main/kotlin/event/guild/VoiceServerUpdateEvent.kt b/core/src/main/kotlin/event/guild/VoiceServerUpdateEvent.kt index 62d90f402dfd..d7a0df2c0f3c 100644 --- a/core/src/main/kotlin/event/guild/VoiceServerUpdateEvent.kt +++ b/core/src/main/kotlin/event/guild/VoiceServerUpdateEvent.kt @@ -28,7 +28,6 @@ public class VoiceServerUpdateEvent( override fun withStrategy(strategy: EntitySupplyStrategy<*>): VoiceServerUpdateEvent = VoiceServerUpdateEvent(token, guildId, endpoint, kord, shard, customContext, strategy.supply(kord)) - override fun toString(): String { - return "VoiceServerUpdateEvent(token='$token', guildId=$guildId, endpoint='$endpoint', kord=$kord, shard=$shard, supplier=$supplier)" - } + override fun toString(): String = "VoiceServerUpdateEvent(token=hunter2, guildId=$guildId, endpoint=$endpoint, " + + "kord=$kord, shard=$shard, customContext=$customContext, supplier=$supplier)" } diff --git a/gateway/src/main/kotlin/DefaultGateway.kt b/gateway/src/main/kotlin/DefaultGateway.kt index 813618c73892..262711a09bb4 100644 --- a/gateway/src/main/kotlin/DefaultGateway.kt +++ b/gateway/src/main/kotlin/DefaultGateway.kt @@ -22,7 +22,7 @@ import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.flow.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import kotlinx.serialization.json.Json +import kotlinx.serialization.json.* import mu.KotlinLogging import java.io.ByteArrayOutputStream import java.util.zip.Inflater @@ -254,8 +254,37 @@ public class DefaultGateway(private val data: DefaultGatewayData) : Gateway { } try { - defaultGatewayLogger.trace { "Gateway <<< $json" } - val event = GatewayJson.decodeFromString(Event.DeserializationStrategy, json) ?: return + val event = GatewayJson.decodeFromString(Event.DeserializationStrategy, json) + + defaultGatewayLogger.trace { + val credentialFreeJson = when (event) { + + is VoiceServerUpdate -> { + when (val payload = GatewayJson.parseToJsonElement(json)) { + is JsonObject -> { + val payloadCopy = buildJsonObject { + for ((k, v) in payload) put(k, v) + + val data = payload["d"] + if (data is JsonObject) putJsonObject("d") { + for ((k, v) in data) put(k, v) + put("token", "hunter2") + } + } + payloadCopy.toString() + } + else -> json + } + } + + else -> json + } + + "Gateway <<< $credentialFreeJson" + } + + if (event == null) return + data.eventFlow.emit(event) } catch (exception: Exception) { defaultGatewayLogger.error(exception) diff --git a/voice/src/main/kotlin/gateway/Command.kt b/voice/src/main/kotlin/gateway/Command.kt index 163678a7c58f..9d600f1e3600 100644 --- a/voice/src/main/kotlin/gateway/Command.kt +++ b/voice/src/main/kotlin/gateway/Command.kt @@ -59,7 +59,10 @@ public data class Identify( @SerialName("session_id") val sessionId: String, val token: String -) : Command() +) : Command() { + override fun toString(): String = + "Identify(serverId=$serverId, userId=$userId, sessionId=$sessionId, token=hunter2)" +} @Serializable public data class Heartbeat(val nonce: Long) : Command() @@ -92,4 +95,6 @@ public data class Resume( val serverId: Snowflake, val sessionId: String, val token: String -) : Command() +) : Command() { + override fun toString(): String = "Resume(serverId=$serverId, sessionId=$sessionId, token=hunter2)" +} diff --git a/voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt b/voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt index 42293ac10f3d..da08ee43a327 100644 --- a/voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt +++ b/voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt @@ -6,7 +6,6 @@ import dev.kord.gateway.retry.Retry import dev.kord.voice.gateway.handler.HandshakeHandler import dev.kord.voice.gateway.handler.HeartbeatHandler import io.ktor.client.* -import io.ktor.client.plugins.* import io.ktor.client.plugins.websocket.* import io.ktor.client.request.* import io.ktor.util.logging.* @@ -60,11 +59,6 @@ public class DefaultVoiceGateway( private val _ping = MutableStateFlow(null) override val ping: StateFlow get() = _ping - private val jsonParser = Json { - ignoreUnknownKeys = true - isLenient = true - } - private val stateMutex = Mutex() private val handshakeHandler: HandshakeHandler @@ -144,7 +138,7 @@ public class DefaultVoiceGateway( val json = String(frame.data, Charsets.UTF_8) try { - val event = jsonParser.decodeFromString(VoiceEvent.DeserializationStrategy, json) + val event = VoiceGatewayJson.decodeFromString(VoiceEvent.DeserializationStrategy, json) if (event is SessionDescription) defaultVoiceGatewayLogger.trace { "Voice Gateway <<< SESSION_DESCRIPTION" } @@ -186,20 +180,22 @@ public class DefaultVoiceGateway( } private suspend fun sendUnsafe(command: Command) { - val json = Json.encodeToString(Command.SerializationStrategy, command) - if (command is Identify) { - defaultVoiceGatewayLogger.trace { - val copy = command.copy(token = "token") - "Voice Gateway >>> ${Json.encodeToString(Command.SerializationStrategy, copy)}" - } - } else if (command is SelectProtocol) { - defaultVoiceGatewayLogger.trace { - val copy = command.copy(data = command.data.copy(address = "ip")) - "Voice Gateway >>> ${Json.encodeToString(Command.SerializationStrategy, copy)}" + val json = VoiceGatewayJson.encodeToString(Command.SerializationStrategy, command) + + defaultVoiceGatewayLogger.trace { + val credentialFreeCopy = when (command) { + is Identify -> command.copy(token = "hunter2") + is Resume -> command.copy(token = "hunter2") + is SelectProtocol -> command.copy(data = command.data.copy(address = "ip")) + else -> null } - } else { - defaultVoiceGatewayLogger.trace { "Voice Gateway >>> $json" } + val credentialFreeJson = credentialFreeCopy // re-encode copy + ?.let { VoiceGatewayJson.encodeToString(Command.SerializationStrategy, it) } + ?: json + + "Voice Gateway >>> $credentialFreeJson" } + socket.send(Frame.Text(json)) } @@ -236,6 +232,14 @@ public class DefaultVoiceGateway( } } } + + + private companion object { + private val VoiceGatewayJson = Json { + ignoreUnknownKeys = true + isLenient = true + } + } } internal val VoiceGatewayCloseCode.retry diff --git a/voice/src/main/kotlin/gateway/VoiceEvent.kt b/voice/src/main/kotlin/gateway/VoiceEvent.kt index f40c93c16bb5..035bef104090 100644 --- a/voice/src/main/kotlin/gateway/VoiceEvent.kt +++ b/voice/src/main/kotlin/gateway/VoiceEvent.kt @@ -95,7 +95,9 @@ public data class SessionDescription( val mode: EncryptionMode, @SerialName("secret_key") val secretKey: List -) : VoiceEvent() +) : VoiceEvent() { + override fun toString(): String = "SessionDescription(mode=$mode, secretKey=hunter2)" +} @Serializable public data class Speaking( diff --git a/voice/src/main/kotlin/gateway/VoiceGatewayConfiguration.kt b/voice/src/main/kotlin/gateway/VoiceGatewayConfiguration.kt index 9633dbd3287f..80ac741767bb 100644 --- a/voice/src/main/kotlin/gateway/VoiceGatewayConfiguration.kt +++ b/voice/src/main/kotlin/gateway/VoiceGatewayConfiguration.kt @@ -6,4 +6,6 @@ import dev.kord.common.annotation.KordVoice public data class VoiceGatewayConfiguration( val token: String, val endpoint: String -) \ No newline at end of file +) { + override fun toString(): String = "VoiceGatewayConfiguration(token=hunter2, endpoint=$endpoint)" +}