-
Notifications
You must be signed in to change notification settings - Fork 2
/
hep-client.js
135 lines (124 loc) · 3.48 KB
/
hep-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
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
/*
HEPIPE-JS
(c) 2017 QXIP BV
For License details, see LICENSE
*/
var HEPjs = require('hep-js');
var dgram = require('dgram');
var socket = dgram.createSocket("udp4");
var debug = false;
var stats = {rcvd: 0, parsed: 0, hepsent: 0, err: 0, heperr: 0 };
var hep_server;
var hep_port;
var hep_pass;
var hep_id;
var socket;
module.exports = {
init:function(config) {
hep_server = config.HEP_SERVER;
hep_port = config.HEP_PORT;
hep_pass = config.HEP_PASS;
hep_id = config.HEP_ID;
debug = config.debug;
socket = dgram.createSocket("udp4");
socket = getSocket('udp4');
},
rcinfo:function(message,type,ptype) {
if (!type) { var type = 1; }
if (!ptype) { var ptype = "SIP"; }
return {
rcinfo: {
type: 'HEP',
version: 3,
payload_type: ptype,
captureId: hep_id,
capturePass: hep_pass,
ip_family: 2,
protocol: 17,
proto_type: type,
srcIp: message.SrcIP,
dstIp: message.DstIP,
srcPort: message.SrcPort,
dstPort: message.DstPort,
correlation_id: message.CallID ? message.CallID : ''
},
payload: message.SipMsg
};
},
rcmos:function(message,mos) {
return {
rcinfo: {
type: 'HEP',
version: 3,
payload_type: 'JSON',
captureId: hep_id,
capturePass: hep_pass,
ip_family: 2,
protocol: 17,
proto_type: 35,
srcIp: message.SrcIP,
dstIp: message.DstIP,
srcPort: message.SrcPort,
dstPort: message.DstPort,
correlation_id: message.CallID ? message.CallID : '',
mos: mos
},
payload: message
};
},
preHep:function(message) {
var rcinfo = message.rcinfo;
var msg = message.payload;
// if (rcinfo.correlation_id == null || !(rcinfo.correlation_id.toString().length)) return;
if (debug) console.log(msg);
stats.rcvd++;
var hrTime = process.hrtime();
var datenow = new Date().getTime();
rcinfo.time_sec = Math.floor( datenow / 1000);
rcinfo.time_usec = datenow - (rcinfo.time_sec*1000);
if (debug) console.log(rcinfo);
sendHEP3(msg, rcinfo);
},
getStats:function() {
return stats;
}
};
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;
}
var sendHEP3 = function(msg,rcinfo){
if (rcinfo && msg) {
try {
if (debug) console.log('Sending HEP3 Packet to '+ hep_server + ':' + hep_port + '...');
if (! typeof msg === 'string' || ! msg instanceof String) msg = JSON.stringify(msg);
var hep_message = HEPjs.encapsulate(msg.toString(),rcinfo);
stats.parsed++;
if (hep_message && hep_message.length) {
socket.send(hep_message, 0, hep_message.length, hep_port, hep_server, function(err) {
stats.hepsent++;
});
} else { console.log('HEP Parsing error!'); stats.heperr++; }
}
catch (e) {
console.log('HEP3 Error sending!');
console.log(e);
stats.heperr++;
}
}
}