-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45.js
More file actions
142 lines (135 loc) · 4.22 KB
/
Copy path45.js
File metadata and controls
142 lines (135 loc) · 4.22 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const async = require('async');
const ping = require('ping');
const arp = require('node-arp');
const dns = require('dns');
const request = require('request');
const ip = require('ip');
const options = {
// ip: '192.168.1',
timeout: 15,
vendor: true,
min: 1,
max: 255
};
function getInfo(ip, callback) {
const result = {
ip,
alive: false,
hostname: null,
mac: null,
vendor: null,
hostnameError: null,
macError: null,
vendorError: null,
};
// console.log(`Checking ${ip}...`);
ping.promise.probe(ip, {
timeout: options.timeout,
}).then((res) => {
if (res.alive) {
result.alive = true;
dns.reverse(ip, (err, host) => {
if (err) {
result.hostnameError = 'Error on get hostname';
} else {
result.hostname = (host && host.length) ? host[0] : null;
}
arp.getMAC(ip, (err2, mac) => {
if(err2 || !mac) {
result.macError = 'Error on get Mac address';
} else {
result.mac = mac.replace(/:([^:]{1}):/g, ':0$1:');
}
if (options.vendor && mac) {
request.get(`https://macvendors.co/api/${mac.replace(/:([^:]{1}):/g, ':0$1:')}/json`, (err3, httpRes, body) => {
// console.log(httpRes.statusCode, body);
if(err3 || httpRes.statusCode !== 200) {
// console.log(`Error on get vendor... ip: ${ip} : Mac: ${mac}`);
result.vendorError = 'Error on get vendor';
callback(null, result);
} else {
const cont = JSON.parse(body);
if (cont && cont.result && cont.result.company) {
result.vendor = cont.result.company;
callback(null, result);
} else {
result.vendorError = 'Vendor has no result';
callback(null, result);
}
}
});
} else {
callback(null, result);
}
});
});
} else {
callback(null, result);
}
});
}
// Keep this function to use in a future version to port scan
function checkPort(port, host, callback) {
var socket = new Socket(),
status = null;
socket.on('connect', () => {
status = 'open';
socket.end();
});
socket.setTimeout(1500);
socket.on('timeout', () => {
status = 'closed';
socket.destroy();
});
socket.on('error', (exception) => {
status = 'closed';
});
socket.on('close', (exception) => {
callback(null, status,host,port);
});
socket.connect(port, host);
}
function getBaseIp(opts) {
if (!('ip' in opts)) {
const ipAddress = ip.address();
if (ipAddress) {
const aIp = ipAddress.split('.');
if (aIp.length === 4) {
opts.ip = aIp.slice(0, -1).join('.');
return opts;
}
}
} else {
const aIp = opts.ip.split('.');
if (aIp.length === 3) {
return opts;
} else {
throw new Error('IP should be xxx.xxx.xxx');
return;
}
}
throw new Error('No IP address');
}
module.exports = {
scanEach: function(opts, callback) {
// console.log(opts);
const finalOpts = getBaseIp(opts);
Object.assign(options, finalOpts);
// console.log(options);
for (let i=options.min; i < options.max; i++) {
getInfo(`${options.ip}.${i}`, callback);
}
},
scan: function(opts, callback) {
const finalOpts = getBaseIp(opts);
Object.assign(options, finalOpts);
// console.log(options);
const aIps = [];
for (let i=options.min; i < options.max; i++) {
aIps.push(`${options.ip}.${i}`);
}
async.map(aIps, getInfo, (err, results) => {
callback(err, results);
});
}
}