From b14e7e651eab8111da58bbc76b103247d1541fcd Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 10 Oct 2020 11:02:29 -0400 Subject: [PATCH] Backport IPv6 modern forwarding fix to Velocity 1.0.x --- .../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 14b7ad22c..f2bf32f62 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 @@ -21,6 +21,7 @@ import com.velocitypowered.proxy.protocol.packet.SetCompression; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.io.IOException; +import java.net.InetSocketAddress; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.concurrent.CompletableFuture; @@ -62,7 +63,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { response.setSuccess(true); response.setId(packet.getId()); response.setData(createForwardingData(configuration.getForwardingSecret(), - serverConn.getPlayer().getRemoteAddress().getHostString(), + cleanRemoteAddress(serverConn.getPlayer().getRemoteAddress()), serverConn.getPlayer().getGameProfile())); mc.write(response); informationForwarded = true; @@ -123,6 +124,16 @@ public class LoginSessionHandler implements MinecraftSessionHandler { .completeExceptionally(new IOException("Unexpectedly disconnected from remote server")); } + 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 dataToForward = Unpooled.buffer();