Created
July 20, 2011 12:20
-
-
Save rodzyn/1094858 to your computer and use it in GitHub Desktop.
Node.js - node_pcap - capturing packets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'), | |
| sys = require("sys"), | |
| pcap = require("pcap"), | |
| ejs = require("ejs"), | |
| fs = require('fs'), | |
| pcap_session; | |
| //It's just a simple code. For a long term solution you should use db storage. | |
| var team = { | |
| rodzyn: { | |
| "hwaddr": "00:21:e9:dc:e9:2c", | |
| "photo" : "http://s3.amazonaws.com/assets.applicake.com/system/avatars/21/medium/IMG_6008_marcin_s.jpg?1297761170", | |
| "time" : null | |
| }, | |
| }; | |
| pcap_session = pcap.createSession(); | |
| pcap_session.on('packet', function (raw_packet) { | |
| var packet = pcap.decode.packet(raw_packet); | |
| var printed_packet = pcap.print.packet(packet) | |
| var pattern = /^(.*?) ->/ | |
| var matched = printed_packet.match(pattern); | |
| var hwaddr = matched[matched.length - 1] | |
| for (member in team) { | |
| if (team[member].hwaddr == hwaddr) { | |
| var date = new Date(); | |
| team[member].time = date.getTime(); | |
| } | |
| } | |
| }); | |
| var server = http.createServer(function(req, res) { | |
| var date = new Date(); | |
| var time = date.getTime(); | |
| res.writeHead(200, { 'Content-type': 'text/html' }); | |
| //In addition I'm passing values to the template engine | |
| str = fs.readFileSync(__dirname + '/index.ejs', 'utf8'); | |
| res.end(ejs.render(str, { | |
| locals: { | |
| applicakers: team, | |
| current_time: time | |
| } | |
| })); | |
| }) | |
| server.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment