From a4ccf6b4961329749eeafc2845faf834db2ad84a Mon Sep 17 00:00:00 2001 From: Gia Thanh Vuong Date: Tue, 12 Jun 2018 16:22:55 -0600 Subject: [PATCH 1/3] add NAT support --- lib/modules/device.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/modules/device.js b/lib/modules/device.js index b0fe712..f5bc4ff 100644 --- a/lib/modules/device.js +++ b/lib/modules/device.js @@ -37,12 +37,14 @@ function OnvifDevice(params) { this.xaddr = ''; this.user = ''; this.pass = ''; + this.keepAddr = false; if(('xaddr' in params) && typeof(params['xaddr']) === 'string') { this.xaddr = params['xaddr']; let ourl = mUrl.parse(this.xaddr); this.address = ourl.hostname; } else if(('address' in params) && typeof(params['address']) === 'string') { + this.keepAddr = true; this.address = params['address']; this.xaddr = 'http://' + this.address +':80/onvif/device_service'; } else { @@ -400,7 +402,7 @@ OnvifDevice.prototype._getCapabilities = function() { let events = c['Events']; if(events && events['XAddr']) { this.services.events = new mOnvifServiceEvents({ - 'xaddr' : events['XAddr'], + 'xaddr' : this._getXaddr(events['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -420,7 +422,7 @@ OnvifDevice.prototype._getCapabilities = function() { let media = c['Media']; if(media && media['XAddr']) { this.services.media = new mOnvifServiceMedia({ - 'xaddr': media['XAddr'], + 'xaddr' : this._getXaddr(media['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -429,7 +431,7 @@ OnvifDevice.prototype._getCapabilities = function() { let ptz = c['PTZ']; if(ptz && ptz['XAddr']) { this.services.ptz = new mOnvifServicePtz({ - 'xaddr': ptz['XAddr'], + 'xaddr' : this._getXaddr(ptz['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -601,6 +603,7 @@ OnvifDevice.prototype._mediaGetStreamURI = function() { this.services.media.getStreamUri(params, (error, result) => { if(!error) { let uri = result['data']['GetStreamUriResponse']['MediaUri']['Uri']; + uri = this._getUri(uri); this.profile_list[profile_index]['stream'][protocol.toLowerCase()] = uri; } protocol_index ++; @@ -647,4 +650,22 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() { return promise; }; +OnvifDevice.prototype._getXaddr = function(directXaddr) { + if (!this.keepAddr) return directXaddr; + const path = mUrl.parse(directXaddr).path; + return 'http://' + this.address + path; +} + +OnvifDevice.prototype._getUri = function(directUri) { + if (!this.keepAddr) return directUri; + const base = mUrl.parse('http://' + this.address); + const parts = mUrl.parse(directUri); + const newParts = { + host: base.host, + pathname: base.pathname + parts.pathname + }; + const newUri = mUrl.format(newParts); + return newUri; +} + module.exports = OnvifDevice; From d7355edbc717482578c2b406a521872e62283156 Mon Sep 17 00:00:00 2001 From: Gia Thanh Vuong Date: Wed, 13 Jun 2018 09:33:49 -0600 Subject: [PATCH 2/3] remove :80 in xaddr, no need --- lib/modules/device.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/device.js b/lib/modules/device.js index f5bc4ff..06c0751 100644 --- a/lib/modules/device.js +++ b/lib/modules/device.js @@ -46,7 +46,7 @@ function OnvifDevice(params) { } else if(('address' in params) && typeof(params['address']) === 'string') { this.keepAddr = true; this.address = params['address']; - this.xaddr = 'http://' + this.address +':80/onvif/device_service'; + this.xaddr = 'http://' + this.address +'/onvif/device_service'; } else { throw new Error('The parameter was invalid.'); } From 1492dcfd0015f35c8c7a02a76995df16b8f05844 Mon Sep 17 00:00:00 2001 From: Gia Thanh Vuong Date: Wed, 13 Jun 2018 10:56:11 -0600 Subject: [PATCH 3/3] fix snapshot to use nat address --- lib/modules/device.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/modules/device.js b/lib/modules/device.js index 06c0751..4fb12db 100644 --- a/lib/modules/device.js +++ b/lib/modules/device.js @@ -635,8 +635,10 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() { this.services.media.getSnapshotUri(params, (error, result) => { if(!error) { try { - profile['snapshot'] = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri']; - } catch(e) {} + let snapshotUri = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri']; + snapshotUri = this._getSnapshotUri(snapshotUri); + profile['snapshot'] = snapshotUri; + } catch(e) { console.log(e);} } profile_index ++; getSnapshotUri(); @@ -668,4 +670,17 @@ OnvifDevice.prototype._getUri = function(directUri) { return newUri; } +OnvifDevice.prototype._getSnapshotUri = function(directUri) { + if (!this.keepAddr) return directUri; + const base = mUrl.parse('http://' + this.address); + const parts = mUrl.parse(directUri); + const newParts = { + protocol: parts.protocol, + host: base.host, + pathname: base.pathname + parts.pathname + }; + const newUri = mUrl.format(newParts); + return newUri; +} + module.exports = OnvifDevice;