-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder.js
executable file
·86 lines (70 loc) · 2.87 KB
/
recorder.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
var spawn = require('child_process').spawn;
var recorder_t;
//Used for emitting Events
var util = require('util');
var EventEmitter = require('events').EventEmitter;
//Preparings for child-process
var command_t = 'tshark';
var port_t = 6454;
var filter_t = 'udp port ' + port_t;
var iface_t = 'eth0';
var packagelimit_t = 0;
var filename_t = 'capture.pcap';
var relativeFilePath_t = 'static/uploads/';
//########################################################Debugging =)############################################
filter_t = 'udp';
//########################################################Debugging =)############################################
var recorder = function recorder(iface, filename, packagelimit) {
//Do some initial Stuff
// we need to store the reference of `this` to `self`, so that we can use the current context in the setTimeout (or any callback) functions
// using `this` in the setTimeout functions will refer to those functions, not the recorder class
var self = this;
//if filename is not specified throw error and return NULL;
if (typeof filename === 'undefined') {
console.log('Empty Filepath');
self.emit('err_filepath', 'Please specify a Filepath');
return;
}
else
filename_t = filename;
if (typeof iface === 'undefined') //if an alternative interface is set, choose this one, otherwise the default one
iface_t = 'eth0';
else
iface_t = iface;
if (typeof packagelimit === 'undefined')
packagelimit_t = 0;
else
packagelimit_t = packagelimit;
//Start Capturing Process with Async child process
//Setup 'Recorder' as ChildProcess and pass Arguments (this will start the process)
if (packagelimit_t > 0) //create recorder with packagelimit
recorder_t = spawn(command_t, ['-f', filter_t, '-i', iface_t, '-c', packagelimit_t, '-w', relativeFilePath_t + filename_t]);
else //create recorder without packagelimit
recorder_t = spawn(command_t, ['-f', filter_t, '-i', iface_t, '-w', relativeFilePath_t + filename_t]);
self.emit('start');
//Listening for errors
recorder_t.stderr.on('data', function (data) {
console.log('Error: ' + data);
self.emit('err', data);
});
//Listening for end of Process
recorder_t.on('close', function (code) {
if (code == 0) {
console.log("Process exited successfully");
self.emit('finished');
}
else {
console.log("Process exited with code: " + code);
self.emit('err_finished', code);
}
});
}
var stopRecord = function () {
if(recorder_t)
recorder_t.kill('SIGQUIT');
}
// extend the EventEmitter class using our Recorder class
util.inherits(recorder, EventEmitter);
// we specify that this module is a reference to the Recorder Class
module.exports = recorder;
module.exports = stopRecord;