-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (63 loc) · 2.28 KB
/
index.js
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
#!/usr/bin/env node
const net = require("net");
const argv = require("minimist")(process.argv);
const cli = require.main === module;
const getServices = ({ search, limit }) => {
return new Promise(async resolve => {
const response = await fetch("https://svn.nmap.org/nmap/nmap-services");
const text = await response.text();
resolve(text.split("\n").map(line => {
if (!line.startsWith("#") && line.length) {
const cells = line.split("\t");
const ports = cells[1].split("/");
if (ports[1] === "tcp") return {
frequency: parseFloat(cells[2]),
service: cells[0],
port: ports[0]
};
}
}).filter(a => a).filter(({ port, service }) => {
return !search || (isNaN(search) ? service.includes(search) : search === port);
}).sort((a, b) => b.frequency - a.frequency).slice(0, limit));
});
};
const scanPort = ({ host, timeout }, { port, service }) => {
return new Promise(resolve => {
const socket = new net.Socket();
const complete = (status = "closed") => {
clearTimeout(timer);
socket.destroy();
resolve({ port, service, status });
};
socket.on("error", () => complete);
socket.on("connect", () => complete("open"));
const timer = setTimeout(complete, timeout || 250);
socket.connect(parseInt(port), host);
});
};
const portscan = options => {
return new Promise(async resolve => {
const results = [];
cli && console.log("\nscanning ports on", options.host, "\n");
for (const service of await getServices(options)) {
const result = await scanPort(options, service);
if (cli) {
const { port, service, status } = result;
console.log(port.padEnd(5), service.padEnd(30), status.toUpperCase());
} else {
results.push(result);
}
}
cli ? console.log() : resolve(results);
});
};
if (cli) {
portscan({
"host": argv._[2]?.toString(),
"search": argv._[3]?.toString(),
"timeout": argv.timeout,
"limit": argv.limit
});
} else {
module.exports = portscan;
}