cloud.tianai.rpc.common.util.IPUtils Maven / Gradle / Ivy
package cloud.tianai.rpc.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
@Slf4j
public class IPUtils {
private static final String LOCALHOST_IP = "127.0.0.1";
private static final String EMPTY_IP = "0.0.0.0";
private static final Pattern IP_PATTERN = Pattern.compile("[0-9]{1,3}(\\.[0-9]{1,3}){3,}");
public static String getIpV4() {
String ip = "";
Enumeration networkInterface;
try {
networkInterface = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
return ip;
}
Set ips = new HashSet();
while (networkInterface.hasMoreElements()) {
NetworkInterface ni = networkInterface.nextElement();
Enumeration inetAddress = null;
try {
if (null != ni) {
inetAddress = ni.getInetAddresses();
}
} catch (Exception e) {
}
while (null != inetAddress && inetAddress.hasMoreElements()) {
InetAddress ia = inetAddress.nextElement();
if (ia instanceof Inet6Address) {
continue; // ignore ipv6
}
String thisIp = ia.getHostAddress();
// 排除 回送地址
if (!ia.isLoopbackAddress() && !thisIp.contains(":") && !"127.0.0.1".equals(thisIp)) {
ips.add(thisIp);
if (StringUtils.isEmpty(ip)) {
ip = thisIp;
}
}
}
}
if (StringUtils.isEmpty(ip)) {
ip = "";
}
return ip;
}
public static String getHostIp() {
InetAddress address = getHostAddress();
return address == null ? null : address.getHostAddress();
}
public static InetAddress getHostAddress() {
InetAddress localAddress = null;
try {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
Enumeration addresses = network.getInetAddresses();
if (addresses != null) {
while (addresses.hasMoreElements()) {
try {
InetAddress address = addresses.nextElement();
if (isValidHostAddress(address)) {
return address;
}
} catch (Throwable e) {
log.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
}
}
} catch (Throwable e) {
log.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
}
}
} catch (Throwable e) {
log.warn("Failed to retriving network card ip address. cause:" + e.getMessage());
}
log.error("Could not get local host ip address, will use 127.0.0.1 instead.");
return localAddress;
}
private static boolean isValidHostAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress()) {
return false;
}
String name = address.getHostAddress();
return (name != null && !EMPTY_IP.equals(name) && !LOCALHOST_IP.equals(name) && IP_PATTERN.matcher(name)
.matches());
}
public static void main(String[] args) {
System.out.println(getHostIp());
}
}