From 04ca1e1ebf49275d468bfc4d442bbd77083b5958 Mon Sep 17 00:00:00 2001 From: Valerio Riva Date: Sun, 18 Sep 2016 23:58:14 +0200 Subject: [PATCH 1/2] Added a new parameter to kill traceroute syscall when a specified amount of null hops is received in a row --- package.json | 2 +- test/index.js | 13 +++++++++++++ traceroute.js | 27 ++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 42de213..25266ad 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "test": "test" }, "scripts": { - "test": "lab -v -m 20000 -p" + "test": "lab -v -m 60000 -p" }, "keywords": [ "traceroute", diff --git a/test/index.js b/test/index.js index 38676a4..932a49c 100644 --- a/test/index.js +++ b/test/index.js @@ -36,4 +36,17 @@ describe('Traceroute', () => { done(); }); }); + + it('traces a fake route and quits after 5 hops in a row', (done) => { + + Traceroute.trace('127.0.0.127', (err, hops) => { + + expect(err).to.not.exist(); + expect(hops).to.exist(); + for (let hop of hops) { + expect(hop).to.equal(false); + } + done(); + }); + }); }); diff --git a/traceroute.js b/traceroute.js index 15414be..925f369 100644 --- a/traceroute.js +++ b/traceroute.js @@ -8,7 +8,7 @@ const Net = require('net'); const Os = require('os'); const Util = require('util'); - +const defaultMaxNullHops = 5; const internals = {}; @@ -18,10 +18,18 @@ internals.isWin = /^win/.test(Os.platform()); module.exports = internals.Traceroute = {}; -internals.Traceroute.trace = function (host, callback) { +internals.Traceroute.trace = function (host, maxNullHops, callback) { - const Emitter = function () { + if (typeof maxNullHops === 'function' && callback === undefined) { + callback = maxNullHops; + maxNullHops = defaultMaxNullHops; + } else if (maxNullHops === undefined) { + maxNullHops = defaultMaxNullHops; + } else if (!Number.isInteger(maxNullHops)) { + throw new Error('second parameter must be a callback or an integer'); + } + const Emitter = function () { EventEmitter.call(this); }; Util.inherits(Emitter, EventEmitter); @@ -39,6 +47,7 @@ internals.Traceroute.trace = function (host, callback) { const hops = []; let counter = 0; + let nullHops = 0; traceroute.stdout.on('data', (data) => { ++counter; @@ -54,6 +63,18 @@ internals.Traceroute.trace = function (host, callback) { const hop = internals.parseHop(result); hops.push(hop); emitter.emit('hop', hop); + + if (hop === false) { + // count every null hop received + nullHops++; + } else { + // reset nullHops counter when a valid hop is received + nullHops = 0; + } + // send a sigint to kill the traceroute process when it reach $maxNullHops hops in a row + if (nullHops >= maxNullHops) { + traceroute.kill('SIGINT'); + } }); traceroute.on('close', (code) => { From 8705e427543728cca3b87d7f965dbb03a0c301a9 Mon Sep 17 00:00:00 2001 From: Valerio Riva Date: Mon, 19 Sep 2016 00:31:20 +0200 Subject: [PATCH 2/2] added ability to pass "false" to deactivate the maxNullHops functionality --- traceroute.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/traceroute.js b/traceroute.js index 925f369..81983ae 100644 --- a/traceroute.js +++ b/traceroute.js @@ -25,8 +25,6 @@ internals.Traceroute.trace = function (host, maxNullHops, callback) { maxNullHops = defaultMaxNullHops; } else if (maxNullHops === undefined) { maxNullHops = defaultMaxNullHops; - } else if (!Number.isInteger(maxNullHops)) { - throw new Error('second parameter must be a callback or an integer'); } const Emitter = function () { @@ -72,7 +70,7 @@ internals.Traceroute.trace = function (host, maxNullHops, callback) { nullHops = 0; } // send a sigint to kill the traceroute process when it reach $maxNullHops hops in a row - if (nullHops >= maxNullHops) { + if (maxNullHops && nullHops >= maxNullHops) { traceroute.kill('SIGINT'); } });