
com.hc360.rsf.common.utils.NetUtils Maven / Gradle / Ivy
The newest version!
/**
* NetUtils.java 2012-5-10
* Copyright(c) 2000-2012 HC360.COM, All Rights Reserved.
*/
package com.hc360.rsf.common.utils;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* IP and Port Helper for RPC,
*
*/
public class NetUtils {
private static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
public static final String LOCALHOST = "127.0.0.1";
public static final String ANYHOST = "0.0.0.0";
private static final int RND_PORT_START = 30000;
private static final int RND_PORT_RANGE = 10000;
private static final Random RANDOM = new Random(System.currentTimeMillis());
public static int getRandomPort() {
return RND_PORT_START + RANDOM.nextInt(RND_PORT_RANGE);
}
/**
* 取得空闲的端口
* @return
*/
public static int getAvailablePort() {
ServerSocket ss = null;
try {
ss = new ServerSocket();
ss.bind(null);
return ss.getLocalPort();
} catch (IOException e) {
return getRandomPort();
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
}
private static final int MIN_PORT = 0;
private static final int MAX_PORT = 65535;
/**
* 验证端口是否合法
* @param port
* @return
*/
public static boolean isInvalidPort(int port){
return port > MIN_PORT || port <= MAX_PORT;
}
private static final Pattern ADDRESS_PATTERN = Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$");
/**
* 验证地址的合法性
* @param address
* @return
*/
public static boolean isValidAddress(String address){
return ADDRESS_PATTERN.matcher(address).matches();
}
//127.*.*.*
private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$");
/**
* 是否是 本地地址 ,如127.0.0.1\ localhost
* @param host
* @return
*/
public static boolean isLocalHost(String host) {
return host != null
&& (LOCAL_IP_PATTERN.matcher(host).matches()
|| host.equalsIgnoreCase("localhost"));
}
public static boolean isAnyHost(String host) {
return "0.0.0.0".equals(host);
}
/**
* 是否是 无效的地址
* @param host
* @return
*/
public static boolean isInvalidLocalHost(String host) {
return host == null
|| host.length() == 0
|| host.equalsIgnoreCase("localhost")
|| host.equals("0.0.0.0")
|| (LOCAL_IP_PATTERN.matcher(host).matches());
}
/**
* 是否是 有效的地址
* @param host
* @return
*/
public static boolean isValidLocalHost(String host) {
return ! isInvalidLocalHost(host);
}
/**
* 取得本地地址
* @param host
* @param port
* @return
*/
public static InetSocketAddress getLocalSocketAddress(String host, int port) {
return isInvalidLocalHost(host) ?
new InetSocketAddress(port) : new InetSocketAddress(host, port);
}
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
/**
* 是否是 有效的地址
* @param address
* @return
*/
private static boolean isValidAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress())
return false;
String name = address.getHostAddress();
return (name != null
&& ! ANYHOST.equals(name)
&& ! LOCALHOST.equals(name)
&& IP_PATTERN.matcher(name).matches());
}
/**
* 取得本机IP
* @return
*/
public static String getLocalHost(){
InetAddress address = getLocalAddress();
return address == null ? LOCALHOST : address.getHostAddress();
}
public static String filterLocalHost(String host) {
if (NetUtils.isInvalidLocalHost(host)) {
return NetUtils.getLocalHost();
}
return host;
}
private static volatile InetAddress LOCAL_ADDRESS = null;
/**
* 遍历本地网卡,返回第一个合理的IP。
*
* @return 本地网卡IP
*/
public static InetAddress getLocalAddress() {
if (LOCAL_ADDRESS != null)
return LOCAL_ADDRESS;
InetAddress localAddress = getLocalAddress0();
LOCAL_ADDRESS = localAddress;
return localAddress;
}
public static String getLogHost() {
InetAddress address = LOCAL_ADDRESS;
return address == null ? LOCALHOST : address.getHostAddress();
}
/*
* 遍历本地网卡
* @return
*/
private static InetAddress getLocalAddress0() {
InetAddress localAddress = null;
try {
// 返回本地主机。
localAddress = InetAddress.getLocalHost();
if (isValidAddress(localAddress)) {
return localAddress;
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
try {
// 获取具有连接到此网络接口的所有子接口(也称虚拟接口)的 Enumeration。
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 (isValidAddress(address)) {
return address;
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
return localAddress;
}
private static final Map hostNameCache = new LRUCache(1000);
/**
* 取得主机名
* @param address
* @return
*/
public static String getHostName(String address) {
try {
int i = address.indexOf(':');
if (i > -1) {
address = address.substring(0, i);
}
String hostname = hostNameCache.get(address);
if (hostname != null && hostname.length() > 0) {
return hostname;
}
// 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
InetAddress inetAddress = InetAddress.getByName(address);
if (inetAddress != null) {
hostname = inetAddress.getHostName();
hostNameCache.put(address, hostname);
return hostname;
}
} catch (Throwable e) {
// ignore
}
return address;
}
/**
* 通过主机名取得IP
* @param hostName
* @return ip address or hostName if UnknownHostException
*/
public static String getIpByHost(String hostName) {
try{
return InetAddress.getByName(hostName).getHostAddress();
}catch (UnknownHostException e) {
return hostName;
}
}
/**
* InetSocketAddress转成String
* @param address
* @return
*/
public static String toAddressString(InetSocketAddress address) {
return address.getAddress().getHostAddress() + ":" + address.getPort();
}
/**
* String转InetSocketAddress
* @param address
* @return
*/
public static InetSocketAddress toAddress(String address) {
int i = address.indexOf(':');
String host;
int port;
if (i > -1) {
host = address.substring(0, i);
port = Integer.parseInt(address.substring(i + 1));
} else {
host = address;
port = 0;
}
return new InetSocketAddress(host, port);
}
/**
* 组装URL
* @param protocol
* @param host
* @param port
* @param path
* @return
*/
public static String toURL(String protocol, String host, int port, String path) {
StringBuilder sb = new StringBuilder();
sb.append(protocol).append("://");
sb.append(host).append(':').append(port);
if( path.charAt(0) != '/' )
sb.append('/');
sb.append(path);
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy