This is an automated email from the ASF dual-hosted git repository.

zihaoxiang pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 40493c3393 [Improvement-17433] Support identify IPv6 addresses 
properly (#18126)
40493c3393 is described below

commit 40493c3393d88a97a376115b7f560486ea0c8753
Author: xiangzihao <[email protected]>
AuthorDate: Thu Apr 2 15:08:53 2026 +0800

    [Improvement-17433] Support identify IPv6 addresses properly (#18126)
---
 .../dolphinscheduler/common/utils/NetUtils.java    | 43 +++++++++++++++++++---
 .../common/utils/NetUtilsTest.java                 | 33 ++++++++++++++++-
 2 files changed, 70 insertions(+), 6 deletions(-)

diff --git 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
index ebefe210b2..12d96e2ad9 100644
--- 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
+++ 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
@@ -67,6 +67,10 @@ public class NetUtils {
      * @return addr
      */
     public static String getAddr(String host, int port) {
+        String normalizedHost = unwrapIpv6Literal(host);
+        if (isIpv6Literal(normalizedHost)) {
+            return String.format("[%s]:%d", normalizedHost, port);
+        }
         return String.format("%s:%d", host, port);
     }
 
@@ -92,6 +96,9 @@ public class NetUtils {
                 }
                 return canonicalHost;
             }
+            if (inetAddress instanceof Inet6Address) {
+                return normalizeV6Address((Inet6Address) 
inetAddress).getHostAddress();
+            }
             return inetAddress.getHostAddress();
         }
         return null;
@@ -134,10 +141,10 @@ public class NetUtils {
 
     private static InetAddress normalizeV6Address(Inet6Address address) {
         String addr = address.getHostAddress();
-        int i = addr.lastIndexOf('%');
-        if (i > 0) {
+        String normalizedAddr = stripIpv6Scope(addr);
+        if (!StringUtils.equals(addr, normalizedAddr)) {
             try {
-                return InetAddress.getByName(addr.substring(0, i) + '%' + 
address.getScopeId());
+                return InetAddress.getByName(normalizedAddr);
             } catch (UnknownHostException e) {
                 log.debug("Unknown IPV6 address: ", e);
             }
@@ -160,11 +167,12 @@ public class NetUtils {
         if (!(address instanceof Inet6Address)) {
             return false;
         }
-        String name = address.getHostAddress();
+        String name = stripIpv6Scope(address.getHostAddress());
         return (name != null
                 && InetAddressUtils.isIPv6Address(name)
                 && !address.isAnyLocalAddress()
-                && !address.isLoopbackAddress());
+                && !address.isLoopbackAddress()
+                && !address.isLinkLocalAddress());
     }
 
     /**
@@ -313,6 +321,31 @@ public class NetUtils {
         return validNetworkInterfaces;
     }
 
+    private static boolean isIpv6Literal(String host) {
+        return StringUtils.isNotEmpty(host) && 
InetAddressUtils.isIPv6Address(stripIpv6Scope(host));
+    }
+
+    private static String stripIpv6Scope(String host) {
+        if (StringUtils.isEmpty(host)) {
+            return host;
+        }
+        int index = host.lastIndexOf('%');
+        if (index > 0) {
+            return host.substring(0, index);
+        }
+        return host;
+    }
+
+    private static String unwrapIpv6Literal(String host) {
+        if (StringUtils.isEmpty(host)) {
+            return host;
+        }
+        if (host.startsWith("[") && host.endsWith("]")) {
+            return host.substring(1, host.length() - 1);
+        }
+        return host;
+    }
+
     private static String specifyNetworkInterfaceName() {
         return 
PropertyUtils.getString(DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED,
                 
System.getProperty(DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED));
diff --git 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
index f3e759e585..880ad4dc90 100644
--- 
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
+++ 
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
@@ -22,7 +22,9 @@ import static org.mockito.Mockito.mockStatic;
 import static org.mockito.Mockito.when;
 
 import java.net.Inet4Address;
+import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -32,9 +34,13 @@ public class NetUtilsTest {
 
     @Test
     public void testGetAddr() {
-        Assertions.assertEquals(NetUtils.getHost() + ":5678", 
NetUtils.getAddr(5678));
+        Assertions.assertEquals(NetUtils.getAddr(NetUtils.getHost(), 5678), 
NetUtils.getAddr(5678));
         Assertions.assertEquals("127.0.0.1:5678", 
NetUtils.getAddr("127.0.0.1", 5678));
         Assertions.assertEquals("localhost:1234", 
NetUtils.getAddr("localhost", 1234));
+        
Assertions.assertEquals("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]:1234",
+                NetUtils.getAddr("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831", 
1234));
+        
Assertions.assertEquals("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]:1234",
+                NetUtils.getAddr("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]", 
1234));
     }
 
     @Test
@@ -77,6 +83,13 @@ public class NetUtilsTest {
         Assertions.assertEquals("172.17.0.15", NetUtils.getHost(address));
     }
 
+    @Test
+    public void testGetHostInNonKubernetesModeShouldStripIpv6Scope() throws 
UnknownHostException {
+        Inet6Address address = Inet6Address.getByAddress(null,
+                
InetAddress.getByName("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831").getAddress(), 
2);
+        Assertions.assertEquals("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831", 
NetUtils.getHost(address));
+    }
+
     @Test
     public void testGetLocalHost() {
         Assertions.assertNotNull(NetUtils.getHost());
@@ -116,4 +129,22 @@ public class NetUtilsTest {
         Assertions.assertFalse(NetUtils.isValidV4Address(address));
     }
 
+    @Test
+    public void testIsValidV6Address() throws UnknownHostException {
+        Assertions.assertFalse(NetUtils.isValidV6Address(null));
+
+        Inet6Address globalAddress = (Inet6Address) InetAddress
+                .getByName("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831");
+        Assertions.assertTrue(NetUtils.isValidV6Address(globalAddress));
+
+        Inet6Address scopedGlobalAddress = Inet6Address.getByAddress(null, 
globalAddress.getAddress(), 2);
+        Assertions.assertTrue(NetUtils.isValidV6Address(scopedGlobalAddress));
+
+        Inet6Address linkLocalAddress = (Inet6Address) 
InetAddress.getByName("fe80::20c:29ff:feac:72e7");
+        Assertions.assertFalse(NetUtils.isValidV6Address(linkLocalAddress));
+
+        Inet6Address loopbackAddress = (Inet6Address) 
InetAddress.getByName("::1");
+        Assertions.assertFalse(NetUtils.isValidV6Address(loopbackAddress));
+    }
+
 }

Reply via email to