14. Setup the Data Source
var rb = new lpb.LongPollingBuffer(70);
var dump = process.createChildProcess("tcpdump",["-i","en1","-A","-n","port", "80"]);
var ignorelist = ['localhost','foxnews.com'];
//Setup the listener to handle the flow of data from the dump
dump.addListener("output", function (data) {
var hosts = data.match(/Host: (.*)/g);
if(hosts){
_.each(hosts, function(item){
var out = item.slice(6);
sys.puts(out);
if(!_.detect(ignorelist, function(s){ return (item.indexOf(s) > -1); })){
rb.push(out);
}
});
}
});
@robrighter
16. Setup the Updater
fu.get("/update", function (req, res) {
//Send the HTTP Headers
res.sendHeader(200,{"Content-Type": "text/html"});
//Parse out the since from the query string
var thesince;
if(url.parse(req.url,true).hasOwnProperty('query') &&
url.parse(req.url,true).query.hasOwnProperty('since')){
thesince = parseInt(url.parse(req.url,true)['query']['since']);
}
else {
thesince = -1;
}
//Setup the datalistener (note why this works so well with JS scoping)
rb.addListenerForUpdateSince(thesince, function(data){
var body = '['+_.map(data,JSON.stringify).join(',n')+']';
res.write( body );
res.close();
});
});
@robrighter