Waterfall/BungeeCord-Patches/0019-Allow-invalid-packet-ids-for-forge-servers.patch
Techcable 3809083007
Turn waterfall into a patch-based fork like Spigot and Paper (WIP)
Allows us to easily see and manage the diff with upstream.
Makes fixing conflicts with upstream easier and shows a cleaner, more acurate commit history.

All credits to scripts go to @md_5 @aikar @Zbob750 @Thinkofname @Byteflux @Techcable (GPL3 Licensed)
2016-05-30 14:51:51 -06:00

123 lines
5.6 KiB
Diff

From e41f8d2465c135cede5d3065647c943c2e3e5435 Mon Sep 17 00:00:00 2001
From: Techcable <Techcable@techcable.net>
Date: Thu, 19 May 2016 17:09:22 -0600
Subject: [PATCH] Allow invalid packet ids for forge servers
Some forge mods (COFH) use negative packet ids instead of plugin channels for 'reasons'.
Vanilla servers still error on negative/invalid packets.
Original issue: https://github.com/WaterfallMC/Waterfall-Old/issues/11
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
index e7cb380..447eaae 100644
--- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
@@ -16,6 +16,14 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf>
private final boolean server;
@Setter
private int protocolVersion;
+ @Setter
+ private boolean supportsForge = false;
+
+ public MinecraftDecoder(Protocol protocol, boolean server, int protocolVersion) {
+ this.protocol = protocol;
+ this.server = server;
+ this.protocolVersion = protocolVersion;
+ }
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
@@ -27,7 +35,7 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf>
{
int packetId = DefinedPacket.readVarInt( in );
- DefinedPacket packet = prot.createPacket( packetId, protocolVersion );
+ DefinedPacket packet = prot.createPacket( packetId, protocolVersion, supportsForge );
if ( packet != null )
{
packet.read( in, prot.getDirection(), protocolVersion );
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java
index 2cbe052..08621b6 100644
--- a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java
@@ -275,14 +275,18 @@ public enum Protocol
return protocol;
}
- public final DefinedPacket createPacket(int id, int version)
+ public boolean hasPacket(int i, boolean supportsForge) {
+ return supportsForge || i >= 0 && i <= MAX_PACKET_ID;
+ }
+
+ public final DefinedPacket createPacket(int id, int version, boolean supportsForge)
{
ProtocolData protocolData = getProtocolData( version );
if (protocolData == null)
{
throw new BadPacketException( "Unsupported protocol version" );
}
- if ( id > MAX_PACKET_ID )
+ if ( !hasPacket(id, supportsForge) )
{
throw new BadPacketException( "Packet with id " + id + " outside of range " );
}
diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
index 5ed9fe4..7d14ea7 100644
--- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
+++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
@@ -29,7 +29,9 @@ import net.md_5.bungee.forge.ForgeUtils;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.PacketHandler;
+import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.DefinedPacket;
+import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.packet.BossBar;
import net.md_5.bungee.protocol.packet.EncryptionRequest;
@@ -183,6 +185,12 @@ public class ServerConnector extends PacketHandler
ServerConnection server = new ServerConnection( ch, target );
ServerConnectedEvent event = new ServerConnectedEvent( user, server );
+
+ if (server.isForgeServer() && user.isForgeUser()) {
+ ((MinecraftDecoder) server.getCh().getHandle().pipeline().get(PipelineUtils.PACKET_DECODER)).setSupportsForge(true);
+ ((MinecraftDecoder) user.getCh().getHandle().pipeline().get(PipelineUtils.PACKET_DECODER)).setSupportsForge(true);
+ }
+
bungee.getPluginManager().callEvent( event );
ch.write( BungeeCord.getInstance().registerChannels() );
diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java
index 9881b75..c88ab49 100644
--- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java
+++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java
@@ -76,6 +76,7 @@ public final class UserConnection implements ProxiedPlayer
@NonNull
private final ProxyServer bungee;
@NonNull
+ @Getter
private final ChannelWrapper ch;
@Getter
@NonNull
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
index 02881ac..9ba9633 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
@@ -103,6 +103,12 @@ public abstract class EntityMap
int packetId = DefinedPacket.readVarInt( packet );
int packetIdLength = packet.readerIndex() - readerIndex;
+ if (packetId < 0 || packetId > ints.length || packetId > varints.length) { // Invalid packet id
+ // Ignore these invalid packets for compatibility reasons
+ packet.readerIndex( readerIndex );
+ return;
+ }
+
if ( ints[packetId] )
{
rewriteInt( packet, oldId, newId, readerIndex + packetIdLength );
--
2.8.3