Skip to content

Commit

Permalink
fix #5390 (#5391)
Browse files Browse the repository at this point in the history
  • Loading branch information
孙继峰 authored Apr 19, 2021
1 parent 36a4a84 commit 3ce1cba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 2 additions & 11 deletions common/src/main/java/com/alibaba/nacos/common/utils/IPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ public static String localHostIP() {
* @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();
}

/**
Expand All @@ -101,12 +97,7 @@ public static boolean isIPv6(String addr) {
* @return boolean
*/
public static boolean isIP(String addr) {
try {
InetAddress.getByName(addr);
return true;
} catch (UnknownHostException e) {
return false;
}
return isIPv4(addr) || isIPv6(addr);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void testIsIPv4() {
Assert.assertFalse(IPUtil.isIPv4("[::1]"));
Assert.assertFalse(IPUtil.isIPv4("asdfasf"));
Assert.assertFalse(IPUtil.isIPv4("ffgertert"));
Assert.assertFalse(IPUtil.isIPv4("127.100.19"));
}

@Test
Expand All @@ -47,6 +48,7 @@ public void testIsIP() {
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
Expand Down

0 comments on commit 3ce1cba

Please sign in to comment.