diff --git a/src/main/java/com/dispose/common/IPAddrType.java b/src/main/java/com/dispose/common/IPAddrType.java index 4e5ff62e..d3f720d1 100644 --- a/src/main/java/com/dispose/common/IPAddrType.java +++ b/src/main/java/com/dispose/common/IPAddrType.java @@ -91,16 +91,35 @@ public enum IPAddrType { * @return the boolean */ public static boolean isIpAddress(String ipAddr) { - if (!ipAddr.contains(GlobalVar.STRING_IPV4_FLAG) && !ipAddr.contains(GlobalVar.STRING_IPV6_FLAG)) { - return false; - } - try { - IPAddressString str = new IPAddressString(ipAddr); - IPAddress addr = str.toAddress(); - return true; - } catch (AddressStringException e) { - return false; + int counts = ipAddr.length() - ipAddr.replaceAll(GlobalVar.STRING_IPV6_FLAG, "").length(); + + String ipv4Regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; + + String ipv6Regex = "^(([\\da-fA-F]{1,4}):){8}$"; + + switch (counts) { + // ipv4 + case 0: + return ipAddr.matches(ipv4Regex); + // v4 + port + case 1: + String ipv4 = ipAddr.substring(0, ipAddr.lastIndexOf(":")); + //String port = ipAddr.substring(ipAddr.lastIndexOf(":") + 1); + return ipv4.matches(ipv4Regex); + default: + // Ip v6 + port + if (ipAddr.contains("[")) { + String ipv6 = ipAddr.substring(1, ipAddr.lastIndexOf("]")); + //String port = ipAddr.substring(ipAddr.lastIndexOf("]:") + 1); + return ipv6.matches(ipv6Regex); + } else { + return ipAddr.matches(ipv6Regex); + } } } } + +