diff --git a/disjob-common/src/main/java/cn/ponfee/disjob/common/util/NetUtils.java b/disjob-common/src/main/java/cn/ponfee/disjob/common/util/NetUtils.java index e3f79f553..d061be8ed 100644 --- a/disjob-common/src/main/java/cn/ponfee/disjob/common/util/NetUtils.java +++ b/disjob-common/src/main/java/cn/ponfee/disjob/common/util/NetUtils.java @@ -13,11 +13,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.net.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -62,6 +62,8 @@ public final class NetUtils { private static final String ANY_IP_ADDRESS = "0.0.0.0"; private static final Pattern IP_ADDRESS_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); + private static final int PING_TIMEOUT = 300; + /** * store the used port. * the set used only on the synchronized method. @@ -84,7 +86,7 @@ public static boolean isConnectableHostPort(String host, int port, int timeout) try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(host, port), timeout); return true; - } catch (IOException ignored) { + } catch (Exception ignored) { return false; } } @@ -102,10 +104,10 @@ public static boolean isAvailablePort(int port) { if (USED_PORT.get(port)) { return false; } - try (ServerSocket ignored = new ServerSocket(port)) { - ignored.setReuseAddress(true); + try (ServerSocket serverSocket = new ServerSocket(port)) { + serverSocket.setReuseAddress(true); return true; - } catch (IOException ignored) { + } catch (Exception ignored) { USED_PORT.set(port); return false; } @@ -116,10 +118,40 @@ public static boolean isValidIpAddress(String ip) { } public static boolean isValidLocalHost(String host) { - return StringUtils.isNotEmpty(host) - && !host.equalsIgnoreCase(LOCAL_HOST_NAME) - && !host.equals(ANY_IP_ADDRESS) - && !host.startsWith("127."); + if (StringUtils.isBlank(host) || + host.equalsIgnoreCase(LOCAL_HOST_NAME) || + host.equals(ANY_IP_ADDRESS) || + host.startsWith("127.")) { + return false; + } + try { + InetAddress inetAddress = InetAddress.getByName(host); + return !inetAddress.isLoopbackAddress(); + } catch (Exception ignored) { + return false; + } + } + + public static boolean isReachableHost(String host) { + if (!isValidLocalHost(host)) { + return false; + } + try { + InetAddress inetAddress = InetAddress.getByName(host); + if (inetAddress.isReachable(PING_TIMEOUT)) { + return true; + } + } catch (Exception ignored) { + // ignored + } + + try { + Process process = Runtime.getRuntime().exec("ping -c 1 " + host); + boolean exited = process.waitFor(PING_TIMEOUT, TimeUnit.MILLISECONDS); + return exited && process.exitValue() == 0; + } catch (Exception ignored) { + return false; + } } public static boolean isAnyHost(String host) { @@ -295,13 +327,10 @@ private static boolean isReachableAddress(InetAddress address) { return false; } try { - if (address.isReachable(100)) { - return true; - } - } catch (IOException ignored) { - // ignored + return address.isReachable(PING_TIMEOUT); + } catch (Exception ignored) { + return false; } - return false; } private static InetAddress getReachableAddress(NetworkInterface networkInterface) { diff --git a/disjob-common/src/main/java/cn/ponfee/disjob/common/util/SystemUtils.java b/disjob-common/src/main/java/cn/ponfee/disjob/common/util/SystemUtils.java index 5703bef21..a6ca59932 100644 --- a/disjob-common/src/main/java/cn/ponfee/disjob/common/util/SystemUtils.java +++ b/disjob-common/src/main/java/cn/ponfee/disjob/common/util/SystemUtils.java @@ -8,6 +8,7 @@ package cn.ponfee.disjob.common.util; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,12 +25,22 @@ public static String getConfig(String name) { String value = null; try { value = System.getProperty(name); - if (value == null) { - value = System.getenv(name); + if (StringUtils.isNotEmpty(value)) { + return value; } - } catch (SecurityException e) { - LOG.error("Get system config occur error: " + name, e); + } catch (Exception e) { + LOG.error("Get system property occur error: " + name, e); } + + try { + value = System.getenv(name); + if (StringUtils.isNotEmpty(value)) { + return value; + } + } catch (Exception e) { + LOG.error("Get system env occur error: " + name, e); + } + return value; } diff --git a/disjob-common/src/test/java/cn/ponfee/disjob/common/util/NetUtilsTest.java b/disjob-common/src/test/java/cn/ponfee/disjob/common/util/NetUtilsTest.java index 97ae9e7e6..1a1d0183a 100644 --- a/disjob-common/src/test/java/cn/ponfee/disjob/common/util/NetUtilsTest.java +++ b/disjob-common/src/test/java/cn/ponfee/disjob/common/util/NetUtilsTest.java @@ -73,6 +73,18 @@ void testIsValidLocalHost() { assertTrue(NetUtils.isValidLocalHost("128.0.0.1")); } + @Test + void testIsReachableHost() { + assertFalse(NetUtils.isReachableHost("1.2.3.4")); + assertFalse(NetUtils.isReachableHost("128.0.0.1")); + assertFalse(NetUtils.isReachableHost("127.0.0.1")); + assertFalse(NetUtils.isReachableHost("localhost")); + assertFalse(NetUtils.isReachableHost("www.baidfdsfffffffxxxxxdsfsdfdsuaaa.com")); + //assertTrue(NetUtils.isReachableHost("www.baidu.com")); + //assertTrue(NetUtils.isReachableHost("www.ponfee.cn")); + //assertTrue(NetUtils.isReachableHost("192.168.1.100")); + } + @Test @Disabled void testIsConnectable() { diff --git a/disjob-core/src/main/java/cn/ponfee/disjob/core/util/JobUtils.java b/disjob-core/src/main/java/cn/ponfee/disjob/core/util/JobUtils.java index a8114b475..4fd8b37a2 100644 --- a/disjob-core/src/main/java/cn/ponfee/disjob/core/util/JobUtils.java +++ b/disjob-core/src/main/java/cn/ponfee/disjob/core/util/JobUtils.java @@ -60,17 +60,17 @@ public static String getLocalHost(String specifiedHost) { } host = System.getProperty(JobConstants.DISJOB_BOUND_SERVER_HOST); - if (isValidHost(host, "jvm")) { + if (isValidHost(host, "System#getProperty")) { return host; } host = System.getenv(JobConstants.DISJOB_BOUND_SERVER_HOST); - if (isValidHost(host, "os")) { + if (isValidHost(host, "System#getenv")) { return host; } host = NetUtils.getLocalHost(); - if (isValidHost(host, "network")) { + if (isValidHost(host, "NetUtils#getLocalHost")) { return host; } @@ -81,11 +81,14 @@ private static boolean isValidHost(String host, String from) { if (StringUtils.isBlank(host)) { return false; } - if (NetUtils.isValidLocalHost(host)) { - return true; + if (!NetUtils.isValidLocalHost(host)) { + LOG.warn("Invalid server host configured {}: {}", from, host); + return false; + } + if (!NetUtils.isReachableHost(host)) { + LOG.warn("Unreachable server host configured {}: {}", from, host); } - LOG.warn("Invalid bound server host configured {}: {}", from, host); - return false; + return true; } }