import com.google.common.collect.Lists; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; import java.util.Enumeration; import java.util.List; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; /** * @author */ @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) public class IpUtil { private static final String LOCAL_IP = getLocalIp(); private static final String DEFAULT_LOCAL_IP = "127.0.0.1"; private static volatile List<String> NETWORK_INTERFACE_NAMES; public static String getOneLocalIP() { return LOCAL_IP; } private static String getLocalIp() { String osName = System.getProperty("os.name"); if (Lists.newArrayList("Linux", "LINUX").stream().filter(s -> s.startsWith(osName)).findAny().isPresent()) { return getLocalHostLANAddress(); } else { Enumeration<?> allNetInterfaces; try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { log.warn(e.getMessage(), e); return DEFAULT_LOCAL_IP; } while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); for (InetAddress address : Collections.list(addresses)) { if (address instanceof Inet4Address) { return address.getHostAddress(); } } } } return DEFAULT_LOCAL_IP; } private static String getLocalHostLANAddress() { if (NETWORK_INTERFACE_NAMES == null) { NETWORK_INTERFACE_NAMES = Lists.newArrayList("bond0", "eth0", "en0"); } try { InetAddress candidateAddress = null; for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { NetworkInterface iface = ifaces.nextElement(); if (!NETWORK_INTERFACE_NAMES.contains(iface.getDisplayName())) { continue; } for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) { InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) { return inetAddr.getHostAddress(); } else if (candidateAddress == null) { candidateAddress = inetAddr; } } } } if (candidateAddress != null) { return candidateAddress.getHostAddress(); } InetAddress jdkSuppliedAddress = InetAddress.getLocalHost(); if (jdkSuppliedAddress == null) { throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null."); } return jdkSuppliedAddress.getHostAddress(); } catch (Exception e) { UnknownHostException unknownHostException = new UnknownHostException( "Failed to determine LAN address: " + e); unknownHostException.initCause(e); log.error(e.getMessage(), e); } return DEFAULT_LOCAL_IP; } public static Long ip2Long(String ip) { byte bytes[] = getByteByIp(ip); if (EmptyUtils.isEmpty(bytes) || bytes.length < 4) { return 0L; } else { long l0 = bytes[0] & 255; long l1 = bytes[1] & 255; long l2 = bytes[2] & 255; long l3 = bytes[3] & 255; return l0 << 24 | l1 << 16 | l2 << 8 | l3; } } public static Long ip2LongReverse(String ip) { byte bytes[] = getByteByIp(ip); if (EmptyUtils.isEmpty(bytes) || bytes.length < 4) { return 0L; } else { long l0 = bytes[3] & 255; long l1 = bytes[2] & 255; long l2 = bytes[1] & 255; long l3 = bytes[0] & 255; return l0 << 24 | l1 << 16 | l2 << 8 | l3; } } private static byte[] getByteByIp(String ip) { InetAddress IPAddr = null; try { IPAddr = InetAddress.getByName(ip); } catch (UnknownHostException e) { throw new SysException(e); } if (IPAddr == null) { return null; } return IPAddr.getAddress(); } public static String long2Ip(long ip) { byte bytes[] = new byte[4]; bytes[3] = (byte) (int) ((ip >> 24) % 256L); bytes[2] = (byte) (int) ((ip >> 16) % 256L); bytes[1] = (byte) (int) ((ip >> 8) % 256L); bytes[0] = (byte) (int) (ip % 256L); InetAddress IPAddr = null; try { IPAddr = InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new SysException(e); } return IPAddr.getHostAddress(); } public static String long2IpReverse(long ip) { byte bytes[] = new byte[4]; bytes[0] = (byte) (int) ((ip >> 24) % 256L); bytes[1] = (byte) (int) ((ip >> 16) % 256L); bytes[2] = (byte) (int) ((ip >> 8) % 256L); bytes[3] = (byte) (int) (ip % 256L); InetAddress IPAddr = null; try { IPAddr = InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new SysException(e); } return IPAddr.getHostAddress(); } public static String long2IpGeo(long ip) { byte bytes[] = new byte[4]; bytes[0] = (byte) (int) ((ip >> 24) % 256L); bytes[1] = (byte) (int) ((ip >> 16) % 256L); bytes[2] = (byte) (int) ((ip >> 8) % 256L); bytes[3] = (byte) (int) (ip % 256L); InetAddress IPAddr = null; try { IPAddr = InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { throw new Exception(e); } return IPAddr.getHostAddress(); } }
IpUtil
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.