-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (115 loc) · 3.3 KB
/
Copy pathapp.js
File metadata and controls
153 lines (115 loc) · 3.3 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
143
144
145
146
147
148
149
150
151
152
var http = require('http'),
express = require('express'),
fs = require('fs'),
watch = require('watch'),
request = require('request'),
parser = require('xml2json'),
spawn = require('child_process').spawn,
isOnline = require('is-online'),
path = require('path'),
app = express();
/* -------------------------------
CONFIGURE YOUR SETTINGS
------------------------------- */
var config = {
port: 3030,
interface: 'wlan0',
dumpName: 'dump',
endpoint: process.env.NODE_ENV === 'dev' ? 'http://192.168.1.15:3005/data':'http://reality-versioning-api.herokuapp.com/data',
env: process.env.NODE_ENV || ''
};
var state = {
newData: false
};
var requestOptions = {
method: 'post',
json: true,
url: config.endpoint
};
//-------------------------------------------
// Create server
//-------------------------------------------
var server = app.listen(config.port, function() {
console.log('Starting up on port: ' + config.port);
if (config.env !== 'dev') {
// Only init if we're running it on the target environment
init();
} else {
// If we're running it in dev
startWatching();
}
});
function init() {
console.log('Attempt to execute airodump-ng');
//ls.stdout.pipe(process.stdout);
var cmd = spawn('airodump-ng', [
'-w ' + config.dumpName,
config.interface
], {cwd: './data'});
//var cmd = spawn('top',['-l 0']);
//console.log(cmd.connected);
cmd.stdout.on('data', function (data) {
//console.log('stdout: ' + data);
});
cmd.stderr.on('data', function (data) {
//console.log('stderr: ' + data);
});
cmd.on('close', function (code) {
console.log('child process exited with code ' + code + '. Make sure your wifi device is set to monitor mode.');
});
// TODO: Start this when cmd is connected instead of on a timeout
setTimeout(function() {startWatching();}, 5000);
}
function startWatching() {
console.log('Watching for changes to airodump data');
// Watch for file changes in data folder
watch.createMonitor('./data', function (monitor) {
monitor.on('changed', function (file, curr, prev) {
// Filter out netxml files
if (path.extname(file) === '.netxml') {
parseData(file);
}
});
});
}
function parseData(file) {
console.log('Parsing data for: ' + file);
try {
var xml = fs.readFileSync(file),
data = parser.toJson(xml, {
sanitize: true
});
isOnline(function(err, online) {
if (err) throw err;
if (online === true) {
// Device is online
console.log('Device is online');
postData(data);
} else {
}
});
} catch(e) {
console.log('There was an error parsing your xml');
console.log(e);
}
}
function postData(json) {
console.log('Posting data to: ' + config.endpoint);
try {
requestOptions.body = JSON.parse(json);
request(requestOptions, function(err, response, body) {
if (err) throw err
console.log('Response from API -------------------');
console.log(body);
});
} catch (e) {
console.log('There was an error in the request to the API');
console.log(e);
}
}
// Routes
//-------------------------------------------
app.get('/test', function(req, res) {
console.log('test');
res.status(200).end('OK');
});