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

chufenggao pushed a commit to branch 3.0.2-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/3.0.2-prepare by this push:
     new c9e467f660 [Bug] [Common] update IPv4 Pattern (#11762) (#12739)
c9e467f660 is described below

commit c9e467f6601285314afd6600c4754dd757183ab4
Author: Eric Gao <[email protected]>
AuthorDate: Sun Nov 6 23:04:40 2022 +0800

    [Bug] [Common] update IPv4 Pattern (#11762) (#12739)
    
    * [Bug] [Common] IPv4 Pattern is wrong. #11758
    
    Co-authored-by: Yann Ann <[email protected]>
---
 .../org/apache/dolphinscheduler/common/utils/NetUtils.java   |  8 ++++----
 .../apache/dolphinscheduler/common/utils/NetUtilsTest.java   | 12 ++++++++++++
 2 files changed, 16 insertions(+), 4 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 926c3ab907..f7027cf78b 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
@@ -32,8 +32,8 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
-import java.util.regex.Pattern;
 
+import org.apache.http.conn.util.InetAddressUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,7 +42,6 @@ import org.slf4j.LoggerFactory;
  */
 public class NetUtils {
 
-    private static final Pattern IP_PATTERN = 
Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
     private static final String NETWORK_PRIORITY_DEFAULT = "default";
     private static final String NETWORK_PRIORITY_INNER = "inner";
     private static final String NETWORK_PRIORITY_OUTER = "outer";
@@ -183,7 +182,7 @@ public class NetUtils {
         }
         String name = address.getHostAddress();
         return (name != null
-                && IP_PATTERN.matcher(name).matches()
+                && InetAddressUtils.isIPv4Address(name)
                 && !address.isAnyLocalAddress()
                 && !address.isLoopbackAddress());
     }
@@ -237,7 +236,8 @@ public class NetUtils {
         Enumeration<NetworkInterface> interfaces = 
NetworkInterface.getNetworkInterfaces();
         while (interfaces.hasMoreElements()) {
             NetworkInterface networkInterface = interfaces.nextElement();
-            if (ignoreNetworkInterface(networkInterface)) { // ignore
+            // ignore
+            if (ignoreNetworkInterface(networkInterface)) {
                 continue;
             }
             validNetworkInterfaces.add(networkInterface);
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 d897696fca..1da8ffdb1b 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
@@ -99,6 +99,18 @@ public class NetUtilsTest {
         address = mock(InetAddress.class);
         when(address.getHostAddress()).thenReturn("1.2.3.4");
         assertTrue(NetUtils.isValidV4Address(address));
+        address = mock(InetAddress.class);
+        when(address.getHostAddress()).thenReturn("1.2.3.4:80");
+        assertFalse(NetUtils.isValidV4Address(address));
+        address = mock(InetAddress.class);
+        when(address.getHostAddress()).thenReturn("256.0.0.1");
+        assertFalse(NetUtils.isValidV4Address(address));
+        address = mock(InetAddress.class);
+        when(address.getHostAddress()).thenReturn("127.0.0.0.1");
+        assertFalse(NetUtils.isValidV4Address(address));
+        address = mock(InetAddress.class);
+        when(address.getHostAddress()).thenReturn("-1.2.3.4");
+        assertFalse(NetUtils.isValidV4Address(address));
     }
 
 }

Reply via email to