From 1651164d10f3eb9a65df7db1fa0b14968238777a Mon Sep 17 00:00:00 2001 From: Mustafa BOLEKEN Date: Mon, 24 Jul 2023 10:12:53 +0300 Subject: [PATCH 1/2] Keep https for onvif protocol --- .../io/antmedia/ipcamera/OnvifCamera.java | 17 +++++++++++- .../test/ipcamera/OnvifCameraTest.java | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java diff --git a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java index b55da62e0..f8c6c53fa 100755 --- a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java +++ b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java @@ -262,8 +262,23 @@ public String getURL (String url) { String[] ipAddrParts = null; String ipAddr = url; + // if protocol is https, return the url with https + if (url != null && url.startsWith("https://")) { + ipAddrParts = url.split("//"); + ipAddr = ipAddrParts[1]; + if (ipAddr != null) { + + if (ipAddr.contains("/")){ + ipAddrParts = ipAddr.split("/"); + ipAddr = ipAddrParts[0]; + } + logger.info("IP: {}", ipAddr); + } + return "https://" + ipAddr; + } + + // if protocol is not https, return ip address with port if(url != null && (url.startsWith(HTTP) || - url.startsWith("https://") || url.startsWith("rtmp://") || url.startsWith("rtmps://") || url.startsWith("rtsp://"))) { diff --git a/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java new file mode 100644 index 000000000..49b0e45e7 --- /dev/null +++ b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java @@ -0,0 +1,27 @@ +package io.antmedia.test.ipcamera; + +import io.antmedia.ipcamera.OnvifCamera; +import junit.framework.TestCase; +import org.junit.Assert; + +public class OnvifCameraTest extends TestCase { + + public void testGetURL() { + + OnvifCamera onvifCamera = new OnvifCamera(); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("http://192.168.1.1:8080")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("192.168.1.1:8080/test/test2")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("rtmp://192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("rtmps://192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("http://192.168.1.1:8080/test")); + + Assert.assertEquals("https://192.168.1.1:8080", onvifCamera.getURL("https://192.168.1.1:8080/test")); + } +} \ No newline at end of file From 576122cf451dbbab6e1d4544a1ca260d771920de Mon Sep 17 00:00:00 2001 From: mekya Date: Wed, 16 Aug 2023 12:18:54 +0300 Subject: [PATCH 2/2] Fix the IP camera connectivity if protocol is defined in the address --- .../io/antmedia/ipcamera/IOnvifCamera.java | 2 +- .../io/antmedia/ipcamera/OnvifCamera.java | 52 ++++++++++--------- .../antmedia/test/StreamFetcherUnitTest.java | 11 ++++ .../test/ipcamera/OnvifCameraTest.java | 33 +++++++++--- 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java b/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java index a37c6637b..2e6bbc8a3 100755 --- a/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java +++ b/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java @@ -10,7 +10,7 @@ public interface IOnvifCamera { /** * - * @param address + * @param address it can be direct ip address with port or it can start with http:// or https:// * @param username * @param password * @return true if connected diff --git a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java index f8c6c53fa..eee4ebc76 100755 --- a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java +++ b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java @@ -26,7 +26,9 @@ public class OnvifCamera implements IOnvifCamera { public static final int CONNECT_ERROR = -1; public static final int AUTHENTICATION_ERROR = -2; String profileToken; - private static final String HTTP = "http://"; + private static final String HTTP_PREFIX = "http://"; + private static final String HTTPS_PREFIX = "https://"; + protected static Logger logger = LoggerFactory.getLogger(OnvifCamera.class); @@ -37,9 +39,12 @@ public int connect(String address, String username, String password) { String camIP = ""; try { - camIP = getURL(address); + //address may include http:// or https:// so get the IPAddress directly + camIP = getIPAddress(address); - nvt = new OnvifDevice(camIP, username, password); + String protocol = getProtocol(address); + + nvt = new OnvifDevice(camIP, protocol, username, password); nvt.getSoap().setLogging(false); nvt.getDevices().getCapabilities().getDevice(); nvt.getDevices().getServices(false); @@ -75,6 +80,19 @@ public int connect(String address, String username, String password) { return result; } + public String getProtocol(String address) { + String protocol = null; + if (address.startsWith(HTTP_PREFIX)) + { + protocol = "http"; + } + else if (address.startsWith(HTTPS_PREFIX)) + { + protocol = "https"; + } + return protocol; + } + @Override public String[] getProfiles() { String profilesStr[] = null; @@ -257,37 +275,23 @@ public boolean setDateTime(java.sql.Date date, java.sql.Time time) { return false; } - public String getURL (String url) { + public String getIPAddress (String url) { String[] ipAddrParts = null; String ipAddr = url; - // if protocol is https, return the url with https - if (url != null && url.startsWith("https://")) { - ipAddrParts = url.split("//"); - ipAddr = ipAddrParts[1]; - if (ipAddr != null) { - - if (ipAddr.contains("/")){ - ipAddrParts = ipAddr.split("/"); - ipAddr = ipAddrParts[0]; - } - logger.info("IP: {}", ipAddr); - } - return "https://" + ipAddr; - } - - // if protocol is not https, return ip address with port - if(url != null && (url.startsWith(HTTP) || + if(url != null && (url.startsWith(HTTP_PREFIX) || + url.startsWith("https://") || url.startsWith("rtmp://") || url.startsWith("rtmps://") || - url.startsWith("rtsp://"))) { + url.startsWith("rtsp://"))) + { ipAddrParts = url.split("//"); ipAddr = ipAddrParts[1]; } - if (ipAddr != null) { - + if (ipAddr != null) + { if (ipAddr.contains("/")){ ipAddrParts = ipAddr.split("/"); ipAddr = ipAddrParts[0]; diff --git a/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java b/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java index ebefb76d9..cd0fc85b9 100644 --- a/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java +++ b/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java @@ -441,10 +441,21 @@ public void testOnvifError() { //it should be -1 because there is a connection error assertEquals(-1, connResult); + + + //Test with protocol + newCam.setIpAddr("http://127.0.0.1:8080"); + connResult = onvif.connect(newCam.getIpAddr(), newCam.getUsername(), newCam.getPassword()); + logger.info("connResult {}", connResult); + + //it should be 0 because URL and credentials are correct + assertEquals(0, connResult); stopCameraEmulator(); } + + diff --git a/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java index 49b0e45e7..7f185fa6e 100644 --- a/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java +++ b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java @@ -1,27 +1,44 @@ package io.antmedia.test.ipcamera; import io.antmedia.ipcamera.OnvifCamera; +import io.antmedia.ipcamera.onvif.soap.OnvifDevice; import junit.framework.TestCase; import org.junit.Assert; public class OnvifCameraTest extends TestCase { - public void testGetURL() { + public void testGetIPAddress() { OnvifCamera onvifCamera = new OnvifCamera(); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("http://192.168.1.1:8080")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080")); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("192.168.1.1:8080/test")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test")); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("192.168.1.1:8080/test/test2")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test/test2")); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("rtmp://192.168.1.1:8080/test")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmp://192.168.1.1:8080/test")); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("rtmps://192.168.1.1:8080/test")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmps://192.168.1.1:8080/test")); - Assert.assertEquals("192.168.1.1:8080", onvifCamera.getURL("http://192.168.1.1:8080/test")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080/test")); - Assert.assertEquals("https://192.168.1.1:8080", onvifCamera.getURL("https://192.168.1.1:8080/test")); + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("https://192.168.1.1:8080/test")); } + + + public void testGetPortAddress() { + + OnvifCamera onvifCamera = new OnvifCamera(); + + Assert.assertEquals("http", onvifCamera.getProtocol("http://192.168.1.1:8080")); + + Assert.assertEquals("https", onvifCamera.getProtocol("https://192.168.1.1:8080/test")); + + Assert.assertNull(onvifCamera.getProtocol("192.168.1.1:8080/test/test2")); + + } + + + } \ No newline at end of file