zulhilmizainuddin / nodejs-traceroute Goto Github PK
View Code? Open in Web Editor NEWNode.js wrapper around tracert and traceroute process
License: MIT License
Node.js wrapper around tracert and traceroute process
License: MIT License
Will nodejs-traceroute support IPv6 anytime soon?
Hey there! Thanks for all the work in making this package. I truly appreciate it.
When using this package, I found out that the destination is never properly returned. I figured out that this is because the header that includes the destination is output in stderr
rather than stdout
. In order to fix this, I've applied this patch in my local repository using patch-package
.
diff --git a/node_modules/nodejs-traceroute/lib/process.js b/node_modules/nodejs-traceroute/lib/process.js
index 0fea080..c169df8 100644
--- a/node_modules/nodejs-traceroute/lib/process.js
+++ b/node_modules/nodejs-traceroute/lib/process.js
@@ -25,24 +25,28 @@ class Process extends events_1.default.EventEmitter {
});
this.emit('pid', process.pid);
let isDestinationCaptured = false;
+ const parseLine = (line) => {
+ if (!isDestinationCaptured) {
+ const destination = this.parseDestination(line);
+ if (destination !== null) {
+ this.emit('destination', destination);
+ isDestinationCaptured = true;
+ }
+ }
+ const hop = this.parseHop(line);
+ if (hop !== null) {
+ this.emit('hop', hop);
+ }
+ };
if (process.pid) {
readline_1.default.createInterface({
input: process.stdout,
terminal: false
- })
- .on('line', (line) => {
- if (!isDestinationCaptured) {
- const destination = this.parseDestination(line);
- if (destination !== null) {
- this.emit('destination', destination);
- isDestinationCaptured = true;
- }
- }
- const hop = this.parseHop(line);
- if (hop !== null) {
- this.emit('hop', hop);
- }
- });
+ }).on('line', parseLine);
+ readline_1.default.createInterface({
+ input: process.stderr,
+ terminal: false
+ }).on('line', parseLine);
}
}
isValidDomainName(domainName) {
diff --git a/node_modules/nodejs-traceroute/src/process.ts b/node_modules/nodejs-traceroute/src/process.ts
index 9843c83..d494ca5 100644
--- a/node_modules/nodejs-traceroute/src/process.ts
+++ b/node_modules/nodejs-traceroute/src/process.ts
@@ -32,26 +32,32 @@ export abstract class Process extends events.EventEmitter {
this.emit('pid', process.pid);
let isDestinationCaptured = false;
+
+ const parseLine = (line: string) => {
+ if (!isDestinationCaptured) {
+ const destination = this.parseDestination(line);
+ if (destination !== null) {
+ this.emit('destination', destination);
+
+ isDestinationCaptured = true;
+ }
+ }
+
+ const hop = this.parseHop(line);
+ if (hop !== null) {
+ this.emit('hop', hop);
+ }
+ }
+
if (process.pid) {
readline.createInterface({
input: process.stdout,
terminal: false
- })
- .on('line', (line) => {
- if (!isDestinationCaptured) {
- const destination = this.parseDestination(line);
- if (destination !== null) {
- this.emit('destination', destination);
-
- isDestinationCaptured = true;
- }
- }
-
- const hop = this.parseHop(line);
- if (hop !== null) {
- this.emit('hop', hop);
- }
- });
+ }).on('line', parseLine);
+ readline.createInterface({
+ input: process.stderr,
+ terminal: false
+ }).on('line', parseLine);
}
}
Pretty self-explanatory. I'm sure that "validator" is a good lib, but it is 0.3mb in size. This is way too much for two functions in one file.
My offer is:
isIP
with built-it net.isIP
functionisFQDN
with https://www.npmjs.com/package/is-fqdnIf repo owner allows this, I can create a pull request.
Mentioned file is https://github.com/zulhilmizainuddin/nodejs-traceroute/blob/d06023a358434d4db3ceb23a5947c149023b8dd7/process.js
I have this:
const Traceroute = require('nodejs-traceroute');
try {
const tracer = new Traceroute();
tracer
.on('pid', (pid) => {
console.log(`pid: ${pid}`);
})
.on('destination', (destination) => {
console.log(`destination: ${destination}`);
})
.on('hop', (hop) => {
console.log(`hop: ${JSON.stringify(hop)}`);
})
.on('close', (code) => {
console.log(`close: code ${code}`);
});
tracer.trace('www.google.com');
} catch (ex) {
console.log(ex);
}
My output:
pid: 29895
hop: {"hop":1,"ip":"192.168.1.254","rtt1":"4.481 ms"}
hop: {"hop":2,"ip":"*","rtt1":"*"}
hop: {"hop":3,"ip":"*","rtt1":"*"}
hop: {"hop":4,"ip":"31.55.185.184","rtt1":"29.203 ms"}
hop: {"hop":5,"ip":"195.99.127.146","rtt1":"32.910 ms"}
hop: {"hop":6,"ip":"109.159.252.158","rtt1":"63.543 ms"}
hop: {"hop":7,"ip":"109.159.253.69","rtt1":"31.889 ms"}
hop: {"hop":8,"ip":"*","rtt1":"*"}
hop: {"hop":9,"ip":"172.253.71.200","rtt1":"28.291 ms"}
hop: {"hop":10,"ip":"108.170.246.144","rtt1":"26.056 ms"}
hop: {"hop":11,"ip":"172.217.169.36","rtt1":"30.695 ms"}
close: code 0
there is no destination logged to the console. What is wrong?
Suggestion to add explicit error message if the environment doesn't have traceroute installed.
Currently, the system fails with a more generic error message:
"spawn traceroute ENOENT"
I would appreciate a nicer message, perhaps letting you know you should install the traceroute.
Of course, this should be obvious, but still, one can forget to install it in a docker image. In such cases, knowing right away what the problem is without putting in effort would save some time and nerves.
Steps to reproduce:
Run the example code without traceroute installed on your machine.
Really nice work, thanks. Both Traceroute
and Tracert
use either ipv4 or ipv6 like said here in the docs:
By default, the program will try to resolve the name given, and choose the appropriate
protocol automatically
I would really love to have the option to force the usage of ipv4 when I need to.
What do you think?
Hey guys, just wandering if it is possible to add a parameter on .trace function, to provide a ttl_max.
How to stop the traceroute task?
In the usage example, how to stop the traceroute task?
const Traceroute = require('nodejs-traceroute');
try {
const tracer = new Traceroute();
tracer
.on('pid', (pid) => {
console.log(`pid: ${pid}`);
})
.on('destination', (destination) => {
console.log(`destination: ${destination}`);
})
.on('hop', (hop) => {
console.log(`hop: ${JSON.stringify(hop)}`);
})
.on('close', (code) => {
console.log(`close: code ${code}`);
});
tracer.trace('github.com');
} catch (ex) {
console.log(ex);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.