Working on traceroute component

This commit is contained in:
Alicia Sykes
2023-07-02 18:03:18 +01:00
parent a8981b8ca7
commit a6ecbd3406
5 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,35 @@
const traceroute = require('traceroute');
const util = require('util');
const url = require('url');
// Convert traceroute.trace method to return a Promise
const traceroutePromise = util.promisify(traceroute.trace);
exports.handler = async function(event, context) {
const urlString = event.queryStringParameters.url;
try {
if (!urlString) {
throw new Error('URL parameter is missing!');
}
// Parse the URL and get the hostname
const urlObject = url.parse(urlString);
const host = urlObject.hostname;
if (!host) {
throw new Error('Invalid URL provided');
}
const result = await traceroutePromise(host);
return {
statusCode: 200,
body: JSON.stringify({ message: "Traceroute completed!", result }),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ message: `Error: ${err.message}` }),
};
}
};