From 525e6f21b75831f9c986acb70052d04705ec6c82 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 2 Mar 2017 11:52:35 -0800 Subject: [PATCH] upnp: better timeouts. --- lib/net/upnp.js | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/net/upnp.js b/lib/net/upnp.js index d385aa89..6fa802e3 100644 --- a/lib/net/upnp.js +++ b/lib/net/upnp.js @@ -45,16 +45,24 @@ function UPNP(host, port, gateway) { UPNP.INTERNET_GATEWAY = 'urn:schemas-upnp-org:device:InternetGatewayDevice:1'; /** - * Default service string. + * Default service types. * @const {String[]} * @default */ -UPNP.SERVICES = [ +UPNP.WAN_SERVICES = [ 'urn:schemas-upnp-org:service:WANIPConnection:1', 'urn:schemas-upnp-org:service:WANPPPConnection:1' ]; +/** + * Timeout before killing request. + * @const {Number} + * @default + */ + +UPNP.RESPONSE_TIMEOUT = 3000; + /** * Clean up current job. * @private @@ -110,7 +118,7 @@ UPNP.prototype.startTimeout = function startTimeout() { this.timeout = setTimeout(function() { self.timeout = null; self.rejectJob(new Error('Request timed out.')); - }, 2000); + }, UPNP.RESPONSE_TIMEOUT); }; /** @@ -217,11 +225,12 @@ UPNP.prototype.resolve = co(function* (location, targets) { var res, xml, services, service; if (!targets) - targets = UPNP.SERVICES; + targets = UPNP.WAN_SERVICES; res = yield request({ method: 'GET', uri: location, + timeout: UPNP.RESPONSE_TIMEOUT, expect: 'xml' }); @@ -340,10 +349,7 @@ UPNP.getLocalIP = function getLocalIP() { if (ip) return ip; - ip = UPNP.getInterfaceIP('private', 'ipv6'); - - if (ip) - return ip; + return UPNP.getInterfaceIP('private', 'ipv6'); }; /** @@ -357,10 +363,7 @@ UPNP.getPublicIP = function getPublicIP() { if (ip) return ip; - ip = UPNP.getInterfaceIP('public', 'ipv6'); - - if (ip) - return ip; + return UPNP.getInterfaceIP('public', 'ipv6'); }; /** @@ -459,6 +462,7 @@ UPNPService.prototype.soapRequest = co(function* soapRequest(action, args) { res = yield request({ method: 'POST', uri: this.controlURL, + timeout: UPNP.RESPONSE_TIMEOUT, expect: 'xml', headers: { 'Content-Type': 'text/xml; charset="utf-8"', @@ -767,24 +771,21 @@ function findIP(el) { function findError(el) { var child = el.find('UPnPError'); - var code, desc; + var code = -1; + var desc = 'Unknown'; + var ccode, cdesc; if (!child) return; - code = child.find('errorCode'); + ccode = child.find('errorCode'); + cdesc = child.find('errorDescription'); - if (code && /^\d+$/.test(code.text)) - code = +code.text; - else - code = -1; + if (ccode && /^\d+$/.test(ccode.text)) + code = +ccode.text; - desc = child.find('errorDescription'); - - if (desc) - desc = desc.text; - else - desc = 'Unknown'; + if (cdesc) + desc = cdesc.text; return new Error('UPnPError: ' + desc + ' (' + code + ')'); }