many changes from testing

This commit is contained in:
Dave Horton
2020-02-22 11:06:39 -05:00
parent 802cc1944f
commit 4bd9e015b5
10 changed files with 131 additions and 46 deletions

2
app.js
View File

@@ -6,6 +6,8 @@ assert.ok(process.env.JAMBONES_MYSQL_HOST &&
assert.ok(process.env.DRACHTIO_PORT || process.env.DRACHTIO_HOST, 'missing DRACHTIO_PORT env var');
assert.ok(process.env.DRACHTIO_SECRET, 'missing DRACHTIO_SECRET env var');
assert.ok(process.env.JAMBONES_FREESWITCH, 'missing JAMBONES_FREESWITCH env var');
assert.ok(process.env.JAMBONES_REDIS_HOST, 'missing JAMBONES_REDIS_HOST env var');
assert.ok(process.env.JAMBONES_NETWORK_CIDR, 'missing JAMBONES_SUBNET env var');
const Srf = require('drachtio-srf');
const srf = new Srf();

View File

@@ -3,4 +3,8 @@ const api = require('express').Router();
api.use('/createCall', require('./create-call'));
api.use('/updateCall', require('./update-call'));
// health checks
api.get('/', (req, res) => res.sendStatus(200));
api.get('/health', (req, res) => res.sendStatus(200));
module.exports = api;

View File

@@ -2,6 +2,7 @@ const Task = require('./task');
const makeTask = require('./make_task');
const {CallStatus, CallDirection, TaskName, TaskPreconditions, MAX_SIMRINGS} = require('../utils/constants');
const assert = require('assert');
const parseUri = require('drachtio-srf').parseUri;
const placeCall = require('../utils/place-outdial');
const sessionTracker = require('../session/session-tracker');
const DtmfCollector = require('../utils/dtmf-collector');
@@ -237,10 +238,21 @@ class TaskDial extends Task {
async _attemptCalls(cs) {
const {req, srf} = cs;
const {getSBC} = srf;
const sbcAddress = cs.direction === CallDirection.Inbound ?
`${req.source_address}:${req.source_port}` :
getSBC();
const {getSBC} = srf.locals;
const sbcAddress = getSBC();
/*
if (CallDirection.Inbound === cs.direction) {
const contact = req.getParsedHeader('Contact');
const uri = parseUri(contact[0].uri);
this.logger.debug({contact}, 'outdialing with contact');
sbcAddress = `${uri.host}:${uri.port || 5060}`;
//sbcAddress = `${req.source_address}:${req.source_port}`;
}
else {
sbcAddress = getSBC();
}
*/
if (!sbcAddress) throw new Error('no SBC found for outbound call');
const opts = {
headers: req && req.has('X-CID') ? Object.assign(this.headers, {'X-CID': req.get('X-CID')}) : this.headers,

View File

@@ -5,6 +5,13 @@ const {TaskPreconditions, CallDirection} = require('../utils/constants');
const CallInfo = require('../session/call-info');
const assert = require('assert');
const ConfirmCallSession = require('../session/confirm-call-session');
const selectSbc = require('./select-sbc');
const Registrar = require('jambonz-mw-registrar');
const registrar = new Registrar({
host: process.env.JAMBONES_REDIS_HOST,
port: process.env.JAMBONES_REDIS_PORT || 6379
});
const moment = require('moment');
const uuidv4 = require('uuid/v4');
@@ -52,28 +59,39 @@ class SingleDialer extends Emitter {
async exec(srf, ms, opts) {
let uri, to;
switch (this.target.type) {
case 'phone':
assert(this.target.number);
uri = `sip:${this.target.number}@${this.sbcAddress}`;
to = this.target.number;
break;
case 'user':
assert(this.target.name);
uri = `sip:${this.target.name}`;
to = this.target.name;
break;
case 'sip':
assert(this.target.sipUri);
uri = this.target.sipUri;
to = this.target.sipUri;
break;
default:
// should have been caught by parser
assert(false, `invalid dial type ${this.target.type}: must be phone, user, or sip`);
}
try {
switch (this.target.type) {
case 'phone':
assert(this.target.number);
uri = `sip:${this.target.number}@${this.sbcAddress}`;
to = this.target.number;
break;
case 'user':
assert(this.target.name);
const aor = this.target.name;
uri = `sip:${this.target.name}`;
to = this.target.name;
// need to send to the SBC registered on
const reg = await registrar.query(aor);
if (reg) {
const sbc = selectSbc(reg.sbcAddress);
if (sbc) {
this.logger.debug(`SingleDialer:exec retrieved registration details for ${aor}, using sbc at ${sbc}`);
this.sbcAddress = sbc;
}
}
break;
case 'sip':
assert(this.target.sipUri);
uri = this.target.sipUri;
to = this.target.sipUri;
break;
default:
// should have been caught by parser
assert(false, `invalid dial type ${this.target.type}: must be phone, user, or sip`);
}
this.updateCallStatus = srf.locals.dbHelpers.updateCallStatus;
this.serviceUrl = srf.locals.serviceUrl;

View File

@@ -41,6 +41,9 @@ class Requestor {
assert(isAbsoluteUrl(this.url));
assert(['GET', 'POST'].includes(this.method));
const {stats} = require('../../').srf.locals;
this.stats = stats;
}
get baseUrl() {
@@ -65,14 +68,26 @@ class Requestor {
const {username, password} = typeof hook === 'object' ? hook : {};
assert.ok(url, 'Requestor:request url was not provided');
assert.ok, (['GET', 'POST'].includes(method), `Requestor:request method must be 'GET' or 'POST' not ${method}`);
assert.ok, (['GET', 'POST'].includes(method), `Requestor:request method must be 'GET' or 'POST' not ${method}`);
this.logger.debug({hook, params}, `Requestor:request ${method} ${url}`);
const buf = isRelativeUrl(url) ?
await this.post(url, params, this.authHeader) :
await bent(method, 'buffer', 200, 201)(url, params, basicAuth(username, password));
this.logger.debug(`Requestor:request ${method} ${url} succeeded`);
const startAt = process.hrtime();
let buf;
try {
buf = isRelativeUrl(url) ?
await this.post(url, params, this.authHeader) :
await bent(method, 'buffer', 200, 201)(url, params, basicAuth(username, password));
} catch (err) {
this.logger.info({baseUrl: this.baseUrl, url: err.statusCode},
`web callback returned unexpected error code ${err.statusCode}`);
throw err;
}
const diff = process.hrtime(startAt);
const time = diff[0] * 1e3 + diff[1] * 1e-6;
const rtt = time.toFixed(0);
this.logger.debug(`Requestor:request ${method} ${url} succeeded in ${rtt}ms`);
if (buf) this.stats.histogram('app.hook.response_time', rtt, ['hook_type:app']);
if (buf && buf.toString().length > 0) {
try {

View File

@@ -11,7 +11,7 @@ module.exports = (logger) => {
assert.ok(process.env.JAMBONES_SBCS, 'missing JAMBONES_SBCS env var');
const sbcs = process.env.JAMBONES_SBCS
.split(',')
.map((sbc) => `sip:${sbc.trim()}`);
.map((sbc) => sbc.trim());
assert.ok(sbcs.length, 'JAMBONES_SBCS env var is empty or misconfigured');
logger.info({sbcs}, 'SBC inventory');
@@ -43,7 +43,7 @@ module.exports = (logger) => {
for (const sbc of sbcs) {
try {
const req = await srf.request({
uri: sbc,
uri: `sip:${sbc}`,
method: 'OPTIONS',
headers: {
'X-FS-Status': 'open'

13
lib/utils/select-sbc.js Normal file
View File

@@ -0,0 +1,13 @@
const CIDRMatcher = require('cidr-matcher');
const matcher = new CIDRMatcher([process.env.JAMBONES_NETWORK_CIDR]);
module.exports = (sbcList) => {
const obj = sbcList
.split(',')
.map((str) => {
const arr = /^(.*)\/(.*):(\d+)$/.exec(str);
return {protocol: arr[1], host: arr[2], port: arr[3]};
})
.find((obj) => 'udp' == obj.protocol && matcher.contains(obj.host));
if (obj) return `${obj.host}:${obj.port}`;
};

40
package-lock.json generated
View File

@@ -246,8 +246,7 @@
"assert-plus": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=",
"dev": true
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
},
"astral-regex": {
"version": "1.0.0",
@@ -518,6 +517,14 @@
"readdirp": "~3.3.0"
}
},
"cidr-matcher": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/cidr-matcher/-/cidr-matcher-2.1.1.tgz",
"integrity": "sha512-QPJRz4HDQxpB8AZWEqd6ejVp+siArXh3u1MYaUFV85cd293StGSMb87jVe0z9gS92KsFwxCxjb3utO3e5HKHTw==",
"requires": {
"ip6addr": "^0.2.2"
}
},
"clear-module": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/clear-module/-/clear-module-4.0.0.tgz",
@@ -684,8 +691,7 @@
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=",
"dev": true
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"coveralls": {
"version": "3.0.9",
@@ -1287,8 +1293,7 @@
"extsprintf": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=",
"dev": true
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU="
},
"fast-deep-equal": {
"version": "3.1.1",
@@ -1817,6 +1822,15 @@
"resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz",
"integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo="
},
"ip6addr": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/ip6addr/-/ip6addr-0.2.3.tgz",
"integrity": "sha512-qA9DXRAUW+lT47/i/4+Q3GHPwZjGt/atby1FH/THN6GVATA6+Pjp2nztH7k6iKeil7hzYnBwfSsxjthlJ8lJKw==",
"requires": {
"assert-plus": "^1.0.0",
"jsprim": "^1.4.0"
}
},
"ipaddr.js": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz",
@@ -2162,6 +2176,15 @@
"mysql2": "^2.0.2"
}
},
"jambonz-mw-registrar": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/jambonz-mw-registrar/-/jambonz-mw-registrar-0.1.3.tgz",
"integrity": "sha512-HzC8EbsoMoZRBvFlkmVAQrm3/2khYBQRUFIL0ji/ficsV4Ts0e0ABaByitR2fEj8+q3K9d2d0iBq1AAfUee/yg==",
"requires": {
"bluebird": "^3.5.5",
"redis": "^2.8.0"
}
},
"jambonz-realtimedb-helpers": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/jambonz-realtimedb-helpers/-/jambonz-realtimedb-helpers-0.1.7.tgz",
@@ -2257,8 +2280,7 @@
"json-schema": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz",
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=",
"dev": true
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
},
"json-schema-traverse": {
"version": "0.4.1",
@@ -2282,7 +2304,6 @@
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
"integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=",
"dev": true,
"requires": {
"assert-plus": "1.0.0",
"extsprintf": "1.3.0",
@@ -5523,7 +5544,6 @@
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
"integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=",
"dev": true,
"requires": {
"assert-plus": "^1.0.0",
"core-util-is": "1.0.2",

View File

@@ -21,18 +21,20 @@
},
"scripts": {
"start": "node app",
"test": "NODE_ENV=test node test/ | ./node_modules/.bin/tap-spec",
"test": "NODE_ENV=test JAMBONES_NETWORK_CIDR=127.0.0.1/32 node test/ | ./node_modules/.bin/tap-spec",
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
"jslint": "eslint app.js lib"
},
"dependencies": {
"bent": "^7.0.6",
"jambonz-mw-registrar": "^0.1.3",
"debug": "^4.1.1",
"drachtio-fn-b2b-sugar": "0.0.12",
"drachtio-fsmrf": "^1.5.14",
"drachtio-srf": "^4.4.28",
"express": "^4.17.1",
"ip": "^1.1.5",
"cidr-matcher": "^2.1.1",
"jambonz-db-helpers": "^0.3.2",
"jambonz-realtimedb-helpers": "^0.1.7",
"jambonz-stats-collector": "^0.0.3",

View File

@@ -47,10 +47,9 @@ test('app payload parsing tests', (t) => {
t.pass('alternate syntax works');
t.end();
process.exit(0);
});
console.log('exiting');
const errInvalidInstruction = () => makeTask(logger, require('./data/bad/unknown-instruction'));
const errUnknownProperty = () => makeTask(logger, require('./data/bad/unknown-property'));