diff --git a/sys/src/main/java/com/alibaba/nacos/sys/utils/InetUtils.java b/sys/src/main/java/com/alibaba/nacos/sys/utils/InetUtils.java index ca4bdba31..7462a5f04 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/utils/InetUtils.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/utils/InetUtils.java @@ -32,6 +32,7 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; @@ -192,12 +193,12 @@ public class InetUtils { for (Enumeration nics = NetworkInterface.getNetworkInterfaces(); nics.hasMoreElements(); ) { NetworkInterface ifc = nics.nextElement(); - if (ifc.isUp()) { + if (isUp(ifc)) { LOG.debug("Testing interface: " + ifc.getDisplayName()); - if (ifc.getIndex() < lowest || result == null) { - lowest = ifc.getIndex(); - } else { + if (ifc.getIndex() >= lowest && result != null) { continue; + } else { + lowest = ifc.getIndex(); } if (!ignoreInterface(ifc.getDisplayName())) { @@ -231,6 +232,20 @@ public class InetUtils { return null; } + /** + * check network intreface isUp, not throw SocketException. + * @param ifc network interface + * @return true or false; + */ + public static boolean isUp(NetworkInterface ifc) { + try { + return ifc.isUp(); + } catch (SocketException e) { + LOG.debug("Network interface can not get isUp, exception: ", e); + } + return false; + } + private static boolean isPreferredAddress(InetAddress address) { if (useOnlySiteLocalInterface) { final boolean siteLocalAddress = address.isSiteLocalAddress(); diff --git a/sys/src/test/java/com/alibaba/nacos/sys/utils/InetUtilsTest.java b/sys/src/test/java/com/alibaba/nacos/sys/utils/InetUtilsTest.java index c07fab524..e20ec3045 100644 --- a/sys/src/test/java/com/alibaba/nacos/sys/utils/InetUtilsTest.java +++ b/sys/src/test/java/com/alibaba/nacos/sys/utils/InetUtilsTest.java @@ -25,6 +25,8 @@ import org.junit.jupiter.api.Test; import org.springframework.mock.env.MockEnvironment; import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.util.concurrent.TimeUnit; import static com.alibaba.nacos.sys.env.Constants.NACOS_SERVER_IP; @@ -32,6 +34,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class InetUtilsTest { @@ -71,4 +75,17 @@ class InetUtilsTest { assertNotNull(address); assertFalse(address.isLoopbackAddress()); } + + @Test + void testisUp() throws SocketException { + NetworkInterface nic = mock(NetworkInterface.class); + when(nic.isUp()).thenReturn(true); + assertTrue(InetUtils.isUp(nic)); + + when(nic.isUp()).thenReturn(false); + assertFalse(InetUtils.isUp(nic)); + + when(nic.isUp()).thenThrow(new SocketException()); + assertFalse(InetUtils.isUp(nic)); + } }