-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
105 lines (90 loc) · 2.7 KB
/
client.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var pcap = require('pcap');
var _packet = require('./packet');
module.exports = function(options) {
var commands = {};
options = options || {};
options.interface = options.interface || 'lo0';
options.port = options.port || '27017';
this.on = function(command, callback) {
// No filtering, just push to the commands variable
if (!commands[command]) {
commands[command] = [];
}
commands[command].push(callback);
};
this.run = function(runOpts) {
var session;
var queryStartTime;
runOpts = runOpts || {};
runOpts.interface = runOpts.interface || options.interface;
runOpts.port = runOpts.port || options.port;
// Start listening on interface and port
session = pcap.createSession(runOpts.interface, 'tcp port ' + options.port);
session.on('packet', function(raw) {
function processPcapData(data) {
var packet = new _packet(data);
var queryReplyTime;
if(commands.raw) {
commands.raw.forEach(function(callback) {
callback(data);
});
}
if(commands.packet) {
commands.packet.forEach(function(callback) {
callback(packet);
});
}
switch(packet.type) {
case 'reply':
queryReplyTime = Date.now() - queryStartTime;
if (commands.reply) {
commands.reply.forEach(function(callback) {
callback(packet, queryReplyTime);
});
}
break;
case 'update':
if (commands.update) {
commands.update.forEach(function(callback) {
callback(packet);
});
}
break;
case 'insert':
if (commands.insert) {
commands.insert.forEach(function(callback) {
callback(packet);
});
}
break;
case 'query':
queryStartTime = Date.now();
if(commands.query) {
commands.query.forEach(function(callback) {
callback(packet);
});
}
break;
case 'getMore':
if (commands.getMore) {
commands.getMore.forEach(function(callback) {
callback(packet);
});
}
break;
case 'delete':
if (commands.delete) {
commands.delete.forEach(function(callback) {
callback(packet);
});
}
break;
}
}
var rawPacket = pcap.decode.packet(raw);
if (rawPacket.link.ip.tcp.data) {
processPcapData(rawPacket.link.ip.tcp.data);
}
});
};
};