forked from hortinstein/node-dash-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (82 loc) · 2.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
// converts a string: "8f:3f:20:33:54:44"
// to a numeric array: [ 143, 63, 32, 51, 84, 68 ]
// for comparison
var hex_to_int_array = function(hex){
var hex_array = hex.split(":");
var int_array = [];
for (var i in hex_array) {
int_array.push( parseInt(hex_array[i], 16));
}
//console.log(hex,int_array)
return int_array;
};
// converts a numeric array: [ 143, 63, 32, 51, 84, 68 ]
// to a string: "8f:3f:20:33:54:44"=
// for comparison
var int_array_to_hex = function (int_array) {
var hex = "";
for (var i in int_array){
var h = int_array[i].toString(16); // converting to hex
if (h.length < 2) {h = '0' + h}; //adding a 0 for non 2 digit numbers
if (i !== int_array.length) {hex+=":"}; //adding a : for all but the last group
hex += h;
}
return hex.slice(1);//slice is to get rid of the leading :
};
var pcap = require('pcap');
var stream = require('stream');
var _ = require('underscore');
var create_session = function () {
try {
var session = pcap.createSession();
} catch (err) {
console.error(err);
console.error("Failed to create pcap session: couldn't find devices to listen on.\n" + "Try running with elevated privileges via 'sudo'");
throw new Error('Error: No devices to listen');
}
return session;
};
//Function to register the node button
var register = function(mac_addresses) {
if (Array.isArray(mac_addresses)){
//console.log("array detected")
} else {
//console.log("single element detected")
mac_addresses = [mac_addresses];//cast to array
}
var pcap_session = create_session();
var readStream = new stream.Readable({
objectMode: true
});
var just_emitted = {};
mac_addresses.forEach(function(mac_address){
just_emitted[mac_address] = false;
});
pcap_session.on('packet', function(raw_packet) {
//console.log(raw_packet)
var packet = pcap.decode.packet(raw_packet); //decodes the packet
if(packet.payload.ethertype === 2054) { //ensures it is an arp packet
//for element in the mac addresses array
mac_addresses.forEach(function(mac_address){
if(!just_emitted[mac_address] &&
_.isEqual(packet.payload.payload.sender_ha.addr,
hex_to_int_array(mac_address))) {
readStream.emit('detected', mac_address);
just_emitted[mac_address] = true;
setTimeout(function () { just_emitted = false; }, 3000);
}
});
}
});
return readStream;
};
if (process.env.NODE_ENV === 'test') {
module.exports = { hex_to_int_array: hex_to_int_array,
int_array_to_hex: int_array_to_hex,
create_session: create_session,
register: register
};
} else {
module.exports = register;
}