-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·88 lines (77 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var express = require('express');
var http = require('http');
var io = require('socket.io');
var ss = require('socket.io-stream');
var fs = require('fs');
// =============================================================================
// SERVER
// =============================================================================
var app = express();
app.get('/', function(req, res){
res.sendFile(__dirname + '/web/index.html');
});
app.use(express.static('web'));
var server = http.createServer(app);
/**
* Zip function to turn httpry logs into something usful
*/
var packet_parts = ['source_ip', 'source_port', 'direction', 'dest_ip', 'dest_port', 'method', 'request_uri', 'status_code'];
function processPacket(packet) {
var zipped = {};
packet = packet.split(/\s+/);
for (var index = 0; index < packet_parts.length; index++) {
zipped[packet_parts[index]] = packet[index] === undefined || packet[index] === '-' ? null : packet[index];
}
return zipped;
}
// =============================================================================
// INSPECT
// =============================================================================
var Inspect = require('./inspect');
Inspect.listContainers(function(err, containers) {
if (err) {
console.error(err);
return err;
}
console.log('got ' + containers.length + ' containers');
// =============================================================================
// ACTIVITY
// =============================================================================
var Activity = require('./activity');
Activity.getActivityStream(function(err, stream) {
if (err) {
console.error(err);
return err;
}
console.log('got activity stream');
stream.setEncoding('utf8');
startServer(containers, stream);
});
});
function startServer(containers, activity_stream) {
// =============================================================================
// SOCKET.IO
// =============================================================================
ioserver = io(server);
ioserver.on('connection', function(socket){
var user = (new Date()).getTime();
console.log('a user connected', user);
socket.on('disconnect', function(){
console.log('user disconnected', user);
});
socket.emit('containers', containers);
activity_stream.on('data', function(chunk) {
var packets = chunk.split('\n');
packets.forEach(function(packet) {
var packet = processPacket(packet);
socket.emit('activity', packet);
});
});
});
// =============================================================================
// START THE SERVER
// =============================================================================
server.listen(3000, function(){
console.log('listening on *:3000');
});
}