From 036864308fae7754bc9407c63d482e2fe4413959 Mon Sep 17 00:00:00 2001 From: Exterminate Date: Mon, 2 Nov 2026 21:14:02 +1100 Subject: [PATCH] Deprecate raknet and bump nethernet dependency --- build.gradle | 3 +- .../netty/raknet/AesEncryptionCodec.java | 100 ------------------ .../viabedrock/netty/raknet/MessageCodec.java | 65 ------------ 3 files changed, 1 insertion(+), 167 deletions(-) delete mode 100644 src/main/java/net/raphimc/viabedrock/netty/raknet/AesEncryptionCodec.java delete mode 100644 src/main/java/net/raphimc/viabedrock/netty/raknet/MessageCodec.java diff --git a/build.gradle b/build.gradle index 8a16054d7..11177e658 100644 --- a/build.gradle +++ b/build.gradle @@ -63,8 +63,7 @@ dependencies { compileOnly "com.google.guava:guava:33.7.2-jre" compileOnly "io.netty:netty-handler:4.2.19.Final" compileOnly "com.google.code.gson:gson:2.15.0" - compileOnly "org.cloudburstmc.netty:netty-transport-raknet:2.0.0.CR4-20260921.144528-1" - compileOnly "org.cloudburstmc.netty:netty-transport-nethernet:2.0.0.CR4-20260921.144528-1" + compileOnly "org.cloudburstmc.netty:netty-transport-nethernet:2.0.0.CR6-20261030.131938-23" api("io.netty:netty-codec-http:4.2.19.Final") { transitive = false diff --git a/src/main/java/net/raphimc/viabedrock/netty/raknet/AesEncryptionCodec.java b/src/main/java/net/raphimc/viabedrock/netty/raknet/AesEncryptionCodec.java deleted file mode 100644 index 2b486d548..000000000 --- a/src/main/java/net/raphimc/viabedrock/netty/raknet/AesEncryptionCodec.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock - * Copyright (C) 2023-2026 RK_01/RaphiMC and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package net.raphimc.viabedrock.netty.raknet; - -import com.google.common.primitives.Longs; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageCodec; -import io.netty.handler.codec.CorruptedFrameException; - -import javax.crypto.Cipher; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.crypto.spec.IvParameterSpec; -import java.nio.ByteBuffer; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.util.List; - -public class AesEncryptionCodec extends ByteToMessageCodec { - - private final SecretKey secretKey; - private final Cipher inCipher; - private final Cipher outCipher; - private final MessageDigest sha256; - private long sentPacketCounter; - private long receivedPacketCounter; - - public AesEncryptionCodec(final SecretKey secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { - final byte[] iv = new byte[16]; - System.arraycopy(secretKey.getEncoded(), 0, iv, 0, 12); - iv[15] = 2; - - this.secretKey = secretKey; - this.inCipher = Cipher.getInstance("AES/CTR/NoPadding"); - this.inCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); - this.outCipher = Cipher.getInstance("AES/CTR/NoPadding"); - this.outCipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); - this.sha256 = MessageDigest.getInstance("SHA-256"); - } - - @Override - protected void encode(final ChannelHandlerContext ctx, final ByteBuf in, final ByteBuf out) throws Exception { - final byte[] hash = this.generateHash(in, this.sentPacketCounter++); - - final ByteBuffer inBuffer = in.nioBuffer(); - out.ensureWritable(in.readableBytes() + 8); - - this.outCipher.update(inBuffer, out.nioBuffer(0, in.readableBytes())); - this.outCipher.update(ByteBuffer.wrap(hash), out.nioBuffer(in.readableBytes(), 8)); - out.writerIndex(in.readableBytes() + 8); - } - - @Override - protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List out) throws Exception { - final ByteBuffer inBuffer = in.nioBuffer(); - final ByteBuffer outBuffer = inBuffer.duplicate(); - - this.inCipher.update(inBuffer, outBuffer); - final ByteBuf output = in.readRetainedSlice(in.readableBytes() - 8); - - final byte[] receivedHash = new byte[8]; - in.readBytes(receivedHash); - final byte[] expectedHash = this.generateHash(output, this.receivedPacketCounter++); - if (!MessageDigest.isEqual(receivedHash, expectedHash)) { - throw new CorruptedFrameException("Invalid encrypted packet"); - } - - out.add(output); - } - - private byte[] generateHash(final ByteBuf buf, final long packetCounter) { - this.sha256.update(Longs.toByteArray(Long.reverseBytes(packetCounter))); - this.sha256.update(ByteBufUtil.getBytes(buf)); - this.sha256.update(this.secretKey.getEncoded()); - final byte[] hash = this.sha256.digest(); - this.sha256.reset(); - return Arrays.copyOf(hash, 8); - } - -} diff --git a/src/main/java/net/raphimc/viabedrock/netty/raknet/MessageCodec.java b/src/main/java/net/raphimc/viabedrock/netty/raknet/MessageCodec.java deleted file mode 100644 index 28dfbf891..000000000 --- a/src/main/java/net/raphimc/viabedrock/netty/raknet/MessageCodec.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock - * Copyright (C) 2023-2026 RK_01/RaphiMC and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package net.raphimc.viabedrock.netty.raknet; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.CompositeByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToMessageCodec; -import net.raphimc.viabedrock.ViaBedrock; -import org.cloudburstmc.netty.channel.raknet.RakReliability; -import org.cloudburstmc.netty.channel.raknet.packet.RakMessage; - -import java.util.List; - -public class MessageCodec extends MessageToMessageCodec { - - public static final String NAME = "viabedrock-raknet-message-codec"; - - private static final int MINECRAFT_MESSAGE_ID = 0xFE; - - @Override - protected void encode(final ChannelHandlerContext ctx, final ByteBuf in, final List out) { - final CompositeByteBuf buf = ctx.alloc().compositeBuffer(2); - try { - buf.addComponent(true, ctx.alloc().ioBuffer(1).writeByte(MINECRAFT_MESSAGE_ID)); - buf.addComponent(true, in.retainedSlice()); - out.add(new RakMessage(buf.retain())); - } finally { - buf.release(); - } - } - - @Override - protected void decode(final ChannelHandlerContext ctx, final RakMessage in, final List out) { - if (in.channel() != 0 && in.reliability() != RakReliability.RELIABLE_ORDERED) { - return; - } - final ByteBuf buf = in.content(); - if (!buf.isReadable()) { - return; - } - final int messageId = buf.readUnsignedByte(); - if (messageId != MINECRAFT_MESSAGE_ID) { // Mojang client seems to ignore invalid messages - ViaBedrock.getPlatform().getLogger().warning("Received invalid RakNet message id: " + messageId); - return; - } - out.add(buf.readRetainedSlice(buf.readableBytes())); - } - -}