mirror of
https://github.com/Lissy93/web-check.git
synced 2024-11-25 17:53:19 +01:00
33 lines
770 B
JavaScript
33 lines
770 B
JavaScript
import url from 'url';
|
|
import traceroute from 'traceroute';
|
|
import middleware from './_common/middleware.js';
|
|
|
|
const traceRouteHandler = async (urlString, context) => {
|
|
// Parse the URL and get the hostname
|
|
const urlObject = url.parse(urlString);
|
|
const host = urlObject.hostname;
|
|
|
|
if (!host) {
|
|
throw new Error('Invalid URL provided');
|
|
}
|
|
|
|
// Traceroute with callback
|
|
const result = await new Promise((resolve, reject) => {
|
|
traceroute.trace(host, (err, hops) => {
|
|
if (err || !hops) {
|
|
reject(err || new Error('No hops found'));
|
|
} else {
|
|
resolve(hops);
|
|
}
|
|
});
|
|
});
|
|
|
|
return {
|
|
message: "Traceroute completed!",
|
|
result,
|
|
};
|
|
};
|
|
|
|
export const handler = middleware(traceRouteHandler);
|
|
export default handler;
|