-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathprogram.js
More file actions
95 lines (87 loc) · 2.48 KB
/
Copy pathprogram.js
File metadata and controls
95 lines (87 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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('.');
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);
}
if (!args.host) {
process.stdout.write(messages().noHost);
process.exit(1);
} else {
openedPortCheck(host, ports.firstPort, function nextIteration () {
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))
.catch(() => process.stdout.write('Adress not found'));