Add error handling if the connection to the remote server errors out (e.g. refused or timed out connections).

This commit is contained in:
Craine Runton 2016-05-24 17:06:32 -06:00
parent f11710f48a
commit fc19c920b9

View File

@ -50,10 +50,69 @@ function get_cert_parameters(element, index, array) {
'valid_to': parse_date(cert.valid_to),
'days_left': get_days_left(cert.valid_to)
}
}
};
add_cert_details(parsed, iteration);
check_iterations();
});
// Abort the request when a timeout event is emitted
req.on('timeout', function () {
this.abort();
});
// Handle errors generated by failed requests
req.on('error', function(e) {
// Increment the error count for the final output
errors++;
if (e.code ==='ECONNREFUSED') {
// The connection was refused by the server (ex. 443 not open, not resonding, etc.)
assert(false, 'Connection to '+element+' refused')
var parsed = {
'server': element,
'subject': {
'org': 'Unknown',
'common_name': 'Unknown',
'sans': 'Unknown'
},
'issuer': {
'org': 'Unknown',
'common_name': 'Last check connection refused'
},
'info': {
'days_left': '??'
}
};
add_cert_details(parsed, iteration);
check_iterations();
} else if (e.code ==='ECONNRESET') {
// The connection to the server timed out
assert(false, 'Connection to '+element+' timed out')
var parsed = {
'server': element,
'subject': {
'org': 'Unknown',
'common_name': 'Unknown',
'sans': 'Unknown'
},
'issuer': {
'org': 'Unknown',
'common_name': 'Last check timed out'
},
'info': {
'days_left': '??'
}
};
add_cert_details(parsed, iteration);
check_iterations();
}
})
// Set the timeout threshold for the https connection. Set in config.js, default 5000ms
req.setTimeout(config.connection_timeout);
// End the request
req.end();
};
@ -95,8 +154,8 @@ function add_cert_details(object, host) {
*/
function check_iterations() {
if (iteration === monitored_hosts.length) {
assert(true, 'Scanned '+iteration+' of '+monitored_hosts.length+' urls, with '+errors+' errors');
write_results();
assert(true, iteration+' of '+host_list.length+' urls complete.');
} else {
iteration++;
}