Skip to content

Commit

Permalink
better inform users when they forget to register packets
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar committed Aug 18, 2024
1 parent 4f3b010 commit 24e57d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,15 +55,18 @@ public void channelInactive(ChannelHandlerContext ctx) {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> 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();
}
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 24e57d2

Please sign in to comment.