-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·390 lines (342 loc) · 15.1 KB
/
server.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// set-up ======================================================================
var express = require('express'); //used to configure server settings
var app = require('express')(); // create our server w/ express
var http = require('http').Server(app); // used for socket.io
var multer = require('multer'); // used for File-Upload
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var walk = require('walk'); // Used to walk recursive through folders
var fs = require('fs'); // Used for filesystem-access
var uuid = require('node-uuid'); // used to generate uuid's to identify files
var Reader = require('./readpcap.js'); // used to read a pcap-file and get the buffer back
var Sender = require('./sender.js'); // used to send packages through network
var Recorder = require('./recorder.js'); // used to record packages from network
var io = require('socket.io')(http); // used for sending push notifications to clients
var uploadFolder = 'static/uploads'; // Define Upload-Folder for all Actions with Files
var files = []; // Storage for file-items
var packagesLength = 0; // size of packages in pcap-file
//PushNotification Update-States
var UPDATE_ALL = 0;
var UPDATE_STATUS = 1;
var UPDATE_ERROR = 2;
var UPDATE_FILESELECTED = 3;
//Serverstates
var selectedFileID = "";
var selectedFilePath = "";
var DEFAULTSTATUS = "Bereit, wenn du's bist! ;)";
var serverStatus = DEFAULTSTATUS;
var serverError = "";
// configuration ======================================================================
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(express.static(__dirname + '/static'));
//Enable CORS to solve the same-origin-policy problem
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// Disable caching for content files
res.header("Cache-Control", "no-cache, no-store, must-revalidate");
res.header("Pragma", "no-cache");
res.header("Expires", 0);
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.status(200).end();
}
else {
next();
}
});
//File-Upload
app.use(multer({
dest: uploadFolder, //Output-Folder of Files
rename: function (fieldname, filename) {
//\W+/g = findet alle Zeichen, die nicht alphanumerisch und auch keine Unterstriche sind
return filename.replace(/\W+/g, '-').toLowerCase();
}
}));
// Functions ==========================================================================
//Push Notifications =====================
//Log connected users on screen, nothing more -> unnecessary
io.on('connection', function (socket) {
console.log('User connected, hooray!');
});
//send Notification to all connected Clients
var sendPushNotification = function(type) {
//Send specific message to clients
switch(type){
case UPDATE_STATUS: {
io.sockets.emit('status', {message: serverStatus});
break;
}
case UPDATE_ERROR: {
io.sockets.emit('error', {message: serverError});
break;
}
case UPDATE_FILESELECTED: {
io.sockets.emit('fileselected', {selectedFilePath: selectedFilePath, selectedFileID: selectedFileID});
break;
}
default: {
console.log("UPDATE_ALL");
io.sockets.emit('status', {message: serverStatus});
io.sockets.emit('error', {message: serverError});
io.sockets.emit('fileselected', {selectedFilePath: selectedFilePath, selectedFileID: selectedFileID});
break;
}
}
}
//File Functions =====================
//checks the complete content of uploadFolder
//if a file isn't in files[] it is added to files[]
//checks also, if every file in files[] is still available
var getFolderContent = function(callback) {
checkFilesExistence(); //check if every file in files[] is still available
// Walker options
var walker = walk.walk(uploadFolder, { followLinks: false });
walker.on('file', function(root, file, next) {
var filename = file.name.split('.'); //generate name from path
filename = filename[filename.length - 2] + "." + filename[filename.length -1];
// = name.fileExtension e.g. record.pcap
if(file.name.split('.')[file.name.split('.').length - 1] == "pcap") { //Just take Files, which are pcap-Files
fs.stat((root + '/' + file.name), function (err, stats) {
if (err) {
console.log(err);
next();
}
else {
FileLookup(root + '/' + file.name, function(result_ID) { //check if this file is already known
if(result_ID == -1) { //if result === -1 the file is not in files[]
var fileid = uuid.v4(); //generate an id for the file
var filesizeinMB = Number((stats["size"] / 1000000.0).toFixed(2)); //get the filesize from stats['size']
files.push({ //add this file to files[]
id: fileid,
name: filename,
path: root + '/' + file.name,
sizeinMB: filesizeinMB
});
next();
}
else
next();
});
}
});
}
else
next();
});
walker.on('end', function() {
callback(files);
});
}
//gets the filepath related to a id from files[]
var getFilePath = function(fileid, callback) {
var output = files.filter(function (item) {
return item.id == fileid
});
if(output[0])
callback(output[0].path); //output should contain just one item, with the right id
else
callback("error");
}
//checks, if a File is already in files[] or not
//if not -1 is returned, otherwise the file-id
var FileLookup = function(filepath, callback) {
var index = -1;
files.forEach(function(item) {
if(item['path'] == filepath) { //found a file
index = item['id']; //return the fileid
}
});
callback(index);
}
//removes a file from disk
var removeFile = function(filePath, callback) {
fs.unlink(filePath, function(err) {});
checkFilesExistence(); //check if every file in files[] is still available
}
//checks, if every file in files[] is still available on disk
//if not, it is removed from files[]
var checkFilesExistence = function() {
var index = 0;
files.forEach(function(item) {
//check if file is still available on server
fs.exists(item['path'], function(exists) {
if (!exists) {
files = files.splice(index,1); //remove this specific item from files[]
}
});
index++;
});
}
//Sending Packages =====================
//Read and send pcap-file
var readAndSend = function(datapath, callback) {
Reader.read(datapath, function(content) {
packagesLength = content.length;
serverStatus = packagesLength + " Packages read";
sendPushNotification(UPDATE_STATUS);
var sender = new Sender(content, '192.168.120.170', 6454);
// Event-Listener
sender.on('openStarted', function() {
serverStatus = "sending packages";
sendPushNotification(UPDATE_STATUS);
})
sender.on('packageSend', function(packagenumber) {
serverStatus = "sending packages";
sendPushNotification(UPDATE_STATUS);
})
sender.on('end', function(length) {
serverStatus = length + " Packages sent";
sendPushNotification(UPDATE_STATUS);
callback(length);
})
sender.on('error', function(err) {
serverError = err;
sendPushNotification(UPDATE_ERROR);
})
})
}
//Recording Packages ===================
//Record pcap-file
var recordPackages = function(iface, filename, packagelimit, callback) {
//Create new Recorder-Object
var recorder = new Recorder(iface, filename, packagelimit, callback);
//Event-Listener
recorder.on('start', function() {
serverStatus = 'recording';
sendPushNotification(UPDATE_STATUS);
})
recorder.on('err', function(data) {
serverError = data;
sendPushNotification(UPDATE_ERROR);
})
recorder.on('err_filepath', function(err) {
serverError = err;
sendPushNotification(UPDATE_ERROR);
})
recorder.on('err_finished', function(code) {
serverError = 'not sure what happened. Recorder returns: ' + code;
sendPushNotification(UPDATE_ERROR);
})
recorder.on('finished', function() {
serverStatus = 'Recording finished =)';
sendPushNotification(UPDATE_STATUS);
})
}
var stopRecord = function() {
recorder.stopRecord();
}
// routes ===========================================================================
//api ---------------------------------------------------------------------------
//get all files
app.get('/api/files', function(req, res, next) {
getFolderContent(function(files) {
res.json(files)
});
});
//delete a file
app.delete('/api/files/:file_id', function(req, res, next) {
getFilePath(req.params.file_id, function(filepath) {
removeFile(filepath);
sendPushNotification(UPDATE_FILESELECTED);
})
});
//upload a file
app.post('/api/upload', function (req, res) {
if (req.files.file.originalname.split('.').pop() == 'pcap') //check, if extension is == 'pcap'
{ //valid pcap-file
var name = req.files.file.originalname;
name = name.split('.')[name.split('.').length - 2]; //cuts the extension '.pcap'
res.send({pcap: true, name: name, file: req.files.file.originalname, _id:5});
}
else
{ //not a valid pcap-file
var name = req.files.file.originalname;
name = name.split('.')[name.split('.').length - 2]; //cuts the extension '.pcap'
console.log("no pcap-file!");
res.send({pcap: false, name: name, file: req.files.file.originalname, _id:-1});
}
});
//select a file
app.get('/api/select/:file_id', function(req, res, next) {
getFilePath(req.params.file_id, function(filepath) {
console.log("selected file: " + filepath);
console.log("fileid: " + req.params.file_id);
if(filepath != "error") {
serverStatus = "File " + filepath + " selected";
selectedFileID = req.params.file_id;
selectedFilePath = filepath;
sendPushNotification(UPDATE_ALL);
res.send({selectedFileID: selectedFileID, selectedFilePath: selectedFilePath});
}
else {
serverStatus = "FileIO-Error! Please refresh site!";
sendPushNotification(UPDATE_STATUS);
res.send({selectedFileID: selectedFileID, selectedFilePath: selectedFilePath});
}
})
});
//deselect a file
app.delete('/api/select', function(req, res, next) {
selectedFileID = "";
selectedFilePath = "";
serverStatus = DEFAULTSTATUS;
sendPushNotification(UPDATE_ALL);
res.send("deselected");
});
//initially get selected Filepath and ID
app.get('/api/getselected', function(req, res, next) {
res.send({selectedFileID: selectedFileID, selectedFilePath: selectedFilePath, serverStatus: serverStatus});
});
//play a file
app.get('/api/play/:file_id', function(req, res, next) {
getFilePath(req.params.file_id, function(filepath) {
if(filepath != "error") {
selectedFileID = req.params.file_id;
selectedFilePath = filepath;
console.log("play: " + selectedFilePath);
console.log("play: " + selectedFileID);
serverStatus = "Reading file";
sendPushNotification(UPDATE_STATUS);
readAndSend(filepath, function (result) { //Push Notifications will be send inside readAndSend
res.send();
setTimeout(function() { //After 20 Seconds, send Update to Client
serverStatus = DEFAULTSTATUS;
sendPushNotification(UPDATE_STATUS);
},20000);
});
}
else {
serverStatus = "FileIO-Error! Please refresh site!";
sendPushNotification(UPDATE_STATUS);
res.send();
}
})
});
//start record a file
app.get('/api/startrecord/:file_name', function(req, res, next) {
console.log("Record " + req.params.file_name + " started");
res.send({recordFileID: 123, recordFilePath: req.params.file_name, state: "recordingStart"});
});
//stop record a file
app.get('/api/stoprecord/:file_name', function(req, res, next) {
console.log("Record " + req.params.file_name + " stopped");
res.send({recordFileID: 123, recordFilePath: req.params.file_name, state: "recordingStop"});
});
// init & start server (start server with "node server.js") ===========================
//Init =====================
//Read Folder Content
getFolderContent(function(content) {
files = content;
});
//recordPackages('eth0','hansdieter.pcap');
setTimeout(function() {
stopRecord();
},50000);
//Start Server
http.listen(8080);
console.log("server listening on port 8080");