Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drachtio srf update #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xtest/*
126 changes: 126 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaFeatures": {
"jsx": false,
"modules": false
},
"ecmaVersion": 2017
},
"plugins": ["promise"],
"rules": {
"promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"promise/no-native": "off",
"promise/no-nesting": "warn",
"promise/no-promise-in-callback": "warn",
"promise/no-callback-in-promise": "warn",
"promise/no-return-in-finally": "warn",

// Possible Errors
// http://eslint.org/docs/rules/#possible-errors
"comma-dangle": [2, "only-multiline"],
"no-control-regex": 2,
"no-debugger": 2,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-ex-assign": 2,
"no-extra-boolean-cast" : 2,
"no-extra-parens": [2, "functions"],
"no-extra-semi": 2,
"no-func-assign": 2,
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-negated-in-lhs": 2,
"no-obj-calls": 2,
"no-proto": 2,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"use-isnan": 2,
"valid-typeof": 2,

// Best Practices
// http://eslint.org/docs/rules/#best-practices
"no-fallthrough": 2,
"no-octal": 2,
"no-redeclare": 2,
"no-self-assign": 2,
"no-unused-labels": 2,

// Strict Mode
// http://eslint.org/docs/rules/#strict-mode
"strict": [2, "never"],

// Variables
// http://eslint.org/docs/rules/#variables
"no-delete-var": 2,
"no-undef": 2,
"no-unused-vars": [2, {"args": "none"}],

// Node.js and CommonJS
// http://eslint.org/docs/rules/#nodejs-and-commonjs
"no-mixed-requires": 2,
"no-new-require": 2,
"no-path-concat": 2,
"no-restricted-modules": [2, "sys", "_linklist"],

// Stylistic Issues
// http://eslint.org/docs/rules/#stylistic-issues
"comma-spacing": 2,
"eol-last": 2,
"indent": [2, 2, {"SwitchCase": 1}],
"keyword-spacing": 2,
"max-len": [2, 120, 2],
"new-parens": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multiple-empty-lines": [2, {"max": 2}],
"no-trailing-spaces": [2, {"skipBlankLines": false }],
"quotes": [2, "single", "avoid-escape"],
"semi": 2,
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": 2,

// ECMAScript 6
// http://eslint.org/docs/rules/#ecmascript-6
"arrow-parens": [2, "always"],
"arrow-spacing": [2, {"before": true, "after": true}],
"constructor-super": 2,
"no-class-assign": 2,
"no-confusing-arrow": 2,
"no-const-assign": 2,
"no-dupe-class-members": 2,
"no-new-symbol": 2,
"no-this-before-super": 2,
"prefer-const": 2
},
"globals": {
"DTRACE_HTTP_CLIENT_REQUEST" : false,
"LTTNG_HTTP_CLIENT_REQUEST" : false,
"COUNTER_HTTP_CLIENT_REQUEST" : false,
"DTRACE_HTTP_CLIENT_RESPONSE" : false,
"LTTNG_HTTP_CLIENT_RESPONSE" : false,
"COUNTER_HTTP_CLIENT_RESPONSE" : false,
"DTRACE_HTTP_SERVER_REQUEST" : false,
"LTTNG_HTTP_SERVER_REQUEST" : false,
"COUNTER_HTTP_SERVER_REQUEST" : false,
"DTRACE_HTTP_SERVER_RESPONSE" : false,
"LTTNG_HTTP_SERVER_RESPONSE" : false,
"COUNTER_HTTP_SERVER_RESPONSE" : false,
"DTRACE_NET_STREAM_END" : false,
"LTTNG_NET_STREAM_END" : false,
"COUNTER_NET_SERVER_CONNECTION_CLOSE" : false,
"DTRACE_NET_SERVER_CONNECTION" : false,
"LTTNG_NET_SERVER_CONNECTION" : false,
"COUNTER_NET_SERVER_CONNECTION" : false
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ node_modules
config.js

.DS_Store
.vscode
.eslint*
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
sudo: required

dist: xenial

language: node_js

node_js:
- "lts/*"

services:
- docker

before_install:
- docker pull drachtio/drachtio-server:latest
- docker pull drachtio/sipp:latest
- docker pull davehorton/freeswitch-hairpin:latest
- docker images

script:
- npm test
87 changes: 38 additions & 49 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
'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'
}
}) ;
}) ;


const Srf = require('drachtio-srf') ;
const srf = new Srf();
const config = require('config');
const pino = require('pino');
const level = config.has('log.level') ? config.get('log.level') : 'info';
const logger = srf.locals.logger = pino({level});
const RouteMgr = require('./lib/routing/route-manager');
const routeMgr = new RouteMgr({logger, srf});
const validate = require('./lib/validation-middleware')({logger, routeMgr});
const inviteHandler = require('./lib/invite')({logger, srf, routeMgr});

process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});

srf.connect(config.get('drachtio'))
.on('connect', (err, hp) => {
if (err) return logger.error(err, 'error connecting');
logger.info(`connected to drachtio server at ${hp}`);
});

if (process.env.NODE_ENV !== 'test') {
srf.on('error', (err) => {
logger.error(err, 'Error connecting to drachtio server');
});
}

srf.use(validate);
srf.invite(inviteHandler);
srf.info((req, res) => res.send(200));


// for test purposes only
if (process.env.NODE_ENV === 'test') {
srf.routeMgr = () => routeMgr;

module.exports = srf;
}
80 changes: 80 additions & 0 deletions config/default.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"drachtio": {
"host": "127.0.0.1",
"port": 9022,
"secret": "cymru"
},
"external-trunks": {
"carrier-1": {
"type": "in",
"signaling-address": ["10.10.100.1:5060", "10.10.100.2:5060"],
"enabled": false
},
"carrier-2": {
"type": "out",
"signaling-address": "10.10.100.5",
"groups": "cheap"
},
"carrier-3": {
"type": "both",
"signaling-address": "10.10.100.6",
"groups": ["cheap", "international"]
}
},
"internal-trunks": {
"appserver1": {
"signaling-address": "10.10.200.1",
"options-ping": true,
"enabled": true
},
"appserver2": {
"signaling-address": "10.10.200.2",
"options-ping": true,
"enabled": false
}
},
"options-ping": {
"interval": "6000"
},
"proxy-request-types": ["INVITE"],
"routing": {
"outbound": {
"default": [
{
"target": "carrier-2",
"ratio": 9
},
{
"target": "carrier-1",
"ratio": 1
}
]
},
"international": {
"regex": "+?1\\d{10}",
"match": ["international"]
},
"toll-free": {
"regex": "+?1\\d{10}",
"match": ["carrier-2"],
"no-match": ["carrier-3"]
},
"test": {
"regex": "+15083084809",
"against": "from",
"match": ["carrier-2"]
}
},
"inbound": {
"default": [
{
"target": "appserver1",
"ratio": 2
},
{
"target": "appserver2",
"ratio": 1
}
]
}
}
Loading