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';
/**
* 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 + ')');
}