support OPTIONS ping to SBCs

This commit is contained in:
Dave Horton
2020-02-17 21:41:35 -05:00
parent 3c89b7fd76
commit 162dfff5a3
9 changed files with 109 additions and 77 deletions

View File

@@ -5,6 +5,8 @@ const PORT = process.env.HTTP_PORT || 3000;
function installSrfLocals(srf, logger) {
if (srf.locals.dbHelpers) return;
const {getSBC, getSrf} = require('./sbc-pinger')(logger);
const freeswitch = process.env.JAMBONES_FREESWITCH
.split(',')
.map((fs) => {
@@ -13,19 +15,6 @@ function installSrfLocals(srf, logger) {
});
logger.info({freeswitch}, 'freeswitch inventory');
const sbcs = process.env.JAMBONES_SBCS
.split(',')
.map((sbc) => sbc.trim());
logger.info({sbcs}, 'SBC inventory');
const drachtio = process.env.JAMBONES_FEATURE_SERVERS
.split(',')
.map((fs) => {
const arr = /^(.*):(.*):(.*)/.exec(fs);
if (arr) return {host: arr[1], port: arr[2], secret: arr[3]};
});
logger.info({drachtio}, 'drachtio feature server inventory');
const {
lookupAppByPhoneNumber,
lookupAppBySid,
@@ -60,9 +49,9 @@ function installSrfLocals(srf, logger) {
parentLogger: logger,
ipv4: localIp,
serviceUrl: `http://${localIp}:${PORT}`,
freeswitch: freeswitch[0],
sbcs,
drachtio
getSBC,
getSrf,
getFreeswitch: () => freeswitch[0]
});
}

View File

@@ -33,8 +33,8 @@ class Requestor {
const myPort = u.port ? `:${u.port}` : '';
const baseUrl = `${u.protocol}://${u.resource}${myPort}`;
this.get = bent(baseUrl, 'GET', 'buffer', 200);
this.post = bent(baseUrl, 'POST', 'buffer', 200);
this.get = bent(baseUrl, 'GET', 'buffer', 200, 201);
this.post = bent(baseUrl, 'POST', 'buffer', 200, 201);
assert(isAbsoluteUrl(this.url));
assert(['GET', 'POST'].includes(this.method));
@@ -64,7 +64,7 @@ class Requestor {
this.logger.debug({hook}, `Requestor:request ${method} ${url}`);
const buf = isRelativeUrl(url) ?
await this.post(url, params, this.authHeader) :
await bent(method, 'buffer', 200)(url, params, basicAuth(username, password));
await bent(method, 'buffer', 200, 201)(url, params, basicAuth(username, password));
//this.logger.debug({body: }, `Requestor:request ${method} ${url} succeeded`);
if (buf && buf.toString().length > 0) {

71
lib/utils/sbc-pinger.js Normal file
View File

@@ -0,0 +1,71 @@
const assert = require('assert');
const noopLogger = {info: () => {}, error: () => {}};
const Srf = require('drachtio-srf');
const debug = require('debug')('jambonz:sbc-inbound');
const srfs = [];
module.exports = (logger) => {
logger = logger || noopLogger;
let idxSbc = 0, idxSrfs = 0;
assert.ok(process.env.JAMBONES_SBCS, 'missing JAMBONES_SBCS env var');
const sbcs = process.env.JAMBONES_SBCS
.split(',')
.map((sbc) => `sip:${sbc.trim()}`);
assert.ok(sbcs.length, 'JAMBONES_SBCS env var is empty or misconfigured');
logger.info({sbcs}, 'SBC inventory');
assert.ok(process.env.JAMBONES_FEATURE_SERVERS, 'missing JAMBONES_FEATURE_SERVERS env var');
const drachtio = process.env.JAMBONES_FEATURE_SERVERS
.split(',')
.map((fs) => {
const arr = /^(.*):(.*):(.*)/.exec(fs);
if (!arr) throw new Error('JAMBONES_FEATURE_SERVERS env var is misconfigured');
const srf = new Srf();
srf.connect({host: arr[1], port: arr[2], secret: arr[3]})
.on('connect', (err, hp) => {
if (err) return logger.info(err, `Error connecting to drachtio server at ${arr[1]}:${arr[2]}`);
srfs.push(srf);
logger.info(err, `Success connecting to FS at ${arr[1]}:${arr[2]}, ${srfs.length} online`);
pingProxies(srf);
})
.on('error', (err) => {
const place = srfs.indexOf(srf);
if (-1 !== place) srfs.splice(place, 1);
logger.info(err, `Error connecting to FS at ${arr[1]}:${arr[2]}, ${srfs.length} remain online`);
});
return {host: arr[1], port: arr[2], secret: arr[3]};
});
assert.ok(drachtio.length, 'JAMBONES_FEATURE_SERVERS env var is empty');
logger.info({drachtio}, 'drachtio feature server inventory');
async function pingProxies(srf) {
for (const sbc of sbcs) {
try {
const req = await srf.request({
uri: sbc,
method: 'OPTIONS',
headers: {
'X-FS-Status': 'open'
}
});
req.on('response', (res) => {
debug(`received ${res.status} from SBC`);
});
} catch (err) {
logger.error(err, `Error sending OPTIONS to ${sbc}`);
}
}
}
// OPTIONS ping the SBCs from each feature server every 60 seconds
setInterval(() => {
srfs.forEach((srf) => pingProxies(srf));
}, 60000);
return {
getSBC: () => sbcs[idxSbc++ % sbcs.length],
getSrf: () => srfs[idxSrfs++ % srfs.length]
};
};