Fixs preferred address list to match properly

This commit is contained in:
Rostyslav Reznichenko
2018-03-06 13:09:19 +02:00
committed by Spencer Gibb
parent 70ff397188
commit afa2fc98fc
2 changed files with 46 additions and 20 deletions

View File

@@ -24,6 +24,7 @@ import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -99,7 +100,7 @@ public class InetUtils implements Closeable {
InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address
&& !address.isLoopbackAddress()
&& !ignoreAddress(address)) {
&& isPreferredAddress(address)) {
log.trace("Found non-loopback interface: "
+ ifc.getDisplayName());
result = address;
@@ -128,19 +129,26 @@ public class InetUtils implements Closeable {
return null;
}
/** for testing */ boolean ignoreAddress(InetAddress address) {
/** for testing */ boolean isPreferredAddress(InetAddress address) {
if (this.properties.isUseOnlySiteLocalInterfaces() && !address.isSiteLocalAddress()) {
log.trace("Ignoring address: " + address.getHostAddress());
if (this.properties.isUseOnlySiteLocalInterfaces()) {
final boolean siteLocalAddress = address.isSiteLocalAddress();
if (!siteLocalAddress) {
log.trace("Ignoring address: " + address.getHostAddress());
}
return siteLocalAddress;
}
final List<String> preferredNetworks = this.properties.getPreferredNetworks();
if (preferredNetworks.isEmpty()) {
return true;
}
for (String regex : this.properties.getPreferredNetworks()) {
if (!address.getHostAddress().matches(regex) && !address.getHostAddress().startsWith(regex)) {
log.trace("Ignoring address: " + address.getHostAddress());
for (String regex : preferredNetworks) {
final String hostAddress = address.getHostAddress();
if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) {
return true;
}
}
log.trace("Ignoring address: " + address.getHostAddress());
return false;
}

View File

@@ -18,8 +18,10 @@ package org.springframework.cloud.commons.util;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import static org.junit.Assert.assertFalse;
@@ -55,7 +57,7 @@ public class InetUtilsTests {
}
@Test
public void testHostInfo() throws Exception {
public void testHostInfo() {
try (InetUtils utils = new InetUtils(new InetUtilsProperties())) {
HostInfo info = utils.findFirstNonLoopbackHostInfo();
assertNotNull(info.getIpAddressAsInt());
@@ -92,30 +94,46 @@ public class InetUtilsTests {
properties.setUseOnlySiteLocalInterfaces(true);
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("192.168.0.1")));
assertFalse(utils.isPreferredAddress(InetAddress.getByName("5.5.8.1")));
}
}
@Test
public void testPrefferedNetworksRegex() throws Exception {
public void testPreferredNetworksRegex() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192.168.*"));
properties.setPreferredNetworks(Arrays.asList("192.168.*", "10.0.*"));
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("192.168.0.1")));
assertFalse(utils.isPreferredAddress(InetAddress.getByName("5.5.8.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("10.0.10.1")));
assertFalse(utils.isPreferredAddress(InetAddress.getByName("10.255.10.1")));
}
}
@Test
public void testPrefferedNetworksSimple() throws Exception {
public void testPreferredNetworksSimple() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192"));
properties.setPreferredNetworks(Arrays.asList("192", "10.0"));
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("192.168.0.1")));
assertFalse(utils.isPreferredAddress(InetAddress.getByName("5.5.8.1")));
assertFalse(utils.isPreferredAddress(InetAddress.getByName("10.255.10.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("10.0.10.1")));
}
}
@Test
public void testPreferredNetworksListIsEmpty() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Collections.emptyList());
try (InetUtils utils = new InetUtils(properties)) {
assertTrue(utils.isPreferredAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("5.5.8.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("10.255.10.1")));
assertTrue(utils.isPreferredAddress(InetAddress.getByName("10.0.10.1")));
}
}
}