Comparing version 0.1.1 to 0.1.2
@@ -12,2 +12,16 @@ var nat = require('../nat-upnp'); | ||
function normalizeOptions(options) { | ||
function toObject(addr) { | ||
if (typeof addr === 'number') return { port: addr }; | ||
if (typeof addr === 'object') return addr; | ||
return {}; | ||
} | ||
return { | ||
remote: toObject(options.public), | ||
internal: toObject(options.private) | ||
}; | ||
} | ||
Client.prototype.portMapping = function portMapping(options, callback) { | ||
@@ -19,24 +33,11 @@ if (!callback) callback = function() {}; | ||
var remote = typeof options.public === 'number' ? | ||
{ port: options.public } | ||
: | ||
options.public, | ||
internal = typeof options.private === 'number' ? | ||
{ port: options.private } | ||
: | ||
options.private; | ||
var ports = normalizeOptions(options); | ||
if (typeof options.public === 'number') { | ||
remote = { port: options.public }; | ||
} else { | ||
remote = options.public; | ||
} | ||
gateway.run('AddPortMapping', [ | ||
['NewRemoteHost', remote.host], | ||
['NewExternalPort', remote.port], | ||
['NewRemoteHost', ports.remote.host], | ||
['NewExternalPort', ports.remote.port], | ||
['NewProtocol', options.protocol ? | ||
options.protocol.toUpperCase() : 'TCP'], | ||
['NewInternalPort', internal.port], | ||
['NewInternalClient', internal.host || address], | ||
['NewInternalPort', ports.internal.port], | ||
['NewInternalClient', ports.internal.host || address], | ||
['NewEnabled', 1], | ||
@@ -49,2 +50,19 @@ ['NewPortMappingDescription', options.description || 'node:nat:upnp'], | ||
Client.prototype.portUnmapping = function portMapping(options, callback) { | ||
if (!callback) callback = function() {}; | ||
this.findGateway(function(err, gateway, address) { | ||
if (err) return callback(err); | ||
var ports = normalizeOptions(options); | ||
gateway.run('DeletePortMapping', [ | ||
['NewRemoteHost', ports.remote.host], | ||
['NewExternalPort', ports.remote.port], | ||
['NewProtocol', options.protocol ? | ||
options.protocol.toUpperCase() : 'TCP'] | ||
], callback); | ||
}); | ||
}; | ||
Client.prototype.externalIp = function externalIp(callback) { | ||
@@ -51,0 +69,0 @@ this.findGateway(function(err, gateway, address) { |
{ | ||
"name": "nat-upnp", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"main": "lib/nat-upnp", | ||
@@ -5,0 +5,0 @@ "author": "Fedor Indutny <fedor@indutny.com>", |
@@ -13,3 +13,3 @@ # NAT UPnP | ||
client.portMapping({ | ||
public: 12345 | ||
public: 12345, | ||
private: 54321, | ||
@@ -21,2 +21,6 @@ ttl: 10 | ||
client.portUnmapping({ | ||
public: 12345 | ||
}); | ||
client.externalIp(function(err, ip) { | ||
@@ -23,0 +27,0 @@ }); |
@@ -11,5 +11,6 @@ var assert = require('assert'), | ||
it('should add port mapping', function(callback) { | ||
it('should add port mapping/unmapping', function(callback) { | ||
var public = ~~(Math.random() * 65536); | ||
c.portMapping({ | ||
public: ~~(Math.random() * 65536), | ||
public: public, | ||
private: ~~(Math.random() * 65536), | ||
@@ -19,3 +20,7 @@ ttl: 5 | ||
assert.equal(err, null); | ||
callback(); | ||
c.portUnmapping({ public: public }, function(err) { | ||
assert.equal(err, null); | ||
callback(); | ||
}); | ||
}); | ||
@@ -22,0 +27,0 @@ }); |
14825
400
52