diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CompressionCodec.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CompressionCodec.kt index bcb713c..adf59ad 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CompressionCodec.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CompressionCodec.kt @@ -42,33 +42,32 @@ class CompressionCodec(val threshold: Int) : MessageToMessageCodec) { - if (input.isReadable) { - val outLength = Type.VAR_INT.readPrimitive(input) - if (outLength == 0) { - out.add(input.retain()) - return - } + if (!input.isReadable || !ctx.channel().isActive) return + val outLength = Type.VAR_INT.readPrimitive(input) + if (outLength == 0) { + out.add(input.retain()) + return + } - if (outLength < threshold) { - throw DecoderException("Badly compressed packet - size of $outLength is below server threshold of $threshold") - } - if (outLength > 2097152) { - throw DecoderException("Badly compressed packet - size of $outLength is larger than protocol maximum of 2097152") - } + if (outLength < threshold) { + throw DecoderException("Badly compressed packet - size of $outLength is below server threshold of $threshold") + } + if (outLength > 2097152) { + throw DecoderException("Badly compressed packet - size of $outLength is larger than protocol maximum of 2097152") + } - inflater.setInput(input.nioBuffer()) - val output = ctx.alloc().buffer(outLength, outLength) - try { - output.writerIndex( - output.writerIndex() + inflater.inflate( - output.nioBuffer(output.writerIndex(), output.writableBytes()) - ) + inflater.setInput(input.nioBuffer()) + val output = ctx.alloc().buffer(outLength, outLength) + try { + output.writerIndex( + output.writerIndex() + inflater.inflate( + output.nioBuffer(output.writerIndex(), output.writableBytes()) ) - out.add(output.retain()) - } finally { - inflater.reset() - output.release() - } + ) + out.add(output.retain()) + } finally { + inflater.reset() + output.release() } } diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CryptoCodec.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CryptoCodec.kt index 5e4de30..df22322 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CryptoCodec.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/codec/CryptoCodec.kt @@ -7,6 +7,7 @@ import javax.crypto.Cipher class CryptoCodec(val cipherDecode: Cipher, var cipherEncode: Cipher) : MessageToMessageCodec() { override fun decode(ctx: ChannelHandlerContext, msg: ByteBuf, out: MutableList) { + if (!ctx.channel().isActive) return val i = msg.readerIndex() val size = msg.readableBytes() msg.writerIndex(i + cipherDecode.update(msg.nioBuffer(), msg.nioBuffer(i, cipherDecode.getOutputSize(size)))) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/ViaCodec.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/ViaCodec.kt index 014ebd0..a12a25a 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/ViaCodec.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/ViaCodec.kt @@ -9,6 +9,7 @@ import us.myles.ViaVersion.exception.CancelEncoderException class ViaCodec(val info: UserConnection) : MessageToMessageCodec() { override fun decode(ctx: ChannelHandlerContext, bytebuf: ByteBuf, out: MutableList) { + if (!ctx.channel().isActive) return if (!info.checkIncomingPacket()) throw CancelDecoderException.generate(null) if (!info.shouldTransformPacket()) { out.add(bytebuf.retain())