Permalink
Browse files

Adding in real-time components, audit log

  • Loading branch information...
1 parent c1520ee commit b18048d35c9d7ef9b8b2b23670403c067be7c6a4 @isiain isiain committed Apr 7, 2012
Showing with 88 additions and 6 deletions.
  1. +6 −3 program.js
  2. +47 −3 server.js
  3. +9 −0 views/audits.jade
  4. +1 −0 views/index.jade
  5. +25 −0 views/layout.jade
View
@@ -2,7 +2,7 @@
var stdin = process.openStdin()
, url = require('url')
- , redis = require('redis-node')
+ , redis = require('redis')
, redis_conn = redis.createClient()
, writeout = console.log;
@@ -34,7 +34,9 @@ function check_user_allowed(ip, ok, fail){
if (!ip){ fail(); return; }
redis_conn.get('u:ip:' + ip, function(err, data){
if (err){ fail(); }
- else if (data == 'free') { ok(); }
+ else if (data == 'free') {
+ ok();
+ }
else { fail(); }
})
}
@@ -44,14 +46,15 @@ stdin.on('data', function(chunk){
var input_raw = (chunk + '').split(' ');
var request = {url_raw: input_raw.shift(), ip: input_raw.shift(), ident: input_raw.shift(), method: input_raw.shift()};
if (request.ip){
- request.ip = request.ip.trimRight();
+ request.ip = request.ip.match(/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)[0];
}
request.url = url.parse(request.url_raw);
if (request.url['hostname']){
check_user_allowed(request.ip, function(){
writeout("True");
}, function(){
check_allowed_host(request.url['hostname'], function(){
+ redis_conn.publish('visits', JSON.stringify({ip: request.ip, host: request.url['hostname'], date: new Date().getTime()}));
writeout("True");
}, function(type){
writeout((request.method == 'CONNECT') ? '302:' : '' + "http://localhost/pproxy.php?h=" + request.url['hostname'] + "&u=" + request.url_raw + '&m=' + request.method);
View
@@ -1,9 +1,30 @@
var express = require('express')
- , redis = require('redis-node')
+ , redis = require('redis')
, redis_conn = redis.createClient()
- , app = express.createServer();
+ , app = express.createServer()
+ , sio = require('socket.io')
+ , io = sio.listen(app);
+
+io.set('log level', 1);
app.use(express.bodyParser());
+app.use(express.logger());
+app.use(function(req, res, next){
+ if (req.headers['Via'] && req.headers['Via'].match(/squid/i)){
+ res.writeHead(405, {'Content-Type': 'text/plain'});
+ res.end('Not Allowed');
+ } else {
+ next();
+ }
+});
+
+io.sockets.on('connection', function(socket){
+ var sub_conn = redis.createClient();
+ sub_conn.on('message', function(channel, message){
+ socket.emit('visits', message);
+ });
+ sub_conn.subscribe('visits');
+});
app.get('/', function(req, res){
var sort = get_sort(req.param('find'))
@@ -22,8 +43,28 @@ app.get('/access', function(req,res){
res.send('/access/(ip)/(action=free/deny)');
});
+function get_ip(req){
+ return req.socket && (req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress));
+}
+
+
+app.get('/audit', function(req, res){
+ var per_page = (req.params.page && parseInt(req.params.page)) ? parseInt(req.params.page) : 0;
+ var page_size = 120;
+ redis_conn.lrange('connaudit', per_page*page_size, 120 + per_page*page_size, function(err, data){
+ res.render('audits.jade', {audits: data.map(function(audit_entry){
+ try {
+ return JSON.parse(audit_entry);
+ } catch (e){ return null; };
+ })});
+ });
+});
+
app.get('/access/:ip/:action', function(req, res){
if (req.params.ip && req.params.action){
+ redis_conn.lpush('connaudit', JSON.stringify({date: new Date(), act: req.params.action, host: req.params.ip, from: get_ip(req)}), function(err, ok){
+ if (err) console.err(err);
+ });
redis_conn.set('u:ip:' + req.params.ip, req.params.action, function(err, ok){
if (err){ res.send('err setting: ' + err) }
else { res.send('ok: ip (' + req.params.ip + ') action '+req.params.action); }
@@ -38,8 +79,11 @@ app.get('/accepted', function(req,res){
});
app.post('/update', function(req, res){
- if (req.body.domain){
+ if (req.body.domain && req.body.action){
var domain = req.body.domain;
+ redis_conn.lpush('connaudit', JSON.stringify({date: new Date(), act: req.body.action, host: req.body.domain, from: get_ip(req)}), function(err, ok){
+ if (err) console.err(err);
+ });
if (req.body.action == 'accept'){
redis_conn.zadd('resdomains', 1, req.body.domain, function(err){
//res.send({status: err});
View
@@ -0,0 +1,9 @@
+table.audits
+ each audit in audits
+ tr#audit
+ td.pad= audit.host
+ td= audit.act
+ td= audit.from
+ td= new Date(audit.date).toString()
+
+
View
@@ -25,3 +25,4 @@ form(action='/update',method='POST',target='sform')
input(type='text',name='domain')
input(type='submit',name='action',value='accept')
input(type='submit',name='action',value='deny')
+
View
@@ -5,14 +5,39 @@ html
style
.links a { padding:10px; color:#555; }
body { font-family: calibri, ubuntu, courier; font-size: 1.25em; margin: 3%; }
+ tr.pad { padding: 6px 0; }
.mv { opacity:0.6; }
iframe#sform { width: 440px; height:30px;border:0 none;overflow:hidden;scroll:none; }
+ #ticker { float: right; width: 50%; height: 100%; overflow-y: auto; }
+ #ticker .date { display:none; }
+ script(src='/socket.io/socket.io.js')
+ script
+ var socket = io.connect()
+ socket.on('connect', function(){
+ var container = document.getElementById('ticker');
+ var div = document.createElement('div');
+ var lastDomain = '';
+ socket.on('visits', function(visit_data){
+ var data = JSON.parse(visit_data);
+ if (data.host == lastDomain){
+
+ } else {
+ var div = document.createElement('div');
+ div.innerHTML = '<div class="bubble"><div class="inner">@ <span class="frm">'+data.ip+'</span>: <span class="host">'+ data.host + '</span> <small class="date">'+(new Date(data.date)).toString() + '</small></div></div>';
+ container.insertBefore(div, container.childNodes[0]);
+ lastDomain = data.host;
+ }
+ });
+ });
body
h2 squidy intranet controller
.container
.links
a(href='/?find=denied') denied
a(href='/?find=accepted') accepted
a(href='/?find=pending') pending
+ a(href='/audit') audit
+ .ticker#ticker
+
!{body}

0 comments on commit b18048d

Please sign in to comment.