From fc19c920b901cff509f2eb79819ab4bd9b5d586f Mon Sep 17 00:00:00 2001 From: Craine Runton Date: Tue, 24 May 2016 17:06:32 -0600 Subject: [PATCH] Add error handling if the connection to the remote server errors out (e.g. refused or timed out connections). --- get_cert_info.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/get_cert_info.js b/get_cert_info.js index 46dd5f7..381a415 100644 --- a/get_cert_info.js +++ b/get_cert_info.js @@ -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++; }