diff --git a/lib/net/upnp.js b/lib/net/upnp.js index 29f52015..95302ae9 100644 --- a/lib/net/upnp.js +++ b/lib/net/upnp.js @@ -9,7 +9,6 @@ var assert = require('assert'); var dgram = require('dgram'); var url = require('url'); -var os = require('os'); var request = require('../http/request'); var co = require('../utils/co'); var Lock = require('../utils/lock'); @@ -299,77 +298,6 @@ UPNP.parseHeader = function parseHeader(str) { return headers; }; -/** - * Get IP address from network interfaces. - * @param {String} name - `public` or `private`. - * @param {String} family - IP family name. - * @returns {String} - */ - -UPNP.getInterfaceIP = function getInterfaceIP(name, family) { - var interfaces = os.networkInterfaces(); - var keys = Object.keys(interfaces); - var i, j, key, items, details, type, ip; - - for (i = 0; i < keys.length; i++) { - key = keys[i]; - items = interfaces[key]; - - for (j = 0; j < items.length; j++) { - details = items[j]; - - type = details.family.toLowerCase(); - - if (type !== family) - continue; - - try { - ip = IP.toBuffer(details.address); - } catch (e) { - continue; - } - - if (IP.isLocal(ip)) - continue; - - if (name === 'public') { - if (!IP.isRoutable(ip)) - continue; - } - - return IP.toString(ip); - } - } -}; - -/** - * Get local IP from network interfaces. - * @returns {String} - */ - -UPNP.getLocalIP = function getLocalIP() { - var ip = UPNP.getInterfaceIP('private', 'ipv4'); - - if (ip) - return ip; - - return UPNP.getInterfaceIP('private', 'ipv6'); -}; - -/** - * Get public IP from network interfaces. - * @returns {String} - */ - -UPNP.getPublicIP = function getPublicIP() { - var ip = UPNP.getInterfaceIP('public', 'ipv4'); - - if (ip) - return ip; - - return UPNP.getInterfaceIP('public', 'ipv6'); -}; - /** * Discover gateway. * @param {String?} host - Multicast IP. @@ -512,7 +440,7 @@ UPNPService.prototype.getExternalIP = co(function* getExternalIP() { UPNPService.prototype.addPortMapping = co(function* addPortMapping(remote, src, dest) { var action = 'AddPortMapping'; - var local = UPNP.getLocalIP(); + var local = IP.getPrivate(); var xml, child; if (!local) diff --git a/lib/utils/ip.js b/lib/utils/ip.js index 13f2ebcb..cff7f2b5 100644 --- a/lib/utils/ip.js +++ b/lib/utils/ip.js @@ -12,6 +12,7 @@ 'use strict'; var assert = require('assert'); +var os = require('os'); var base32 = require('./base32'); /** @@ -981,6 +982,80 @@ IP.isEqual = function isEqual(a, b) { return true; }; +/** + * Get IP address from network interfaces. + * @param {String} name - `public` or `private`. + * @param {String} family - IP family name. + * @returns {String} + */ + +IP.getInterface = function getInterface(name, family) { + var interfaces = getInterfaces(); + var keys = Object.keys(interfaces); + var i, j, key, items, details, type, ip; + + for (i = 0; i < keys.length; i++) { + key = keys[i]; + items = interfaces[key]; + + for (j = 0; j < items.length; j++) { + details = items[j]; + + type = details.family.toLowerCase(); + + if (type !== family) + continue; + + if (details.internal) + continue; + + try { + ip = IP.toBuffer(details.address); + } catch (e) { + continue; + } + + if (IP.isLocal(ip)) + continue; + + if (name === 'public') { + if (!IP.isRoutable(ip)) + continue; + } + + return IP.toString(ip); + } + } +}; + +/** + * Get private IP from network interfaces. + * @returns {String} + */ + +IP.getPrivate = function getPrivate() { + var ip = IP.getInterface('private', 'ipv4'); + + if (ip) + return ip; + + return IP.getInterface('private', 'ipv6'); +}; + +/** + * Get public IP from network interfaces. + * @returns {String} + */ + +IP.getPublic = function getPublic() { + var ip = IP.getInterface('public', 'ipv4'); + + if (ip) + return ip; + + return IP.getInterface('public', 'ipv6'); +}; + /** * Represents a parsed address. * @constructor @@ -1005,6 +1080,16 @@ function Address(host, port, type, hostname, raw) { this.raw = raw || ZERO_IP; } +/* + * Helpers + */ + +function getInterfaces() { + if (os.unsupported) + return {}; + return os.networkInterfaces(); +} + /* * Expose */