Add comments to module functions

This commit is contained in:
Craine Runton 2016-05-18 09:30:19 -06:00
parent a652237205
commit 411c504805
2 changed files with 57 additions and 4 deletions

View File

@ -24,9 +24,8 @@ Take a look at a live example page [here on GitLab][1]. Screenshot is below.
![Example dashboard](https://raw.githubusercontent.com/cmrunton/tls-dashboard/master/screenshot.png) ![Example dashboard](https://raw.githubusercontent.com/cmrunton/tls-dashboard/master/screenshot.png)
## TODO ## TODO
1. Comment the module functions 1. Separate the host_list into another file that can be read and versioned on its own (useful for large deployments, if you're managing 1,000s of certs)
2. Separate the host_list into another file that can be read and versioned on its own (useful for large deployments, if you're managing 1,000s of certs) 2. Error handling for when the connection fails (right now the script hangs/exits with an uncaught error)
3. Error handling for when the connection fails (right now the script hangs/exits with an uncaught error)
[1]:https://pages.runtondev.com/tls-dashboard/ [1]:https://pages.runtondev.com/tls-dashboard/

View File

@ -1,3 +1,10 @@
/**
* TLS Dashboard by Craine Runton
* Source: https://github.com/cmrunton/tls-dashboard
*
* See /LICENSE for licensing details
*/
var https = require('https'); var https = require('https');
const fs = require('fs'); const fs = require('fs');
@ -15,8 +22,16 @@ var host_list = [
var output = {}; var output = {};
var iteration = 1; var iteration = 1;
// Run the module
host_list.forEach(get_cert_parameters) host_list.forEach(get_cert_parameters)
/**
* Creates a connection to the host, and then reads the resulting peer certificate to extract the desired info
*
* @param {string} element The
* @param {int} index
* @param {array} array The
*/
function get_cert_parameters(element, index, array) { function get_cert_parameters(element, index, array) {
var options = { var options = {
host: element, host: element,
@ -47,19 +62,43 @@ function get_cert_parameters(element, index, array) {
}); });
req.end(); req.end();
}; };
/**
* Parses the date string passed to it and returs a new date object
*
* @param {string} date_string The human readble date string that needs to be parsed
*/
function parse_date(date_string) { function parse_date(date_string) {
var date = new Date(Date.parse(date_string)); var date = new Date(Date.parse(date_string));
return date; return date;
}; };
/**
* Takes a date string and returns the nuumber of days between now and the future date
*
* @param {string} date_string The human readble date string that needs to be parsed
*/
function get_days_left(date_string) { function get_days_left(date_string) {
var now = Date.now(); var now = Date.now();
var then = new Date(Date.parse(date_string)); var then = new Date(Date.parse(date_string));
var days_left = Math.round((then - now)/86400000); var days_left = Math.round((then - now)/86400000);
return days_left; return days_left;
}; };
/**
* Helper function to put the resolved/parsed cert info into the module output object
*
* @param {object} object Contains the parsed certificate info
* @param {string} host The name of the host that the certificate info is taken from
*/
function add_cert_details(object, host) { function add_cert_details(object, host) {
output[host] = object; output[host] = object;
}; };
/**
* Checks the iteration count. If the forEach has iterated over all the hosts, then call the write_results function,
* otherwise log the iteration to the console and increment the count
*/
function check_iterations() { function check_iterations() {
if (iteration === host_list.length) { if (iteration === host_list.length) {
write_results(); write_results();
@ -69,11 +108,26 @@ function check_iterations() {
iteration++; iteration++;
} }
}; };
/**
* Writes out the final object to a file, along with the run date to be used by the HTML page later
*/
function write_results() { function write_results() {
fs.writeFile(directory+file_name, 'var run_date = \''+run_date+'\'; \nvar cert_info = '+JSON.stringify(output, null, 2), function(err) { fs.writeFile(directory+file_name, 'var run_date = \''+run_date+'\'; \nvar cert_info = '+JSON.stringify(output, null, 2), function(err) {
// If the write errored out, notify // If the write errored out, notify
if (err) { if (err) {
console.log('Error writing file. \n'+stats); console.log('Error writing file. \n');
} }
}) })
};
/**
* Test function and used to write out the final iteration in green
*/
function assert(value, desc) {
if (value) {
console.log("\033[32m "+desc+"\033[0m");
} else {
console.log("\033[31m "+desc+"\033[0m");
}
}; };