-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp.js
41 lines (36 loc) · 1.1 KB
/
http.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
'use strict';
//
// Count the number of new_connection events and keep most_recent_limit
// connections around to include in responses.
//
// Expose this information via HTTP on port 3000.
//
const http = require('http');
// Serialize BitInt as strings.
BigInt.prototype.toJSON = function() {
return this.toString();
}
var connections_total = 0;
var most_recent_connections_limit = 5;
var most_recent_connections = []
zeek.on('new_connection', (c) => {
console.log(`New connection ${c.id.resp_h} ${c.id.resp_p.port}`);
connections_total++;
most_recent_connections.push(c);
while (most_recent_connections.length > most_recent_connections_limit)
most_recent_connections.shift();
});
const server = http.createServer((req, res) => {
let data = {
'connections': {
'total': connections_total,
'most_recent': {
'limit': most_recent_connections_limit,
'count': most_recent_connections.length,
'connections': most_recent_connections,
}
}
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data, null, 2));
}).listen(3000);