一、前言
关于alibaba的dubbo源码包com.alibaba.dubbo.common.utils.NetUtils网络工具类,对主机host、网络地址InetSocketAddress及URL等相关转换处理,详情参见源码说明。
二、源码说明
package com.alibaba.dubbo.common.utils;@b@@b@import com.alibaba.dubbo.common.URL; @b@import java.io.IOException;@b@import java.net.InetAddress;@b@import java.net.InetSocketAddress;@b@import java.net.NetworkInterface;@b@import java.net.ServerSocket;@b@import java.net.UnknownHostException;@b@import java.util.Enumeration;@b@import java.util.Map;@b@import java.util.Random;@b@import java.util.regex.Pattern;@b@@b@public class NetUtils {@b@@b@ public static final String LOCALHOST = "127.0.0.1";@b@ public static final String ANYHOST = "0.0.0.0";@b@ private static final int RND_PORT_START = 30000;@b@@b@ private static final int RND_PORT_RANGE = 10000;@b@@b@ private static final Random RANDOM = new Random(System.currentTimeMillis());@b@ private static final int MIN_PORT = 0;@b@ private static final int MAX_PORT = 65535;@b@ private static final Pattern ADDRESS_PATTERN = Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$");@b@ private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$");@b@ private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");@b@ private static final Map<String, String> hostNameCache = new LRUCache<String, String>(1000);@b@ private static volatile InetAddress LOCAL_ADDRESS = null;@b@@b@ public static int getRandomPort() {@b@ return RND_PORT_START + RANDOM.nextInt(RND_PORT_RANGE);@b@ }@b@@b@ public static int getAvailablePort() {@b@ ServerSocket ss = null;@b@ try {@b@ ss = new ServerSocket();@b@ ss.bind(null);@b@ return ss.getLocalPort();@b@ } catch (IOException e) {@b@ return getRandomPort();@b@ } finally {@b@ if (ss != null) {@b@ try {@b@ ss.close();@b@ } catch (IOException e) {@b@ }@b@ }@b@ }@b@ }@b@@b@ public static int getAvailablePort(int port) {@b@ if (port <= 0) {@b@ return getAvailablePort();@b@ }@b@ for (int i = port; i < MAX_PORT; i++) {@b@ ServerSocket ss = null;@b@ try {@b@ ss = new ServerSocket(i);@b@ return i;@b@ } catch (IOException e) {@b@ // continue@b@ } finally {@b@ if (ss != null) {@b@ try {@b@ ss.close();@b@ } catch (IOException e) {@b@ }@b@ }@b@ }@b@ }@b@ return port;@b@ }@b@@b@ public static boolean isInvalidPort(int port) {@b@ return port <= MIN_PORT || port > MAX_PORT;@b@ }@b@@b@ public static boolean isValidAddress(String address) {@b@ return ADDRESS_PATTERN.matcher(address).matches();@b@ }@b@@b@ public static boolean isLocalHost(String host) {@b@ return host != null@b@ && (LOCAL_IP_PATTERN.matcher(host).matches()@b@ || host.equalsIgnoreCase("localhost"));@b@ }@b@@b@ public static boolean isAnyHost(String host) {@b@ return "0.0.0.0".equals(host);@b@ }@b@@b@ public static boolean isInvalidLocalHost(String host) {@b@ return host == null@b@ || host.length() == 0@b@ || host.equalsIgnoreCase("localhost")@b@ || host.equals("0.0.0.0")@b@ || (LOCAL_IP_PATTERN.matcher(host).matches());@b@ }@b@@b@ public static boolean isValidLocalHost(String host) {@b@ return !isInvalidLocalHost(host);@b@ }@b@@b@ public static InetSocketAddress getLocalSocketAddress(String host, int port) {@b@ return isInvalidLocalHost(host) ?@b@ new InetSocketAddress(port) : new InetSocketAddress(host, port);@b@ }@b@@b@ private static boolean isValidAddress(InetAddress address) {@b@ if (address == null || address.isLoopbackAddress())@b@ return false;@b@ String name = address.getHostAddress();@b@ return (name != null@b@ && !ANYHOST.equals(name)@b@ && !LOCALHOST.equals(name)@b@ && IP_PATTERN.matcher(name).matches());@b@ }@b@@b@ public static String getLocalHost() {@b@ InetAddress address = getLocalAddress();@b@ return address == null ? LOCALHOST : address.getHostAddress();@b@ }@b@@b@ public static String filterLocalHost(String host) {@b@ if (host == null || host.length() == 0) {@b@ return host;@b@ }@b@ if (host.contains("://")) {@b@ URL u = URL.valueOf(host);@b@ if (NetUtils.isInvalidLocalHost(u.getHost())) {@b@ return u.setHost(NetUtils.getLocalHost()).toFullString();@b@ }@b@ } else if (host.contains(":")) {@b@ int i = host.lastIndexOf(':');@b@ if (NetUtils.isInvalidLocalHost(host.substring(0, i))) {@b@ return NetUtils.getLocalHost() + host.substring(i);@b@ }@b@ } else {@b@ if (NetUtils.isInvalidLocalHost(host)) {@b@ return NetUtils.getLocalHost();@b@ }@b@ }@b@ return host;@b@ }@b@@b@ /**@b@ * Find first valid IP from local network card@b@ *@b@ * @return first valid local IP@b@ */@b@ public static InetAddress getLocalAddress() {@b@ if (LOCAL_ADDRESS != null)@b@ return LOCAL_ADDRESS;@b@ InetAddress localAddress = getLocalAddress0();@b@ LOCAL_ADDRESS = localAddress;@b@ return localAddress;@b@ }@b@@b@ public static String getLogHost() {@b@ InetAddress address = getLocalAddress();@b@ return address == null ? LOCALHOST : address.getHostAddress();@b@ }@b@@b@ private static InetAddress getLocalAddress0() {@b@ InetAddress localAddress = null;@b@ try {@b@ localAddress = InetAddress.getLocalHost();@b@ if (isValidAddress(localAddress)) {@b@ return localAddress;@b@ }@b@ } catch (Throwable e) {@b@ }@b@ try {@b@ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();@b@ if (interfaces != null) {@b@ while (interfaces.hasMoreElements()) {@b@ try {@b@ NetworkInterface network = interfaces.nextElement();@b@ Enumeration<InetAddress> addresses = network.getInetAddresses();@b@ if (addresses != null) {@b@ while (addresses.hasMoreElements()) {@b@ try {@b@ InetAddress address = addresses.nextElement();@b@ if (isValidAddress(address)) {@b@ return address;@b@ }@b@ } catch (Throwable e) {@b@ }@b@ }@b@ }@b@ } catch (Throwable e) {@b@ }@b@ }@b@ }@b@ } catch (Throwable e) {@b@ }@b@ return localAddress;@b@ }@b@@b@ public static String getHostName(String address) {@b@ try {@b@ int i = address.indexOf(':');@b@ if (i > -1) {@b@ address = address.substring(0, i);@b@ }@b@ String hostname = hostNameCache.get(address);@b@ if (hostname != null && hostname.length() > 0) {@b@ return hostname;@b@ }@b@ InetAddress inetAddress = InetAddress.getByName(address);@b@ if (inetAddress != null) {@b@ hostname = inetAddress.getHostName();@b@ hostNameCache.put(address, hostname);@b@ return hostname;@b@ }@b@ } catch (Throwable e) {@b@ // ignore@b@ }@b@ return address;@b@ }@b@@b@ /**@b@ * @param hostName@b@ * @return ip address or hostName if UnknownHostException@b@ */@b@ public static String getIpByHost(String hostName) {@b@ try {@b@ return InetAddress.getByName(hostName).getHostAddress();@b@ } catch (UnknownHostException e) {@b@ return hostName;@b@ }@b@ }@b@@b@ public static String toAddressString(InetSocketAddress address) {@b@ return address.getAddress().getHostAddress() + ":" + address.getPort();@b@ }@b@@b@ public static InetSocketAddress toAddress(String address) {@b@ int i = address.indexOf(':');@b@ String host;@b@ int port;@b@ if (i > -1) {@b@ host = address.substring(0, i);@b@ port = Integer.parseInt(address.substring(i + 1));@b@ } else {@b@ host = address;@b@ port = 0;@b@ }@b@ return new InetSocketAddress(host, port);@b@ }@b@@b@ public static String toURL(String protocol, String host, int port, String path) {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(protocol).append("://");@b@ sb.append(host).append(':').append(port);@b@ if (path.charAt(0) != '/')@b@ sb.append('/');@b@ sb.append(path);@b@ return sb.toString();@b@ }@b@@b@}