From 536e0ddefbaf495ae3c0db527377215431c29d2b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 18:38:02 +0000 Subject: [PATCH] ONVIF behind NAT: use the reachable address for the camera's services and URIs Tested with the real camera reached as 2.54.237.28:8080 (port forward). It reports its services and URIs at its LAN address 192.168.200.118: GetCapabilities XAddrs http://192.168.200.118:8080/onvif/..., stream URIs rtsp://192.168.200.118:554/... toto.php - initialize(): a service URL (media, device, ptz) on another host than the one the device service was reached at is moved to that address, keeping its path (_reachableUri). GetVideoSources went to 192.168.200.118 and failed with "Couldn't connect", and the UI said "Credentials are Wrong!". A service on the same host is unchanged. doCameraOperation.php - Stream / snapshot URIs are split with parse_url (camMediaUri): protocol, port (scheme default if none) and path + query, whatever the host is. Before, only "proto://" was removed, so with the LAN host the whole URL was stored as the path. The two copies of that code are replaced by the helper. Real camera, request of the camera editor (CID=5, AU=7, CMD=3) through the real login (users table / session): before, status "error" (LAN address unreachable). Now status "ok", credentials, auth mode and PTZ token stored, 3 profiles: mainStream 1920x1080 /Streaming/Channels/101?..., subStream, thirdStream; snapshot http:8080 /onvif-http/snapshot?Profile_n. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/controller/doCameraOperation.php | 69 +++++++++++----------------- lib/php/onvif/toto.php | 22 +++++++-- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/lib/controller/doCameraOperation.php b/lib/controller/doCameraOperation.php index 23d061d..c008a5e 100644 --- a/lib/controller/doCameraOperation.php +++ b/lib/controller/doCameraOperation.php @@ -31,6 +31,25 @@ }; return array( $U, $P ); }; + // Stream / snapshot URI of the camera -> array( proto, port, path ): the camera may report its LAN address + // (192.168.x.y behind NAT / port forwarding) - the path is taken whatever the host is. Before, only + // "proto://" was cut off, so with another host the whole URL was stored as the path. + function camMediaUri( $U ){ + if( !is_string( $U ) || $U == '' ) return array( '', 0, '' ); + $H = parse_url( $U ); + if( $H === false || !isset( $H['scheme'] ) ) return array( '', 0, $U ); + $Proto = strtolower( $H['scheme'] ); + $Port = isset( $H['port'] ) ? intval( $H['port'] ) : 0; + if( $Port == 0 ){ + switch( $Proto ){ + case 'rtsp': $Port = 554; break; + case 'http': $Port = 80; break; + case 'https': $Port = 443; break; + }; + }; + $Path = ( isset( $H['path'] ) ? $H['path'] : '/' ) . ( isset( $H['query'] ) ? '?' . $H['query'] : '' ); + return array( $Proto, $Port, $Path ); + }; if( $CUser->Data['id'] > 1 ){ $Data = array(); $Data['status'] = 'na'; @@ -211,28 +230,10 @@ }; $mrl = $onvif->media_GetStreamUri($source['profiletoken']); $pic = $onvif->media_GetSnapshotUri($source['profiletoken']); - $TPArr = explode(':',$mrl); - $mrl_proto = $TPArr[0]; - $mrl_port = intval( $TPArr[2] ); - if( $mrl_port == 0 ) { - switch( $mrl_proto ){ - case 'rtsp': $mrl_port = 554; break; - case 'http': $mrl_port = 80; break; - case 'https': $mrl_port = 443; break; - }; - }; - $Emrl = $DATABASE->escape( $mrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP . ':' . $mrl_port, '', $Emrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP, '', $Emrl ); - $TPArr = explode(':',$pic ); - $pic_proto = $TPArr[0]; - if( count( $TPArr ) > 2 ){ - $pic_port = intval( $TPArr[2] ); - if($pic_port == 0 ) $pic_port = 80; - } else $pic_port = 80; - $Epic = $DATABASE->escape( $pic ); - $Epic = str_replace( $pic_proto . '://' . $IP . ':' . $pic_port, '', $Epic ); - $Epic = str_replace( $pic_proto . '://' . $IP , '', $Epic ); + list( $mrl_proto, $mrl_port, $Emrl ) = camMediaUri( $mrl ); + $Emrl = $DATABASE->escape( $Emrl ); + list( $pic_proto, $pic_port, $Epic ) = camMediaUri( $pic ); + $Epic = $DATABASE->escape( $Epic ); $QS = "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`compression_type`,`bitrate`,`fps`,`mrl`,`mrl_proto`,`mrl_port`,`picture_proto`,`picture_port`,`picture`) VALUES({$CameraID},'D',1,'{$Ename}','{$Etoken}',{$Ewidth},{$Eheight},'{$Eencode}',{$Ebitrate},{$Efps},'{$Emrl}','{$mrl_proto}','{$mrl_port}','{$pic_proto}','{$pic_port}','{$Epic}') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `source_type`='D', `valid`=1, `mrl`='{$Emrl}', `mrl_proto`='{$mrl_proto}', `mrl_port`='{$mrl_port}', `picture_proto`='{$pic_proto}', `picture_port`='{$pic_port}', `picture`='{$Epic}'"; $Data['ss'] .='Added'; $Data['QS'] = $QS; @@ -280,26 +281,10 @@ }; $mrl = $onvif->media_GetStreamUri($source['profiletoken']); $pic = $onvif->media_GetSnapshotUri($source['profiletoken']); - $TPArr = explode(':',$mrl); - $mrl_proto = $TPArr[0]; - $mrl_port = intval( $TPArr[2] ); - if( $mrl_port == 0 ) { - switch( $mrl_proto ){ - case 'rtsp': $mrl_port = 554; break; - case 'http': $mrl_port = 80; break; - case 'https': $mrl_port = 443; break; - }; - }; - $Emrl = $DATABASE->escape( $mrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP . ':' . $mrl_port, '', $Emrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP, '', $Emrl ); - $TPArr = explode(':',$pic ); - $pic_proto = $TPArr[0]; - $pic_port = intval( $TPArr[2] ); - if($pic_port == 0 ) $pic_port = 80; - $Epic = $DATABASE->escape( $pic ); - $Epic = str_replace( $pic_proto . '://' . $IP . ':' . $pic_port, '', $Epic ); - $Epic = str_replace( $pic_proto . '://' . $IP , '', $Epic ); + list( $mrl_proto, $mrl_port, $Emrl ) = camMediaUri( $mrl ); + $Emrl = $DATABASE->escape( $Emrl ); + list( $pic_proto, $pic_port, $Epic ) = camMediaUri( $pic ); + $Epic = $DATABASE->escape( $Epic ); $QS = "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`compression_type`,`bitrate`,`fps`,`mrl`,`mrl_proto`,`mrl_port`,`picture_proto`,`picture_port`,`picture`) VALUES({$CameraID},'D',1,'{$Ename}','{$Etoken}',{$Ewidth},{$Eheight},'{$Eencode}',{$Ebitrate},{$Efps},'{$Emrl}','{$mrl_proto}','{$mrl_port}','{$pic_proto}','{$pic_port}','{$Epic}') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `source_type`='D', `valid`=1, `mrl`='{$Emrl}', `mrl_proto`='{$mrl_proto}', `mrl_port`='{$mrl_port}', `picture_proto`='{$pic_proto}', `picture_port`='{$pic_port}', `picture`='{$Epic}'"; $DATABASE->Query( $QS, false ); }; diff --git a/lib/php/onvif/toto.php b/lib/php/onvif/toto.php index 1d95aca..18224ea 100644 --- a/lib/php/onvif/toto.php +++ b/lib/php/onvif/toto.php @@ -142,9 +142,9 @@ class Ponvif { if (!is_array($this->capabilities) || !isset($this->capabilities['Media']['XAddr'])) throw new Exception('GetCapabilities: no media service in the reply'); $onvifVersion=$this->_getOnvifVersion($this->capabilities); - $this->mediauri=$onvifVersion['media']; - $this->deviceuri=$onvifVersion['device']; - $this->ptzuri=$onvifVersion['ptz']; + $this->mediauri=$this->_reachableUri($onvifVersion['media']); + $this->deviceuri=$this->_reachableUri($onvifVersion['device']); + $this->ptzuri=$this->_reachableUri($onvifVersion['ptz']); preg_match("/^http(.*)onvif\//",$this->mediauri,$matches); $this->baseuri=isset($matches[0]) ? $matches[0] : ''; $this->onvifversion=array('major'=>$onvifVersion['major'],'minor'=>$onvifVersion['minor']); @@ -662,6 +662,22 @@ class Ponvif { return $version; } + /* + A camera behind NAT / port forwarding reports its services at its LAN address (e.g. + http://192.168.200.118:8080/onvif/media while it is reached as 2.54.237.28:8080): the next + request went to an address that cannot be reached and the UI said "Credentials are Wrong". + A service URL on another host is moved to the address the device service was reached at + (same scheme, host and port), keeping its path. A service on the same host is kept as is. + */ + protected function _reachableUri($url) { + if (!is_string($url) || $url=='') return $url; + $u=parse_url($url); + $r=parse_url('http://'.$this->ipaddress); + if ($u===false || $r===false || !isset($u['host']) || !isset($r['host'])) return $url; + if (strcasecmp($u['host'], $r['host'])==0) return $url; + return 'http://'.$this->ipaddress.(isset($u['path']) ? $u['path'] : '/').(isset($u['query']) ? '?'.$u['query'] : ''); + } + // Why the last request failed: " (HTTP 401: reply is not a SOAP envelope)" or the SOAP Fault reason protected function _faultText() { $r=$this->lastresponse;