Skip to content

Commit

Permalink
Merge branch 'master' into mp4-s3upload-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
USAMAWIZARD authored Jan 18, 2025
2 parents d8d0298 + 60e0f83 commit da96db3
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 4 deletions.
77 changes: 73 additions & 4 deletions src/main/java/io/antmedia/settings/ServerSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -331,6 +334,64 @@ public static String getGlobalHostAddress()

return globalHostAddress;
}

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException {
return NetworkInterface.getNetworkInterfaces();
}

public static InetAddress getNoneLoopbackHostAddress()
{

InetAddress noneLoopbackAddress = null;
Enumeration<NetworkInterface> 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<InetAddress> 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() {

Expand All @@ -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));
}
Expand Down Expand Up @@ -631,7 +703,4 @@ public void setAppIngestsSrtStreamsWithoutStreamId(String appIngestsSrtStreamsWi
this.appIngestsSrtStreamsWithoutStreamId = appIngestsSrtStreamsWithoutStreamId;
}




}
74 changes: 74 additions & 0 deletions src/test/java/io/antmedia/test/settings/ServerSettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InetAddress> 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());
}
}



}

0 comments on commit da96db3

Please sign in to comment.