@Test public void isMatchIPByPattern() throws Exception { Assert.assertTrue(NetUtils.isMatchIPByPattern("*", "127.0.0.1")); Assert.assertTrue(NetUtils.isMatchIPByPattern("10.1.*", "10.1.1.1")); Assert.assertFalse(NetUtils.isMatchIPByPattern("10.1.*", "10.2.1.1")); Assert.assertFalse(NetUtils.isMatchIPByPattern("10.1.1.1,10.1.1.2", "10.2.1.1")); Assert.assertTrue(NetUtils.isMatchIPByPattern("10.1.1.1,10.1.1.2", "10.1.1.1")); Assert.assertTrue(NetUtils.isMatchIPByPattern("10.1.1.1", "10.1.1.1")); Assert.assertTrue(NetUtils.isMatchIPByPattern("10.1.1.[1-3]", "10.1.1.1")); Assert.assertFalse(NetUtils.isMatchIPByPattern("10.1.1.[1-3]", "10.1.1.4")); }
public static boolean isMatchIPByPattern(String whiteList, String localIP) { if (StringUtils.isNotBlank(whiteList)) { if (StringUtils.ALL.equals(whiteList)) { return true; } for (String ips : whiteList.replace(',', ';').split(";", -1)) { try { if (ips.contains(StringUtils.ALL)) { String regex = ips.trim().replace(".", "\\.").replace("*", ".*"); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else if (!isIPv4Host(ips)) { String regex = ips.trim().replace(".", "\\."); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else { if (ips.equals(localIP)) { return true; } } } catch (Exception e) { LOGGER.warn("syntax of pattern {} is invalid", ips); } } } return false; }
NetUtils { public static boolean isMatchIPByPattern(String whiteList, String localIP) { if (StringUtils.isNotBlank(whiteList)) { if (StringUtils.ALL.equals(whiteList)) { return true; } for (String ips : whiteList.replace(',', ';').split(";", -1)) { try { if (ips.contains(StringUtils.ALL)) { String regex = ips.trim().replace(".", "\\.").replace("*", ".*"); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else if (!isIPv4Host(ips)) { String regex = ips.trim().replace(".", "\\."); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else { if (ips.equals(localIP)) { return true; } } } catch (Exception e) { LOGGER.warn("syntax of pattern {} is invalid", ips); } } } return false; } }
NetUtils { public static boolean isMatchIPByPattern(String whiteList, String localIP) { if (StringUtils.isNotBlank(whiteList)) { if (StringUtils.ALL.equals(whiteList)) { return true; } for (String ips : whiteList.replace(',', ';').split(";", -1)) { try { if (ips.contains(StringUtils.ALL)) { String regex = ips.trim().replace(".", "\\.").replace("*", ".*"); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else if (!isIPv4Host(ips)) { String regex = ips.trim().replace(".", "\\."); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else { if (ips.equals(localIP)) { return true; } } } catch (Exception e) { LOGGER.warn("syntax of pattern {} is invalid", ips); } } } return false; }  }
NetUtils { public static boolean isMatchIPByPattern(String whiteList, String localIP) { if (StringUtils.isNotBlank(whiteList)) { if (StringUtils.ALL.equals(whiteList)) { return true; } for (String ips : whiteList.replace(',', ';').split(";", -1)) { try { if (ips.contains(StringUtils.ALL)) { String regex = ips.trim().replace(".", "\\.").replace("*", ".*"); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else if (!isIPv4Host(ips)) { String regex = ips.trim().replace(".", "\\."); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else { if (ips.equals(localIP)) { return true; } } } catch (Exception e) { LOGGER.warn("syntax of pattern {} is invalid", ips); } } } return false; }  static boolean isInvalidPort(int port); static boolean isRandomPort(int port); static int getAvailablePort(String host, int port); static int getAvailablePort(String host, int port, int maxPort); static boolean isLocalHost(String host); static boolean isAnyHost(String host); static boolean isIPv4Host(String host); static boolean isHostInNetworkCard(String host); static String getLocalIpv4(); static InetAddress getLocalAddress(); static String toAddressString(InetSocketAddress address); static String toIpString(InetSocketAddress address); static String getLocalHostByRegistry(String registryIp); static List<InetSocketAddress> getIpListByRegistry(String registryIp); static boolean isMatchIPByPattern(String whiteList, String localIP); static String connectToString(InetSocketAddress local, InetSocketAddress remote); static String channelToString(SocketAddress local1, SocketAddress remote1); static boolean canTelnet(String ip, int port, int timeout); static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress); static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6); }
NetUtils { public static boolean isMatchIPByPattern(String whiteList, String localIP) { if (StringUtils.isNotBlank(whiteList)) { if (StringUtils.ALL.equals(whiteList)) { return true; } for (String ips : whiteList.replace(',', ';').split(";", -1)) { try { if (ips.contains(StringUtils.ALL)) { String regex = ips.trim().replace(".", "\\.").replace("*", ".*"); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else if (!isIPv4Host(ips)) { String regex = ips.trim().replace(".", "\\."); Pattern pattern = Pattern.compile(regex); if (pattern.matcher(localIP).find()) { return true; } } else { if (ips.equals(localIP)) { return true; } } } catch (Exception e) { LOGGER.warn("syntax of pattern {} is invalid", ips); } } } return false; }  static boolean isInvalidPort(int port); static boolean isRandomPort(int port); static int getAvailablePort(String host, int port); static int getAvailablePort(String host, int port, int maxPort); static boolean isLocalHost(String host); static boolean isAnyHost(String host); static boolean isIPv4Host(String host); static boolean isHostInNetworkCard(String host); static String getLocalIpv4(); static InetAddress getLocalAddress(); static String toAddressString(InetSocketAddress address); static String toIpString(InetSocketAddress address); static String getLocalHostByRegistry(String registryIp); static List<InetSocketAddress> getIpListByRegistry(String registryIp); static boolean isMatchIPByPattern(String whiteList, String localIP); static String connectToString(InetSocketAddress local, InetSocketAddress remote); static String channelToString(SocketAddress local1, SocketAddress remote1); static boolean canTelnet(String ip, int port, int timeout); static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress); static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6); static final String ANYHOST; static final Pattern IPV4_PATTERN; }