diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IPUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/IPUtil.java index c0346e4f6..547ed8f2c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IPUtil.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IPUtil.java @@ -73,11 +73,7 @@ public class IPUtil { * @return boolean */ public static boolean isIPv4(String addr) { - try { - return InetAddress.getByName(addr).getAddress().length == IPV4_ADDRESS_LENGTH; - } catch (UnknownHostException e) { - return false; - } + return ipv4Pattern.matcher(addr).matches(); } /** @@ -101,12 +97,7 @@ public class IPUtil { * @return boolean */ public static boolean isIP(String addr) { - try { - InetAddress.getByName(addr); - return true; - } catch (UnknownHostException e) { - return false; - } + return isIPv4(addr) || isIPv6(addr); } /** diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/IPUtilTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/IPUtilTest.java index 5da23dba5..f94ea3cbc 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/IPUtilTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/IPUtilTest.java @@ -33,6 +33,7 @@ public class IPUtilTest { Assert.assertFalse(IPUtil.isIPv4("[::1]")); Assert.assertFalse(IPUtil.isIPv4("asdfasf")); Assert.assertFalse(IPUtil.isIPv4("ffgertert")); + Assert.assertFalse(IPUtil.isIPv4("127.100.19")); } @Test @@ -47,6 +48,7 @@ public class IPUtilTest { Assert.assertTrue(IPUtil.isIP("[::1]")); Assert.assertTrue(IPUtil.isIP("127.0.0.1")); Assert.assertFalse(IPUtil.isIP("er34234")); + Assert.assertFalse(IPUtil.isIP("127.100.19")); } @Test