From 05ddcd3e6186628e4d62c551435c3624df38a6b6 Mon Sep 17 00:00:00 2001 From: Vitamin Date: Thu, 31 Oct 2019 22:22:37 +0200 Subject: [PATCH 1/6] port-sniffer task --- submissions/Vitamin/port-sniffer/helpers.js | 26 ++++++ submissions/Vitamin/port-sniffer/sniffer.js | 91 +++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 submissions/Vitamin/port-sniffer/helpers.js create mode 100644 submissions/Vitamin/port-sniffer/sniffer.js diff --git a/submissions/Vitamin/port-sniffer/helpers.js b/submissions/Vitamin/port-sniffer/helpers.js new file mode 100644 index 0000000..e33c335 --- /dev/null +++ b/submissions/Vitamin/port-sniffer/helpers.js @@ -0,0 +1,26 @@ +const help = ` + Usage: sniffer.js [--help] [--ports -] [--host ] + + These are commands used in sniffer application: + + host Host address, IP or URL. + Examples: --host ukr.net, + --host 172.217.3.110 + + ports Ports range. Numbers must be more than 0 and less than 65535. + Examples: --port 30-562, + --port 1-65535. + + help Show all commands. +`; +const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); + +const composeAsync = (...fns) => async x => { + let res = x; + for (const fn of fns) { + res = await fn(res); + } + return res; +}; + +module.exports = { help, compose, composeAsync }; diff --git a/submissions/Vitamin/port-sniffer/sniffer.js b/submissions/Vitamin/port-sniffer/sniffer.js new file mode 100644 index 0000000..abcbe0f --- /dev/null +++ b/submissions/Vitamin/port-sniffer/sniffer.js @@ -0,0 +1,91 @@ +const Net = require("net"); +const dns = require("dns").promises; +const { help } = require("./helpers"); + +const [, , ...arguments] = process.argv; +const [minPortValue, maxPortValue] = [0, 65535]; + +const ipToURL = async host => { + try { + const result = await dns.lookup(host); + return await result.address; + } catch (err) { + throw Error("Invalid host"); + } +}; + +const isPortInRange = port => port >= minPortValue && port <= maxPortValue; + +const getPortsRange = ports => { + const [firstPort, last] = ports.split("-"); + const lastPort = isPortInRange(last) ? last : firstPort; + if (isPortInRange(firstPort)) { + return [firstPort, lastPort]; + } else { + console.log("Invalid ports range"); + process.exit(1); + } +}; + +const tryToConnect = (host, port) => { + const client = new Net.Socket(); + client.setTimeout(300); + client.connect({ port, host }); + return new Promise(resolve => { + client.on("connect", () => { + process.stdout.write("."); + client.destroy(); + return resolve(port); + }); + client.on("timeout", () => { + client.destroy(); + return resolve(false); + }); + client.on("error", () => { + client.destroy(); + return resolve(false); + }); + }); +}; + +const getOpenedPorts = async ({ host, firstPort, lastPort }) => { + const promises = []; + for (let i = firstPort; i <= lastPort; i++) { + promises.push(tryToConnect(host, i)); + } + const ports = await Promise.all(promises); + return ports.filter(Boolean); +}; + +parseArguments = async args => { + if (args.includes("--help")) { + console.log(help); + process.exit(0); + } else if ( + args.length < 4 || + !args.includes("--ports") || + !args.includes("--host") + ) { + console.log("Use --help to correct input"); + process.exit(1); + } + const [firstPort, lastPort] = getPortsRange(args[1]); + const host = await ipToURL(args[3]); + return { host, firstPort, lastPort }; +}; + +const getMessageToLog = openPorts => { + if (openPorts.length > 1) return `\n${openPorts} ports are opened`; + else if (openPorts.length === 1) return `\n${openPorts} port is opened`; + return "\nAll ports in range are closed"; +}; + +const sniffing = async args => { + const { host, firstPort, lastPort } = await parseArguments(args); + return await getOpenedPorts({ host, firstPort, lastPort }); +}; + +sniffing(arguments) + .then(getMessageToLog) + .then(console.log); + From 3880b349d1bf4a843e8c3f6f8ee04133c4116d5d Mon Sep 17 00:00:00 2001 From: Vitamin Date: Mon, 4 Nov 2019 21:43:48 +0200 Subject: [PATCH 2/6] fix according to code review --- submissions/Vitamin/port-sniffer/helpers.js | 12 +--- submissions/Vitamin/port-sniffer/sniffer.js | 63 ++++++++++++--------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/submissions/Vitamin/port-sniffer/helpers.js b/submissions/Vitamin/port-sniffer/helpers.js index e33c335..cd94c92 100644 --- a/submissions/Vitamin/port-sniffer/helpers.js +++ b/submissions/Vitamin/port-sniffer/helpers.js @@ -13,14 +13,6 @@ const help = ` help Show all commands. `; -const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); +const isDefined = value => value != null && value !== false; -const composeAsync = (...fns) => async x => { - let res = x; - for (const fn of fns) { - res = await fn(res); - } - return res; -}; - -module.exports = { help, compose, composeAsync }; +module.exports = { help, isDefined }; diff --git a/submissions/Vitamin/port-sniffer/sniffer.js b/submissions/Vitamin/port-sniffer/sniffer.js index abcbe0f..476ba90 100644 --- a/submissions/Vitamin/port-sniffer/sniffer.js +++ b/submissions/Vitamin/port-sniffer/sniffer.js @@ -1,8 +1,8 @@ -const Net = require("net"); -const dns = require("dns").promises; -const { help } = require("./helpers"); +const Net = require('net'); +const dns = require('dns').promises; +const { help, isDefined } = require('./helpers'); -const [, , ...arguments] = process.argv; +const [, , ...processArgs] = process.argv; const [minPortValue, maxPortValue] = [0, 65535]; const ipToURL = async host => { @@ -10,21 +10,20 @@ const ipToURL = async host => { const result = await dns.lookup(host); return await result.address; } catch (err) { - throw Error("Invalid host"); + throw Error('Invalid host'); } }; const isPortInRange = port => port >= minPortValue && port <= maxPortValue; const getPortsRange = ports => { - const [firstPort, last] = ports.split("-"); + const [firstPort, last] = ports.split('-'); const lastPort = isPortInRange(last) ? last : firstPort; if (isPortInRange(firstPort)) { return [firstPort, lastPort]; - } else { - console.log("Invalid ports range"); - process.exit(1); } + process.exit(1); + throw Error('Invalid ports range'); }; const tryToConnect = (host, port) => { @@ -32,16 +31,16 @@ const tryToConnect = (host, port) => { client.setTimeout(300); client.connect({ port, host }); return new Promise(resolve => { - client.on("connect", () => { - process.stdout.write("."); + client.on('connect', () => { + process.stdout.write('.'); client.destroy(); return resolve(port); }); - client.on("timeout", () => { + client.on('timeout', () => { client.destroy(); return resolve(false); }); - client.on("error", () => { + client.on('error', () => { client.destroy(); return resolve(false); }); @@ -50,24 +49,26 @@ const tryToConnect = (host, port) => { const getOpenedPorts = async ({ host, firstPort, lastPort }) => { const promises = []; + // eslint-disable-next-line no-plusplus for (let i = firstPort; i <= lastPort; i++) { promises.push(tryToConnect(host, i)); } const ports = await Promise.all(promises); - return ports.filter(Boolean); + return ports.filter(isDefined); }; -parseArguments = async args => { - if (args.includes("--help")) { +const parseArguments = async args => { + if (args.includes('--help')) { + // eslint-disable-next-line no-console console.log(help); process.exit(0); } else if ( args.length < 4 || - !args.includes("--ports") || - !args.includes("--host") + !args.includes('--ports') || + !args.includes('--host') ) { - console.log("Use --help to correct input"); process.exit(1); + throw Error('Use --help to correct input'); } const [firstPort, lastPort] = getPortsRange(args[1]); const host = await ipToURL(args[3]); @@ -75,17 +76,23 @@ parseArguments = async args => { }; const getMessageToLog = openPorts => { - if (openPorts.length > 1) return `\n${openPorts} ports are opened`; - else if (openPorts.length === 1) return `\n${openPorts} port is opened`; - return "\nAll ports in range are closed"; + let result; + if (openPorts.length > 1) { + result = `\n${openPorts} ports are opened`; + } else if (openPorts.length === 1) { + result = `\n${openPorts} port is opened`; + } else { + result = '\nAll ports in range are closed'; + } + return result; }; -const sniffing = async args => { +const sniff = async args => { const { host, firstPort, lastPort } = await parseArguments(args); - return await getOpenedPorts({ host, firstPort, lastPort }); + return getOpenedPorts({ host, firstPort, lastPort }); }; -sniffing(arguments) - .then(getMessageToLog) - .then(console.log); - +sniff(processArgs) + .then(getMessageToLog) + // eslint-disable-next-line no-console + .then(console.log); From 39659ed9c2bebecaafae3abd7731330ba1fb2822 Mon Sep 17 00:00:00 2001 From: Vitamin Date: Sat, 9 Nov 2019 21:42:36 +0200 Subject: [PATCH 3/6] empty commit to pass tests --- submissions/Vitamin/port-sniffer/helpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/submissions/Vitamin/port-sniffer/helpers.js b/submissions/Vitamin/port-sniffer/helpers.js index cd94c92..7a57ddc 100644 --- a/submissions/Vitamin/port-sniffer/helpers.js +++ b/submissions/Vitamin/port-sniffer/helpers.js @@ -13,6 +13,7 @@ const help = ` help Show all commands. `; + const isDefined = value => value != null && value !== false; module.exports = { help, isDefined }; From 45d1bbe175467ab7595c6f0de98ea17a97b91e15 Mon Sep 17 00:00:00 2001 From: Vitamin Date: Sat, 9 Nov 2019 21:49:42 +0200 Subject: [PATCH 4/6] modify to pass tests --- submissions/Vitamin/port-sniffer/sniffer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/submissions/Vitamin/port-sniffer/sniffer.js b/submissions/Vitamin/port-sniffer/sniffer.js index 476ba90..2783995 100644 --- a/submissions/Vitamin/port-sniffer/sniffer.js +++ b/submissions/Vitamin/port-sniffer/sniffer.js @@ -22,8 +22,10 @@ const getPortsRange = ports => { if (isPortInRange(firstPort)) { return [firstPort, lastPort]; } + // eslint-disable-next-line no-console + console.log('Invalid ports range'); process.exit(1); - throw Error('Invalid ports range'); + return undefined; }; const tryToConnect = (host, port) => { @@ -67,8 +69,9 @@ const parseArguments = async args => { !args.includes('--ports') || !args.includes('--host') ) { + // eslint-disable-next-line no-console + console.log('Use --help to correct input'); process.exit(1); - throw Error('Use --help to correct input'); } const [firstPort, lastPort] = getPortsRange(args[1]); const host = await ipToURL(args[3]); From 49873e34cbfd08066cbb7a029df3d99f9819f1ae Mon Sep 17 00:00:00 2001 From: Vitamin Date: Sat, 9 Nov 2019 21:52:30 +0200 Subject: [PATCH 5/6] modify to pass tests --- submissions/Vitamin/port-sniffer/sniffer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/submissions/Vitamin/port-sniffer/sniffer.js b/submissions/Vitamin/port-sniffer/sniffer.js index 2783995..549f4bb 100644 --- a/submissions/Vitamin/port-sniffer/sniffer.js +++ b/submissions/Vitamin/port-sniffer/sniffer.js @@ -24,7 +24,6 @@ const getPortsRange = ports => { } // eslint-disable-next-line no-console console.log('Invalid ports range'); - process.exit(1); return undefined; }; From 9b4dbe8d13db32210765d3ae20fe293d0513df7e Mon Sep 17 00:00:00 2001 From: Vitamin Date: Mon, 11 Nov 2019 19:39:12 +0200 Subject: [PATCH 6/6] code review --- submissions/Vitamin/port-sniffer/helpers.js | 6 +++--- submissions/Vitamin/port-sniffer/sniffer.js | 16 ++++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/submissions/Vitamin/port-sniffer/helpers.js b/submissions/Vitamin/port-sniffer/helpers.js index 7a57ddc..cc531e1 100644 --- a/submissions/Vitamin/port-sniffer/helpers.js +++ b/submissions/Vitamin/port-sniffer/helpers.js @@ -8,12 +8,12 @@ const help = ` --host 172.217.3.110 ports Ports range. Numbers must be more than 0 and less than 65535. - Examples: --port 30-562, - --port 1-65535. + Examples: --ports 30-562, + --ports 1-65535. help Show all commands. `; -const isDefined = value => value != null && value !== false; +const isDefined = value => value != null; module.exports = { help, isDefined }; diff --git a/submissions/Vitamin/port-sniffer/sniffer.js b/submissions/Vitamin/port-sniffer/sniffer.js index 549f4bb..0a59871 100644 --- a/submissions/Vitamin/port-sniffer/sniffer.js +++ b/submissions/Vitamin/port-sniffer/sniffer.js @@ -5,10 +5,10 @@ const { help, isDefined } = require('./helpers'); const [, , ...processArgs] = process.argv; const [minPortValue, maxPortValue] = [0, 65535]; -const ipToURL = async host => { +const ipFromHostname = async host => { try { const result = await dns.lookup(host); - return await result.address; + return result.address; } catch (err) { throw Error('Invalid host'); } @@ -16,15 +16,14 @@ const ipToURL = async host => { const isPortInRange = port => port >= minPortValue && port <= maxPortValue; +// eslint-disable-next-line consistent-return const getPortsRange = ports => { const [firstPort, last] = ports.split('-'); const lastPort = isPortInRange(last) ? last : firstPort; if (isPortInRange(firstPort)) { return [firstPort, lastPort]; } - // eslint-disable-next-line no-console console.log('Invalid ports range'); - return undefined; }; const tryToConnect = (host, port) => { @@ -39,11 +38,11 @@ const tryToConnect = (host, port) => { }); client.on('timeout', () => { client.destroy(); - return resolve(false); + return resolve(null); }); client.on('error', () => { client.destroy(); - return resolve(false); + return resolve(null); }); }); }; @@ -60,7 +59,6 @@ const getOpenedPorts = async ({ host, firstPort, lastPort }) => { const parseArguments = async args => { if (args.includes('--help')) { - // eslint-disable-next-line no-console console.log(help); process.exit(0); } else if ( @@ -68,12 +66,11 @@ const parseArguments = async args => { !args.includes('--ports') || !args.includes('--host') ) { - // eslint-disable-next-line no-console console.log('Use --help to correct input'); process.exit(1); } const [firstPort, lastPort] = getPortsRange(args[1]); - const host = await ipToURL(args[3]); + const host = await ipFromHostname(args[3]); return { host, firstPort, lastPort }; }; @@ -96,5 +93,4 @@ const sniff = async args => { sniff(processArgs) .then(getMessageToLog) - // eslint-disable-next-line no-console .then(console.log);