package com.qianwen.core.launch.utils; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.UnknownHostException; import java.util.Enumeration; import org.springframework.util.StringUtils; public class INetUtil { public static final String LOCAL_HOST = "127.0.0.1"; public static String getHostName() { String hostname; try { InetAddress address = InetAddress.getLocalHost(); hostname = address.getHostName(); if (StringUtils.isEmpty(hostname)) { hostname = address.toString(); } } catch (UnknownHostException e) { hostname = LOCAL_HOST; } return hostname; } public static String getHostIp() { String hostAddress; try { InetAddress address = getLocalHostLANAddress(); hostAddress = address.getHostAddress(); if (StringUtils.isEmpty(hostAddress)) { hostAddress = address.toString(); } } catch (UnknownHostException e) { hostAddress = LOCAL_HOST; } return hostAddress; } private static InetAddress getLocalHostLANAddress() throws UnknownHostException { try { InetAddress candidateAddress = null; for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { NetworkInterface iface = ifaces.nextElement(); for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) { InetAddress inetAddr = inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) return inetAddr; if (candidateAddress == null) candidateAddress = inetAddr; } } } if (candidateAddress != null) return candidateAddress; InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); if (jdkSuppliedAddress == null) throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null."); return jdkSuppliedAddress; } catch (Exception e) { UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e); unknownHostException.initCause(e); throw unknownHostException; } } public static boolean tryPort(int port) { try (ServerSocket ignore = new ServerSocket(port)) { return true; } catch (Exception e) { return false; } } public static InetAddress getInetAddress(String ip) { try { return InetAddress.getByName(ip); } catch (UnknownHostException e) { return null; } } public static boolean isInternalIp(String ip) { return isInternalIp(getInetAddress(ip)); } public static boolean isInternalIp(InetAddress address) { if (isLocalIp(address)) return true; return isInternalIp(address.getAddress()); } public static boolean isLocalIp(InetAddress address) { return address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isSiteLocalAddress(); } public static boolean isInternalIp(byte[] addr) { byte b0 = addr[0]; byte b1 = addr[1]; byte section1 = 10; byte section2 = -84; byte section3 = 16; byte section4 = 31; byte section5 = -64; byte section6 = -88; switch (b0) { case 10: return true; case -84: if (b1 >= 16 && b1 <= 31) return true; case -64: if (b1 == -88) return true; break; } return false; } }