Skip to content

Commit

Permalink
add NAT support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gia Thanh Vuong committed Jun 12, 2018
1 parent 758d7e1 commit a4ccf6b
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/modules/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ++;
Expand Down Expand Up @@ -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;

0 comments on commit a4ccf6b

Please sign in to comment.