mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 19:39:22 +00:00
Validate packet directions for Forge packets. Closes #10740
This commit is contained in:
5 files changed
+40
-2
No files matched your search
@@ -47,6 +47,8 @@ public class FMLLoginWrapper {
|
||||
}
|
||||
|
||||
private <T extends NetworkEvent> void wrapperReceived(final T packet) {
|
||||
// we don't care about channel registration change events on this channel
|
||||
if (packet instanceof NetworkEvent.ChannelRegistrationChangeEvent) return;
|
||||
final NetworkEvent.Context wrappedContext = packet.getSource().get();
|
||||
final PacketBuffer payload = packet.getPayload();
|
||||
ResourceLocation targetNetworkReceiver = FMLNetworkConstants.FML_HANDSHAKE_RESOURCE;
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.stream.Collectors;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.world.dimension.DimensionType;
|
||||
import net.minecraftforge.registries.ClearableRegistry;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -48,6 +49,7 @@ import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
|
||||
import net.minecraftforge.fml.common.thread.EffectiveSide;
|
||||
import net.minecraftforge.fml.config.ConfigTracker;
|
||||
|
||||
public class NetworkHooks
|
||||
@@ -70,8 +72,14 @@ public class NetworkHooks
|
||||
}
|
||||
|
||||
public static boolean onCustomPayload(final ICustomPacket<?> packet, final NetworkManager manager) {
|
||||
return NetworkRegistry.findTarget(packet.getName()).
|
||||
map(ni->ni.dispatch(packet.getDirection(), packet, manager)).orElse(Boolean.FALSE);
|
||||
if (packet.getDirection().getReceptionSide() != EffectiveSide.get()) {
|
||||
manager.closeChannel(new StringTextComponent("Illegal packet received, terminating connection"));
|
||||
return false;
|
||||
}
|
||||
NetworkInstance target = NetworkRegistry.findTarget(packet.getName()).orElse(null);
|
||||
if (target == null)
|
||||
return false;
|
||||
return target.dispatch(packet.getDirection(), packet, manager);
|
||||
}
|
||||
|
||||
public static void registerServerLoginChannel(NetworkManager manager, CHandshakePacket packet)
|
||||
|
||||
@@ -38,6 +38,7 @@ class NetworkInitialization {
|
||||
simpleChannel();
|
||||
|
||||
handshakeChannel.messageBuilder(FMLHandshakeMessages.C2SAcknowledge.class, 99).
|
||||
direction(NetworkDirection.LOGIN_TO_SERVER).
|
||||
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(FMLHandshakeMessages.C2SAcknowledge::decode).
|
||||
encoder(FMLHandshakeMessages.C2SAcknowledge::encode).
|
||||
@@ -45,6 +46,7 @@ class NetworkInitialization {
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CModList.class, 1).
|
||||
direction(NetworkDirection.LOGIN_TO_CLIENT).
|
||||
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(FMLHandshakeMessages.S2CModList::decode).
|
||||
encoder(FMLHandshakeMessages.S2CModList::encode).
|
||||
@@ -53,6 +55,7 @@ class NetworkInitialization {
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(FMLHandshakeMessages.C2SModListReply.class, 2).
|
||||
direction(NetworkDirection.LOGIN_TO_SERVER).
|
||||
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(FMLHandshakeMessages.C2SModListReply::decode).
|
||||
encoder(FMLHandshakeMessages.C2SModListReply::encode).
|
||||
@@ -60,6 +63,7 @@ class NetworkInitialization {
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CRegistry.class, 3).
|
||||
direction(NetworkDirection.LOGIN_TO_CLIENT).
|
||||
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(FMLHandshakeMessages.S2CRegistry::decode).
|
||||
encoder(FMLHandshakeMessages.S2CRegistry::encode).
|
||||
@@ -68,6 +72,7 @@ class NetworkInitialization {
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CConfigData.class, 4).
|
||||
direction(NetworkDirection.LOGIN_TO_CLIENT).
|
||||
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(FMLHandshakeMessages.S2CConfigData::decode).
|
||||
encoder(FMLHandshakeMessages.S2CConfigData::encode).
|
||||
@@ -87,18 +92,21 @@ class NetworkInitialization {
|
||||
simpleChannel();
|
||||
|
||||
playChannel.messageBuilder(FMLPlayMessages.SpawnEntity.class, 0).
|
||||
direction(NetworkDirection.PLAY_TO_CLIENT).
|
||||
decoder(FMLPlayMessages.SpawnEntity::decode).
|
||||
encoder(FMLPlayMessages.SpawnEntity::encode).
|
||||
consumer(FMLPlayMessages.SpawnEntity::handle).
|
||||
add();
|
||||
|
||||
playChannel.messageBuilder(FMLPlayMessages.OpenContainer.class,1).
|
||||
direction(NetworkDirection.PLAY_TO_CLIENT).
|
||||
decoder(FMLPlayMessages.OpenContainer::decode).
|
||||
encoder(FMLPlayMessages.OpenContainer::encode).
|
||||
consumer(FMLPlayMessages.OpenContainer::handle).
|
||||
add();
|
||||
|
||||
playChannel.messageBuilder(FMLPlayMessages.DimensionInfoMessage.class, 2)
|
||||
.direction(NetworkDirection.PLAY_TO_CLIENT)
|
||||
.decoder(FMLPlayMessages.DimensionInfoMessage::decode)
|
||||
.encoder(FMLPlayMessages.DimensionInfoMessage::encode)
|
||||
.consumer(FMLPlayMessages.DimensionInfoMessage::handle)
|
||||
|
||||
@@ -22,6 +22,8 @@ package net.minecraftforge.fml.network.simple;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraftforge.fml.network.NetworkDirection;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
import net.minecraftforge.fml.network.NetworkInstance;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -70,6 +72,7 @@ public class IndexedMessageCodec
|
||||
private final Class<MSG> messageType;
|
||||
private Optional<BiConsumer<MSG, Integer>> loginIndexSetter;
|
||||
private Optional<Function<MSG, Integer>> loginIndexGetter;
|
||||
private NetworkDirection direction;
|
||||
|
||||
public MessageHandler(int index, Class<MSG> messageType, BiConsumer<MSG, PacketBuffer> encoder, Function<PacketBuffer, MSG> decoder, BiConsumer<MSG, Supplier<NetworkEvent.Context>> messageConsumer)
|
||||
{
|
||||
@@ -84,6 +87,12 @@ public class IndexedMessageCodec
|
||||
types.put(messageType, this);
|
||||
}
|
||||
|
||||
void setDirection(NetworkDirection value) {
|
||||
if (this.direction != null)
|
||||
throw new IllegalStateException("Can not set direction to " + value + " it is already set to " + this.direction);
|
||||
this.direction = value;
|
||||
}
|
||||
|
||||
void setLoginIndexSetter(BiConsumer<MSG, Integer> loginIndexSetter)
|
||||
{
|
||||
this.loginIndexSetter = Optional.of(loginIndexSetter);
|
||||
@@ -154,6 +163,10 @@ public class IndexedMessageCodec
|
||||
LOGGER.error(SIMPLENET, "Received invalid discriminator byte {} on channel {}", discriminator, Optional.ofNullable(networkInstance).map(NetworkInstance::getChannelName).map(Objects::toString).orElse("MISSING CHANNEL"));
|
||||
return;
|
||||
}
|
||||
if (messageHandler.direction != null && messageHandler.direction != context.get().getDirection()) {
|
||||
context.get().getNetworkManager().closeChannel(new StringTextComponent("Illegal packet received, terminating connection"));
|
||||
throw new IllegalStateException("Invalid packet received, aborting connection");
|
||||
}
|
||||
tryDecode(payload, context, payloadIndex, messageHandler);
|
||||
}
|
||||
|
||||
|
||||
@@ -150,6 +150,7 @@ public class SimpleChannel
|
||||
private Function<MSG, Integer> loginIndexGetter;
|
||||
private BiConsumer<MSG, Integer> loginIndexSetter;
|
||||
private Function<Boolean, List<Pair<String, MSG>>> loginPacketGenerators;
|
||||
private NetworkDirection direction;
|
||||
|
||||
private static <MSG> MessageBuilder<MSG> forType(final SimpleChannel channel, final Class<MSG> type, int id) {
|
||||
MessageBuilder<MSG> builder = new MessageBuilder<>();
|
||||
@@ -159,6 +160,11 @@ public class SimpleChannel
|
||||
return builder;
|
||||
}
|
||||
|
||||
public MessageBuilder<MSG> direction(NetworkDirection expected) {
|
||||
this.direction = expected;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MessageBuilder<MSG> encoder(BiConsumer<MSG, PacketBuffer> encoder) {
|
||||
this.encoder = encoder;
|
||||
return this;
|
||||
@@ -216,6 +222,7 @@ public class SimpleChannel
|
||||
|
||||
public void add() {
|
||||
final IndexedMessageCodec.MessageHandler<MSG> message = this.channel.registerMessage(this.id, this.type, this.encoder, this.decoder, this.consumer);
|
||||
message.setDirection(direction);
|
||||
if (this.loginIndexSetter != null) {
|
||||
message.setLoginIndexSetter(this.loginIndexSetter);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user