Support IPv6 address compression for logged IPs

This commit is contained in:
Paint_Ninja authored and LexManos committed 2023-07-14 11:14:56 -07:00
1 parent 5271ef79fe
commit d947d35eb1
5 files changed
+50 -12

No files matched your search

@@ -105,6 +105,7 @@ public class ForgeConfig {
public final BooleanValue useCombinedDepthStencilAttachment;
@Deprecated(since = "1.20.1", forRemoval = true) // Config option ignored.
public final BooleanValue compressLanIPv6Addresses;
Client(ForgeConfigSpec.Builder builder) {
@@ -134,7 +135,7 @@ public class ForgeConfig {
.define("useCombinedDepthStencilAttachment", false);
compressLanIPv6Addresses = builder
.comment("When enabled, Forge will convert discovered 'Open to LAN' IPv6 addresses to their more compact, compressed representation")
.comment("[DEPRECATED] [NOW IN COMMON CONFIG] When enabled, Forge will convert discovered 'Open to LAN' IPv6 addresses to their more compact, compressed representation")
.translation("forge.configgui.compressLanIPv6Addresses")
.define("compressLanIPv6Addresses", true);
@@ -6,6 +6,7 @@
package net.minecraftforge.network;
import com.mojang.logging.LogUtils;
import com.google.common.net.InetAddresses;
import net.minecraft.client.multiplayer.resolver.ResolvedServerAddress;
import net.minecraft.client.multiplayer.resolver.ServerAddress;
import net.minecraft.client.multiplayer.resolver.ServerNameResolver;
@@ -15,6 +16,10 @@ import org.slf4j.Logger;
import javax.annotation.Nullable;
import java.net.Inet4Address;
import net.minecraftforge.common.ForgeConfig;
import javax.annotation.Nullable;
import java.net.SocketAddress;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@@ -150,4 +155,26 @@ public class DualStackUtils
LOGGER.debug("Initial IPv4 stack preference: " + INITIAL_PREFER_IPv4_STACK);
LOGGER.debug("Initial IPv6 addresses preference: " + INITIAL_PREFER_IPv6_ADDRESSES);
}
/**
* {@link SocketAddress#toString()} but with IPv6 address compression support
*/
public static String getAddressString(final SocketAddress address) {
if (address instanceof final InetSocketAddress inetAddress) {
String formatted;
if (inetAddress.isUnresolved()) {
formatted = inetAddress.getHostName() + "/<unresolved>";
} else {
formatted = InetAddresses.toAddrString(inetAddress.getAddress());
if (inetAddress.getAddress() instanceof Inet6Address)
formatted = '[' + formatted + ']';
formatted = '/' + formatted;
}
return formatted + ':' + inetAddress.getPort();
}
return address.toString();
}
}