Skip to content

Commit

Permalink
Revert "Merge pull request #3520, fix #538 polish the process of deci…
Browse files Browse the repository at this point in the history
…ding the ip to bind." (#3935)

This reverts commit ade0cd7
  • Loading branch information
beiwei30 authored and lexburner committed Apr 26, 2019
1 parent 2d0c07d commit b8d9f76
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ 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
Expand All @@ -115,7 +114,6 @@ 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);
}
Expand All @@ -126,6 +124,9 @@ 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()
Expand All @@ -152,10 +153,6 @@ 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.
Expand Down Expand Up @@ -235,17 +232,15 @@ public static InetAddress getLocalAddress() {
}

private static Optional<InetAddress> toValidAddress(InetAddress address) {
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 (address instanceof Inet6Address) {
Inet6Address v6Address = (Inet6Address) address;
if (isValidV6Address(v6Address)) {
return Optional.ofNullable(normalizeV6Address(v6Address));
}
}
if (isValidV4Address(address)) {
return Optional.of(address);
}
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ 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));
Expand All @@ -132,6 +133,7 @@ public void testGetLocalHost() throws Exception {
public void testGetLocalAddress() throws Exception {
InetAddress address = NetUtils.getLocalAddress();
assertNotNull(address);
assertTrue(NetUtils.isValidLocalHost(address.getHostAddress()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.Map;
import java.util.Properties;

import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;

/**
* ReferenceConfig
Expand Down Expand Up @@ -107,7 +108,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
* The interface class of the reference service
*/
private Class<?> interfaceClass;

/**
* client type
*/
Expand Down Expand Up @@ -303,6 +304,8 @@ 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
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;
Expand All @@ -62,6 +64,7 @@
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;

/**
Expand Down Expand Up @@ -624,20 +627,43 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> 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)) {
hostToBind = protocolConfig.getHost();
if (provider != null && StringUtils.isEmpty(hostToBind)) {
hostToBind = provider.getHost();
}

if (StringUtils.isEmpty(hostToBind)) {
if (isInvalidLocalHost(hostToBind)) {
anyhost = true;
hostToBind = getLocalHost();

if (StringUtils.isEmpty(hostToBind)) {
hostToBind = findHostToBindByConnectRegistries(registryURLs);
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();
}
}
}
}
Expand All @@ -646,7 +672,9 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> regist

// registry ip is not used for bind ip by default
String hostToRegistry = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY);
if (StringUtils.isEmpty(hostToRegistry)) {
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)) {
// bind ip is used as registry ip by default
hostToRegistry = hostToBind;
}
Expand All @@ -656,25 +684,6 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> regist
return hostToRegistry;
}

private String findHostToBindByConnectRegistries(List<URL> registryURLs) {
if (CollectionUtils.isNotEmpty(registryURLs)) {
for (URL registryURL : registryURLs) {
if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter(Constants.REGISTRY_KEY))) {
// 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
Expand Down

0 comments on commit b8d9f76

Please sign in to comment.