upnp: better timeouts.

This commit is contained in:
Christopher Jeffrey 2017-03-02 11:52:35 -08:00
parent da5851ed51
commit 525e6f21b7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -45,16 +45,24 @@ function UPNP(host, port, gateway) {
UPNP.INTERNET_GATEWAY = 'urn:schemas-upnp-org:device:InternetGatewayDevice:1'; UPNP.INTERNET_GATEWAY = 'urn:schemas-upnp-org:device:InternetGatewayDevice:1';
/** /**
* Default service string. * Default service types.
* @const {String[]} * @const {String[]}
* @default * @default
*/ */
UPNP.SERVICES = [ UPNP.WAN_SERVICES = [
'urn:schemas-upnp-org:service:WANIPConnection:1', 'urn:schemas-upnp-org:service:WANIPConnection:1',
'urn:schemas-upnp-org:service:WANPPPConnection:1' 'urn:schemas-upnp-org:service:WANPPPConnection:1'
]; ];
/**
* Timeout before killing request.
* @const {Number}
* @default
*/
UPNP.RESPONSE_TIMEOUT = 3000;
/** /**
* Clean up current job. * Clean up current job.
* @private * @private
@ -110,7 +118,7 @@ UPNP.prototype.startTimeout = function startTimeout() {
this.timeout = setTimeout(function() { this.timeout = setTimeout(function() {
self.timeout = null; self.timeout = null;
self.rejectJob(new Error('Request timed out.')); 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; var res, xml, services, service;
if (!targets) if (!targets)
targets = UPNP.SERVICES; targets = UPNP.WAN_SERVICES;
res = yield request({ res = yield request({
method: 'GET', method: 'GET',
uri: location, uri: location,
timeout: UPNP.RESPONSE_TIMEOUT,
expect: 'xml' expect: 'xml'
}); });
@ -340,10 +349,7 @@ UPNP.getLocalIP = function getLocalIP() {
if (ip) if (ip)
return ip; return ip;
ip = UPNP.getInterfaceIP('private', 'ipv6'); return UPNP.getInterfaceIP('private', 'ipv6');
if (ip)
return ip;
}; };
/** /**
@ -357,10 +363,7 @@ UPNP.getPublicIP = function getPublicIP() {
if (ip) if (ip)
return ip; return ip;
ip = UPNP.getInterfaceIP('public', 'ipv6'); return UPNP.getInterfaceIP('public', 'ipv6');
if (ip)
return ip;
}; };
/** /**
@ -459,6 +462,7 @@ UPNPService.prototype.soapRequest = co(function* soapRequest(action, args) {
res = yield request({ res = yield request({
method: 'POST', method: 'POST',
uri: this.controlURL, uri: this.controlURL,
timeout: UPNP.RESPONSE_TIMEOUT,
expect: 'xml', expect: 'xml',
headers: { headers: {
'Content-Type': 'text/xml; charset="utf-8"', 'Content-Type': 'text/xml; charset="utf-8"',
@ -767,24 +771,21 @@ function findIP(el) {
function findError(el) { function findError(el) {
var child = el.find('UPnPError'); var child = el.find('UPnPError');
var code, desc; var code = -1;
var desc = 'Unknown';
var ccode, cdesc;
if (!child) if (!child)
return; return;
code = child.find('errorCode'); ccode = child.find('errorCode');
cdesc = child.find('errorDescription');
if (code && /^\d+$/.test(code.text)) if (ccode && /^\d+$/.test(ccode.text))
code = +code.text; code = +ccode.text;
else
code = -1;
desc = child.find('errorDescription'); if (cdesc)
desc = cdesc.text;
if (desc)
desc = desc.text;
else
desc = 'Unknown';
return new Error('UPnPError: ' + desc + ' (' + code + ')'); return new Error('UPnPError: ' + desc + ' (' + code + ')');
} }