web-check/api/trace-route.js

33 lines
778 B
JavaScript
Raw Normal View History

2023-07-02 19:03:18 +02:00
const traceroute = require('traceroute');
const url = require('url');
const middleware = require('./_common/middleware');
2023-07-02 19:03:18 +02:00
const handler = async (urlString, context) => {
// Parse the URL and get the hostname
const urlObject = url.parse(urlString);
const host = urlObject.hostname;
2023-07-02 19:03:18 +02:00
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,
};
2023-07-02 19:03:18 +02:00
};
module.exports = middleware(handler);
module.exports.handler = middleware(handler);