-
Notifications
You must be signed in to change notification settings - Fork 14
Add port-sniffer by OperKH #28
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
Open
OperKH
wants to merge
2
commits into
kottans:master
Choose a base branch
from
OperKH:port-sniffer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 () { | ||
| 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); | ||
| })(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.