diff --git a/src/main/java/io/antmedia/settings/ServerSettings.java b/src/main/java/io/antmedia/settings/ServerSettings.java index ed953e4f2..9fddfd751 100755 --- a/src/main/java/io/antmedia/settings/ServerSettings.java +++ b/src/main/java/io/antmedia/settings/ServerSettings.java @@ -4,10 +4,13 @@ import java.io.InputStream; import java.io.Serializable; import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.net.URL; import java.net.UnknownHostException; import java.nio.charset.Charset; import java.util.Collections; +import java.util.Enumeration; import java.util.LinkedList; import java.util.List; import java.util.Queue; @@ -331,6 +334,64 @@ public static String getGlobalHostAddress() return globalHostAddress; } + + public static Enumeration getNetworkInterfaces() throws SocketException { + return NetworkInterface.getNetworkInterfaces(); + } + + public static InetAddress getNoneLoopbackHostAddress() + { + + InetAddress noneLoopbackAddress = null; + Enumeration interfaces; + try + { + interfaces = getNetworkInterfaces(); + + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + + // Skip loopback and non-active interfaces + if (isLoopBackOrDown(networkInterface)) { + continue; + } + + noneLoopbackAddress = getAddress(networkInterface.getInetAddresses()); + + if (noneLoopbackAddress != null) + { + //break the outer loop to not to check other interfaces + break; + } + } + + } + catch (SocketException e) { + logger.error(ExceptionUtils.getStackTrace(e)); + } + + return noneLoopbackAddress; + + + } + + public static boolean isLoopBackOrDown(NetworkInterface networkInterface) throws SocketException { + return networkInterface.isLoopback() || !networkInterface.isUp(); + } + + public static InetAddress getAddress(Enumeration inetAddresses) { + while (inetAddresses.hasMoreElements()) + { + InetAddress address = inetAddresses.nextElement(); + //check if it's IPv4 address and not loopback + if (!address.isLoopbackAddress() && address.getAddress().length == 4) + { + logger.info("Non-loopback address: {}", address.getHostAddress()); + return address; + } + } + return null; + } public static String getLocalHostAddress() { @@ -341,7 +402,18 @@ public static String getLocalHostAddress() { * InetAddress.getLocalHost().getHostAddress() takes long time(5sec in macos) to return. * Let it is run once */ - localHostAddress = InetAddress.getLocalHost().getHostAddress(); + InetAddress noneLoopbackHostAddress = getNoneLoopbackHostAddress(); + if (noneLoopbackHostAddress != null) + { + logger.info("localhost address is set to none loopback address: {}", noneLoopbackHostAddress.getHostAddress()); + localHostAddress = noneLoopbackHostAddress.getHostAddress(); + } + else + { + + localHostAddress = InetAddress.getLocalHost().getHostAddress(); + logger.info("localhost address is set to default localhost address: {}", localHostAddress); + } } catch (UnknownHostException e) { logger.error(ExceptionUtils.getStackTrace(e)); } @@ -631,7 +703,4 @@ public void setAppIngestsSrtStreamsWithoutStreamId(String appIngestsSrtStreamsWi this.appIngestsSrtStreamsWithoutStreamId = appIngestsSrtStreamsWithoutStreamId; } - - - } diff --git a/src/test/java/io/antmedia/test/settings/ServerSettingsTest.java b/src/test/java/io/antmedia/test/settings/ServerSettingsTest.java index af8ce003a..2372fe1a1 100644 --- a/src/test/java/io/antmedia/test/settings/ServerSettingsTest.java +++ b/src/test/java/io/antmedia/test/settings/ServerSettingsTest.java @@ -2,11 +2,21 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; + +import org.apache.commons.lang3.exception.ExceptionUtils; import org.junit.Test; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.red5.server.scope.WebScope; import org.springframework.beans.factory.annotation.Value; @@ -176,4 +186,68 @@ public void testDefaultBeanSettings() { assertTrue(serverSettings.isOfflineLicense()); } + @Test + public void testNoneLoopbackHostAddress() { + + String localHostAddress = ServerSettings.getLocalHostAddress(); + assertNotNull(localHostAddress); + + //it should never return 127.0.0.1 address + assertNotEquals("127.0.0.1", localHostAddress); + assertNotEquals("127.0.1.1", localHostAddress); + + } + + @Test + public void testNoneLoopbackAddress() { + try { + + NetworkInterface networkInterface = Mockito.mock(NetworkInterface.class); + + assertTrue(ServerSettings.isLoopBackOrDown(networkInterface)); + + Mockito.when(networkInterface.isLoopback()).thenReturn(true); + assertTrue(ServerSettings.isLoopBackOrDown(networkInterface)); + + + Mockito.when(networkInterface.isUp()).thenReturn(true); + assertTrue(ServerSettings.isLoopBackOrDown(networkInterface)); + + + Mockito.when(networkInterface.isLoopback()).thenReturn(false); + + assertFalse(ServerSettings.isLoopBackOrDown(networkInterface)); + + + InetAddress inetAddress = Mockito.mock(InetAddress.class); + Enumeration inetAddresses = Collections.enumeration(Arrays.asList(inetAddress)); + + Mockito.when(inetAddress.getAddress()).thenReturn(new byte[6]); + assertNull(ServerSettings.getAddress(inetAddresses)); + + inetAddresses = Collections.enumeration(Arrays.asList(inetAddress)); + Mockito.when(inetAddress.isLoopbackAddress()).thenReturn(false); + assertNull(ServerSettings.getAddress(inetAddresses)); + + inetAddresses = Collections.enumeration(Arrays.asList(inetAddress)); + Mockito.when(inetAddress.getAddress()).thenReturn(new byte[4]); + assertNotNull(ServerSettings.getAddress(inetAddresses)); + + Mockito.when(inetAddress.isLoopbackAddress()).thenReturn(true); + Mockito.when(inetAddress.getAddress()).thenReturn(new byte[4]); + assertNull(ServerSettings.getAddress(inetAddresses)); + + Mockito.when(inetAddress.isLoopbackAddress()).thenReturn(true); + Mockito.when(inetAddress.getAddress()).thenReturn(new byte[5]); + assertNull(ServerSettings.getAddress(inetAddresses)); + + } + catch (Exception e) { + logger.error(ExceptionUtils.getStackTrace(e)); + fail(e.getMessage()); + } + } + + + }