Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Created April 30, 2019 08:14
Show Gist options
  • Save tuor4eg/1ebc010f10eadfd9149fe36c8234ac53 to your computer and use it in GitHub Desktop.
Save tuor4eg/1ebc010f10eadfd9149fe36c8234ac53 to your computer and use it in GitHub Desktop.
const spawn = require('child_process').spawn,
LineStream = require('./byline.js').LineStream;
function ndpiReader(params, cb)
{
let subproc = null;
let p = new Promise((resolv, reject) => {
const lineStream = new LineStream();
lineStream.on('data', (line) => {
const parts = line.toString().trim().split(/\s+/);
if(parts.length > 8)
cb({
'proto4': parts[1],
'proto7id': parts[6].split(']')[0].split('/')[0],
'src': parts[2],
'dst': parts[4]
}, null);
});
subproc = spawn("stdbuf", ['-o', '0', 'ndpiReader', '-i', 'vmx0', '-m', '1', '-v', '1', '-q']);
subproc.stdout.pipe(lineStream);
subproc.stderr.on('data', (data) => {
cb(null, data);
});
subproc.on('close', (code) => {
if(code === 0)
resolv();
else
reject(`return code ${code}`);
});
subproc.on('error', (err) => {
reject(err);
});
});
p.subproc = subproc;
return p;
}
module.exports = { ndpiReader };
// ============= MAIN ========================
let prom = null;
process.on('SIGINT', async () => {
prom.subproc.kill();
});
(async () => {
prom = ndpiReader({}, (streams, stderr) => {
if(streams)
console.log(streams);
if(stderr)
console.log(stderr);
});
await prom;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment