Skip to content

Commit

Permalink
network
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Jan 28, 2024
1 parent 811abe8 commit 0c51c9a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
}
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package cn.ponfee.disjob.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 10 additions & 7 deletions disjob-core/src/main/java/cn/ponfee/disjob/core/util/JobUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

}

0 comments on commit 0c51c9a

Please sign in to comment.