Skip to content

Commit

Permalink
Don't log voice tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Mar 9, 2023
1 parent fd11373 commit 064ec37
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 30 deletions.
5 changes: 4 additions & 1 deletion common/src/main/kotlin/entity/DiscordGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/kotlin/event/guild/VoiceServerUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
35 changes: 32 additions & 3 deletions gateway/src/main/kotlin/DefaultGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions voice/src/main/kotlin/gateway/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)"
}
42 changes: 23 additions & 19 deletions voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -60,11 +59,6 @@ public class DefaultVoiceGateway(
private val _ping = MutableStateFlow<Duration?>(null)
override val ping: StateFlow<Duration?> get() = _ping

private val jsonParser = Json {
ignoreUnknownKeys = true
isLenient = true
}

private val stateMutex = Mutex()

private val handshakeHandler: HandshakeHandler
Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -236,6 +232,14 @@ public class DefaultVoiceGateway(
}
}
}


private companion object {
private val VoiceGatewayJson = Json {
ignoreUnknownKeys = true
isLenient = true
}
}
}

internal val VoiceGatewayCloseCode.retry
Expand Down
4 changes: 3 additions & 1 deletion voice/src/main/kotlin/gateway/VoiceEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public data class SessionDescription(
val mode: EncryptionMode,
@SerialName("secret_key")
val secretKey: List<UByte>
) : VoiceEvent()
) : VoiceEvent() {
override fun toString(): String = "SessionDescription(mode=$mode, secretKey=hunter2)"
}

@Serializable
public data class Speaking(
Expand Down
4 changes: 3 additions & 1 deletion voice/src/main/kotlin/gateway/VoiceGatewayConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import dev.kord.common.annotation.KordVoice
public data class VoiceGatewayConfiguration(
val token: String,
val endpoint: String
)
) {
override fun toString(): String = "VoiceGatewayConfiguration(token=hunter2, endpoint=$endpoint)"
}

0 comments on commit 064ec37

Please sign in to comment.