2023-07-02 19:03:18 +02:00
|
|
|
const traceroute = require('traceroute');
|
|
|
|
const url = require('url');
|
2023-08-09 23:33:36 +02:00
|
|
|
const middleware = require('./_common/middleware');
|
2023-07-02 19:03:18 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
const executeTraceroute = 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
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
if (!host) {
|
|
|
|
throw new Error('Invalid URL provided');
|
|
|
|
}
|
2023-07-10 00:23:50 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
// 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);
|
2023-07-07 22:30:54 +02:00
|
|
|
}
|
|
|
|
});
|
2023-08-09 23:33:36 +02:00
|
|
|
});
|
2023-07-07 22:30:54 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
return {
|
|
|
|
message: "Traceroute completed!",
|
|
|
|
result,
|
|
|
|
};
|
2023-07-02 19:03:18 +02:00
|
|
|
};
|
2023-08-09 23:33:36 +02:00
|
|
|
|
|
|
|
exports.handler = middleware(executeTraceroute);
|