From 7fea1c4cb212df197a7f08ba509768d39369535a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 10 Oct 2020 10:58:32 -0400 Subject: [PATCH] Fix modern forwarding if a user connects over IPv6(?!?) You don't see this every day... --- .../connection/backend/LoginSessionHandler.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index dc839640c..80e6bd7ce 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -20,6 +20,7 @@ import com.velocitypowered.proxy.protocol.packet.SetCompression; import com.velocitypowered.proxy.util.except.QuietRuntimeException; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import java.net.InetSocketAddress; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.concurrent.CompletableFuture; @@ -58,7 +59,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { if (configuration.getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN && packet .getChannel().equals(VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL)) { ByteBuf forwardingData = createForwardingData(configuration.getForwardingSecret(), - serverConn.getPlayer().getRemoteAddress().getHostString(), + cleanRemoteAddress(serverConn.getPlayer().getRemoteAddress()), serverConn.getPlayer().getGameProfile()); LoginPluginResponse response = new LoginPluginResponse(packet.getId(), true, forwardingData); mc.write(response); @@ -127,6 +128,16 @@ public class LoginSessionHandler implements MinecraftSessionHandler { } } + private static String cleanRemoteAddress(InetSocketAddress address) { + String addressString = address.getAddress().getHostAddress(); + int ipv6ScopeIdx = addressString.indexOf('%'); + if (ipv6ScopeIdx == -1) { + return addressString; + } else { + return addressString.substring(0, ipv6ScopeIdx); + } + } + private static ByteBuf createForwardingData(byte[] hmacSecret, String address, GameProfile profile) { ByteBuf forwarded = Unpooled.buffer(2048);