mirror of
https://github.com/Lissy93/web-check.git
synced 2025-06-17 18:36:54 +02:00
Fix for when Traceroute is not installed on host
This commit is contained in:
parent
83272e4536
commit
dd2baec866
@ -1,37 +1,88 @@
|
|||||||
const traceroute = require('traceroute');
|
const traceroute = require('traceroute');
|
||||||
const util = require('util');
|
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
|
|
||||||
// Convert traceroute.trace method to return a Promise
|
|
||||||
const traceroutePromise = util.promisify(traceroute.trace);
|
|
||||||
|
|
||||||
exports.handler = async function(event, context) {
|
exports.handler = async function(event, context) {
|
||||||
const urlString = event.queryStringParameters.url;
|
const urlString = event.queryStringParameters.url;
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!urlString) {
|
if (!urlString) {
|
||||||
throw new Error('URL parameter is missing!');
|
|
||||||
|
return {
|
||||||
|
statusCode: 400,
|
||||||
|
body: JSON.stringify({
|
||||||
|
|
||||||
|
|
||||||
|
error: 'URL parameter is missing!'
|
||||||
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the URL and get the hostname
|
// Parse the URL and get the hostname
|
||||||
const urlObject = url.parse(urlString);
|
const urlObject = url.parse(urlString);
|
||||||
const host = urlObject.hostname;
|
const host = urlObject.hostname;
|
||||||
|
|
||||||
if (!host) {
|
if (!host) {
|
||||||
throw new Error('Invalid URL provided');
|
|
||||||
|
return {
|
||||||
|
statusCode: 400,
|
||||||
|
body: JSON.stringify({
|
||||||
|
error: 'Invalid URL provided'
|
||||||
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await traceroutePromise(host);
|
// Traceroute with callback
|
||||||
const timeTaken = Date.now() - startTime;
|
return await new Promise((resolve, reject) => {
|
||||||
return {
|
traceroute.trace(host, (err, hops) => {
|
||||||
statusCode: 200,
|
if (err) {
|
||||||
body: JSON.stringify({ message: "Traceroute completed!", result, timeTaken }),
|
reject(err);
|
||||||
};
|
} else if (hops) {
|
||||||
|
resolve(hops);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Check if remaining time is less than 8.8 seconds, then reject promise
|
||||||
|
if (context.getRemainingTimeInMillis() < 8800) {
|
||||||
|
reject(new Error('Lambda is about to timeout'));
|
||||||
|
}
|
||||||
|
}).then((result) => {
|
||||||
|
const timeTaken = Date.now() - startTime;
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: JSON.stringify({
|
||||||
|
message: "Traceroute completed!",
|
||||||
|
result,
|
||||||
|
timeTaken
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}).catch((err) => {
|
||||||
|
return {
|
||||||
|
statusCode: 500,
|
||||||
|
body: JSON.stringify({
|
||||||
|
error: err.message
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
if (err.code === 'ENOENT') {
|
||||||
statusCode: 500,
|
|
||||||
body: JSON.stringify({ message: `Error: ${err.message}` }),
|
return {
|
||||||
};
|
statusCode: 400,
|
||||||
|
body: JSON.stringify({
|
||||||
|
error: 'Traceroute command is not installed on the host.'
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: 500,
|
||||||
|
body: JSON.stringify({
|
||||||
|
error: err.message
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -9,6 +9,7 @@ import Card from 'components/Form/Card';
|
|||||||
import Modal from 'components/Form/Modal';
|
import Modal from 'components/Form/Modal';
|
||||||
import Footer from 'components/misc/Footer';
|
import Footer from 'components/misc/Footer';
|
||||||
import { RowProps } from 'components/Form/Row';
|
import { RowProps } from 'components/Form/Row';
|
||||||
|
import ErrorBoundary from 'components/misc/ErrorBoundary';
|
||||||
import docs from 'utils/docs';
|
import docs from 'utils/docs';
|
||||||
|
|
||||||
|
|
||||||
@ -435,12 +436,14 @@ const Results = (): JSX.Element => {
|
|||||||
{
|
{
|
||||||
resultCardData.map(({ id, title, result, refresh, Component }, index: number) => (
|
resultCardData.map(({ id, title, result, refresh, Component }, index: number) => (
|
||||||
(result && !result.error) ? (
|
(result && !result.error) ? (
|
||||||
<Component
|
<ErrorBoundary title={title}>
|
||||||
key={`${title}-${index}`}
|
<Component
|
||||||
data={{...result}}
|
key={`${title}-${index}`}
|
||||||
title={title}
|
data={{...result}}
|
||||||
actionButtons={refresh ? MakeActionButtons(title, refresh, () => showInfo(id)) : undefined}
|
title={title}
|
||||||
/>
|
actionButtons={refresh ? MakeActionButtons(title, refresh, () => showInfo(id)) : undefined}
|
||||||
|
/>
|
||||||
|
</ErrorBoundary>
|
||||||
) : <></>
|
) : <></>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user