-
Notifications
You must be signed in to change notification settings - Fork 30
/
Network.js
82 lines (73 loc) · 1.86 KB
/
Network.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict'
const debug = require('debug')('kickthemout:Network')
const network = require('network')
const nodeARP = require('node-arp')
const getmac = require('getmac')
const oui = require('oui')
/*
* Class containing network releated functions
*/
class Network {
/*
constructor () {
this.availableInterfaces
this.iface
this.hosts
}
*/
getInterfaces (callback) {
var self = this
network.get_interfaces_list(function (err, ifaces) {
if (err) return callback(err)
debug('Getting available interfaces', ifaces)
if (ifaces.length === 0) return callback(new Error('No network interfaces available'))
self.availableInterfaces = ifaces
callback(null, ifaces)
})
}
selectInterface (iface) {
this.iface = iface
}
scan (myIP, cb) {
var self = this
debug('Starting host discovery in LAN')
var LAN = myIP.substr(0, myIP.lastIndexOf('.'))
var result = []
var count = 0
for (var i = 1; i <= 255; i++) {
var ip = LAN + '.' + i
;(function (ip) {
// Ping Sweep
nodeARP.getMAC(ip, function (err, mac) {
if (!err && mac) {
if (getmac.isMac(mac)) {
result.push({ip: ip, mac: mac})
}
}
if ((++count) > 254) {
self.hosts = result
cb(result)
}
})
})(ip)
}
}
resolveVendors () {
// populate a new field 'vendor' for the hosts
if (Array.isArray(this.hosts)) {
this.hosts = this.hosts.map(function (host) {
debug('getting vendor for:', host)
host.vendor = _getVendorName(host.mac)
return host
})
} else {
return _getVendorName(this.hosts)
}
}
}
/* Private */
function _getVendorName (mac) {
var vendor = oui(mac)
return vendor ? vendor.split('\n')[0] : 'Vendor unknown'
}
module.exports = Network