Skip to content

Commit

Permalink
v0.4.11 - Staged version bump
Browse files Browse the repository at this point in the history
Addresses unhandled exceptions with use of error domain
Creates module based libraries
Moves further towards ES6
  • Loading branch information
jas- committed Mar 21, 2018
1 parent bd1f79f commit efce9d5
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 92 deletions.
20 changes: 8 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ addons:
- nmap

language: node_js

env:
- NODE_VERSION="4"
- NODE_VERSION="5"
- NODE_VERSION="6"
- NODE_VERSION="7"
- NODE_VERSION="8"

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install nvm; git clone https://github.com/creationix/nvm.git /tmp/.nvm; source /tmp/.nvm/nvm.sh; nvm install $NODE_VERSION; fi
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"

install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update && brew install nmap; npm install; npm test; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update && sudo apt-get install -y nmap; nvm install $NODE_VERSION; npm install; npm test; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update && brew install nmap; npm install; node examples/discover.js; npm test; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update && sudo apt-get install -y nmap; npm install; npm test; fi

2 changes: 1 addition & 1 deletion examples/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const nmap = require('../');

nmap.discover(function(err, report) {
nmap.discover({verbose: true}, function(err, report) {
if (err) throw new Error(err);

for (let item in report) {
Expand Down
51 changes: 25 additions & 26 deletions lib/classes/networking.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class network {

if (splitat > 1) {

// Spllit blocks up by offset
// Split blocks up by offset
tarray = tools.chunk(blocks, splitat);
tarray.forEach(block => {
results.push(block);
Expand Down Expand Up @@ -138,43 +138,42 @@ class network {
let subnet = '';
const adapters = os.networkInterfaces();

if (obj.verbose)
console.log(adapters);

for (const iface in adapters) {

for (const dev in adapters[iface]) {
let netobj = adapters[iface][dev];
let adapter = adapters[iface][dev];

for (let adapter in netobj) {

// Skip if internal or missing netmask
if (adapter.internal || !adapter.netmask)
// Skip if internal
if (adapter.internal)
continue;

// If cidr provide use and continue
if (adapter.cidr) {
ret.push(`${adapter.cidr}`);
continue;
}

// If only netmask exists calculate the CIDR
if (adapter.netmask) {
// If cidr provide use and continue
if (adapter.cidr) {
ret.push(`${adapter.cidr}`);
continue;
}

subnet = adapter.netmask;
// If only netmask exists calculate the CIDR
if (adapter.netmask) {

if (validation.test(validation.patterns.IPv6, subnet)) {
subnet = adapter.netmask;

/* Convert netmask to CIDR notation if IPv6 */
subnet = new v6(netmask).subnet.substring(1);
} else {
if (validation.test(validation.patterns.IPv6, subnet)) {

/* Convert netmask to CIDR */
subnet = new netmask(`${adapter.address}/${subnet}`);
adapter.address = subnet.base;
subnet = subnet.bitmask;
}
/* Convert netmask to CIDR notation if IPv6 */
subnet = new v6(netmask).subnet.substring(1);
} else {

ret.push(`${adapter.address}/${subnet}`);
continue;
/* Convert netmask to CIDR */
subnet = new netmask(`${adapter.address}/${subnet}`);
adapter.address = subnet.base;
subnet = subnet.bitmask;
}

ret.push(`${adapter.address}/${subnet}`);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/classes/reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class reporting {
*/
reports(opts, report, cb) {
if ((!/object/.test(typeof report)) || (report.hasOwnProperty('code')))
return cb(new Error(report));
return cb(report);

const xml = report.join('');

Expand All @@ -37,14 +37,14 @@ class reporting {

const xmlParser = new xml2js.Parser(parserOptions);

xmlParser.parseString(xml, function parseXML(err, json) {
xmlParser.parseString(xml, (err, json) => {
if(err)
return cb(new Error(err));
return cb(err);

return cb(null, json.nmaprun);
});
} catch(err) {
return cb(new Error(err));
return cb(err);
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions lib/classes/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const async = require('async');
const merge = require('deepmerge');
const caller = require('caller-id');
const caller = require('stack-trace');
const proc = require('child_process').exec;
const emitter = require('events').EventEmitter;

Expand Down Expand Up @@ -98,7 +98,7 @@ class tools extends emitter {
const reports = [];

if (opts.range.length <= 0)
return new Error("Range of hosts could not be created");
return "Range of hosts could not be created";

Object.keys(opts.range).forEach(function blocks(block) {

Expand All @@ -111,23 +111,21 @@ class tools extends emitter {

const report = [];

const execute = proc(cmd, function exe(err, stdout, stderr) {
const execute = proc(cmd, (err, stdout, stderr) => {
if (err)
//return this.emit('error', reporting.reports(opts, err, callback));
return reporting.reports(opts, err, callback);
});

execute.stderr.on('data', function errbytes(chunk) {
execute.stderr.on('data', (chunk) => {
/* Silently discard stderr messages to not interupt scans */
});

execute.stdout.on('data', function bytes(chunk) {
execute.stdout.on('data', (chunk) => {
report.push(chunk);
});

execute.stdout.on('end', function bytesend() {
execute.stdout.on('end', () => {
if (report.length > 0)
//this.emit('report', reporting.reports(opts, report, callback));
return reporting.reports(opts, report, callback);
});
};
Expand Down Expand Up @@ -185,6 +183,7 @@ class tools extends emitter {
init(defaults, opts, cb) {
let funcs = [];
const ranges = [];
const called = caller.get()[1].getFunctionName();

/* Override 'defaults.flags' array with 'opts.flags' (prevents merge) */
if (/array/.test(typeof opts.flags))
Expand All @@ -196,9 +195,12 @@ class tools extends emitter {
if (opts.flags.indexOf('-oX -') === -1)
opts.flags.push('-oX -');

if (opts.range.length === 0) {
/* If we are in a discover mode get adapters for associated network(s) */
if (/nmap.discover/.test(called)) {

/* Bail early if we cannot determine any ranges */
if (!(opts.range = network.adapters(opts)))
return cb(new Error(validation.messages.version));
return cb(validation.messages.version);

/* Set scan options as static values for 'discover' mode */
opts.ports = '';
Expand Down
44 changes: 22 additions & 22 deletions lib/classes/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class validation {
*/
this.messages = {

version: 'Discover method requires nodejs v0.11.2 or greater',
version: 'Discover method requires nodejs v0.11.2 or greater; network ' +
'range(s) could not be determined',

path: 'Supplied path for nmap binary is invalid',

Expand All @@ -47,7 +48,8 @@ class validation {
* Regex for matching port ranges
* @ref http://stackoverflow.com/a/21075138/901697
*/
ports: /^(?:(?:^|[-,])(?:[1-9][0-9]{0,3}|[1-5][0-9]{4}|6(?:[0-4][0-9]{3}|5(?:[0-4][0-9]{2}|5(?:[0-2][0-9]|3[0-5])))))+$/,
//ports: /^(?:(?:^|[-,])(?:[1-9][0-9]{0,3}|[1-5][0-9]{4}|6(?:[0-4][0-9]{3}|5(?:[0-4][0-9]{2}|5(?:[0-2][0-9]|3[0-5])))))+$/,
ports: /\d{1,6}(?:-\d+)?/g,

/**
* @var hostname
Expand Down Expand Up @@ -99,40 +101,38 @@ class validation {
const scope = this;
const errors = [];

scope.exists(opts.nmap, function exists(result) {
scope.exists(opts.nmap, (result) => {
if (!result) {

// Try full path vs. process.env.PATH
fs.access(opts.nmap, fs.constants.F_OK|fs.constants.X_OK, e => {
if (e)
cb(errors.push(new Error(scope.messages.path)));
return cb(scope.messages.path);
});
}
});

if (opts.blocksize > 128)
errors.push(new Error(scope.messages.block));

if (!/discover/.test(opts.called)) {
if ((!opts.range) || (!/array|object/.test(typeof(opts.range))) ||
(opts.range.length === 0))
errors.push(new Error(scope.messages.range));

if (opts.range.length >= 1) {
opts.range.forEach(value => {
scope.verify(value, (err, result) => {
if (err) return errors.push(err);
});
return cb(scope.messages.block);

if ((!opts.range) || (!/array|object/.test(typeof(opts.range))) ||
(opts.range.length === 0))
return cb(scope.messages.range);

if (opts.range.length >= 1) {
opts.range.forEach(value => {
scope.verify(value, (err, result) => {
if (err) return cb(scope.messages.range);
});
}
});
}

if (opts.ports) {
if (!scope.patterns.ports.test(opts.ports))
errors.push(new Error(scope.messages.port));
return cb(scope.messages.port);
}

return (errors.length > 0) ? cb(errors) : cb(null, true);
return cb(null, true);
}


Expand All @@ -155,8 +155,8 @@ class validation {
this.test(this.patterns.IPv4Range, host)) {
return cb(null, true);
} else {
return cb(new Error(`Supplied host (${host}) did not pass validation.
${this.messages.range}`));
return cb(`Supplied host (${host}) did not pass validation.
${this.messages.range}`);
}
}

Expand Down Expand Up @@ -189,4 +189,4 @@ class validation {
}


module.exports = new validation;
module.exports = new validation;
25 changes: 11 additions & 14 deletions lib/libnmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ const nmap = function(options, fn) {
json: true
};

tools.on('report', (report) => {
console.log('myEvent triggered', report);
});

/**
* @function discover
Expand All @@ -62,21 +59,21 @@ const nmap = function(options, fn) {
* @param {Object} obj User supplied options
* @param {Function} cb User supplied callback function
*/
nmap.prototype.discover = function(obj, cb) {
nmap.prototype.discover = (obj, cb) => {
cb = cb || obj;

let opts = {};

tools.init(defaults, obj, function config(err, settings) {
tools.init(defaults, obj, (err, settings) => {
if (err)
return cb(err);
return cb(new Error(err));

opts = settings.opts;
opts.funcs = settings.funcs;

tools.worker(opts, function discover(err, data) {
tools.worker(opts, (err, data) => {
if (err)
return cb(err);
return cb(new Error(err));

cb(null, data);
});
Expand All @@ -91,21 +88,21 @@ const nmap = function(options, fn) {
* @param {Object} obj User supplied options
* @param {Function} cb User supplied callback function
*/
nmap.prototype.scan = function(obj, cb) {
nmap.prototype.scan = (obj, cb) => {
cb = cb || obj;

let opts = {};

tools.init(defaults, obj, function config(err, settings) {
tools.init(defaults, obj, (err, settings) => {
if (err)
return cb(err);
return cb(new Error(err));

opts = settings.opts;
opts.funcs = settings.funcs;

tools.worker(opts, function scan(err, data) {
tools.worker(opts, (err, data) => {
if (err)
return cb(err);
return cb(new Error(err));

cb(null, data);
});
Expand All @@ -116,7 +113,7 @@ const nmap = function(options, fn) {

/* Catch uncaught exceptions */
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
console.trace(new Error(err));
});


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
},
"dependencies": {
"async": "^2.6.0",
"caller-id": "^0.1.0",
"cidr-js": "^2.3.1",
"deepmerge": "^2.0.1",
"hasbin": "^1.2.3",
"ip-address": "^5.8.9",
"netmask": "^1.0.6",
"stack-trace": "0.0.10",
"xml2js": "^0.4.19"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const expect = chai.expect;
const ifaces = require('os').networkInterfaces();

describe('nmap', function() {
describe('discovery method', function() {
context('discovery method', function() {

this.timeout(timeout);

Expand Down
Loading

0 comments on commit efce9d5

Please sign in to comment.