-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
347 lines (307 loc) · 9.52 KB
/
app.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'use strict'
var cfg = require('./config.json');
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
var sip = require('sip');
var redis = require("redis");
var cache = redis.createClient();
const Client = require('pg').Client;
const client = new Client(cfg);
client.connect((err) => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('db connected');
}
});
var remip = '127.0.0.1'; //test server IP to send to forward the message
var remport = '5061'; //test server port to send to forward the message
var dialogTrash = [];
var dialogTrashTimer = setInterval(dialogTrashExecuter, 2000);
/* CRYPTO */
var crypto = require('crypto');
var algorithm = 'aes-256-ctr';
var password = 'gHH53cx!qK86ggnV#';
/* END CRYPTO */
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
checkMessage(msg, rinfo);
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
server.bind(5060);
function checkMessage(msg, rinfo) {
let message = sip.parse(msg);
//check for undefined
if (typeof message === 'undefined') return;
//console.log('newMsg',message);
if (message.method) {
console.log(message.method + ' ' + message.headers.from.uri + '->' + message.headers.to.uri);
switch (message.method) {
case 'REGISTER':
checkREGISTER(message, rinfo);
break;
case 'OPTIONS':
checkOPTIONS(message, rinfo);
break;
case 'INVITE':
checkINVITE(message, rinfo);
break;
default:
checkGENERIC(message, rinfo);
break;
}
} else if (message.status) {
checkRESPONSE(message, rinfo);
}
}
function checkREGISTER(msg, rinfo) {
console.log('checkREGISTER');
let msgid = msg.headers['call-id'] + '-' + msg.headers.cseq.seq + '-' + msg.headers.cseq.method;
//console.log('uri', msg.headers.via);
cache.hsetnx('requests', msgid, JSON.stringify({
ts: new Date().getTime(),
msg: msg
}), function () {
try {
//URI (must be *.sipflare.com)
let reguri = sip.parseUri(msg.uri);
console.log('uri host ', reguri.host);
getSubdomain(reguri.host,function(err,data){
try {
console.log(data);
if (data.rowCount > 0) {
let tmpserver;
let dataRow = data.rows[0].row_to_json;
for (var i = 0; i < dataRow.options.servers.length; i++) {
var entry = dataRow.options.servers[i];
console.log('entry', entry);
if (tmpserver) {
if (entry.last > tmpserver.last) {
tmpserver = entry;
}
} else {
tmpserver = entry;
}
}
if (tmpserver) {
//VIA
msg.headers.via[0].host = cfg.listen_ip;
msg.headers.via[0].port = cfg.listen_port;
// CONTACT
let contacturi = sip.parseUri(msg.headers.contact[0].uri);
contacturi.host = cfg.listen_ip;
contacturi.port = cfg.listen_port;
contacturi.params.sipflare = encrypt(rinfo.address + ':' + rinfo.port);
//console.log('uri',contacturi);
msg.headers.contact[0].uri = sip.stringifyUri(contacturi);
sendMessage(sip.stringify(msg), tmpserver.server, tmpserver.port);
}
} else {
try {
sendMessage(sip.stringify(sip.makeResponse(msg, 403, 'Forbidden')), msg.headers.via[0].host, msg.headers.via[0].port);
} catch (e2) {}
}
} catch (e1) {
console.log('EX checkREGISTER', e1);
}
});
} catch (e) {
console.log('EX checkREGISTER', e);
}
});
}
function checkOPTIONS(msg, rinfo) {
console.log('checkOPTIONS');
let msgid = msg.headers['call-id'] + '-' + msg.headers.cseq.seq + '-' + msg.headers.cseq.method;
//console.log('uri', msg.headers.via);
cache.hsetnx('requests', msgid, JSON.stringify({
ts: new Date().getTime(),
msg: msg
}), function () {
//VIA
msg.headers.via[0].host = cfg.listen_ip;
msg.headers.via[0].port = cfg.listen_port;
// CONTACT
if (msg.headers.contact) {
let contacturi = sip.parseUri(msg.headers.contact[0].uri);
contacturi.host = cfg.listen_ip;
contacturi.port = cfg.listen_port;
msg.headers.contact[0].uri = sip.stringifyUri(contacturi);
}
let tmpip = remip;
let tmpport = remport;
let touri = sip.parseUri(msg.headers.to.uri);
//check for sipflare
if (touri.params.sipflare) {
//console.log('SIPFLARE recv');
try {
let tmpdecr = decrypt(touri.params.sipflare);
if (tmpdecr.indexOf(':') > -1) {
let tmpaddr = tmpdecr.split(':');
if (tmpaddr.length === 2) {
tmpip = tmpaddr[0];
tmpport = tmpaddr[1];
//console.log('new addr ', tmpip,tmpport);
delete touri.params.sipflare;
msg.headers.to.uri = sip.stringifyUri(touri);
}
}
} catch (e) {}
}
sendMessage(sip.stringify(msg), tmpip, tmpport);
});
}
function checkINVITE(msg, rinfo) {
console.log('checkINVITE');
let msgid = msg.headers['call-id'] + '-' + msg.headers.cseq.seq + '-' + msg.headers.cseq.method;
//console.log('uri', msg.headers.via);
cache.hsetnx('requests', msgid, JSON.stringify({
ts: new Date().getTime(),
msg: msg
}), function () {
//VIA
msg.headers.via[0].host = cfg.listen_ip;
msg.headers.via[0].port = cfg.listen_port;
// CONTACT
let contacturi = sip.parseUri(msg.headers.contact[0].uri);
contacturi.host = cfg.listen_ip;
contacturi.port = cfg.listen_port;
let tmpip = remip;
let tmpport = remport;
let touri = sip.parseUri(msg.headers.to.uri);
//check for sipflare
if (touri.params.sipflare) {
//console.log('SIPFLARE recv');
try {
let tmpdecr = decrypt(touri.params.sipflare);
if (tmpdecr.indexOf(':') > -1) {
let tmpaddr = tmpdecr.split(':');
if (tmpaddr.length === 2) {
tmpip = tmpaddr[0];
tmpport = tmpaddr[1];
//console.log('new addr ', tmpip,tmpport);
delete touri.params.sipflare;
msg.headers.to.uri = sip.stringifyUri(touri);
}
}
} catch (e) {}
}
msg.headers.contact[0].uri = sip.stringifyUri(contacturi);
sendMessage(sip.stringify(msg), tmpip, tmpport);
});
}
function checkGENERIC(msg, rinfo) {
console.log('checkGENERIC');
let msgid = msg.headers['call-id'] + '-' + msg.headers.cseq.seq + '-' + msg.headers.cseq.method;
//console.log('uri', msg.headers.via);
cache.hsetnx('requests', msgid, JSON.stringify({
ts: new Date().getTime(),
msg: msg
}), function () {
//VIA
msg.headers.via[0].host = cfg.listen_ip;
msg.headers.via[0].port = cfg.listen_port;
// CONTACT
if (msg.headers.contact) {
let contacturi = sip.parseUri(msg.headers.contact[0].uri);
contacturi.host = cfg.listen_ip;
contacturi.port = cfg.listen_port;
msg.headers.contact[0].uri = sip.stringifyUri(contacturi);
}
let tmpip = remip;
let tmpport = remport;
let touri = sip.parseUri(msg.headers.to.uri);
//check for sipflare
if (touri.params.sipflare) {
//console.log('SIPFLARE recv');
try {
let tmpdecr = decrypt(touri.params.sipflare);
if (tmpdecr.indexOf(':') > -1) {
let tmpaddr = tmpdecr.split(':');
if (tmpaddr.length === 2) {
tmpip = tmpaddr[0];
tmpport = tmpaddr[1];
//console.log('new addr ', tmpip,tmpport);
delete touri.params.sipflare;
msg.headers.to.uri = sip.stringifyUri(touri);
}
}
} catch (e) {}
}
sendMessage(sip.stringify(msg), tmpip, tmpport);
});
}
function checkRESPONSE(msg, rinfo) {
let msgid = msg.headers['call-id'] + '-' + msg.headers.cseq.seq + '-' + msg.headers.cseq.method;
cache.hget('requests', msgid, function (err, origmsg) {
try {
let reqmsg = JSON.parse(origmsg);
//REPLACE VIA
msg.headers.via[0].host = cfg.listen_ip;
msg.headers.via[0].port = cfg.listen_port;
//let contacturi = sip.parseUri(reqmsg.msg.headers.contact[0].uri);
// CONTACT
if (msg.headers.contact) {
let tmpcontacturi = sip.parseUri(msg.headers.contact[0].uri);
tmpcontacturi.host = cfg.listen_ip;
tmpcontacturi.port = cfg.listen_port;
msg.headers.contact[0].uri = sip.stringifyUri(tmpcontacturi);
}
console.log('sendMsg', msg.status, reqmsg.msg.headers.via[0].host, reqmsg.msg.headers.via[0].port);
sendMessage(sip.stringify(msg), reqmsg.msg.headers.via[0].host, reqmsg.msg.headers.via[0].port);
//sendMessage(sip.stringify(msg),contacturi.host,contacturi.port);
dialogTrash.push({
ts: (new Date().getTime() + 10000),
dlgid: msgid
});
} catch (e) {}
});
}
function sendMessage(msg, address, port) {
//console.log('sendMessage',address,port,msg);
server.send(msg, port, address, (err) => {
//console.log('sendMessage err',err);
});
}
function dialogTrashExecuter() {
for (let x = 0; x < dialogTrash.length; x++) {
let trashobj = dialogTrash[x];
if (trashobj) {
if (new Date().getTime() > trashobj.ts) {
dialogTrash.splice(x, 1);
cache.hexists('requests', trashobj.dlgid, function (exists) {
if (exists) {
cache.hdel('requests', trashobj.dlgid, function () {});
console.log('[TRASH] transaction ' + trashobj.dlgid + ' removed');
}
});
}
}
}
}
function getSubdomain(subdomain, callback) {
let qry = 'select row_to_json(t) from (select managed,options from dns_domains where domain = $1) t';
let param = [subdomain];
client.query(qry, param, (err, res) => {
callback(err, res);
});
}
/* CRYPTO FUNCTIONS */
function encrypt(text) {
var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text) {
var decipher = crypto.createDecipher(algorithm, password)
var dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8');
return dec;
}