holepunch.js/lib/upnp.js

123 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-12-11 03:20:56 +00:00
'use strict';
2015-12-30 21:40:52 +00:00
var PromiseA = require('bluebird').Promise;
2015-12-30 08:22:04 +00:00
var natUpnp = require('holepunch-upnp');
2015-12-31 02:21:29 +00:00
var client;
2015-12-11 03:20:56 +00:00
2015-12-30 08:22:04 +00:00
function upnpForward(opts) {
2015-12-30 21:40:52 +00:00
if (opts.debug) {
console.log('[HP] [upnp] opts');
console.log(opts);
}
2015-12-31 02:21:29 +00:00
function useClient(client) {
2015-12-30 21:40:52 +00:00
if (opts.debug) {
console.log('[HP] [upnp] created client');
console.log(client);
}
2015-12-11 03:20:56 +00:00
return client.portMapping({
2015-12-30 08:22:04 +00:00
public: opts.external || opts.public || opts.internal
, private: opts.internal || opts.private || opts.public || opts.external
, ttl: opts.ttl || 0
}).then(function (result) {
if (opts.debug) {
2015-12-30 21:40:52 +00:00
console.log('[HP] [upnp] result');
2015-12-30 08:22:04 +00:00
console.log(result);
}
return result;
});
/*.then(function () {
2015-12-11 03:20:56 +00:00
var promitter = client.getMappings();
promitter.on('entry', function (entry, i) {
console.log('entry', i);
console.log(entry);
}).then(function (mappings) {
console.log('mappings');
console.log(mappings);
});
return promitter;
2015-12-30 08:22:04 +00:00
});*/
2015-12-31 02:21:29 +00:00
}
if (client) {
return useClient(client);
} else {
return natUpnp.createClient({ timeout: 3 * 1000 }).then(function (_client) {
client = _client;
useClient(client);
});
}
2015-12-30 08:22:04 +00:00
}
module.exports = function (args, ips, portInfo) {
// TODO ips.forEach
return upnpForward({
debug: args.debug
, private: portInfo.internal
2015-12-30 21:40:52 +00:00
, internal: portInfo.internal
2015-12-30 08:22:04 +00:00
, public: portInfo.external
2015-12-30 21:40:52 +00:00
, external: portInfo.external
2015-12-30 08:22:04 +00:00
// , localAddress: ip.localAddress
2015-12-11 03:20:56 +00:00
});
};
2015-12-30 08:22:04 +00:00
module.exports.upnpForward = upnpForward;
2015-12-11 03:20:56 +00:00
/*
client.portUnmapping({
public: 80
});
.findGateway().then(function (stuff) {
console.log('[a] gateway');
console.log(stuff.gateway);
console.log('[a] address');
console.log(stuff.address);
}).then(function () {
return client
*/
/*
client.getMappings({ local: true }, function(err, results) {
console.log('local mappings', results);
});
client.externalIp(function(err, ip) {
console.log('ext-ip', ip);
});
*/
function usage() {
console.warn("");
console.warn("node helpers/upnp-forward [public port] [private port] [ttl]");
console.warn("");
}
function run() {
var pubPort = parseInt(process.argv[2], 10) || 0;
var privPort = parseInt(process.argv[3], 10) || pubPort;
var ttl = parseInt(process.argv[4], 10) || 0;
var options = { public: pubPort, private: privPort, ttl: ttl };
if (!pubPort) {
usage();
return;
}
exports.upnpForward(options).then(function () {
console.log('done');
}).catch(function (err) {
2015-12-30 16:48:33 +00:00
console.error('[HP] Error: upnpForward:');
console.error(err.stack);
return PromiseA.reject(err);
2015-12-11 03:20:56 +00:00
});
}
if (require.main === module) {
run();
return;
}