From 12ffcb2fa3c0c791c36b08bfb6cc11a53c642ce3 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 11:33:37 -0500 Subject: [PATCH 1/7] a little linting --- lib/nat-upnp/ssdp.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/nat-upnp/ssdp.js b/lib/nat-upnp/ssdp.js index 493ba05..947520f 100644 --- a/lib/nat-upnp/ssdp.js +++ b/lib/nat-upnp/ssdp.js @@ -20,7 +20,7 @@ function Ssdp() { // Create sockets on all external interfaces this.createSockets(); -}; +} util.inherits(Ssdp, EventEmitter); ssdp.create = function create() { @@ -55,7 +55,7 @@ Ssdp.prototype.search = function search(device, promise) { } // If promise was ended before binding - do not send queries - if (promise._ended) return; + if (promise._ended) { return; } var self = this, query = new Buffer('M-SEARCH * HTTP/1.1\r\n' + @@ -71,8 +71,8 @@ Ssdp.prototype.search = function search(device, promise) { }, this); function ondevice(info, address) { - if (promise._ended) return; - if (info.st !== device) return; + if (promise._ended) { return; } + if (info.st !== device) { return; } promise.emit('device', info, address); } @@ -93,7 +93,7 @@ Ssdp.prototype.createSocket = function createSocket(interface) { socket.on('message', function (message, info) { // Ignore messages after closing sockets - if (self._closed) return; + if (self._closed) { return; } // Parse response self.parseResponse(message.toString(), socket.address, info); @@ -103,7 +103,7 @@ Ssdp.prototype.createSocket = function createSocket(interface) { process.nextTick(function() { // Unqueue this._queue once all sockets are ready function onready() { - if (self._boundCount === self.sockets.length) return; + if (self._boundCount === self.sockets.length) { return; } self._bound = true; self._queue.forEach(function(item) { @@ -138,7 +138,7 @@ Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) { headers = response.slice(1); // Ignore incorrect packets - if (!/^(HTTP|NOTIFY)/.test(status)) return; + if (!/^(HTTP|NOTIFY)/.test(status)) { return; } // Parse headers from lines to hashmap headers = headers.reduce(function(headers, line) { @@ -149,7 +149,7 @@ Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) { }, {}); // We do not have interest in headers without location - if (!headers.location) return; + if (!headers.location) { return; } var interfaces = os.networkInterfaces(), routes = netroute.getInfo(), @@ -157,7 +157,7 @@ Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) { local = routes.IPv4.concat(routes.IPv6).filter(function(route) { if (route.gateway) { - if (ip.isEqual(remote.address, route.gateway)) return true; + if (ip.isEqual(remote.address, route.gateway)) { return true; } } // Remove /24 part from address From e5f53735e76ef76b0e7fabc45af6c7c721557953 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 15:35:52 -0500 Subject: [PATCH 2/7] remove netroute and extra ip checks --- lib/nat-upnp/ssdp.js | 86 ++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 64 deletions(-) diff --git a/lib/nat-upnp/ssdp.js b/lib/nat-upnp/ssdp.js index 947520f..7ec1c89 100644 --- a/lib/nat-upnp/ssdp.js +++ b/lib/nat-upnp/ssdp.js @@ -1,16 +1,15 @@ var dgram = require('dgram'), util = require('util'), os = require('os'), - net = require('net'), - netroute = require('netroute'), - ip = require('ip'), EventEmitter = require('events').EventEmitter; var ssdp = exports; -function Ssdp() { +function Ssdp(opts) { EventEmitter.call(this); + this._opts = opts || {}; + this._sourcePort = this._opts.sourcePort || 0; this.multicast = '239.255.255.250'; this.port = 1900; this._bound = false; @@ -27,6 +26,18 @@ ssdp.create = function create() { return new Ssdp(); }; +Ssdp.parseMimeHeader = function (headerStr) { + var lines = headerStr.split(/\r\n/g); + + // Parse headers from lines to hashmap + return lines.reduce(function(headers, line) { + line.replace(/^([^:]*)\s*:\s*(.*)$/, function (a, key, value) { + headers[key.toLowerCase()] = value; + }); + return headers; + }, {}); +}; + Ssdp.prototype.createSockets = function createSockets() { var self = this, interfaces = os.networkInterfaces(); @@ -125,76 +136,23 @@ Ssdp.prototype.createSocket = function createSocket(interface) { }); socket.address = interface.address; - socket.bind(self.port, interface.address); + socket.bind(self._sourcePort, interface.address); }); return socket; }; +// TODO create separate logic for parsing unsolicited upnp broadcasts, if and when that need arises Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) { - response = response.split(/\r\n/g); - - var status = response[0], - headers = response.slice(1); - // Ignore incorrect packets - if (!/^(HTTP|NOTIFY)/.test(status)) { return; } + if (!/^(HTTP|NOTIFY)/m.test(response)) { return; } - // Parse headers from lines to hashmap - headers = headers.reduce(function(headers, line) { - line.replace(/^([^:]*)\s*:\s*(.*)$/, function(a, key, value) { - headers[key.toLowerCase()] = value; - }); - return headers; - }, {}); - - // We do not have interest in headers without location - if (!headers.location) { return; } - - var interfaces = os.networkInterfaces(), - routes = netroute.getInfo(), - local; - - local = routes.IPv4.concat(routes.IPv6).filter(function(route) { - if (route.gateway) { - if (ip.isEqual(remote.address, route.gateway)) { return true; } - } - - // Remove /24 part from address - if (route.destination) { - route.destination = route.destination.replace(/\/\d+$/, ''); - } + var headers = Ssdp.parseMimeHeader(response); - if (net.isIP(route.destination) && route.netmask) { - return ip.isEqual(ip.mask(remote.address, route.netmask), - ip.mask(route.destination, route.netmask)); - } - if (net.isIP(route.destination)) { - return ip.isEqual(remote.address, route.destination) || - ip.isEqual(route.destination, '::'); - } - }).map(function(route) { - return interfaces[route.interface].filter(function(addr) { - return net.isIP(addr.address) === net.isIP(route.gateway) || - net.isIP(addr.address) === net.isIP(route.destination) || - net.isIP(addr.address) && net.isIP(route.netmask) && - net.isIP(route.gateway) && - ip.isEqual(ip.mask(addr.address, route.netmask), - ip.mask(route.gateway, route.netmask)); - }).map(function(addr) { - return addr.address; - }); - }).reduce(function(prev, next) { - return prev.concat(next); - }, []); - - if (local.length === 0) { - local = addr; - } else { - local = local[0]; - } + // We are only interested in messages that can be matched against the original search target + if (!headers.st) { return; } - this.emit('_device', headers, local); + this.emit('_device', headers, remote.address); }; Ssdp.prototype.close = function close() { From 56d8fb460be611f8ad1771302703ec72f6209155 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 15:47:15 -0500 Subject: [PATCH 3/7] jshinting --- lib/nat-upnp/client.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/nat-upnp/client.js b/lib/nat-upnp/client.js index a391501..9b85493 100644 --- a/lib/nat-upnp/client.js +++ b/lib/nat-upnp/client.js @@ -6,7 +6,7 @@ var client = exports; function Client() { this.ssdp = nat.ssdp.create(); this.timeout = 1800; -}; +} client.create = function create() { return new Client(); @@ -14,8 +14,8 @@ client.create = function create() { function normalizeOptions(options) { function toObject(addr) { - if (typeof addr === 'number') return { port: addr }; - if (typeof addr === 'object') return addr; + if (typeof addr === 'number') { return { port: addr }; } + if (typeof addr === 'object') { return addr; } return {}; } @@ -27,10 +27,10 @@ function normalizeOptions(options) { } Client.prototype.portMapping = function portMapping(options, callback) { - if (!callback) callback = function() {}; + if (!callback) { callback = function() {}; } this.findGateway(function(err, gateway, address) { - if (err) return callback(err); + if (err) { return callback(err); } var ports = normalizeOptions(options); @@ -50,10 +50,10 @@ Client.prototype.portMapping = function portMapping(options, callback) { }; Client.prototype.portUnmapping = function portMapping(options, callback) { - if (!callback) callback = function() {}; + if (!callback) { callback = function() {}; } - this.findGateway(function(err, gateway, address) { - if (err) return callback(err); + this.findGateway(function(err, gateway/*, address*/) { + if (err) { return callback(err); } var ports = normalizeOptions(options); @@ -72,10 +72,10 @@ Client.prototype.getMappings = function getMappings(options, callback) { options = null; } - if (!options) options = {}; + if (!options) { options = {}; } this.findGateway(function(err, gateway, address) { - if (err) return callback(err); + if (err) { return callback(err); } var i = 0, end = false, results = []; @@ -93,7 +93,7 @@ Client.prototype.getMappings = function getMappings(options, callback) { var key; Object.keys(data).some(function(k) { - if (!/:GetGenericPortMappingEntryResponse/.test(k)) return false; + if (!/:GetGenericPortMappingEntryResponse/.test(k)) { return false; } key = k; return true; @@ -111,7 +111,7 @@ Client.prototype.getMappings = function getMappings(options, callback) { port: parseInt(data.NewInternalPort, 10) }, protocol: data.NewProtocol.toLowerCase(), - enabled: data.NewEnabled == 1, + enabled: data.NewEnabled === 1, description: data.NewPortMappingDescription, ttl: parseInt(data.NewLeaseDuration, 10) }; @@ -122,7 +122,7 @@ Client.prototype.getMappings = function getMappings(options, callback) { callback(null); }); }, function(err) { - if (err) return callback(err); + if (err) { return callback(err); } if (options.local) { results = results.filter(function(item) { @@ -141,25 +141,25 @@ Client.prototype.getMappings = function getMappings(options, callback) { } callback(null, results); - }) + }); }); }; Client.prototype.externalIp = function externalIp(callback) { - this.findGateway(function(err, gateway, address) { - if (err) return callback(err); + this.findGateway(function(err, gateway/*, address*/) { + if (err) { return callback(err); } gateway.run('GetExternalIPAddress', [], function(err, data) { - if (err) return callback(err); + if (err) { return callback(err); } var key; Object.keys(data).some(function(k) { - if (!/:GetExternalIPAddressResponse$/.test(k)) return false; + if (!/:GetExternalIPAddressResponse$/.test(k)) { return false; } key = k; return true; }); - if (!key) return callback(Error('Incorrect response')); + if (!key) { return callback(Error('Incorrect response')); } callback(null, data[key].NewExternalIPAddress); }); }); @@ -178,8 +178,8 @@ Client.prototype.findGateway = function findGateway(callback) { callback(new Error('timeout')); }, this.timeout); - p.on('device', function(info, address) { - if (timeouted) return; + p.on('device', function (info, address) { + if (timeouted) { return; } p.emit('end'); clearTimeout(timeout); From 46abc9e95f129a979f164bebb40c8e3b4b9af19f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 16:25:42 -0500 Subject: [PATCH 4/7] fix logic error --- lib/nat-upnp/ssdp.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/nat-upnp/ssdp.js b/lib/nat-upnp/ssdp.js index 7ec1c89..261d3ae 100644 --- a/lib/nat-upnp/ssdp.js +++ b/lib/nat-upnp/ssdp.js @@ -114,18 +114,16 @@ Ssdp.prototype.createSocket = function createSocket(interface) { process.nextTick(function() { // Unqueue this._queue once all sockets are ready function onready() { - if (self._boundCount === self.sockets.length) { return; } - self._bound = true; + if (self._boundCount < self.sockets.length) { return; } + self._bound = true; self._queue.forEach(function(item) { - if (item.action === 'search') { - return self.search(item.device, item.promise); - } + return self[item.action](item.device, item.promise); }); } socket.on('listening', function() { - self._boundCount++; + self._boundCount += 1; onready(); }); From 1c49daece3d9b3ca766b81c8b386e7117fc08d24 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 22:19:15 -0500 Subject: [PATCH 5/7] add .jscsrc... whatever that is --- .jscsrc | 1281 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1281 insertions(+) create mode 100644 .jscsrc diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..37b8e18 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,1281 @@ + + + + + + + + + + + + bn.js/.jscsrc at master · indutny/bn.js · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Skip to content +
+ + + + + + + + + + + +
+
+
+ +
+
+
+ + + +

+ + /bn.js + + + + + +

+
+
+ +
+
+
+ + + +
+ +
+

HTTPS clone URL

+
+ + + + +
+
+ + +
+

Subversion checkout URL

+
+ + + + +
+
+ + + +

You can clone with + HTTPS or Subversion. + + + +

+ + + + + + Download ZIP + +
+
+ +
+ + + + + + +
+ +
+ + + branch: + master + + + +
+ +
+ + + + +
+ + +
+ + +
+
+ Fedor Indutny + + + +
+ + + +
+ +
+
+
+
+ 47 lines (44 sloc) + + 1.623 kb +
+
+
+ Raw + Blame + History +
+ + + + + + + +
+
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{
"disallowKeywordsOnNewLine": [ "else" ],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleLineStrings": true,
"disallowMultipleVarDecl": true,
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInCallExpression": true,
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"disallowYodaConditions": true,
+
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ],
"requireSpaceAfterLineComment": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeKeywords": [ "else", "catch" ],
"requireSpaceBeforeObjectValues": true,
"requireSpaceBetweenArguments": true,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionDeclaration": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"requireSpacesInsideArrayBrackets": "all",
"requireSpacesInsideObjectBrackets": "all",
"requireDotNotation": true,
+
"maximumLineLength": 80,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateParameterSeparator": ", ",
"validateQuoteMarks": "'"
}
+ +
+ +
+
+ +Jump to Line + + +
+ +
+ +
+
+ + +
+ +
+ +
+ + +
+
+
+ +
+
+
+
+
+ +
+ + + +
+ + + Something went wrong with that request. Please try again. +
+ + + + + + + + + + From fafcb5c45756fb2361695b132293274bcb22c6ae Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 22:29:12 -0500 Subject: [PATCH 6/7] add the correct .jscsrc --- .jscsrc | 1327 ++----------------------------------------------------- 1 file changed, 46 insertions(+), 1281 deletions(-) diff --git a/.jscsrc b/.jscsrc index 37b8e18..dbaae20 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,1281 +1,46 @@ - - - - - - - - - - - - bn.js/.jscsrc at master · indutny/bn.js · GitHub - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Skip to content -
- - - - - - - - - - - -
-
-
- -
-
-
- - - -

- - /bn.js - - - - - -

-
-
- -
-
-
- - - -
- -
-

HTTPS clone URL

-
- - - - -
-
- - -
-

Subversion checkout URL

-
- - - - -
-
- - - -

You can clone with - HTTPS or Subversion. - - - -

- - - - - - Download ZIP - -
-
- -
- - - - - - -
- -
- - - branch: - master - - - -
- -
- - - - -
- - -
- - -
-
- Fedor Indutny - - - -
- - - -
- -
-
-
-
- 47 lines (44 sloc) - - 1.623 kb -
-
-
- Raw - Blame - History -
- - - - - - - -
-
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{
"disallowKeywordsOnNewLine": [ "else" ],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleLineStrings": true,
"disallowMultipleVarDecl": true,
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInCallExpression": true,
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"disallowYodaConditions": true,
-
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ],
"requireSpaceAfterLineComment": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeKeywords": [ "else", "catch" ],
"requireSpaceBeforeObjectValues": true,
"requireSpaceBetweenArguments": true,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionDeclaration": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"requireSpacesInsideArrayBrackets": "all",
"requireSpacesInsideObjectBrackets": "all",
"requireDotNotation": true,
-
"maximumLineLength": 80,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateParameterSeparator": ", ",
"validateQuoteMarks": "'"
}
- -
- -
-
- -Jump to Line - - -
- -
- -
-
- - -
- -
- -
- - -
-
-
- -
-
-
-
-
- -
- - - -
- - - Something went wrong with that request. Please try again. -
- - - - - - - - - - +{ + "disallowKeywordsOnNewLine": [ "else" ], + "disallowMixedSpacesAndTabs": true, + "disallowMultipleLineStrings": true, + "disallowMultipleVarDecl": true, + "disallowNewlineBeforeBlockStatements": true, + "disallowQuotedKeysInObjects": true, + "disallowSpaceAfterObjectKeys": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowSpaceBeforePostfixUnaryOperators": true, + "disallowSpacesInCallExpression": true, + "disallowTrailingComma": true, + "disallowTrailingWhitespace": true, + "disallowYodaConditions": true, + + "requireCommaBeforeLineBreak": true, + "requireOperatorBeforeLineBreak": true, + "requireSpaceAfterBinaryOperators": true, + "requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ], + "requireSpaceAfterLineComment": true, + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceBeforeBlockStatements": true, + "requireSpaceBeforeKeywords": [ "else", "catch" ], + "requireSpaceBeforeObjectValues": true, + "requireSpaceBetweenArguments": true, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionDeclaration": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInConditionalExpression": true, + "requireSpacesInForStatement": true, + "requireSpacesInsideArrayBrackets": "all", + "requireSpacesInsideObjectBrackets": "all", + "requireDotNotation": true, + + "maximumLineLength": 80, + "validateIndentation": 2, + "validateLineBreaks": "LF", + "validateParameterSeparator": ", ", + "validateQuoteMarks": "'" +} From d02f5dc01a943bd92f1be8d9f3ec2413717a44ab Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 10 Feb 2015 23:54:27 -0500 Subject: [PATCH 7/7] back out curly braces --- lib/nat-upnp/client.js | 76 +++++++++++++++++++++--------------------- lib/nat-upnp/ssdp.js | 41 ++++++++++++----------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/lib/nat-upnp/client.js b/lib/nat-upnp/client.js index 9b85493..9627dc2 100644 --- a/lib/nat-upnp/client.js +++ b/lib/nat-upnp/client.js @@ -1,5 +1,5 @@ -var nat = require('../nat-upnp'), - async = require('async'); +var nat = require('../nat-upnp'); +var async = require('async'); var client = exports; @@ -14,8 +14,8 @@ client.create = function create() { function normalizeOptions(options) { function toObject(addr) { - if (typeof addr === 'number') { return { port: addr }; } - if (typeof addr === 'object') { return addr; } + if (typeof addr === 'number') return { port: addr }; + if (typeof addr === 'object') return addr; return {}; } @@ -27,41 +27,41 @@ function normalizeOptions(options) { } Client.prototype.portMapping = function portMapping(options, callback) { - if (!callback) { callback = function() {}; } + if (!callback) callback = function() {}; this.findGateway(function(err, gateway, address) { - if (err) { return callback(err); } + if (err) return callback(err); var ports = normalizeOptions(options); gateway.run('AddPortMapping', [ - ['NewRemoteHost', ports.remote.host], - ['NewExternalPort', ports.remote.port], - ['NewProtocol', options.protocol ? - options.protocol.toUpperCase() : 'TCP'], - ['NewInternalPort', ports.internal.port], - ['NewInternalClient', ports.internal.host || address], - ['NewEnabled', 1], - ['NewPortMappingDescription', options.description || 'node:nat:upnp'], - ['NewLeaseDuration', typeof options.ttl === 'number' ? - options.ttl : 60 * 30] + [ 'NewRemoteHost', ports.remote.host ], + [ 'NewExternalPort', ports.remote.port ], + [ 'NewProtocol', options.protocol ? + options.protocol.toUpperCase() : 'TCP' ], + [ 'NewInternalPort', ports.internal.port ], + [ 'NewInternalClient', ports.internal.host || address ], + [ 'NewEnabled', 1 ], + [ 'NewPortMappingDescription', options.description || 'node:nat:upnp' ], + [ 'NewLeaseDuration', typeof options.ttl === 'number' ? + options.ttl : 60 * 30 ] ], callback); }); }; Client.prototype.portUnmapping = function portMapping(options, callback) { - if (!callback) { callback = function() {}; } + if (!callback) callback = function() {}; this.findGateway(function(err, gateway/*, address*/) { - if (err) { return callback(err); } + 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'] + [ 'NewRemoteHost', ports.remote.host ], + [ 'NewExternalPort', ports.remote.port ], + [ 'NewProtocol', options.protocol ? + options.protocol.toUpperCase() : 'TCP' ] ], callback); }); }; @@ -72,19 +72,19 @@ Client.prototype.getMappings = function getMappings(options, callback) { options = null; } - if (!options) { options = {}; } + if (!options) options = {}; this.findGateway(function(err, gateway, address) { - if (err) { return callback(err); } - var i = 0, - end = false, - results = []; + if (err) return callback(err); + var i = 0; + var end = false; + var results = []; async.whilst(function() { return !end; }, function(callback) { gateway.run('GetGenericPortMappingEntry', [ - ['NewPortMappingIndex', ++i] + [ 'NewPortMappingIndex', ++i ] ], function(err, data) { if (err) { end = true; @@ -93,7 +93,7 @@ Client.prototype.getMappings = function getMappings(options, callback) { var key; Object.keys(data).some(function(k) { - if (!/:GetGenericPortMappingEntryResponse/.test(k)) { return false; } + if (!/:GetGenericPortMappingEntryResponse/.test(k)) return false; key = k; return true; @@ -122,7 +122,7 @@ Client.prototype.getMappings = function getMappings(options, callback) { callback(null); }); }, function(err) { - if (err) { return callback(err); } + if (err) return callback(err); if (options.local) { results = results.filter(function(item) { @@ -147,28 +147,28 @@ Client.prototype.getMappings = function getMappings(options, callback) { Client.prototype.externalIp = function externalIp(callback) { this.findGateway(function(err, gateway/*, address*/) { - if (err) { return callback(err); } + if (err) return callback(err); gateway.run('GetExternalIPAddress', [], function(err, data) { - if (err) { return callback(err); } + if (err) return callback(err); var key; Object.keys(data).some(function(k) { - if (!/:GetExternalIPAddressResponse$/.test(k)) { return false; } + if (!/:GetExternalIPAddressResponse$/.test(k)) return false; key = k; return true; }); - if (!key) { return callback(Error('Incorrect response')); } + if (!key) return callback(Error('Incorrect response')); callback(null, data[key].NewExternalIPAddress); }); }); }; Client.prototype.findGateway = function findGateway(callback) { - var timeout, - timeouted = false, - p = this.ssdp.search( + var timeout; + var timeouted = false; + var p = this.ssdp.search( 'urn:schemas-upnp-org:device:InternetGatewayDevice:1' ); @@ -179,7 +179,7 @@ Client.prototype.findGateway = function findGateway(callback) { }, this.timeout); p.on('device', function (info, address) { - if (timeouted) { return; } + if (timeouted) return; p.emit('end'); clearTimeout(timeout); diff --git a/lib/nat-upnp/ssdp.js b/lib/nat-upnp/ssdp.js index 261d3ae..d5d200b 100644 --- a/lib/nat-upnp/ssdp.js +++ b/lib/nat-upnp/ssdp.js @@ -1,8 +1,7 @@ -var dgram = require('dgram'), - util = require('util'), - os = require('os'), - EventEmitter = require('events').EventEmitter; - +var dgram = require('dgram'); +var util = require('util'); +var os = require('os'); +var EventEmitter = require('events').EventEmitter; var ssdp = exports; function Ssdp(opts) { @@ -39,8 +38,8 @@ Ssdp.parseMimeHeader = function (headerStr) { }; Ssdp.prototype.createSockets = function createSockets() { - var self = this, - interfaces = os.networkInterfaces(); + var self = this; + var interfaces = os.networkInterfaces(); this.sockets = Object.keys(interfaces).reduce(function(a, key) { return a.concat(interfaces[key].filter(function(item) { @@ -66,10 +65,10 @@ Ssdp.prototype.search = function search(device, promise) { } // If promise was ended before binding - do not send queries - if (promise._ended) { return; } + if (promise._ended) return; - var self = this, - query = new Buffer('M-SEARCH * HTTP/1.1\r\n' + + var self = this; + var query = new Buffer('M-SEARCH * HTTP/1.1\r\n' + 'HOST: ' + this.multicast + ':' + this.port + '\r\n' + 'MAN: "ssdp:discover"\r\n' + 'MX: 1\r\n' + @@ -82,8 +81,8 @@ Ssdp.prototype.search = function search(device, promise) { }, this); function ondevice(info, address) { - if (promise._ended) { return; } - if (info.st !== device) { return; } + if (promise._ended) return; + if (info.st !== device) return; promise.emit('device', info, address); } @@ -98,13 +97,13 @@ Ssdp.prototype.search = function search(device, promise) { }; Ssdp.prototype.createSocket = function createSocket(interface) { - var self = this, - socket = dgram.createSocket(interface.family === 'IPv4' ? + var self = this; + var socket = dgram.createSocket(interface.family === 'IPv4' ? 'udp4' : 'udp6'); socket.on('message', function (message, info) { // Ignore messages after closing sockets - if (self._closed) { return; } + if (self._closed) return; // Parse response self.parseResponse(message.toString(), socket.address, info); @@ -114,7 +113,7 @@ Ssdp.prototype.createSocket = function createSocket(interface) { process.nextTick(function() { // Unqueue this._queue once all sockets are ready function onready() { - if (self._boundCount < self.sockets.length) { return; } + if (self._boundCount < self.sockets.length) return; self._bound = true; self._queue.forEach(function(item) { @@ -140,15 +139,17 @@ Ssdp.prototype.createSocket = function createSocket(interface) { return socket; }; -// TODO create separate logic for parsing unsolicited upnp broadcasts, if and when that need arises +// TODO create separate logic for parsing unsolicited upnp broadcasts, +// if and when that need arises Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) { // Ignore incorrect packets - if (!/^(HTTP|NOTIFY)/m.test(response)) { return; } + if (!/^(HTTP|NOTIFY)/m.test(response)) return; var headers = Ssdp.parseMimeHeader(response); - // We are only interested in messages that can be matched against the original search target - if (!headers.st) { return; } + // We are only interested in messages that can be matched against the original + // search target + if (!headers.st) return; this.emit('_device', headers, remote.address); };