Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add port-sniffer by OperKH #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions submissions/OperKH/port-sniffer/sniffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const net = require('net');

function printFatal (message) {
process.stdout.write(message);
process.exit(1);
}

function printSuccess (message) {
process.stdout.write(message);
process.exit(0);
}

function getParamsFromArguments () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has too much responsibility. Although it is called "getParamsFromArguments", it actually not only gets the parameters, but also processes them. I suggest to split it into several functions.

This might not be a case for this small program, but usually argument parsing is reused and thus should not contain any specific logic.

return process.argv
.slice(2)
.join(' ')
.split(/\s?--/)
.reduce(function (acc, str) {
if (!str) return acc;
const [key, value] = str.split(' ');

if (key === 'ports' && value) {
acc.ports = value.split('-').map(port => parseInt(port, 10));
} else {
acc[key] = value;
}

return acc;
}, {});
}

function validateParams (params) {
if (Object.hasOwnProperty.call(params, 'help')) {
printSuccessHelp();
}

if (!Object.hasOwnProperty.call(params, 'host')) {
printFatal('Host is required, e.g. "--host 8.8.8.8"');
}
if (!params.host) {
printFatal('Host value must be provided, e.g. "--host 8.8.8.8"');
}

if (Object.hasOwnProperty.call(params, 'ports')) {
if (!params.ports) {
printFatal('Ports value must be provided, e.g. "--ports 300-1024"');
}
if (!Array.isArray(params.ports) || params.ports.length !== 2) {
printFatal('Ports format invalid, must be e.g. "--ports 300-1024"');
}
const [startPort, endPort] = params.ports;
if (isNaN(startPort) || startPort < 0) {
printFatal('Start port format invalid');
}
if (isNaN(endPort) || endPort < 0) {
printFatal('End port format invalid');
}
if (endPort > 65535) {
printFatal('End port must be between 0-65535');
}
if (startPort > endPort) {
printFatal('End port must be bigger then start port');
}
}
}

function printSuccessHelp () {
printSuccess(`NAME
TCP sniffer - scans open ports on specific host.

OPTIONS
--help
Output a usage message and exit.

--host
Set a host for scan.
E.g. "--host 127.0.0.1".

--ports
Set ports to scan.
E.g. "--ports 300-1024".
Default values: 0-65535.

EXAMPLES
node sniffer.js --host localhost
node sniffer.js --host localhost --ports 300-1024
`);
}

function sniffConnectionAvailabilityAsync (port, host) {
return new Promise(function (resolve) {
const socket = new net.Socket();
socket.setTimeout(300);

socket.on('connect', function () {
socket.destroy();
resolve(true);
});
socket.on('timeout', function () {
socket.destroy();
resolve(false);
});
socket.on('error', function () {
socket.destroy();
resolve(false);
});

socket.connect(port, host);
});
}

async function scanAsync (host, port, portLimit, availablePorts = []) {
if (port > portLimit) {
return availablePorts;
}
const isPortOpen = await sniffConnectionAvailabilityAsync(port, host);
if (isPortOpen) {
process.stdout.write('.');
availablePorts.push(port);
}
return scanAsync(host, port + 1, portLimit, availablePorts);
}

(async function () {
const params = getParamsFromArguments();
validateParams(params);
const { host, ports = [] } = params;
const [startPort = 0, endPort = 65535] = ports;
const openedPorts = await scanAsync(host, startPort, endPort);
const result = openedPorts.length ? `\n${openedPorts.join(',')} ports are opened` : `\nNo open ports on host: ${host}`;
printSuccess(result);
})();