Waterfall/BungeeCord-Patches/0012-Add-support-for-FML-wi...

122 lines
6.2 KiB
Diff

From 181fb185d832bbea34d4744bb7e426d9ff6bc216 Mon Sep 17 00:00:00 2001
From: Daniel Naylor <git@drnaylor.co.uk>
Date: Sun, 9 Aug 2015 13:43:57 +0100
Subject: [PATCH] Add support for FML with IP Forwarding enabled
FML adds a \00FML\00 marker to the host field, so Forge can determine whether or not to start a Forge handshake, making way to allow vanilla clients to connect to Forge servers that don't need a client modification. However, Bungee also uses the field, and the two implementations collide when using Spigot.
The original fix was to not send the FML information at the same time as the IP forwarding, you could have one or the other, but not both. This was OK, as no FML servers supported IP forwarding as of time of the patch. This was implemented in commit 4809f1f80ace9ae87b91453c8887c70f5e098bd0.
However, there is now at least one Forge coremod that intends to support IP forwarding. To be able to support Forge with IP forwarding, a way to be able to support the FML token (and any other host data) is needed. This adds a property to the user Game Profile to forward on whether the user is a FML client, along with whether there is any extra data.
No breaking changes occur due to this patch.
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 dd00192..5ed9fe4 100644
--- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
+++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java
@@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
+import java.util.Arrays;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
@@ -92,15 +93,39 @@ public class ServerConnector extends PacketHandler
String newHost = copiedHandshake.getHost() + "\00" + user.getAddress().getHostString() + "\00" + user.getUUID();
LoginResult profile = user.getPendingConnection().getLoginProfile();
+
+ // Handle properties.
+ LoginResult.Property[] properties = new LoginResult.Property[0];
+
if ( profile != null && profile.getProperties() != null && profile.getProperties().length > 0 )
{
- newHost += "\00" + BungeeCord.getInstance().gson.toJson( profile.getProperties() );
+ properties = profile.getProperties();
}
+
+ if ( user.getForgeClientHandler().isFmlTokenInHandshake() )
+ {
+ // Get the current properties and copy them into a slightly bigger array.
+ LoginResult.Property[] newp = Arrays.copyOf( properties, properties.length + 2 );
+
+ // Add a new profile property that specifies that this user is a Forge user.
+ newp[newp.length - 2] = new LoginResult.Property( ForgeConstants.FML_LOGIN_PROFILE, "true", null );
+
+ // If we do not perform the replacement, then the IP Forwarding code in Spigot et. al. will try to split on this prematurely.
+ newp[newp.length - 1] = new LoginResult.Property( ForgeConstants.EXTRA_DATA, user.getExtraDataInHandshake().replaceAll( "\0", "\1"), "" );
+
+ // All done.
+ properties = newp;
+ }
+
+ // If we touched any properties, then append them
+ if (properties.length > 0) {
+ newHost += "\00" + BungeeCord.getInstance().gson.toJson(properties);
+ }
+
copiedHandshake.setHost( newHost );
} else if ( !user.getExtraDataInHandshake().isEmpty() )
{
- // Only restore the extra data if IP forwarding is off.
- // TODO: Add support for this data with IP forwarding.
+ // Restore the extra data
copiedHandshake.setHost( copiedHandshake.getHost() + user.getExtraDataInHandshake() );
}
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 a5010f3..018c02e 100644
--- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java
+++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java
@@ -12,6 +12,7 @@ import io.netty.channel.ChannelOption;
import io.netty.util.internal.PlatformDependent;
import java.net.InetSocketAddress;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -40,6 +41,8 @@ import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.score.Scoreboard;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.connection.InitialHandler;
+import net.md_5.bungee.connection.LoginResult;
+import net.md_5.bungee.connection.LoginResult.Property;
import net.md_5.bungee.entitymap.EntityMap;
import net.md_5.bungee.forge.ForgeClientHandler;
import net.md_5.bungee.forge.ForgeConstants;
@@ -181,8 +184,12 @@ public final class UserConnection implements ProxiedPlayer
forgeClientHandler = new ForgeClientHandler( this );
+ // No-config FML handshake marker.
// Set whether the connection has a 1.8 FML marker in the handshake.
- forgeClientHandler.setFmlTokenInHandshake( this.getPendingConnection().getExtraDataInHandshake().contains( ForgeConstants.FML_HANDSHAKE_TOKEN ) );
+ if (this.getPendingConnection().getExtraDataInHandshake().contains( ForgeConstants.FML_HANDSHAKE_TOKEN ))
+ {
+ forgeClientHandler.setFmlTokenInHandshake( true );
+ }
}
public void sendPacket(PacketWrapper packet)
diff --git a/proxy/src/main/java/net/md_5/bungee/forge/ForgeConstants.java b/proxy/src/main/java/net/md_5/bungee/forge/ForgeConstants.java
index 6dca204..f5253b8 100644
--- a/proxy/src/main/java/net/md_5/bungee/forge/ForgeConstants.java
+++ b/proxy/src/main/java/net/md_5/bungee/forge/ForgeConstants.java
@@ -14,6 +14,10 @@ public class ForgeConstants
public static final String FML_HANDSHAKE_TAG = "FML|HS";
public static final String FML_REGISTER = "REGISTER";
+ // Game profile key
+ public static final String FML_LOGIN_PROFILE = "forgeClient";
+ public static final String EXTRA_DATA = "extraData";
+
/**
* The FML 1.8 handshake token.
*/
--
2.8.3