2021-06-19 15:46:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2021-08-27 00:17:16 +03:00
|
|
|
|
const https = require('https');
|
2021-06-19 15:46:02 +03:00
|
|
|
|
|
|
|
|
|
/* Determines if successful from the HTTP response code */
|
2022-01-29 02:23:16 +03:00
|
|
|
|
const getResponseType = (code, validCodes) => {
|
|
|
|
|
if (validCodes && String(validCodes).includes(String(code))) return true;
|
2021-06-19 15:46:02 +03:00
|
|
|
|
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 */
|
2022-03-14 00:58:40 +03:00
|
|
|
|
const makeRequest = (url, options, render) => {
|
|
|
|
|
const {
|
|
|
|
|
headers, enableInsecure, acceptCodes, maxRedirects,
|
|
|
|
|
} = options;
|
2022-01-29 02:23:16 +03:00
|
|
|
|
const validCodes = acceptCodes && acceptCodes !== 'null' ? acceptCodes : null;
|
2021-06-19 15:46:02 +03:00
|
|
|
|
const startTime = new Date();
|
2021-08-27 00:17:16 +03:00
|
|
|
|
const requestMaker = axios.create({
|
|
|
|
|
httpsAgent: new https.Agent({
|
2022-03-14 00:58:40 +03:00
|
|
|
|
rejectUnauthorized: !enableInsecure,
|
2021-08-27 00:17:16 +03:00
|
|
|
|
}),
|
|
|
|
|
});
|
2022-03-14 00:58:40 +03:00
|
|
|
|
requestMaker.request({
|
|
|
|
|
url,
|
|
|
|
|
headers,
|
|
|
|
|
maxRedirects,
|
|
|
|
|
})
|
2021-06-19 15:46:02 +03:00
|
|
|
|
.then((response) => {
|
|
|
|
|
const statusCode = response.status;
|
|
|
|
|
const { statusText } = response;
|
2022-01-29 02:23:16 +03:00
|
|
|
|
const successStatus = getResponseType(statusCode, validCodes);
|
2021-06-19 15:46:02 +03:00
|
|
|
|
const serverName = response.request.socket.servername;
|
|
|
|
|
const timeTaken = (new Date() - startTime);
|
|
|
|
|
const results = {
|
|
|
|
|
statusCode, statusText, serverName, successStatus, timeTaken,
|
|
|
|
|
};
|
2022-01-29 02:23:16 +03:00
|
|
|
|
results.message = makeMessageText(results);
|
2021-06-19 15:46:02 +03:00
|
|
|
|
return results;
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2022-01-29 02:23:16 +03:00
|
|
|
|
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),
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-06-19 15:46:02 +03:00
|
|
|
|
}).then((results) => {
|
2022-01-29 02:23:16 +03:00
|
|
|
|
// Request completed (either successfully, or failed) - render results
|
2021-06-19 15:46:02 +03:00
|
|
|
|
render(JSON.stringify(results));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-27 00:17:16 +03:00
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-29 02:23:16 +03:00
|
|
|
|
/* Returned if the URL param is not present or correct */
|
|
|
|
|
const immediateError = (render) => {
|
|
|
|
|
render(JSON.stringify({
|
|
|
|
|
successStatus: false,
|
|
|
|
|
message: '❌ Missing or Malformed URL',
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-19 15:46:02 +03:00
|
|
|
|
/* Main function, will check if a URL present, and call function */
|
2021-08-27 00:17:16 +03:00
|
|
|
|
module.exports = (paramStr, render) => {
|
|
|
|
|
if (!paramStr || !paramStr.includes('=')) {
|
2022-01-29 02:23:16 +03:00
|
|
|
|
immediateError(render);
|
2021-06-19 15:46:02 +03:00
|
|
|
|
} else {
|
2021-08-27 00:17:16 +03:00
|
|
|
|
// Prepare the parameters, which are got from the URL
|
|
|
|
|
const params = new URLSearchParams(paramStr);
|
|
|
|
|
const url = decodeURIComponent(params.get('url'));
|
2022-01-29 02:23:16 +03:00
|
|
|
|
const acceptCodes = decodeURIComponent(params.get('acceptCodes'));
|
2022-03-14 00:58:40 +03:00
|
|
|
|
const maxRedirects = decodeURIComponent(params.get('maxRedirects')) || 0;
|
2021-08-27 00:17:16 +03:00
|
|
|
|
const headers = decodeHeaders(params.get('headers'));
|
|
|
|
|
const enableInsecure = !!params.get('enableInsecure');
|
2022-01-29 02:23:16 +03:00
|
|
|
|
if (!url || url === 'undefined') immediateError(render);
|
2022-03-14 00:58:40 +03:00
|
|
|
|
const options = {
|
|
|
|
|
headers, enableInsecure, acceptCodes, maxRedirects,
|
|
|
|
|
};
|
|
|
|
|
makeRequest(url, options, render);
|
2021-06-19 15:46:02 +03:00
|
|
|
|
}
|
|
|
|
|
};
|