Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Last active April 30, 2019 08:29
Show Gist options
  • Save tuor4eg/004b84b8bb5763d61e1448908e53b7a5 to your computer and use it in GitHub Desktop.
Save tuor4eg/004b84b8bb5763d61e1448908e53b7a5 to your computer and use it in GitHub Desktop.
const { spawn } = require('child_process');
const { LineStream } = require('./byline.js');
class ndpiReader {
constructor(options = {}) {
this.iface = options.iface;
this.subproc = null;
if(!options.iface)
throw Error("ndpiReader: network is not defined!");
}
listen(callback) {
const lineStream = new LineStream();
lineStream.on('data', (line) => {
const parts = line.toString().trim().split(/\s+/);
if(parts.length > 8)
callback({
'proto4': parts[1],
'proto7id': parts[6].split(']')[0].split('/')[0],
'src': parts[2],
'dst': parts[4]
}, null);
});
this.subproc = spawn("stdbuf", ['-o', '0', 'ndpiReader', '-i', `${this.iface}`, '-m', '1', '-v', '1', '-q']);
this.subproc.stdout.pipe(lineStream);
this.subproc.stderr.on('data', (data) => {
cb(null, data);
});
this.subproc.on('close', (code) => {
if(code === 0)
return;
else
return(`return code ${code}`);
});
this.subproc.on('error', (err) => {
return(err);
});
}
start(callback) {
const proc = this.listen(callback);
return proc;
}
}
const ndpi = new ndpiReader({iface: 'vmx0'});
process.on('SIGINT', async () => {
ndpi.subproc.kill();
});
(() => {
ndpi.start((streams, stderr) => {
if(streams)
console.log(streams);
if(stderr)
console.log(stderr);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment