From 3d5a1249dac79c454c4d6db16d906bacbd328bb0 Mon Sep 17 00:00:00 2001 From: bixilon Date: Wed, 3 Jun 2020 14:28:32 +0200 Subject: [PATCH] handling of broken packets --- .../minosoft/protocol/network/Network.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java index 6b9de44e9..5f4fae028 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java @@ -136,29 +136,32 @@ public class Network { while (binQueueIn.size() > 0) { // read data - byte[] raw = binQueueIn.get(0); + byte[] decrypted = binQueueIn.get(0); InPacketBuffer inPacketBuffer; if (encryptionEnabled) { // decrypt - byte[] decrypted = cipherDecrypt.update(raw); - inPacketBuffer = new InPacketBuffer(decrypted); - } else { - inPacketBuffer = new InPacketBuffer(raw); - } - Packets.Clientbound p = connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand()); - Class clazz = Protocol.getPacketByPacket(p); - - if (clazz == null) { - Log.warn(String.format("[IN] Unknown packet with command 0x%x (%s)", inPacketBuffer.getCommand(), ((p != null) ? p.name() : "UNKNOWN"))); - binQueueIn.remove(0); - continue; + decrypted = cipherDecrypt.update(decrypted); } try { - ClientboundPacket packet = clazz.getConstructor().newInstance(); - packet.read(inPacketBuffer, connection.getVersion()); - connection.handle(packet); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - // safety first, but will not occur + inPacketBuffer = new InPacketBuffer(decrypted); + Packets.Clientbound p = connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand()); + Class clazz = Protocol.getPacketByPacket(p); + + if (clazz == null) { + Log.warn(String.format("[IN] Unknown packet with command 0x%x (%s)", inPacketBuffer.getCommand(), ((p != null) ? p.name() : "UNKNOWN"))); + binQueueIn.remove(0); + continue; + } + try { + ClientboundPacket packet = clazz.getConstructor().newInstance(); + packet.read(inPacketBuffer, connection.getVersion()); + connection.handle(packet); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + // safety first, but will not occur + e.printStackTrace(); + } + } catch (ArrayIndexOutOfBoundsException e) { + Log.protocol("Received broken packet!"); e.printStackTrace(); }