NetUtils { public static boolean matchIpRange(String pattern, String host, int port) throws UnknownHostException { if (pattern == null || host == null) { throw new IllegalArgumentException("Illegal Argument pattern or hostName. Pattern:" + pattern + ", Host:" + host); } pattern = pattern.trim(); if ("*.*.*.*".equals(pattern) || "*".equals(pattern)) { return true; } InetAddress inetAddress = InetAddress.getByName(host); boolean isIpv4 = isValidV4Address(inetAddress); String[] hostAndPort = getPatternHostAndPort(pattern, isIpv4); if (hostAndPort[1] != null && !hostAndPort[1].equals(String.valueOf(port))) { return false; } pattern = hostAndPort[0]; String splitCharacter = SPLIT_IPV4_CHARACTER; if (!isIpv4) { splitCharacter = SPLIT_IPV6_CHARACTER; } String[] mask = pattern.split(splitCharacter); checkHostPattern(pattern, mask, isIpv4); host = inetAddress.getHostAddress(); if (pattern.equals(host)) { return true; } if (!ipPatternContainExpression(pattern)) { InetAddress patternAddress = InetAddress.getByName(pattern); return patternAddress.getHostAddress().equals(host); } String[] ipAddress = host.split(splitCharacter); for (int i = 0; i < mask.length; i++) { if ("*".equals(mask[i]) || mask[i].equals(ipAddress[i])) { continue; } else if (mask[i].contains("-")) { String[] rangeNumStrs = StringUtils.split(mask[i], '-'); if (rangeNumStrs.length != 2) { throw new IllegalArgumentException("There is wrong format of ip Address: " + mask[i]); } Integer min = getNumOfIpSegment(rangeNumStrs[0], isIpv4); Integer max = getNumOfIpSegment(rangeNumStrs[1], isIpv4); Integer ip = getNumOfIpSegment(ipAddress[i], isIpv4); if (ip < min || ip > max) { return false; } } else if ("0".equals(ipAddress[i]) && ("0".equals(mask[i]) || "00".equals(mask[i]) || "000".equals(mask[i]) || "0000".equals(mask[i]))) { continue; } else if (!mask[i].equals(ipAddress[i])) { return false; } } return true; }  static int getRandomPort(); static int getAvailablePort(); static int getAvailablePort(int port); static boolean isInvalidPort(int port); static boolean isValidAddress(String address); static boolean isLocalHost(String host); static boolean isAnyHost(String host); static boolean isInvalidLocalHost(String host); static boolean isValidLocalHost(String host); static InetSocketAddress getLocalSocketAddress(String host, int port); static String getLocalHost(); static String filterLocalHost(String host); static String getIpByConfig(); static InetAddress getLocalAddress(); static boolean isPreferredNetworkInterface(NetworkInterface networkInterface); static NetworkInterface findNetworkInterface(); static String getHostName(String address); static String getIpByHost(String hostName); static String toAddressString(InetSocketAddress address); static InetSocketAddress toAddress(String address); static String toURL(String protocol, String host, int port, String path); static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress); static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6); static boolean matchIpExpression(String pattern, String host, int port); static boolean matchIpRange(String pattern, String host, int port); static boolean isMulticastAddress(String host);  }
@Test public void testMatchIpRangeMatchWhenIpv4() throws UnknownHostException { assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63", 90)); assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63", 90)); assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63", 90)); assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63", 90)); assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63", 90)); }
@Test public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff", 90)); assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee", 90)); assertTrue(NetUtils.matchIpRange("234e:0:4567::3d:ee", "234e:0:4567::3d:ee", 90)); assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee", 90)); assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee", 90)); assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee", 90)); assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee", 90)); }
@Test public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff", 90)); assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); thrown = assertThrows(IllegalArgumentException.class, () -> NetUtils.matchIpRange("234e:0:4567:3d", "234e:0:4567::3d:ff", 90)); assertTrue(thrown.getMessage().contains("The host is ipv6, but the pattern is not ipv6 pattern")); thrown = assertThrows(IllegalArgumentException.class, () -> NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63", 90)); assertTrue(thrown.getMessage().contains("There is wrong format of ip Address")); }
@Test public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { UnknownHostException thrown = assertThrows(UnknownHostException.class, () -> NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff", 90)); assertTrue(thrown.getMessage().contains("192.168.1.ff")); }
@Test public void testMatchIpv6WithIpPort() throws UnknownHostException { assertTrue(NetUtils.matchIpRange("[234e:0:4567::3d:ee]", "234e:0:4567::3d:ee", 8090)); assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]", "234e:0:4567::3d:ee", 8090)); assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:8090", "234e:0:4567::3d:ee", 8090)); assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:0-ee]:8090", "234e:0:4567::3d:ee", 8090)); assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 8090)); assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:*]:90", "234e:0:4567::3d:ff", 90)); assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:7289", "234e:0:4567::3d:ee", 8090)); assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 9090)); }