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 1 commit
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
129 changes: 129 additions & 0 deletions submissions/OperKH/port-sniffer/sniffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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.

if (process.argv.includes('--help')) {
printSuccessHelp();
}
if (!process.argv.includes('--host')) {
printFatal('Host is required, e.g. "--host 8.8.8.8"');
}
return process.argv
.slice(2)
.join(' ')
.split(/\s?--/)
.reduce(function (acc, str) {
if (!str) return acc;
const [key, value] = str.split(' ');
switch (key) {
case 'help':
break;
case 'host':
if (!value) {
printFatal('Host value must be provided, e.g. "--host 8.8.8.8"');
}
acc[key] = value;
break;
case 'ports':
if (!value) {
printFatal('Ports value must be provided, e.g. "--ports 300-1024"');
}
if (!value.includes('-')) {
printFatal('Ports format invalid, must be e.g. "--ports 300-1024"');
} else {
const [startPort, endPort] = value.split('-').map(port => parseInt(port, 10));
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');
}
acc.startPort = startPort;
acc.endPort = endPort;
}
break;
default:
acc[key] = value;
}
return acc;
}, {});
}

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, reject) {
const socket = new net.Socket();
socket.setTimeout(300);

socket.on('connect', function () {
socket.destroy();
resolve();
});
socket.on('timeout', function () {
socket.destroy();
reject(new Error('timeout'));
Copy link
Member

Choose a reason for hiding this comment

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

I think that in terms of this program timeout is not an error, but expected behavior for closed port. Your solution is ok, but to me it looks cleaner to resolve the Promise with boolean value (true for open ports and false for closed ports). This way you don't need to have empty catch block.

});
socket.on('error', function (err) {
socket.destroy();
reject(err);
});

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

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

(async function () {
const { host, startPort = 0, endPort = 65535 } = getParamsFromArguments();
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);
})();