-
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
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,15 @@ | ||
{ | ||
"name": "port_sniffer", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "program.js", | ||
"dependencies": { | ||
"minimist": "^1.2.0" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const net = require('net'); | ||
const dns = require('dns'); | ||
const args = require('minimist')(process.argv.slice(2)); | ||
const range = findPortsRange(); | ||
|
||
const ports = { | ||
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(); | ||
process.stdout.write('.'); | ||
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. Dont use process.stdout.write 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, changed to console.log |
||
ports.openedPorts.push(port); | ||
checkCallback(true); | ||
}); | ||
socket.on('error', function () { | ||
clearTimeout(time); | ||
checkCallback(false); | ||
}); | ||
}; | ||
|
||
const showResult = host => { | ||
if (args.help) { | ||
process.stdout.write(messages().help); | ||
process.exit(1); | ||
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. Bad practice to fail app with such aggressive methods :) 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. Understand, i'll just return false than |
||
} | ||
if (!args.host) { | ||
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. If no hosts -> let's ask again about hosts? 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. Removed this message |
||
process.stdout.write(messages().noHost); | ||
process.exit(1); | ||
} else { | ||
openedPortCheck(host, ports.firstPort, function nextIteration () { | ||
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. This recursion is not effective due to huge memory usage, slowness, and other reasons. I propose to you rewrite this to one of the ways:
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. Thanks, i changed recursion with mapLimit |
||
if (ports.firstPort === ports.lastPort) { | ||
if (ports.openedPorts.length) { | ||
process.stdout.write(messages(ports.openedPorts.join()).openedPorts); | ||
process.exit(); | ||
} else { | ||
process.stdout.write(messages().portsNotFound); | ||
process.exit(1); | ||
} | ||
} | ||
openedPortCheck(host, ++ports.firstPort, nextIteration); | ||
}); | ||
} | ||
}; | ||
|
||
function findPortsRange () { | ||
if (args.ports) { | ||
if (args.ports.length) { | ||
return args.ports.toString().split('-'); | ||
} else { | ||
process.stdout.write(messages().emptyPortsParameter); | ||
process.exit(1); | ||
} | ||
} 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)' | ||
}; | ||
} | ||
|
||
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(() => process.stdout.write('Adress not found')); |
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.
Don
t need every time add
+` for converting type to number.Just guarantee, that findPortsRange will return numbers
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.
Thanks for advice, changed type in function's return