Skip to content

Commit

Permalink
fix net code handling packets on wrong side (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease authored Apr 8, 2023
1 parent bd1c3ce commit 11c063f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/main/java/buildcraft/BuildCraftTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package buildcraft;

import static buildcraft.BuildCraftCore.GTNH;
import static buildcraft.core.lib.network.ChannelHandler.CLIENT_ONLY;
import static buildcraft.core.lib.network.ChannelHandler.SERVER_ONLY;

import java.io.PrintWriter;
import java.util.LinkedList;
Expand Down Expand Up @@ -514,11 +516,11 @@ public void init(FMLInitializationEvent evt) {
transportChannelHandler = new ChannelHandler();
MinecraftForge.EVENT_BUS.register(this);

transportChannelHandler.registerPacketType(PacketFluidUpdate.class);
transportChannelHandler.registerPacketType(PacketPipeTransportItemStack.class);
transportChannelHandler.registerPacketType(PacketPipeTransportItemStackRequest.class);
transportChannelHandler.registerPacketType(PacketPipeTransportTraveler.class);
transportChannelHandler.registerPacketType(PacketPowerUpdate.class);
transportChannelHandler.registerPacketType(PacketFluidUpdate.class, CLIENT_ONLY);
transportChannelHandler.registerPacketType(PacketPipeTransportItemStack.class, CLIENT_ONLY);
transportChannelHandler.registerPacketType(PacketPipeTransportItemStackRequest.class, SERVER_ONLY);
transportChannelHandler.registerPacketType(PacketPipeTransportTraveler.class, CLIENT_ONLY);
transportChannelHandler.registerPacketType(PacketPowerUpdate.class, CLIENT_ONLY);

channels = NetworkRegistry.INSTANCE.newChannel(
DefaultProps.NET_CHANNEL_NAME + "-TRANSPORT",
Expand Down
60 changes: 52 additions & 8 deletions src/main/java/buildcraft/core/lib/network/ChannelHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
import java.lang.ref.WeakReference;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import buildcraft.api.core.BCLog;
import buildcraft.core.lib.network.command.PacketCommand;
import buildcraft.core.proxy.CoreProxy;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import cpw.mods.fml.relauncher.Side;
import gnu.trove.map.TByteIntMap;
import gnu.trove.map.hash.TByteIntHashMap;
import gnu.trove.map.hash.TByteObjectHashMap;
import gnu.trove.map.hash.TObjectByteHashMap;
import io.netty.buffer.ByteBuf;
Expand All @@ -29,26 +40,42 @@
@io.netty.channel.ChannelHandler.Sharable
public final class ChannelHandler extends MessageToMessageCodec<FMLProxyPacket, Packet> {

public static final int CLIENT_ONLY = 1;
public static final int SERVER_ONLY = 2;
public static final int BOTH_SIDES = SERVER_ONLY | CLIENT_ONLY;

public static final Marker SUSPICIOUS_PACKETS = MarkerManager.getMarker("SuspiciousPackets");
public static final AttributeKey<ThreadLocal<WeakReference<FMLProxyPacket>>> INBOUNDPACKETTRACKER = new AttributeKey<ThreadLocal<WeakReference<FMLProxyPacket>>>(
"bc:inboundpacket");
private TByteObjectHashMap<Class<? extends Packet>> discriminators = new TByteObjectHashMap<Class<? extends Packet>>();
private TObjectByteHashMap<Class<? extends Packet>> types = new TObjectByteHashMap<Class<? extends Packet>>();
private TByteIntMap sides = new TByteIntHashMap();
private int maxDiscriminator;

public ChannelHandler() {
// Packets common to buildcraft.core.network
addDiscriminator(0, PacketTileUpdate.class);
addDiscriminator(1, PacketTileState.class);
addDiscriminator(0, PacketTileUpdate.class, CLIENT_ONLY);
addDiscriminator(1, PacketTileState.class, CLIENT_ONLY);
addDiscriminator(2, PacketNBT.class);
addDiscriminator(3, PacketSlotChange.class);
addDiscriminator(4, PacketGuiReturn.class);
addDiscriminator(5, PacketGuiWidget.class);
addDiscriminator(6, PacketUpdate.class);
// this won't work whatsoever. this is a freaking abstract class
// addDiscriminator(6, PacketUpdate.class);
addDiscriminator(7, PacketCommand.class);
addDiscriminator(8, PacketEntityUpdate.class);
addDiscriminator(8, PacketEntityUpdate.class, CLIENT_ONLY);
maxDiscriminator = 9;
}

protected void logForgedPackets(EntityPlayer player, String packetType, ByteBuf rawPacket) {
BCLog.logger.info(
SUSPICIOUS_PACKETS,
"Player {} tried to send packet of type {} to invalid side {}. This could be a false warning due to custom mods/addon, or it could be an legit attempt at hacking!",
player.getGameProfile(),
player instanceof EntityPlayerMP ? Side.SERVER : Side.CLIENT,
packetType);
}

public byte getDiscriminator(Class<? extends Packet> clazz) {
return types.get(clazz);
}
Expand All @@ -60,8 +87,13 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
}

public ChannelHandler addDiscriminator(int discriminator, Class<? extends Packet> type) {
return addDiscriminator(discriminator, type, BOTH_SIDES);
}

public ChannelHandler addDiscriminator(int discriminator, Class<? extends Packet> type, int handlingSide) {
discriminators.put((byte) discriminator, type);
types.put(type, (byte) discriminator);
sides.put((byte) discriminator, handlingSide);
return this;
}

Expand Down Expand Up @@ -91,10 +123,18 @@ protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object
throw new NullPointerException(
"Undefined message for discriminator " + discriminator + " in channel " + msg.channel());
}
Packet newMsg = clazz.newInstance();
ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg));
newMsg.readData(payload.slice());
out.add(newMsg);
int expectedSide = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get() == Side.CLIENT ? CLIENT_ONLY
: SERVER_ONLY;
if ((expectedSide & sides.get(discriminator)) != expectedSide) {
INetHandler handler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(handler);
logForgedPackets(player, clazz.getSimpleName(), payload.slice());
} else {
Packet newMsg = clazz.newInstance();
ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<>(msg));
newMsg.readData(payload.slice());
out.add(newMsg);
}
}

/**
Expand All @@ -114,4 +154,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
public void registerPacketType(Class<? extends Packet> packetType) {
addDiscriminator(maxDiscriminator++, packetType);
}

public void registerPacketType(Class<? extends Packet> packetType, int handlingSide) {
addDiscriminator(maxDiscriminator++, packetType);
}
}
3 changes: 1 addition & 2 deletions src/main/java/buildcraft/core/network/PacketHandlerCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import buildcraft.core.tablet.TabletBase;
import buildcraft.core.tablet.manager.TabletManagerClient;
import buildcraft.core.tablet.manager.TabletManagerServer;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -25,7 +24,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Packet packet) {

switch (packet.getID()) {
case PacketIds.TABLET_MESSAGE: {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
if (ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get() == Side.SERVER) {
handleTabletClient((PacketTabletMessage) packet);
} else {
handleTabletServer(player, (PacketTabletMessage) packet);
Expand Down

0 comments on commit 11c063f

Please sign in to comment.