From 432d887531d478bbd02655b4ced09e6dcc1afaa7 Mon Sep 17 00:00:00 2001 From: Oleksii Nelin Date: Mon, 11 Nov 2019 23:37:56 +0200 Subject: [PATCH 1/2] submission added --- package.json | 1 + submissions/xsorter/port-sniffer/package.json | 15 +++ submissions/xsorter/port-sniffer/program.js | 95 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 submissions/xsorter/port-sniffer/package.json create mode 100644 submissions/xsorter/port-sniffer/program.js diff --git a/package.json b/package.json index b3c3c7c..2f22c1e 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "lint-staged": "lint-staged", "install:void": ":", "install:kissik/port-sniffer": "cd ./submissions/kissik/port-sniffer && yarn", + "install:xsorter/port-sniffer": "cd ./submissions/xsorter/port-sniffer && yarn", "postinstall": "run-p install:**" }, "repository": { diff --git a/submissions/xsorter/port-sniffer/package.json b/submissions/xsorter/port-sniffer/package.json new file mode 100644 index 0000000..7e8d3f7 --- /dev/null +++ b/submissions/xsorter/port-sniffer/package.json @@ -0,0 +1,15 @@ +{ + "name": "port_sniffer", + "version": "1.0.0", + "description": "", + "main": "program.js", + "dependencies": { + "minimist": "^1.2.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/submissions/xsorter/port-sniffer/program.js b/submissions/xsorter/port-sniffer/program.js new file mode 100644 index 0000000..780f050 --- /dev/null +++ b/submissions/xsorter/port-sniffer/program.js @@ -0,0 +1,95 @@ +const net = require('net'); +const dns = require('dns'); +const args = require('minimist')(process.argv.slice(2)); +const range = findPortsRange(); + +const ports = { + firstPort: +range[0], + lastPort: +range[1] ? +range[1] : +range[0], + openedPorts: [] +}; + +const openedPortCheck = (host, port, checkCallback) => { + const socket = net.createConnection(port, host); + const time = socket.setTimeout(300, () => { + socket.destroy(); + checkCallback(false); + }); + + socket.on('connect', () => { + clearTimeout(time); + socket.destroy(); + process.stdout.write('.'); + ports.openedPorts.push(port); + checkCallback(true); + }); + socket.on('error', function () { + clearTimeout(time); + checkCallback(false); + }); +}; + +const showResult = host => { + if (args.help) { + process.stdout.write(messages().help); + process.exit(1); + } + if (!args.host) { + process.stdout.write(messages().noHost); + process.exit(1); + } else { + openedPortCheck(host, ports.firstPort, function nextIteration () { + if (ports.firstPort === ports.lastPort) { + if (ports.openedPorts.length) { + process.stdout.write(messages(ports.openedPorts.join()).openedPorts); + process.exit(); + } else { + process.stdout.write(messages().portsNotFound); + process.exit(1); + } + } + openedPortCheck(host, ++ports.firstPort, nextIteration); + }); + } +}; + +function findPortsRange () { + if (args.ports) { + if (args.ports.length) { + return args.ports.toString().split('-'); + } else { + process.stdout.write(messages().emptyPortsParameter); + process.exit(1); + } + } else { + return ['0', '65535']; + } +} + +function messages (openedPortsNumbers) { + return { + help: `Port sniffer CLI tool. \n + Parameters: + --ports - type ports range + --host - provide host IP adress or domain name \n + Usage example: node program.js --ports 80-87 --host google.com`, + noHost: 'Please provide host name. Type --help for usage manual.', + openedPorts: `\nports ${openedPortsNumbers} are opened`, + portsNotFound: 'No opened ports was found', + emptyPortsParameter: + 'Please provide a port numbers range or skip --port parameter for default values (0-65535)' + }; +} + +const ipLookup = () => { + return new Promise((resolve, reject) => { + dns.lookup(args.host, (err, address) => { + if (err) reject(err); + resolve(address); + }); + }); +}; + +ipLookup() + .then(res => showResult(res)) + .catch(() => process.stdout.write('Adress not found')); From 6ed27a407a6cf38eeb172f1613d90372fee460c8 Mon Sep 17 00:00:00 2001 From: Oleksii Nelin Date: Wed, 13 Nov 2019 22:43:34 +0200 Subject: [PATCH 2/2] replaced recursion with async iterator --- submissions/xsorter/port-sniffer/package.json | 3 +- submissions/xsorter/port-sniffer/program.js | 52 +++++++++++-------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/submissions/xsorter/port-sniffer/package.json b/submissions/xsorter/port-sniffer/package.json index 7e8d3f7..307e74f 100644 --- a/submissions/xsorter/port-sniffer/package.json +++ b/submissions/xsorter/port-sniffer/package.json @@ -4,7 +4,8 @@ "description": "", "main": "program.js", "dependencies": { - "minimist": "^1.2.0" + "minimist": "^1.2.0", + "async": "^3.1.0" }, "devDependencies": {}, "scripts": { diff --git a/submissions/xsorter/port-sniffer/program.js b/submissions/xsorter/port-sniffer/program.js index 780f050..462c5b8 100644 --- a/submissions/xsorter/port-sniffer/program.js +++ b/submissions/xsorter/port-sniffer/program.js @@ -1,11 +1,13 @@ +const mapLimit = require('async/mapLimit'); const net = require('net'); const dns = require('dns'); const args = require('minimist')(process.argv.slice(2)); const range = findPortsRange(); const ports = { - firstPort: +range[0], - lastPort: +range[1] ? +range[1] : +range[0], + portList: fillArrayRange((range[1] - range[0]) + 1, range[0]), + firstPort: range[0], + lastPort: range[1] ? range[1] : range[0], openedPorts: [] }; @@ -19,7 +21,7 @@ const openedPortCheck = (host, port, checkCallback) => { socket.on('connect', () => { clearTimeout(time); socket.destroy(); - process.stdout.write('.'); + console.log('.'); ports.openedPorts.push(port); checkCallback(true); }); @@ -31,24 +33,26 @@ const openedPortCheck = (host, port, checkCallback) => { const showResult = host => { if (args.help) { - process.stdout.write(messages().help); - process.exit(1); + console.table(messages().help); + return false; } if (!args.host) { - process.stdout.write(messages().noHost); - process.exit(1); + return false; } else { - openedPortCheck(host, ports.firstPort, function nextIteration () { - if (ports.firstPort === ports.lastPort) { - if (ports.openedPorts.length) { - process.stdout.write(messages(ports.openedPorts.join()).openedPorts); - process.exit(); - } else { - process.stdout.write(messages().portsNotFound); - process.exit(1); + mapLimit(ports.portList, 10, async currentPort => { + await openedPortCheck(host, currentPort, () => { + if (currentPort === ports.lastPort) { + if (ports.openedPorts.length) { + console.table( + messages(ports.openedPorts.join()).openedPorts + ); + process.exit(0); + } else { + console.table(messages().portsNotFound); + return false; + } } - } - openedPortCheck(host, ++ports.firstPort, nextIteration); + }); }); } }; @@ -56,13 +60,13 @@ const showResult = host => { function findPortsRange () { if (args.ports) { if (args.ports.length) { - return args.ports.toString().split('-'); + return args.ports.split('-').map(Number); } else { - process.stdout.write(messages().emptyPortsParameter); - process.exit(1); + console.table(messages().emptyPortsParameter); + return false; } } else { - return ['0', '65535']; + return [0, 65535]; } } @@ -81,6 +85,10 @@ function messages (openedPortsNumbers) { }; } +function fillArrayRange (arrLength, firstVal) { + return Array.from(new Array(arrLength), (_, i) => (i + firstVal)); +} + const ipLookup = () => { return new Promise((resolve, reject) => { dns.lookup(args.host, (err, address) => { @@ -92,4 +100,4 @@ const ipLookup = () => { ipLookup() .then(res => showResult(res)) - .catch(() => process.stdout.write('Adress not found')); + .catch(() => console.table('Adress not found'));