From 24e57d235f223c777b71816fe621143c25ef4592 Mon Sep 17 00:00:00 2001 From: MrNavaStar Date: Sun, 18 Aug 2024 01:17:18 -0600 Subject: [PATCH] better inform users when they forget to register packets --- .../protoweaver/api/protocol/Protocol.java | 5 +++-- .../core/netty/ProtoPacketHandler.java | 20 +++++++++++++------ .../protoweaver/core/util/Furious.java | 20 ++++++++++++++----- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/me/mrnavastar/protoweaver/api/protocol/Protocol.java b/common/src/main/java/me/mrnavastar/protoweaver/api/protocol/Protocol.java index 057aecd..68f5908 100644 --- a/common/src/main/java/me/mrnavastar/protoweaver/api/protocol/Protocol.java +++ b/common/src/main/java/me/mrnavastar/protoweaver/api/protocol/Protocol.java @@ -7,6 +7,7 @@ import me.mrnavastar.protoweaver.api.auth.ServerAuthHandler; import me.mrnavastar.protoweaver.api.netty.ProtoConnection; import me.mrnavastar.protoweaver.core.util.Furious; +import org.apache.fury.exception.InsecureException; import java.lang.reflect.Modifier; import java.util.Objects; @@ -81,11 +82,11 @@ public ClientAuthHandler newClientAuthHandler() { return clientAuthHandler.getDeclaredConstructor().newInstance(); } - public byte[] serialize(@NonNull Object packet) { + public byte[] serialize(@NonNull Object packet) throws InsecureException { return Furious.serialize(packet); } - public Object deserialize(byte @NonNull [] packet) { + public Object deserialize(byte @NonNull [] packet) throws InsecureException { return Furious.deserialize(packet); } diff --git a/common/src/main/java/me/mrnavastar/protoweaver/core/netty/ProtoPacketHandler.java b/common/src/main/java/me/mrnavastar/protoweaver/core/netty/ProtoPacketHandler.java index 82ec6f3..790286f 100644 --- a/common/src/main/java/me/mrnavastar/protoweaver/core/netty/ProtoPacketHandler.java +++ b/common/src/main/java/me/mrnavastar/protoweaver/core/netty/ProtoPacketHandler.java @@ -12,6 +12,7 @@ import me.mrnavastar.protoweaver.core.util.DrunkenBishop; import me.mrnavastar.protoweaver.core.util.ProtoConstants; import me.mrnavastar.protoweaver.core.util.ProtoLogger; +import org.apache.fury.exception.InsecureException; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -54,15 +55,18 @@ public void channelInactive(ChannelHandlerContext ctx) { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List list) { if (byteBuf.readableBytes() == 0) return; - - byte[] bytes = new byte[byteBuf.readInt()]; - byteBuf.readBytes(bytes); - Object packet = connection.getProtocol().deserialize(bytes); + Object packet = null; try { + byte[] bytes = new byte[byteBuf.readInt()]; + byteBuf.readBytes(bytes); + packet = connection.getProtocol().deserialize(bytes); handler.handlePacket(connection, packet); + + } catch (InsecureException e) { + ProtoLogger.warn("Protocol: " + connection.getProtocol() + " ignoring an " + e.getMessage()); } catch (Exception e) { - ProtoLogger.error("Protocol: " + connection.getProtocol() + " threw an error on when trying to handle: " + packet.getClass() + "!"); + if (packet != null) ProtoLogger.error("Protocol: " + connection.getProtocol() + " threw an error when trying to handle: " + packet.getClass() + "!"); e.printStackTrace(); } } @@ -79,8 +83,12 @@ public Sender send(Object packet) { Sender sender = new Sender(connection, ctx.writeAndFlush(buf), true); buf = Unpooled.buffer(); return sender; + + } catch (InsecureException e) { + ProtoLogger.error("Protocol: " + connection.getProtocol() + " tried to send an " + e.getMessage()); + return new Sender(connection, ctx.newSucceededFuture(), false); } catch (Exception e) { - ProtoLogger.error("Failed to encode object: " + packet.getClass().getName()); + ProtoLogger.error("Protocol: " + connection.getProtocol() + " threw an error when trying to send: " + packet.getClass() + "!"); e.printStackTrace(); return new Sender(connection, ctx.newSucceededFuture(), false); } diff --git a/common/src/main/java/me/mrnavastar/protoweaver/core/util/Furious.java b/common/src/main/java/me/mrnavastar/protoweaver/core/util/Furious.java index 545c4d6..ad8ecc0 100644 --- a/common/src/main/java/me/mrnavastar/protoweaver/core/util/Furious.java +++ b/common/src/main/java/me/mrnavastar/protoweaver/core/util/Furious.java @@ -3,6 +3,7 @@ import me.mrnavastar.r.R; import org.apache.fury.Fury; import org.apache.fury.ThreadSafeFury; +import org.apache.fury.exception.InsecureException; import org.apache.fury.logging.LoggerFactory; import java.util.ArrayList; @@ -31,11 +32,20 @@ public static void register(Class type) { recursiveRegister(type, new ArrayList<>()); } - public static byte[] serialize(Object object) { - return FURY.serialize(object); + public static byte[] serialize(Object object) throws InsecureException { + try { + return FURY.serialize(object); + } catch (InsecureException e) { + throw new InsecureException("unregistered packet: " + object.getClass().getName()); + } } - public static Object deserialize(byte[] bytes) { - return FURY.deserialize(bytes); + public static Object deserialize(byte[] bytes) throws InsecureException { + try { + return FURY.deserialize(bytes); + } catch (InsecureException e) { + String packet = e.getMessage().split(" is not registered")[0].replace("class ", ""); + throw new InsecureException("unregistered packet: " + packet); + } } -} +} \ No newline at end of file