Skip to content

Commit fceebc5

Browse files
authored
Merge pull request #34 from thanhvg/nat-support
Add NAT support
2 parents 758d7e1 + 1492dcf commit fceebc5

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

lib/modules/device.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ function OnvifDevice(params) {
3737
this.xaddr = '';
3838
this.user = '';
3939
this.pass = '';
40+
this.keepAddr = false;
4041

4142
if(('xaddr' in params) && typeof(params['xaddr']) === 'string') {
4243
this.xaddr = params['xaddr'];
4344
let ourl = mUrl.parse(this.xaddr);
4445
this.address = ourl.hostname;
4546
} else if(('address' in params) && typeof(params['address']) === 'string') {
47+
this.keepAddr = true;
4648
this.address = params['address'];
47-
this.xaddr = 'http://' + this.address +':80/onvif/device_service';
49+
this.xaddr = 'http://' + this.address +'/onvif/device_service';
4850
} else {
4951
throw new Error('The parameter was invalid.');
5052
}
@@ -400,7 +402,7 @@ OnvifDevice.prototype._getCapabilities = function() {
400402
let events = c['Events'];
401403
if(events && events['XAddr']) {
402404
this.services.events = new mOnvifServiceEvents({
403-
'xaddr' : events['XAddr'],
405+
'xaddr' : this._getXaddr(events['XAddr']),
404406
'time_diff': this.time_diff,
405407
'user' : this.user,
406408
'pass' : this.pass
@@ -420,7 +422,7 @@ OnvifDevice.prototype._getCapabilities = function() {
420422
let media = c['Media'];
421423
if(media && media['XAddr']) {
422424
this.services.media = new mOnvifServiceMedia({
423-
'xaddr': media['XAddr'],
425+
'xaddr' : this._getXaddr(media['XAddr']),
424426
'time_diff': this.time_diff,
425427
'user' : this.user,
426428
'pass' : this.pass
@@ -429,7 +431,7 @@ OnvifDevice.prototype._getCapabilities = function() {
429431
let ptz = c['PTZ'];
430432
if(ptz && ptz['XAddr']) {
431433
this.services.ptz = new mOnvifServicePtz({
432-
'xaddr': ptz['XAddr'],
434+
'xaddr' : this._getXaddr(ptz['XAddr']),
433435
'time_diff': this.time_diff,
434436
'user' : this.user,
435437
'pass' : this.pass
@@ -601,6 +603,7 @@ OnvifDevice.prototype._mediaGetStreamURI = function() {
601603
this.services.media.getStreamUri(params, (error, result) => {
602604
if(!error) {
603605
let uri = result['data']['GetStreamUriResponse']['MediaUri']['Uri'];
606+
uri = this._getUri(uri);
604607
this.profile_list[profile_index]['stream'][protocol.toLowerCase()] = uri;
605608
}
606609
protocol_index ++;
@@ -632,8 +635,10 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() {
632635
this.services.media.getSnapshotUri(params, (error, result) => {
633636
if(!error) {
634637
try {
635-
profile['snapshot'] = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri'];
636-
} catch(e) {}
638+
let snapshotUri = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri'];
639+
snapshotUri = this._getSnapshotUri(snapshotUri);
640+
profile['snapshot'] = snapshotUri;
641+
} catch(e) { console.log(e);}
637642
}
638643
profile_index ++;
639644
getSnapshotUri();
@@ -647,4 +652,35 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() {
647652
return promise;
648653
};
649654

655+
OnvifDevice.prototype._getXaddr = function(directXaddr) {
656+
if (!this.keepAddr) return directXaddr;
657+
const path = mUrl.parse(directXaddr).path;
658+
return 'http://' + this.address + path;
659+
}
660+
661+
OnvifDevice.prototype._getUri = function(directUri) {
662+
if (!this.keepAddr) return directUri;
663+
const base = mUrl.parse('http://' + this.address);
664+
const parts = mUrl.parse(directUri);
665+
const newParts = {
666+
host: base.host,
667+
pathname: base.pathname + parts.pathname
668+
};
669+
const newUri = mUrl.format(newParts);
670+
return newUri;
671+
}
672+
673+
OnvifDevice.prototype._getSnapshotUri = function(directUri) {
674+
if (!this.keepAddr) return directUri;
675+
const base = mUrl.parse('http://' + this.address);
676+
const parts = mUrl.parse(directUri);
677+
const newParts = {
678+
protocol: parts.protocol,
679+
host: base.host,
680+
pathname: base.pathname + parts.pathname
681+
};
682+
const newUri = mUrl.format(newParts);
683+
return newUri;
684+
}
685+
650686
module.exports = OnvifDevice;

0 commit comments

Comments
 (0)