first commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Checks that conf.yml is present + parsable, then validates it against the schema
|
||||
* Prints detailed info about any errors or warnings to help the user fix the issue
|
||||
*/
|
||||
|
||||
const fs = require('fs'); // For opening + reading files
|
||||
const yaml = require('js-yaml'); // For parsing YAML
|
||||
const Ajv = require('ajv'); // For validating with schema
|
||||
|
||||
const schema = require('../src/utils/ConfigSchema.json');
|
||||
|
||||
/* Tell AJV to use strict mode, and report all errors */
|
||||
const validatorOptions = {
|
||||
strict: true,
|
||||
allowUnionTypes: true,
|
||||
allErrors: true,
|
||||
};
|
||||
|
||||
/* Initiate AJV validator */
|
||||
const ajv = new Ajv(validatorOptions);
|
||||
|
||||
/* Message printed when validation was successful */
|
||||
const successMsg = () => '\x1b[1m\x1b[32m✔️ Config file is valid, no issues found\x1b[0m\n';
|
||||
|
||||
/* Just a wrapper to system's console.log */
|
||||
const logToConsole = (msg) => { console.log(msg || '\n'); }; // eslint-disable-line no-console
|
||||
|
||||
/* Formats error message. ready for printing to the console */
|
||||
const errorMsg = (output) => {
|
||||
const warningFont = '\x1b[103m\x1b[34m';
|
||||
const line = `${warningFont}${new Array(42).fill('━').join('')}\x1b[0m`;
|
||||
const formatParams = (params) => {
|
||||
if (params.additionalProperty) return `(${params.additionalProperty})`;
|
||||
return '';
|
||||
};
|
||||
let msg = `\n${line}\n${warningFont} Warning: ${output.length} `
|
||||
+ `issue${output.length > 1 ? 's' : ''} found in config file \x1b[0m\n${line}\n`;
|
||||
output.forEach((details, index) => {
|
||||
msg += `${'\x1b[36m'}${index + 1}. \x1b[4m${details.instancePath}\x1b[0m\x1b[36m `
|
||||
+ `${details.message} ${formatParams(details.params)}\x1b[0m\n`;
|
||||
});
|
||||
return msg;
|
||||
};
|
||||
|
||||
/* Sets valid status as environmental variable */
|
||||
const setIsValidVariable = (isValid) => {
|
||||
process.env.VUE_APP_CONFIG_VALID = isValid;
|
||||
};
|
||||
|
||||
/* Start the validation */
|
||||
const validate = (config) => {
|
||||
logToConsole('\nChecking config file against schema...');
|
||||
const valid = ajv.validate(schema, config);
|
||||
if (valid) {
|
||||
setIsValidVariable(true);
|
||||
logToConsole(successMsg());
|
||||
} else {
|
||||
setIsValidVariable(false);
|
||||
logToConsole(errorMsg(ajv.errors));
|
||||
}
|
||||
};
|
||||
|
||||
/* Error message printed when the file could not be opened */
|
||||
const bigError = () => {
|
||||
const formatting = '\x1b[30m\x1b[43m';
|
||||
const line = `${formatting}${new Array(38).fill('━').join('')}\x1b[0m\n`;
|
||||
const msg = `${formatting} Error, unable to validate 'conf.yml' \x1b[0m\n`;
|
||||
return `\n${line}${msg}${line}`;
|
||||
};
|
||||
|
||||
/* Given an error object, prints helpful info to the user */
|
||||
const printFileReadError = (e) => {
|
||||
let customError = '';
|
||||
if (e.mark) { // YAML syntax error
|
||||
customError = `\x1b[33m\x1b[4m⚠️ Error on line ${e.mark.line}, column ${e.mark.column}: `
|
||||
+ `${e.reason}\x1b[0m\n\n${e.mark.snippet}\n\x1b[0m`
|
||||
+ '\n\x1b[36m ℹ️ You might find it helpful to use a YAML validator'
|
||||
+ ', like: \x1b[4mhttps://yamlchecker.com/\x1b[0m\n';
|
||||
}
|
||||
if (e.code === 'ENOENT') { // File not found error
|
||||
customError = `\x1b[33m⚠️ Config file could not be found at ${e.path}\x1b[0m\n`;
|
||||
}
|
||||
if (e.code === 'EISDIR') { // Not a file
|
||||
customError = '\x1b[33m⚠️ Config needs to be a file, but found a directory instead \x1b[0m\n';
|
||||
}
|
||||
if (e.code === 'EACCES' || e.code === 'EPERM') { // File permissions error
|
||||
customError = '\x1b[33m⚠️ Permission denied \x1b[0m\n';
|
||||
}
|
||||
logToConsole(customError);
|
||||
if (customError === '') { // Unknown error, print stack trace
|
||||
const moreInfo = 'Ensure that your config file is present, readable, and valid YAML. '
|
||||
+ 'If this issue persists, you can get support by raising a ticket on GitHub. '
|
||||
+ 'Please include the following stack trace';
|
||||
logToConsole(moreInfo);
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('\x1b[33mStack Trace for config-validator.js:\x1b[0m\n', e);
|
||||
logToConsole();
|
||||
}
|
||||
};
|
||||
|
||||
try { // Try to open and parse the YAML file
|
||||
const config = yaml.load(fs.readFileSync('./public/conf.yml', 'utf8'));
|
||||
validate(config);
|
||||
} catch (e) { // Something went very wrong...
|
||||
setIsValidVariable(false);
|
||||
logToConsole(bigError());
|
||||
printFileReadError(e);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* A simple CORS proxy, for accessing API services which aren't CORS-enabled.
|
||||
* Receives requests from frontend, applies correct access control headers,
|
||||
* makes request to endpoint, then responds to the frontend with the response
|
||||
*/
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
module.exports = (req, res) => {
|
||||
// Apply allow-all response headers
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE');
|
||||
if (req.header('access-control-request-headers')) {
|
||||
res.header('Access-Control-Allow-Headers', req.header('access-control-request-headers'));
|
||||
}
|
||||
|
||||
// Pre-flight
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.send();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get desired URL, from Target-URL header
|
||||
const targetURL = req.header('Target-URL');
|
||||
if (!targetURL) {
|
||||
res.status(500).send({ error: 'There is no Target-Endpoint header in the request' });
|
||||
return;
|
||||
}
|
||||
// Apply any custom headers, if needed
|
||||
const headers = req.header('CustomHeaders') ? JSON.parse(req.header('CustomHeaders')) : {};
|
||||
|
||||
// Prepare the request
|
||||
const requestConfig = {
|
||||
method: req.method,
|
||||
url: targetURL,
|
||||
json: req.body,
|
||||
headers,
|
||||
};
|
||||
|
||||
// Make the request, and respond with result
|
||||
axios.request(requestConfig)
|
||||
.then((response) => {
|
||||
res.status(200).send(response.data);
|
||||
}).catch((error) => {
|
||||
res.status(500).send({ error });
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* An endpoint for confirming that the application is up and running
|
||||
* Used for better Docker healthcheck results
|
||||
* Note that exiting with code 1 indicates failure, and 0 is success
|
||||
*/
|
||||
|
||||
const isSsl = !!process.env.SSL_PRIV_KEY_PATH && !!process.env.SSL_PUB_KEY_PATH;
|
||||
|
||||
const http = require(isSsl ? 'https' : 'http');
|
||||
|
||||
/* Location of the server to test */
|
||||
const isDocker = !!process.env.IS_DOCKER;
|
||||
const port = isSsl ? (process.env.SSL_PORT || (isDocker ? 443 : 4001)) : (process.env.PORT || isDocker ? 80 : 4000);
|
||||
const host = process.env.HOST || '0.0.0.0';
|
||||
const timeout = 2000;
|
||||
|
||||
const agent = new http.Agent({
|
||||
rejectUnauthorized: false, // Allow self-signed certificates
|
||||
});
|
||||
|
||||
const requestOptions = { host, port, timeout, agent };
|
||||
|
||||
const startTime = new Date(); // Initialize timestamp to calculate time taken
|
||||
|
||||
console.log(`[${startTime}] Running health check...`);
|
||||
|
||||
/* Creates an HTTP Request to attempt to send GET to app, then exits with appropriate exit code */
|
||||
const healthCheck = http.request(requestOptions, (response) => {
|
||||
const totalTime = (new Date() - startTime) / 1000;
|
||||
const status = response.statusCode;
|
||||
const color = status === 200 ? '\x1b[32m' : '\x1b[31m';
|
||||
const message = `${color}Status: ${status}\nRequest took ${totalTime} seconds\n\x1b[0m---`;
|
||||
console.log(message); // Print out healthcheck response
|
||||
process.exit(status === 200 ? 0 : 1); // Exit with 0 (success), if response is 200 okay
|
||||
});
|
||||
|
||||
/* If the server is not running, then print the error code, and exit with 1 */
|
||||
healthCheck.on('error', (err) => {
|
||||
console.error(`\x1b[31mHealthceck Failed, Error: ${'\x1b[33m'}${err.code}\x1b[0m`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
healthCheck.end();
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Returns a welcome message, to be printed to the user when they start the app
|
||||
* Contains essential info about restarting and managing the container or service
|
||||
* @param String ip: The users local IP address or hostname
|
||||
* @param Integer port: the port number that the app is running at
|
||||
* @param Boolean isDocker: whether or not the app is being run within a container
|
||||
* @returns A string formatted for the terminal
|
||||
*/
|
||||
module.exports = (ip, port, isDocker) => {
|
||||
let msg = ''; // To return
|
||||
const chars = { // Color codes used in the message
|
||||
RESET: '\x1b[0m',
|
||||
CYAN: '\x1b[36m',
|
||||
GREEN: '\x1b[32m',
|
||||
BLUE: '\x1b[34m',
|
||||
BRIGHT: '\x1b[1m',
|
||||
BR: '\n',
|
||||
};
|
||||
// Functions to insert string of set length of characters
|
||||
const printChars = (count, char) => new Array(count).fill(char).join('');
|
||||
const stars = (count) => printChars(count, '*');
|
||||
const line = (count) => printChars(count, '━');
|
||||
const blanks = (count) => printChars(count, ' ');
|
||||
if (isDocker) {
|
||||
// Prepare message for Docker users
|
||||
const containerId = process.env.HOST || undefined;
|
||||
msg = `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`
|
||||
+ `${chars.CYAN}Welcome to Dashy! 🚀${chars.RESET}${chars.BR}`
|
||||
+ `${chars.GREEN}Your new dashboard is now up and running `
|
||||
+ `${containerId ? `in container ID ${containerId}` : 'with Docker'}${chars.BR}`
|
||||
+ `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`;
|
||||
} else {
|
||||
// Prepare message for users running app on bare metal
|
||||
msg = `${chars.GREEN}┏${line(75)}┓${chars.BR}`
|
||||
+ `┃ ${chars.CYAN}Welcome to Dashy! 🚀${blanks(55)}${chars.GREEN}┃${chars.BR}`
|
||||
+ `┃ ${chars.CYAN}Your new dashboard is now up and running at ${chars.BRIGHT}`
|
||||
+ `http://${ip}:${port}${chars.RESET}${blanks(18 - ip.length)}${chars.GREEN}┃${chars.BR}`
|
||||
+ `┗${line(75)}┛${chars.BR}${chars.BR}${chars.RESET}`;
|
||||
}
|
||||
// Make some sexy ascii art ;)
|
||||
const ascii = `${chars.CYAN}\n\n`
|
||||
+ ' ██████╗ █████╗ ███████╗██╗ ██╗██╗ ██╗\n'
|
||||
+ ' ██╔══██╗██╔══██╗██╔════╝██║ ██║╚██╗ ██╔╝\n'
|
||||
+ ' ██║ ██║███████║███████╗███████║ ╚████╔╝\n'
|
||||
+ ' ██║ ██║██╔══██║╚════██║██╔══██║ ╚██╔╝\n'
|
||||
+ ' ██████╔╝██║ ██║███████║██║ ██║ ██║\n'
|
||||
+ ` ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝\n${chars.RESET}\n`;
|
||||
|
||||
return ascii + msg;
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* This script programmatically triggers a production build
|
||||
* and responds with the status, message and full output
|
||||
*/
|
||||
const { exec } = require('child_process');
|
||||
|
||||
module.exports = () => new Promise((resolve, reject) => {
|
||||
const buildProcess = exec('npm run build'); // Trigger the build command
|
||||
|
||||
let output = ''; // Will store console output
|
||||
|
||||
// Write output to console, and append to var for returning
|
||||
buildProcess.stdout.on('data', (data) => {
|
||||
process.stdout.write(data);
|
||||
output += data;
|
||||
});
|
||||
|
||||
// Handle errors, by sending the reject
|
||||
buildProcess.on('error', (error) => {
|
||||
reject(Error({
|
||||
success: false,
|
||||
error,
|
||||
output,
|
||||
}));
|
||||
});
|
||||
|
||||
// When finished, check success, make message and resolve response
|
||||
buildProcess.on('exit', (response) => {
|
||||
const success = response === 0;
|
||||
const message = `Build process exited with ${response}: `
|
||||
+ `${success ? 'Success' : 'Possible Error'}`;
|
||||
resolve({ success, message, output });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* This file exports a function, used by the write config endpoint.
|
||||
* It will make a backup of the users conf.yml file
|
||||
* and then write their new config into the main conf.yml file.
|
||||
* Finally, it will call a function with the status message
|
||||
*/
|
||||
const fsPromises = require('fs').promises;
|
||||
const path = require('path');
|
||||
|
||||
module.exports = async (newConfig, render) => {
|
||||
/* Either returns nothing (if using default path), or strips navigational characters from path */
|
||||
const makeSafeFileName = (configObj) => {
|
||||
if (!configObj || !configObj.filename) return undefined;
|
||||
return configObj.filename.replaceAll('/', '').replaceAll('..', '');
|
||||
};
|
||||
|
||||
const usersFileName = makeSafeFileName(newConfig);
|
||||
|
||||
// Define constants for the config file
|
||||
const settings = {
|
||||
defaultLocation: './public/',
|
||||
defaultFile: 'conf.yml',
|
||||
filename: 'conf',
|
||||
backupDenominator: '.backup.yml',
|
||||
};
|
||||
|
||||
// Make the full file name and path to save the backup config file
|
||||
const backupFilePath = path.normalize(process.env.BACKUP_DIR || settings.defaultLocation)
|
||||
+ `/${usersFileName || settings.filename}-`
|
||||
+ `${Math.round(new Date() / 1000)}${settings.backupDenominator}`;
|
||||
|
||||
// The path where the main conf.yml should be read and saved to
|
||||
const defaultFilePath = settings.defaultLocation + (usersFileName || settings.defaultFile);
|
||||
|
||||
// Returns a string confirming successful job
|
||||
const getSuccessMessage = () => `Successfully backed up ${settings.defaultFile} to`
|
||||
+ ` ${backupFilePath}, and updated the contents of ${defaultFilePath}`;
|
||||
|
||||
// Encoding options for writing to conf file
|
||||
const writeFileOptions = { encoding: 'utf8' };
|
||||
|
||||
// Prepare the response returned by the API
|
||||
const getRenderMessage = (success, errorMsg) => JSON.stringify({
|
||||
success,
|
||||
message: !success ? errorMsg : getSuccessMessage(),
|
||||
});
|
||||
|
||||
// Makes a backup of the existing config file
|
||||
await fsPromises
|
||||
.copyFile(defaultFilePath, backupFilePath)
|
||||
.catch((error) => render(getRenderMessage(false, `Unable to backup conf.yml: ${error}`)));
|
||||
|
||||
// Writes the new content to the conf.yml file
|
||||
await fsPromises
|
||||
.writeFile(defaultFilePath, newConfig.config.toString(), writeFileOptions)
|
||||
.catch((error) => render(getRenderMessage(false, `Unable to write to conf.yml: ${error}`)));
|
||||
|
||||
// If successful, then render hasn't yet been called- call it
|
||||
await render(getRenderMessage(true));
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
/* A cloud function that wraps the status checking method, for use on Netlify */
|
||||
const statusCheck = require('../status-check');
|
||||
|
||||
exports.handler = (event, context, callback) => {
|
||||
const paramStr = event.rawQuery;
|
||||
statusCheck(paramStr, (results) => {
|
||||
callback(null, {
|
||||
statusCode: 200,
|
||||
body: results,
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
/* A Netlify cloud function to handle requests to CORS-disabled services */
|
||||
const axios = require('axios');
|
||||
|
||||
exports.handler = (event, context, callback) => {
|
||||
// Get input data
|
||||
const { body, headers, queryStringParameters } = event;
|
||||
|
||||
// Get URL from header or GET param
|
||||
const requestUrl = queryStringParameters.url || headers['Target-URL'] || headers['target-url'];
|
||||
|
||||
const returnError = (msg, error) => {
|
||||
callback(null, {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify({ success: false, msg, error }),
|
||||
});
|
||||
};
|
||||
// If URL missing, return error
|
||||
if (!requestUrl) {
|
||||
returnError('Missing Target-URL header', null);
|
||||
}
|
||||
|
||||
let custom = {};
|
||||
try {
|
||||
custom = JSON.parse(headers.CustomHeaders || headers.customheaders || '{}');
|
||||
} catch (e) { returnError('Unable to parse custom headers'); }
|
||||
|
||||
// Response headers
|
||||
const requestHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
...custom,
|
||||
};
|
||||
|
||||
// Prepare request
|
||||
const requestConfig = {
|
||||
method: 'GET',
|
||||
url: requestUrl,
|
||||
json: body,
|
||||
headers: requestHeaders,
|
||||
};
|
||||
|
||||
// Make request
|
||||
axios.request(requestConfig)
|
||||
.then((response) => {
|
||||
callback(null, { statusCode: 200, body: JSON.stringify(response.data) });
|
||||
}).catch((error) => {
|
||||
returnError('Request failed', error);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
/* A Netlify cloud function to return a message endpoints that are not available */
|
||||
exports.handler = async () => ({
|
||||
statusCode: 200,
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
error: 'This action is not supported on Netlify',
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
const https = require('https');
|
||||
|
||||
const promise = util.promisify;
|
||||
const stat = promise(fs.stat);
|
||||
|
||||
const host = process.env.HOST || '0.0.0.0';
|
||||
|
||||
const httpsCerts = {
|
||||
private: process.env.SSL_PRIV_KEY_PATH || '/etc/ssl/certs/dashy-priv.key',
|
||||
public: process.env.SSL_PUB_KEY_PATH || '/etc/ssl/certs/dashy-pub.pem',
|
||||
};
|
||||
|
||||
const isDocker = !!process.env.IS_DOCKER;
|
||||
const SSLPort = process.env.SSL_PORT || (isDocker ? 443 : 4001);
|
||||
const redirectHttps = process.env.REDIRECT_HTTPS ? process.env.REDIRECT_HTTPS : true;
|
||||
|
||||
const printNotSoGood = (msg) => {
|
||||
console.log(`SSL Not Enabled: ${msg}`);
|
||||
};
|
||||
|
||||
const printSuccess = () => {
|
||||
console.log(`🔐 HTTPS server successfully started (port: ${SSLPort} ${isDocker ? 'of container' : ''})`);
|
||||
};
|
||||
|
||||
// Check if the SSL certs are present and SSL should be enabled
|
||||
let enableSSL = false;
|
||||
const checkCertificateFiles = stat(httpsCerts.public).then(() => {
|
||||
return stat(httpsCerts.private).then(() => {
|
||||
enableSSL = true;
|
||||
}).catch(() => { printNotSoGood('Private key not present'); });
|
||||
}).catch(() => { printNotSoGood('Public key not present'); });
|
||||
|
||||
const startSSLServer = (app) => {
|
||||
checkCertificateFiles.then(() => {
|
||||
// If SSL should be enabled, create a secured server and start it
|
||||
if (enableSSL) {
|
||||
const httpsServer = https.createServer({
|
||||
key: fs.readFileSync(httpsCerts.private),
|
||||
cert: fs.readFileSync(httpsCerts.public),
|
||||
}, app);
|
||||
httpsServer.listen(SSLPort, host, () => { printSuccess(); });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const middleware = (req, res, next) => {
|
||||
if (enableSSL && redirectHttps && req.protocol === 'http') {
|
||||
res.redirect(`https://${req.hostname + ((SSLPort === 443) ? '' : `:${SSLPort}`) + req.url}`);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { startSSLServer, middleware };
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* This file contains the Node.js code, used for the optional status check feature
|
||||
* It accepts a single url parameter, and will make an empty GET request to that
|
||||
* endpoint, and then resolve the response status code, time taken, and short message
|
||||
*/
|
||||
const axios = require('axios').default;
|
||||
const https = require('https');
|
||||
|
||||
/* Determines if successful from the HTTP response code */
|
||||
const getResponseType = (code, validCodes) => {
|
||||
if (validCodes && String(validCodes).includes(String(code))) return true;
|
||||
if (Number.isNaN(code)) return false;
|
||||
const numericCode = parseInt(code, 10);
|
||||
return (numericCode >= 200 && numericCode <= 302);
|
||||
};
|
||||
|
||||
/* Makes human-readable response text for successful check */
|
||||
const makeMessageText = (data) => `${data.successStatus ? '✅' : '⚠️'} `
|
||||
+ `${data.serverName || 'Server'} responded with `
|
||||
+ `${data.statusCode} - ${data.statusText}. `
|
||||
+ `\n⏱️Took ${data.timeTaken} ms`;
|
||||
|
||||
/* Makes human-readable response text for failed check */
|
||||
const makeErrorMessage = (data) => `❌ Service Unavailable: ${data.hostname || 'Server'} `
|
||||
+ `resulted in ${data.code || 'a fatal error'} ${data.errno ? `(${data.errno})` : ''}`;
|
||||
|
||||
const makeErrorMessage2 = (data) => '❌ Service Error - '
|
||||
+ `${data.status} - ${data.statusText}`;
|
||||
|
||||
/* Kicks of a HTTP request, then formats and renders results */
|
||||
const makeRequest = (url, options, render) => {
|
||||
const {
|
||||
headers, enableInsecure, acceptCodes, maxRedirects,
|
||||
} = options;
|
||||
const validCodes = acceptCodes && acceptCodes !== 'null' ? acceptCodes : null;
|
||||
const startTime = new Date();
|
||||
const requestMaker = axios.create({
|
||||
httpsAgent: new https.Agent({
|
||||
rejectUnauthorized: !enableInsecure,
|
||||
}),
|
||||
});
|
||||
requestMaker.request({
|
||||
url,
|
||||
headers,
|
||||
maxRedirects,
|
||||
})
|
||||
.then((response) => {
|
||||
const statusCode = response.status;
|
||||
const { statusText } = response;
|
||||
const successStatus = getResponseType(statusCode, validCodes);
|
||||
const serverName = response.request.socket.servername;
|
||||
const timeTaken = (new Date() - startTime);
|
||||
const results = {
|
||||
statusCode, statusText, serverName, successStatus, timeTaken,
|
||||
};
|
||||
results.message = makeMessageText(results);
|
||||
return results;
|
||||
})
|
||||
.catch((error) => {
|
||||
const response = error ? (error.response || {}) : {};
|
||||
const returnCode = response.status || response.code;
|
||||
if (validCodes && String(validCodes).includes(returnCode)) { // Success overridden by user
|
||||
const results = {
|
||||
successStatus: getResponseType(returnCode, validCodes),
|
||||
statusCode: returnCode,
|
||||
statusText: response.statusText,
|
||||
timeTaken: (new Date() - startTime),
|
||||
};
|
||||
results.message = makeMessageText(results);
|
||||
return results;
|
||||
} else { // Request failed
|
||||
return {
|
||||
successStatus: false,
|
||||
message: error.response ? makeErrorMessage2(error.response) : makeErrorMessage(error),
|
||||
};
|
||||
}
|
||||
}).then((results) => {
|
||||
// Request completed (either successfully, or failed) - render results
|
||||
render(JSON.stringify(results));
|
||||
});
|
||||
};
|
||||
|
||||
const decodeHeaders = (maybeHeaders) => {
|
||||
if (!maybeHeaders) return {};
|
||||
const decodedHeaders = decodeURIComponent(maybeHeaders);
|
||||
let parsedHeaders = {};
|
||||
try {
|
||||
parsedHeaders = JSON.parse(decodedHeaders);
|
||||
} catch (e) { /* Not valid JSON, will just return false */ }
|
||||
return parsedHeaders;
|
||||
};
|
||||
|
||||
/* Returned if the URL param is not present or correct */
|
||||
const immediateError = (render) => {
|
||||
render(JSON.stringify({
|
||||
successStatus: false,
|
||||
message: '❌ Missing or Malformed URL',
|
||||
}));
|
||||
};
|
||||
|
||||
/* Main function, will check if a URL present, and call function */
|
||||
module.exports = (paramStr, render) => {
|
||||
if (!paramStr || !paramStr.includes('=')) {
|
||||
immediateError(render);
|
||||
} else {
|
||||
// Prepare the parameters, which are got from the URL
|
||||
const params = new URLSearchParams(paramStr);
|
||||
const url = decodeURIComponent(params.get('url'));
|
||||
const acceptCodes = decodeURIComponent(params.get('acceptCodes'));
|
||||
const maxRedirects = decodeURIComponent(params.get('maxRedirects')) || 0;
|
||||
const headers = decodeHeaders(params.get('headers'));
|
||||
const enableInsecure = !!params.get('enableInsecure');
|
||||
if (!url || url === 'undefined') immediateError(render);
|
||||
const options = {
|
||||
headers, enableInsecure, acceptCodes, maxRedirects,
|
||||
};
|
||||
makeRequest(url, options, render);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Gets basic system info, for the resource usage widget
|
||||
*/
|
||||
const os = require('os');
|
||||
|
||||
module.exports = () => {
|
||||
const meta = {
|
||||
timestamp: new Date(),
|
||||
uptime: os.uptime(),
|
||||
hostname: os.hostname(),
|
||||
username: os.userInfo().username,
|
||||
system: `${os.version()} (${os.platform()})`,
|
||||
};
|
||||
|
||||
const memory = {
|
||||
total: `${Math.round(os.totalmem() / (1024 * 1024 * 1024))} GB`,
|
||||
freePercent: (os.freemem() / os.totalmem()).toFixed(2),
|
||||
};
|
||||
|
||||
const loadAv = os.loadavg();
|
||||
const load = { one: loadAv[0], five: loadAv[1], fifteen: loadAv[2] };
|
||||
|
||||
return { meta, memory, load };
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
const axios = require('axios').default;
|
||||
|
||||
const currentVersion = require('../package.json').version;
|
||||
|
||||
const packageUrl = 'https://raw.githubusercontent.com/Lissy93/dashy/master/package.json';
|
||||
|
||||
const logToConsole = (msg) => {
|
||||
console.log(msg); // eslint-disable-line no-console
|
||||
};
|
||||
|
||||
const makeMsg = (latestVersion) => {
|
||||
const parse = (version) => parseInt(version.replace(/\./g, ''), 10);
|
||||
const difference = parse(latestVersion) - parse(currentVersion);
|
||||
let msg = '';
|
||||
if (difference <= 0) {
|
||||
msg = '\x1b[1m\x1b[32m✅ Dashy is Up-to-Date\x1b[0m\n';
|
||||
} else {
|
||||
msg = `\x1b[103m\x1b[34m${new Array(27).fill('━').join('')}\x1b[0m\n`
|
||||
+ `\x1b[103m\x1b[34m⚠️ Update Available: ${latestVersion} \x1b[0m\n`
|
||||
+ `\x1b[103m\x1b[34m${new Array(27).fill('━').join('')}\x1b[0m\n`;
|
||||
}
|
||||
return msg;
|
||||
};
|
||||
|
||||
axios.get(packageUrl).then((response) => {
|
||||
if (response && response.data && response.data.version) {
|
||||
logToConsole(`\nUsing Dashy V-${currentVersion}. Update Check Complete`);
|
||||
logToConsole(makeMsg(response.data.version));
|
||||
}
|
||||
}).catch(() => {
|
||||
logToConsole('Unable to check for updates');
|
||||
});
|
||||
Reference in New Issue
Block a user