-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (98 loc) · 3.08 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
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
const spawn = require('child_process').spawn;
const debounce = require('debounce');
const ndb = require('node-dash-button');
const moment = require('moment');
module.exports = (function() {
let buttons = [];
let processStartTime = null;
let config = {
iface: {
arp: "eth0",
tshark: 'wlan0'
},
channel: 7,
timeout: 8000,
listeners: {
arp: true,
tshark: true
},
debug: false
}
return {
config: config,
AddButton: (name, mac_address, callback) => {
callback = debounce(callback, config.timeout, true);
buttons.push({ name, mac_address, callback });
},
Listen: () => {
if (!config.listeners.tshark && !config.listeners.arp) {
throw new exception("You need to enable at least one listener (tshark or arp)");
}
if (config.listeners.tshark === true) {
let processName = 'tshark';
let args = ['-i', config.iface.tshark, '-l', '-T', 'fields', '-e', 'wlan.sa'];
console.log("Launching " + processName);
StartProcess(processName, args);
}
if (config.listeners.arp === true) {
let macAddresses = buttons.map(b => b.mac_address);
let dash = ndb(macAddresses, config.iface.arp, 0, 'all');
dash.on('detected', HandleArpDetected);
console.log('Listening for dash buttons via ARP/UDP...');
}
}
}
function HandleTsharkOutput(data) {
if (data.toString().length === 0) {
return;
}
// Split lines
let lines = data.toString().split('\n');
// Get rid of empty lines
lines = lines.filter(l => l.length > 0);
lines.forEach(line => {
// See if any of our buttons have this MAC address
let button = buttons.find(b => b.mac_address.toLowerCase() === line.toLowerCase());
if (button) {
// Button press detected!
if (config.debug) {
console.log(Timestamp(), `Detected ${button.name} via Tshark`);
}
button.callback(button);
}
});
}
function HandleArpDetected(mac) {
let button = buttons.find(b => b.mac_address === mac);
if (button) {
if (config.debug) {
console.log(Timestamp(), `Detected ${button.name} via ARP/UDP`);
}
button.callback(button);
}
}
function StartProcess(name, args) {
processStartTime = new Date();
let process = spawn(name, args);
process.stdout.on('data', function(data) { HandleTsharkOutput(data); });
process.stderr.on('data', function(data) { HandleTsharkOutput(data); });
process.on('close', function(code) {
// If the process was up for less than 5 seconds, show some info for troubleshooting
if (config.debug || processStartTime - new Date() <= 5000) {
console.log();
console.log(Timestamp(), 'Monitor process ended! Button presses will not be detected. Code: ' + code);
console.log();
console.log("Try running the command to troubleshoot (check configuration, etc):");
let commandStr = `${name} ${args.join(' ')}`;
console.log(commandStr);
} else {
// If it was up for more than 5 seconds, restart it
console.log(Timestamp(), "Monitor process ended. Restarting...");
return StartProcess(name, args);
}
});
}
function Timestamp() {
return moment().format("M/D hh:mm:ss a") + ": ";
}
})();