From 6ff68a98551a74577773d6a126784c7a61a5d2b2 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Wed, 20 Feb 2019 17:06:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix=20issue#538:=20=E5=85=B3=E4=BA=8Edubbo?= =?UTF-8?q?=E4=B8=AD=E8=8E=B7=E5=8F=96=E6=9C=AC=E6=9C=BAIP=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/dubbo/common/utils/NetUtils.java | 25 +++++--- .../dubbo/config/AbstractInterfaceConfig.java | 3 - .../apache/dubbo/config/ReferenceConfig.java | 5 +- .../apache/dubbo/config/ServiceConfig.java | 61 ++++++++----------- .../remoting/transport/AbstractServer.java | 4 +- 5 files changed, 44 insertions(+), 54 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 9dc404e14e5..eae4574edfa 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -102,6 +102,7 @@ public static boolean isAnyHost(String host) { return Constants.ANYHOST_VALUE.equals(host); } + // FIXME: should remove this method completely public static boolean isInvalidLocalHost(String host) { return host == null || host.length() == 0 @@ -110,6 +111,7 @@ public static boolean isInvalidLocalHost(String host) { || (LOCAL_IP_PATTERN.matcher(host).matches()); } + // FIXME: should remove this method completely public static boolean isValidLocalHost(String host) { return !isInvalidLocalHost(host); } @@ -120,9 +122,6 @@ public static InetSocketAddress getLocalSocketAddress(String host, int port) { } static boolean isValidV4Address(InetAddress address) { - if (address == null || address.isLoopbackAddress()) { - return false; - } String name = address.getHostAddress(); return (name != null && IP_PATTERN.matcher(name).matches() @@ -149,6 +148,10 @@ static boolean isValidV6Address(Inet6Address address) { return false; } + static boolean isValidPublicAddress(InetAddress address) { + return !address.isSiteLocalAddress() && !address.isLoopbackAddress(); + } + /** * normalize the ipv6 Address, convert scope name to scope id. * e.g. @@ -219,14 +222,16 @@ public static InetAddress getLocalAddress() { } private static Optional toValidAddress(InetAddress address) { - if (address instanceof Inet6Address) { - Inet6Address v6Address = (Inet6Address) address; - if (isValidV6Address(v6Address)) { - return Optional.ofNullable(normalizeV6Address(v6Address)); + if (isValidPublicAddress(address)) { + if (address instanceof Inet6Address) { + Inet6Address v6Address = (Inet6Address) address; + if (isValidV6Address(v6Address)) { + return Optional.ofNullable(normalizeV6Address(v6Address)); + } + } + if (isValidV4Address(address)) { + return Optional.of(address); } - } - if (isValidV4Address(address)) { - return Optional.of(address); } return Optional.empty(); } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java index c787ceffae8..937ed377344 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java @@ -333,9 +333,6 @@ protected URL loadMonitor(URL registryURL) { String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY); if (StringUtils.isEmpty(hostToRegistry)) { hostToRegistry = NetUtils.getLocalHost(); - } else if (NetUtils.isInvalidLocalHost(hostToRegistry)) { - throw new IllegalArgumentException("Specified invalid registry ip from property:" + - Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); } map.put(Constants.REGISTER_IP_KEY, hostToRegistry); appendParameters(map, monitor); diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index 71d05739a1d..dcbd315e6aa 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -54,7 +54,6 @@ import java.util.Map; import java.util.Properties; -import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; /** * ReferenceConfig @@ -107,7 +106,7 @@ public class ReferenceConfig extends AbstractReferenceConfig { * The interface class of the reference service */ private Class interfaceClass; - + /** * client type */ @@ -298,8 +297,6 @@ private void init() { String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY); if (StringUtils.isEmpty(hostToRegistry)) { hostToRegistry = NetUtils.getLocalHost(); - } else if (isInvalidLocalHost(hostToRegistry)) { - throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); } map.put(Constants.REGISTER_IP_KEY, hostToRegistry); diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index af7605b2873..73fd83ffa51 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -43,11 +43,9 @@ import org.apache.dubbo.rpc.support.ProtocolUtils; import java.lang.reflect.Method; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -62,7 +60,6 @@ import static org.apache.dubbo.common.Constants.LOCALHOST_VALUE; import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort; import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; -import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost; import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort; /** @@ -602,9 +599,6 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist boolean anyhost = false; String hostToBind = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND); - if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) { - throw new IllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind); - } // if bind ip is not found in environment, keep looking up if (StringUtils.isEmpty(hostToBind)) { @@ -612,33 +606,13 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist if (provider != null && StringUtils.isEmpty(hostToBind)) { hostToBind = provider.getHost(); } - if (isInvalidLocalHost(hostToBind)) { + + if (StringUtils.isEmpty(hostToBind)) { anyhost = true; - try { - hostToBind = InetAddress.getLocalHost().getHostAddress(); - } catch (UnknownHostException e) { - logger.warn(e.getMessage(), e); - } - if (isInvalidLocalHost(hostToBind)) { - if (CollectionUtils.isNotEmpty(registryURLs)) { - for (URL registryURL : registryURLs) { - if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) { - // skip multicast registry since we cannot connect to it via Socket - continue; - } - try (Socket socket = new Socket()) { - SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort()); - socket.connect(addr, 1000); - hostToBind = socket.getLocalAddress().getHostAddress(); - break; - } catch (Exception e) { - logger.warn(e.getMessage(), e); - } - } - } - if (isInvalidLocalHost(hostToBind)) { - hostToBind = getLocalHost(); - } + hostToBind = getLocalHost(); + + if (StringUtils.isEmpty(hostToBind)) { + hostToBind = findHostToBindByConnectRegistries(registryURLs); } } } @@ -647,9 +621,7 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist // registry ip is not used for bind ip by default String hostToRegistry = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY); - if (hostToRegistry != null && hostToRegistry.length() > 0 && isInvalidLocalHost(hostToRegistry)) { - throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); - } else if (StringUtils.isEmpty(hostToRegistry)) { + if (StringUtils.isEmpty(hostToRegistry)) { // bind ip is used as registry ip by default hostToRegistry = hostToBind; } @@ -659,6 +631,25 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List regist return hostToRegistry; } + private String findHostToBindByConnectRegistries(List registryURLs) { + if (CollectionUtils.isNotEmpty(registryURLs)) { + for (URL registryURL : registryURLs) { + if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) { + // skip multicast registry since we cannot connect to it via Socket + continue; + } + try (Socket socket = new Socket()) { + SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort()); + socket.connect(addr, 1000); + return socket.getLocalAddress().getHostAddress(); + } catch (Exception e) { + logger.warn(e.getMessage(), e); + } + } + } + return null; + } + /** * Register port and bind port for the provider, can be configured separately * Configuration priority: environment variable -> java system properties -> port property in protocol config file diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java index 2020fee79fb..b6038d4c371 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java @@ -23,7 +23,7 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.store.DataStore; import org.apache.dubbo.common.utils.ExecutorUtil; -import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; @@ -53,7 +53,7 @@ public AbstractServer(URL url, ChannelHandler handler) throws RemotingException String bindIp = getUrl().getParameter(Constants.BIND_IP_KEY, getUrl().getHost()); int bindPort = getUrl().getParameter(Constants.BIND_PORT_KEY, getUrl().getPort()); - if (url.getParameter(Constants.ANYHOST_KEY, false) || NetUtils.isInvalidLocalHost(bindIp)) { + if (url.getParameter(Constants.ANYHOST_KEY, false) || StringUtils.isEmpty(bindIp)) { bindIp = Constants.ANYHOST_VALUE; } bindAddress = new InetSocketAddress(bindIp, bindPort); From 43335b60d2e77caff626975fd61aa413e2a031eb Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Fri, 22 Feb 2019 10:36:18 +0800 Subject: [PATCH 2/4] fix unit test --- .../test/java/org/apache/dubbo/common/utils/NetUtilsTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 682cc3a85a0..1e356e62902 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -104,7 +104,6 @@ public void testGetLocalSocketAddress() throws Exception { @Test public void testIsValidAddress() throws Exception { - assertFalse(NetUtils.isValidV4Address((InetAddress) null)); InetAddress address = mock(InetAddress.class); when(address.isLoopbackAddress()).thenReturn(true); assertFalse(NetUtils.isValidV4Address(address)); @@ -209,4 +208,4 @@ public void testNormalizeV6Address() { InetAddress normalized = NetUtils.normalizeV6Address(address); assertThat(normalized.getHostAddress(), equalTo("fe80:0:0:0:894:aeec:f37d:23e1%5")); } -} \ No newline at end of file +} From ce678f812443155e185a2e1c7f3d2e9b89e48fcf Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Fri, 22 Feb 2019 10:50:34 +0800 Subject: [PATCH 3/4] fix unit test --- .../test/java/org/apache/dubbo/common/utils/NetUtilsTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 1e356e62902..d193fe007d2 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -130,7 +130,6 @@ public void testGetLocalHost() throws Exception { public void testGetLocalAddress() throws Exception { InetAddress address = NetUtils.getLocalAddress(); assertNotNull(address); - assertTrue(NetUtils.isValidLocalHost(address.getHostAddress())); } @Test From 8d1b3d07c579422da8bfbbbac247434ae9a6b42c Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Fri, 22 Feb 2019 17:16:44 +0800 Subject: [PATCH 4/4] fix unit test --- .../org/apache/dubbo/remoting/transport/AbstractServer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java index b6038d4c371..2020fee79fb 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java @@ -23,7 +23,7 @@ import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.store.DataStore; import org.apache.dubbo.common.utils.ExecutorUtil; -import org.apache.dubbo.common.utils.StringUtils; +import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.ChannelHandler; import org.apache.dubbo.remoting.RemotingException; @@ -53,7 +53,7 @@ public AbstractServer(URL url, ChannelHandler handler) throws RemotingException String bindIp = getUrl().getParameter(Constants.BIND_IP_KEY, getUrl().getHost()); int bindPort = getUrl().getParameter(Constants.BIND_PORT_KEY, getUrl().getPort()); - if (url.getParameter(Constants.ANYHOST_KEY, false) || StringUtils.isEmpty(bindIp)) { + if (url.getParameter(Constants.ANYHOST_KEY, false) || NetUtils.isInvalidLocalHost(bindIp)) { bindIp = Constants.ANYHOST_VALUE; } bindAddress = new InetSocketAddress(bindIp, bindPort);