Skip to content

Commit e6279e2

Browse files
SiusarnaAMashoshyna
authored andcommitted
Add working sniffer.js (#8)
* port sniffer completed * implemented suggested improvements adn added function import * linter installed * Port-sniffer task completed * new suggestions implemented * Review changes * Block deletion * airbnb linter applied * some more linting problems resolved * eslint fix * Revert "eslint fix" This reverts commit 76e891a. * resolved conflicts * data structures task completed * comment explaining api added to main.js * moved responses to separate functions * make get request react only on list * Add working sniffer.js * moved parsing json request to separate function * bug in linked list isert corrected * Review change * some corrections * some more corrections * review sniffer v 2.0 * remove ionide folder * Monorepo chores (#19) * chores: Add subprojects CI build checks support * chores: Add a note on yarn for consistency * chores: Add .editorconfig to enforce UNIX style LF and end-of-file newline * chores: Add pull request template * submissions/Zakkarat/port-sniffer: Fix code style * Add eslint disable rule * Fix newline * submissions/eneoff/data_structures: Fix CRLF (#23) * Update README.md (#25) * restructured instructions so that student makes any file operations when in a feature branch context to avoid commits to `master` before student reads instructions on pull request initialization * added instructions on PR updates * Port sniffer (#11) * Add port-sniffer homework * Add code after prettier and eslint check * Fix eslint errors * Add dependencies: commander, async * Fixes respectively to PR 'Port sniffer' #11, #11 * Delete dependencies from package.json * Add all package-lock.jsons into .gitignore * Add .gitignore in port-sniffer subdirectory * Remove global package-lock.json * Resolve integration with CI manager * kissik/port-sniffer: integrate project into root package * chores: make eslint ignoring nested node_modules * change eslint+airbnb to semistandard (#24) * change eslint+airbnb to semistandard * fix linting errors * submissions/kissik/port-sniffer: Fix linting errors * Add working sniffer.js * Review change * review sniffer v 2.0 * change eslint and fix bug
1 parent 372db49 commit e6279e2

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
const net = require('net');
2+
const dns = require('dns').promises;
3+
4+
const args = process.argv;
5+
const help = `
6+
Simple port sniffer
7+
This script takes 2 parameters:
8+
9+
--host obligatory parameter which defines the host address for a port scan.
10+
Address must be specified after the parameter.
11+
It can be specified as IP address or URL.
12+
13+
--ports Parameter which specifies port range.
14+
After the parameter two numbers from 0 to 65535 must be specified with a delimiter '-' between them.
15+
16+
Example: node sniffer.js --host google.com --port 100-2345
17+
18+
--help Parameter will show this instructions.
19+
`;
20+
21+
const validHost = async host => {
22+
if (!host) {
23+
process.stdout.write(
24+
'Invalid host parameter. Try node sniffer.js --help \n'
25+
);
26+
}
27+
try {
28+
return await dns
29+
.lookup(host)
30+
.then(({ address }) => address)
31+
.catch(error => {
32+
process.stdout.write(
33+
`Invalit host name. ${error}. Try node sniffer.js --help \n`
34+
);
35+
return 0;
36+
});
37+
} catch (e) {
38+
process.stdout.write(e);
39+
return 0;
40+
}
41+
};
42+
43+
const checkRangePort = port => {
44+
if (port[0] < 0 || port[1] > 65535 || port[2] || port[0] > port[1]) {
45+
return false;
46+
}
47+
return true;
48+
};
49+
50+
const validatorForPort = portRange => {
51+
let validPort = [1, 65535];
52+
if (portRange) {
53+
if (checkRangePort(portRange)) {
54+
validPort = portRange;
55+
} else {
56+
process.stdout.write('Invalid port range. See help \n');
57+
}
58+
} else {
59+
process.stdout.write(
60+
'No port range specified. Setting to default. For more information try --help \n'
61+
);
62+
}
63+
return validPort;
64+
};
65+
66+
const checkConnection = async (port, host) => {
67+
return new Promise(resolve => {
68+
const socket = new net.Socket();
69+
socket.setTimeout(300);
70+
socket.on('connect', () => {
71+
process.stdout.write('.');
72+
resolve(port);
73+
socket.destroy();
74+
});
75+
socket.on('timeout', () => {
76+
resolve(false);
77+
socket.destroy();
78+
});
79+
socket.on('error', () => {
80+
resolve(false);
81+
socket.destroy();
82+
});
83+
socket.connect(port, host);
84+
});
85+
};
86+
87+
const findOpenPorts = async (ports, host) => {
88+
const openPorts = [];
89+
for (let i = ports[0]; i <= ports[1]; i += 1) {
90+
openPorts.push(checkConnection(i, host));
91+
}
92+
return Promise.all(openPorts).then(values => values.filter(Number.isFinite));
93+
};
94+
95+
const sniffer = async ({ helpFlag, port, host }) => {
96+
if (helpFlag) {
97+
process.stdout.write(help);
98+
}
99+
if (!host && !port) {
100+
process.stdout.write('No arguments. Try node sniffer.js --help. \n');
101+
return 0;
102+
}
103+
const checkPort = validatorForPort(port);
104+
const checkHost = await validHost(host);
105+
const result = await findOpenPorts(checkPort, checkHost);
106+
process.stdout.write('\n');
107+
for (let i = 0; i < result.length; i += 1) {
108+
if (i !== result.length - 1) {
109+
process.stdout.write(`${result[i].toString()},`);
110+
} else {
111+
process.stdout.write(result[i].toString());
112+
}
113+
}
114+
process.stdout.write(' ports are opened \n');
115+
return 0;
116+
};
117+
118+
const parseArgs = () => {
119+
const objWithInputData = {
120+
helpFlag: false
121+
};
122+
if (args.indexOf('--help') !== -1) {
123+
objWithInputData.helpFlag = true;
124+
}
125+
const indexPort = args.indexOf('--ports');
126+
if (indexPort !== -1) {
127+
objWithInputData.port = args[indexPort + 1].split('-').map(Number);
128+
}
129+
const indexHost = args.indexOf('--host');
130+
if (indexHost !== -1) objWithInputData.host = args[indexHost + 1];
131+
return objWithInputData;
132+
};
133+
134+
sniffer(parseArgs());

0 commit comments

Comments
 (0)