fix(network): log uncompressed packets (#84)

Insert the compression codec in front of the protocol logger so the
logger sees raw packets: decompressed on the way in, not yet compressed
on the way out.

Closes #61
This commit is contained in:
Fiereu authored and GitHub committed 2026-08-04 07:51:50 +02:00
1 parent 22c922ffe3
commit e24bc5518d
4 files changed
+95 -7

No files matched your search

@@ -12,6 +12,7 @@ import de.fiereu.network.handshake.ClientSessionHandshakeHandler
import de.fiereu.network.handshake.ServerSessionHandshakeHandler
import de.fiereu.network.internal.MutableSessionContext
import de.fiereu.network.internal.SESSION_KEY
import io.netty.channel.ChannelHandler
import io.netty.channel.ChannelPipeline
import io.netty.handler.logging.LogLevel
import io.netty.handler.logging.LoggingHandler
@@ -69,11 +70,22 @@ fun installPipeline(
pipeline.addLast(PipelineNames.CHECKSUM_ENCODER, ChecksumFrameEncoder(NoOpChecksum))
pipeline.addLast(PipelineNames.CIPHER_DECODER, CipherDecoder(NoOpSessionCipher))
pipeline.addLast(PipelineNames.CIPHER_ENCODER, CipherEncoder(NoOpSessionCipher))
// TODO: the compression codec is inserted between this logger and the protocol handler, so this
// logs compressed frames. Reposition it next to the protocol handler to log raw packets, before
// compression on the way out and after decompression on the way in.
if (options.frameLogging) {
pipeline.addLast(PipelineNames.PROTOCOL_LOGGER, LoggingHandler(LogLevel.TRACE))
}
pipeline.addLast(PipelineNames.PROTOCOL_HANDLER, handshakeHandler)
}
/**
* Adds a handler in front of the protocol logger, so the logger only sees uncompressed packets:
* decompressed on the way in, not yet compressed on the way out.
*/
internal fun ChannelPipeline.addBeforeProtocolLogger(name: String, handler: ChannelHandler) {
val anchor =
if (get(PipelineNames.PROTOCOL_LOGGER) != null) {
PipelineNames.PROTOCOL_LOGGER
} else {
PipelineNames.PROTOCOL_HANDLER
}
addBefore(anchor, name, handler)
}
@@ -8,6 +8,7 @@ import de.fiereu.network.ProtocolHandler
import de.fiereu.network.SessionPhase
import de.fiereu.network.Side
import de.fiereu.network.TypedProtocolHandler
import de.fiereu.network.addBeforeProtocolLogger
import de.fiereu.network.cipher.AesCtrSessionCipher
import de.fiereu.network.handlers.ChecksumFrameDecoder
import de.fiereu.network.handlers.ChecksumFrameEncoder
@@ -57,8 +58,7 @@ internal class ClientSessionHandshakeHandler(
(pipeline.get(PipelineNames.CIPHER_DECODER) as CipherDecoder).cipher = crypto.cipher
(pipeline.get(PipelineNames.CIPHER_ENCODER) as CipherEncoder).cipher = crypto.cipher
if (applicationProtocol.compressed) {
pipeline.addBefore(
PipelineNames.PROTOCOL_HANDLER,
pipeline.addBeforeProtocolLogger(
PipelineNames.COMPRESSION_DECODER,
CompressionDecoder(),
)
@@ -8,6 +8,7 @@ import de.fiereu.network.SessionPhase
import de.fiereu.network.Side
import de.fiereu.network.StaleClientHelloException
import de.fiereu.network.TypedProtocolHandler
import de.fiereu.network.addBeforeProtocolLogger
import de.fiereu.network.cipher.AesCtrSessionCipher
import de.fiereu.network.handlers.ChecksumFrameDecoder
import de.fiereu.network.handlers.ChecksumFrameEncoder
@@ -66,8 +67,7 @@ internal class ServerSessionHandshakeHandler(
(pipeline.get(PipelineNames.CIPHER_DECODER) as CipherDecoder).cipher = crypto.cipher
(pipeline.get(PipelineNames.CIPHER_ENCODER) as CipherEncoder).cipher = crypto.cipher
if (applicationProtocol.compressed) {
pipeline.addBefore(
PipelineNames.PROTOCOL_HANDLER,
pipeline.addBeforeProtocolLogger(
PipelineNames.COMPRESSION_ENCODER,
CompressionEncoder(options.compressionThreshold),
)
@@ -3,6 +3,7 @@ package de.fiereu.network.handshake
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.U16LE
import de.fiereu.network.PipelineNames
import de.fiereu.network.PipelineOptions
import de.fiereu.network.Protocol
import de.fiereu.network.ProtocolHandler
@@ -14,6 +15,7 @@ import de.fiereu.network.bidi
import de.fiereu.network.installPipeline
import de.fiereu.network.internal.SESSION_KEY
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.ints.shouldBeLessThan
import io.kotest.matchers.shouldBe
import io.netty.buffer.ByteBuf
import io.netty.channel.Channel
@@ -34,6 +36,14 @@ private object EchoProtocol : Protocol() {
}
}
private object CompressedEchoProtocol : Protocol() {
override val compressed = true
init {
bidi<Echo>(0x55u, EchoCodec)
}
}
private class CollectingHandler(side: Side) :
TypedProtocolHandler<EchoProtocol>(EchoProtocol, side) {
val received = mutableListOf<Echo>()
@@ -43,6 +53,28 @@ private class CollectingHandler(side: Side) :
}
}
private fun embedded(
side: Side,
identity: SessionIdentity,
protocol: Protocol,
handler: ProtocolHandler,
options: PipelineOptions,
) =
EmbeddedChannel(
object : ChannelInitializer<Channel>() {
override fun initChannel(ch: Channel) {
installPipeline(
pipeline = ch.pipeline(),
side = side,
identity = identity,
applicationProtocol = protocol,
applicationHandlerFactory = { handler },
options = options,
)
}
},
)
private fun drain(from: EmbeddedChannel, into: EmbeddedChannel) {
while (true) {
val obj = from.readOutbound<ByteBuf>() ?: break
@@ -114,4 +146,48 @@ class EndToEndHandshakeTest :
drain(serverChannel, clientChannel)
clientApp.received shouldBe listOf(Echo(0xBABE))
}
test("compression codecs sit in front of the protocol logger") {
val rootKeyPair = EcKeys.generateEphemeralKeyPair()
val rootPrivate = rootKeyPair.private as ECPrivateKey
val rootPublic = rootKeyPair.public as ECPublicKey
val serverApp = CollectingHandler(Side.SERVER)
val clientApp = CollectingHandler(Side.CLIENT)
val options = PipelineOptions(checksumSize = 8, frameLogging = true)
val serverChannel =
embedded(
Side.SERVER,
SessionIdentity.ServerRoot(rootPrivate),
CompressedEchoProtocol,
serverApp,
options,
)
val clientChannel =
embedded(
Side.CLIENT,
SessionIdentity.ClientTrust(rootPublic),
CompressedEchoProtocol,
clientApp,
options,
)
drain(clientChannel, serverChannel)
drain(serverChannel, clientChannel)
drain(clientChannel, serverChannel)
val serverNames = serverChannel.pipeline().names()
serverNames.indexOf(PipelineNames.COMPRESSION_ENCODER) shouldBeLessThan
serverNames.indexOf(PipelineNames.PROTOCOL_LOGGER)
val clientNames = clientChannel.pipeline().names()
clientNames.indexOf(PipelineNames.COMPRESSION_DECODER) shouldBeLessThan
clientNames.indexOf(PipelineNames.PROTOCOL_LOGGER)
serverChannel.attr(SESSION_KEY).get().send(Echo(0xBABE))
drain(serverChannel, clientChannel)
clientApp.received shouldBe listOf(Echo(0xBABE))
}
})