-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
49 lines (40 loc) · 1.3 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
'use strict';
var drachtio = require('drachtio') ;
var app = drachtio() ;
var config = app.config = require('./config');
var fs = require('fs') ;
var rangeCheck = require('range_check');
var debug = app.debug = require('debug')('simple-sip-proxy') ;
// Expose app
exports = module.exports = app;
//connect to drachtio server
app.connect({
host: config.drachtioServer.address,
port: config.drachtioServer.port,
secret: config.drachtioServer.secret,
}) ;
app.on('connect', function(err, hostport) {
if( err ) throw err ;
console.log('connected to drachtio server at %s', hostport) ;
})
.on('error', function(err){
console.warn(err.message ) ;
}) ;
app.use(function checkSender(req, res, next) {
if( !rangeCheck.inRange( req.source_address, app.config.authorizedSources) ) { return res.send(403) ; }
next() ;
}) ;
app.use(function onlyInvites( req, res, next ) {
if( -1 === ['invite','cancel','prack','ack'].indexOf( req.method.toLowerCase() ) ) { return res.send(405); }
next() ;
});
require('./lib/proxy.js')(app);
//handle any errors thrown while executing middleware stack
app.use(function myErrorHandler(err, req, res, next) {
debug('caught error generated from middleware: ', err);
res.send(500, {
headers: {
'X-Error-Info': err.message || 'unknown application error'
}
}) ;
}) ;