-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathportScan.js
More file actions
46 lines (38 loc) · 1.56 KB
/
Copy pathportScan.js
File metadata and controls
46 lines (38 loc) · 1.56 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
var net = require('net');
// the machine to scan
var host = 'localhost';
// starting from port number
var start = 1;
// to port number
var end = 10000;
// sockets should timeout asap to ensure no resources are wasted
// but too low a timeout value increases the likelyhood of missing open sockets, so be careful
var timeout = 2000;
// the port scanning loop
while (start <= end) {
// it is always good to give meaningful names to your variables
// since the context is changing, we use `port` to refer to current port to scan
var port = start;
// we create an anonynous function, pass the current port, and operate on it
// the reason we encapsulate the socket creation process is because we want to preseve the value of `port` for the callbacks
(function(port) {
// console.log('CHECK: ' + port);
var s = new net.Socket();
s.setTimeout(timeout, function() { s.destroy(); });
s.connect(port, host, function() {
console.log('OPEN: ' + port);
// we don't destroy the socket cos we want to listen to data event
// the socket will self-destruct in 2 secs cos of the timeout we set, so no worries
});
// if any data is written to the client on connection, show it
s.on('data', function(data) {
console.log(port +': '+ data);
s.destroy();
});
s.on('error', function(e) {
// silently catch all errors - assume the port is closed
s.destroy();
});
})(port);
start++;
}