-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (34 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var network = require('network')
var natpmp = require('nat-pmp')
var natupnp = require('nat-upnp')
var ip = require('ip')
var async = require('async')
module.exports = function(opts, success, error) {
opts = opts || {}
opts.timeout = opts.timeout || 5000
// returns the IP of the gateway that your active network interface is linked to
network.get_gateway_ip(function(err, gateway) {
if (err) return error(err)
// use regex to check if ip address is public
if (ip.isPublic(gateway))
return success(gateway)
var strategies = {
pmp: natpmp.connect(gateway),
upnp: natupnp.createClient()
};
// find a strategy to traverse nat
async.detectSeries(strategies, function(client, next) {
var portMapping = async.timeout(client.portMapping.bind(client), opts.timeout)
portMapping(opts, function(err) {
next(null, !err)
});
}, function(err, client) {
if (err) return error(err)
if (!client) return error(new Error('All NAT strategies failed or timed out'))
client.externalIp(function(err, external) {
if (err) return error(err)
success(external)
})
})
})
}