-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 () { | ||
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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( |
||
}); | ||
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); | ||
})(); |
There was a problem hiding this comment.
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.