This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
forked from sipcapture/captagent-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captagent.js
273 lines (230 loc) · 8.19 KB
/
captagent.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* NODEJS Captagent w/ HEP3 Support via HEP-js module
* (C) 2015 L. Mangani, QXIP BV
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
Example Usage:
HEP3:
nodejs captagent.js -debug true -s 127.0.0.1 -p 9063 -i 2001
Daemonize using forever:
npm install forever -g
forever start captagent.js
*/
var version = 'v0.4';
var debug = false;
var sipdebug = false;
var stats = {rcvd: 0, parsed: 0, hepsent: 0, err: 0, heperr: 0 };
/* HELP MENU */
if(process.argv.indexOf("-h") != -1){
console.log('NodeAgent is an HEP3 Capture Agent implementation for HOMER / SIPCAPTURE');
console.log('For more information please visit: http://sipcapture.org ');
console.log('Usage:');
console.log();
console.log(' -r: BPF Capture filter (ie: port 5060)');
console.log();
console.log(' -s: HEP3 Collector IP');
console.log(' -p: HEP3 Collector Port');
console.log(' -i: HEP3 Agent ID');
console.log(' -P: HEP3 Password');
console.log();
console.log(' -debug: Debug Internals (ie: -debug true)');
console.log(' CRTL-C: Exit');
console.log();
process.exit();
}
/* Settings Section */
// CAPTURE ARGS & DEFAULTS
var bpf_filter = 'port 5060';
if(process.argv.indexOf("-r") != -1){
bpf_filter = process.argv[process.argv.indexOf("-r") + 1];
}
if(process.argv.indexOf("-debug") != -1){
debug = process.argv[process.argv.indexOf("-debug") + 1];
}
// HEP ARGS & DEFAULTS
var hep_server = 'localhost';
if(process.argv.indexOf("-s") != -1){
hep_server = process.argv[process.argv.indexOf("-s") + 1];
}
var hep_port = 9063;
if(process.argv.indexOf("-p") != -1){
hep_port = process.argv[process.argv.indexOf("-p") + 1];
}
var hep_id = '2001';
if(process.argv.indexOf("-i") != -1){
hep_id = process.argv[process.argv.indexOf("-i") + 1];
}
var hep_pass = 'myHep6';
if(process.argv.indexOf("-P") != -1){
hep_pass = process.argv[process.argv.indexOf("-P") + 1];
}
console.log('Starting JSAgent '+version);
/* NODE.JS Requirements */
var SIP = require('sipcore'),
Cap = require('cap').Cap,
decoders = require('cap').decoders,
PROTOCOL = decoders.PROTOCOL,
HEPjs = require('hep-js');
/* HEP OUT SOCKET */
var dgram = require('dgram'),
socket = dgram.createSocket("udp4");
/* CAPTURE SOCKET */
var c = new Cap(),
device = Cap.findDevice(),
filter = bpf_filter,
bufSize = 10 * 1024 * 1024,
buffer = new Buffer(65535);
/* APP START */
console.log('Capturing from device '+device+ ' with BPF ('+bpf_filter+')');
console.log('Sending HEP3 Packets to '+hep_server+':'+hep_port+' with id '+hep_id);
var linkType = c.open(device, filter, bufSize, buffer);
c.setMinBytes && c.setMinBytes(0);
c.on('packet', function(nbytes, trunc) {
if (debug) console.log('packet: length ' + nbytes + ' bytes, truncated? '
+ (trunc ? 'yes' : 'no'));
stats.rcvd++;
var hep_proto = { "type": "HEP", "version": 3, "payload_type": "SIP", "captureId": hep_id, "capturePass": hep_pass, "ip_family": 2};
// raw packet data === buffer.slice(0, nbytes)
if (linkType === 'ETHERNET') {
var ret = decoders.Ethernet(buffer);
var datenow = new Date().getTime();
hep_proto.time_sec = Math.floor(datenow / 1000);
hep_proto.time_usec = datenow - (hep_proto.time_sec*1000);
if (ret.info.type === PROTOCOL.ETHERNET.IPV4) {
if (debug) console.log('Decoding IPv4 ...');
ret = decoders.IPV4(buffer, ret.offset);
if (debug) console.log('from: ' + ret.info.srcaddr + ' to ' + ret.info.dstaddr);
if (ret.info.protocol === PROTOCOL.IP.TCP) {
/* TCP DECODE */
var datalen = ret.info.totallen - ret.hdrlen;
if (debug) console.log('Decoding TCP ...');
var tcpret = decoders.TCP(buffer, ret.offset);
if (debug) console.log(' TCP from: ' + ret.info.srcip + ':' + tcpret.info.srcport + ' to: ' + ret.info.dstaddr + ':' + tcpret.info.dstport);
datalen -= tcpret.hdrlen;
// if (debug) console.log(buffer.toString('binary', tcpret.offset, tcpret.offset + datalen));
var msg = buffer.toString('binary', tcpret.offset, tcpret.offset + datalen);
// Build HEP3
hep_proto.ip_family = 2;
hep_proto.protocol = 6;
hep_proto.proto_type = 1;
hep_proto.srcIp = ret.info.srcaddr;
hep_proto.dstIp = ret.info.dstaddr;
hep_proto.srcPort = tcpret.info.srcport;
hep_proto.dstPort = tcpret.info.dstport;
// Ship to parser
parseSIP(msg, hep_proto);
} else if (ret.info.protocol === PROTOCOL.IP.UDP) {
/* UDP DECODE */
if (debug) console.log('Decoding UDP ...');
var udpret = decoders.UDP(buffer, ret.offset);
if (debug) console.log(' UDP from: ' + ret.info.srcaddr + ':' + udpret.info.srcport + ' to: ' + ret.info.dstaddr+ ':' + udpret.info.dstport);
// if (debug) console.log(buffer.toString('binary', udpret.offset, udpret.offset + udpret.info.length));
var msg = buffer.toString('binary', udpret.offset, udpret.offset + udpret.info.length);
// Build HEP3
hep_proto.ip_family = 2;
hep_proto.protocol = 17;
hep_proto.proto_type = 1;
hep_proto.srcIp = ret.info.srcaddr;
hep_proto.dstIp = ret.info.dstaddr;
hep_proto.srcPort = udpret.info.srcport;
hep_proto.dstPort = udpret.info.dstport;
// Ship to parser
parseSIP(msg, hep_proto);
} else
if (debug) console.log('Unsupported IPv4 protocol: ' + PROTOCOL.IP[ret.info.protocol]);
stats.err++;
} else
if (debug) console.log('Unsupported Ethertype: ' + PROTOCOL.ETHERNET[ret.info.type]);
stats.err++;
}
});
/* SIP Parsing */
var parseSIP = function(msg, rcinfo){
try {
var sipmsg = SIP.parse(msg);
if (sipdebug) console.log(sipmsg);
if (debug) console.log('CSeq: '+sipmsg.headers.cseq);
stats.parsed++;
// SEND HEP3 Packet
sendHEP3(sipmsg,msg, rcinfo);
}
catch (e) {
if (debug) console.log(e);
var sipmsg = false;
stats.err++;
}
}
/* HEP3 Socket OUT */
var sendHEP3 = function(sipmsg,msg, rcinfo){
if (sipmsg) {
try {
if (debug) console.log('Sending HEP3 Packet...');
var hep_message = HEPjs.encapsulate(msg,rcinfo);
if (hep_message) {
socket = getSocket('udp4');
// socket.send(hep_message, 0, hep_message.length, 9063, "localhost", function(err) {
socket.send(hep_message, 0, hep_message.length, hep_port, hep_server, function(err) {
stats.hepsent++;
// socket.close();
});
}
}
catch (e) {
console.log('HEP3 Error sending!');
console.log(e);
stats.heperr++;
}
}
}
/* UDP Socket Handler */
var getSocket = function (type) {
if (undefined === socket) {
socket = dgram.createSocket(type);
socket.on('error', socketErrorHandler);
/**
* Handles socket's 'close' event,
* recover socket in case of unplanned closing.
*/
var socketCloseHandler = function () {
if (socketUsers > 0) {
socket = undefined;
--socketUsers;
getSocket(type);
}
};
socket.on('close', socketCloseHandler);
}
return socket;
}
/* Stats & Kill Thread */
var exit = false;
process.on('SIGINT', function() {
console.log();
if (exit) {
if (es_buffer && es_buffer.length > 1) sendHTTP(es_buffer+'\n');
console.log("Exiting...");
process.exit();
} else {
console.log('Statistics:', stats);
if (es_buffer && es_buffer.length > 1) sendHTTP(es_buffer+'\n');
console.log("Press CTRL-C within 2 seconds to Exit...");
exit = true;
setTimeout(function () {
// console.log("Continuing...");
exit = false;
}, 2000)
}
});