-
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 Xsorter #32
Open
xsorter
wants to merge
2
commits into
kottans:master
Choose a base branch
from
xsorter: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 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
This file contains 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,16 @@ | ||
{ | ||
"name": "port_sniffer", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "program.js", | ||
"dependencies": { | ||
"minimist": "^1.2.0", | ||
"async": "^3.1.0" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains 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,103 @@ | ||
const mapLimit = require('async/mapLimit'); | ||
const net = require('net'); | ||
const dns = require('dns'); | ||
const args = require('minimist')(process.argv.slice(2)); | ||
const range = findPortsRange(); | ||
|
||
const ports = { | ||
portList: fillArrayRange((range[1] - range[0]) + 1, range[0]), | ||
firstPort: range[0], | ||
lastPort: range[1] ? range[1] : range[0], | ||
openedPorts: [] | ||
}; | ||
|
||
const openedPortCheck = (host, port, checkCallback) => { | ||
const socket = net.createConnection(port, host); | ||
const time = socket.setTimeout(300, () => { | ||
socket.destroy(); | ||
checkCallback(false); | ||
}); | ||
|
||
socket.on('connect', () => { | ||
clearTimeout(time); | ||
socket.destroy(); | ||
console.log('.'); | ||
ports.openedPorts.push(port); | ||
checkCallback(true); | ||
}); | ||
socket.on('error', function () { | ||
clearTimeout(time); | ||
checkCallback(false); | ||
}); | ||
}; | ||
|
||
const showResult = host => { | ||
if (args.help) { | ||
console.table(messages().help); | ||
return false; | ||
} | ||
if (!args.host) { | ||
return false; | ||
} else { | ||
mapLimit(ports.portList, 10, async currentPort => { | ||
await openedPortCheck(host, currentPort, () => { | ||
if (currentPort === ports.lastPort) { | ||
if (ports.openedPorts.length) { | ||
console.table( | ||
messages(ports.openedPorts.join()).openedPorts | ||
); | ||
process.exit(0); | ||
} else { | ||
console.table(messages().portsNotFound); | ||
return false; | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
function findPortsRange () { | ||
if (args.ports) { | ||
if (args.ports.length) { | ||
return args.ports.split('-').map(Number); | ||
} else { | ||
console.table(messages().emptyPortsParameter); | ||
return false; | ||
} | ||
} else { | ||
return [0, 65535]; | ||
} | ||
} | ||
|
||
function messages (openedPortsNumbers) { | ||
return { | ||
help: `Port sniffer CLI tool. \n | ||
Parameters: | ||
--ports - type ports range | ||
--host - provide host IP adress or domain name \n | ||
Usage example: node program.js --ports 80-87 --host google.com`, | ||
noHost: 'Please provide host name. Type --help for usage manual.', | ||
openedPorts: `\nports ${openedPortsNumbers} are opened`, | ||
portsNotFound: 'No opened ports was found', | ||
emptyPortsParameter: | ||
'Please provide a port numbers range or skip --port parameter for default values (0-65535)' | ||
}; | ||
} | ||
|
||
function fillArrayRange (arrLength, firstVal) { | ||
return Array.from(new Array(arrLength), (_, i) => (i + firstVal)); | ||
} | ||
|
||
const ipLookup = () => { | ||
return new Promise((resolve, reject) => { | ||
dns.lookup(args.host, (err, address) => { | ||
if (err) reject(err); | ||
resolve(address); | ||
}); | ||
}); | ||
}; | ||
|
||
ipLookup() | ||
.then(res => showResult(res)) | ||
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. Here you can use console.table to write better output 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. Done |
||
.catch(() => console.table('Adress not found')); |
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.
If no hosts -> let's ask again about hosts?
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.
Removed this message