diff --git a/lib/middleware.js b/lib/middleware.js index e71c054a..b114c38c 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -20,7 +20,8 @@ module.exports = function(srf, logger) { lookupAppByRegex, lookupAppBySid, lookupAppByRealm, - lookupAppByTeamsTenant + lookupAppByTeamsTenant, + registrar } = srf.locals.dbHelpers; const { writeAlerts, @@ -191,8 +192,37 @@ module.exports = function(srf, logger) { const sipRealm = arr[2]; logger.debug(`looking for device calling app for realm ${sipRealm}`); app = await lookupAppByRealm(sipRealm); - if (app) logger.debug({app}, `retrieved device calling app for realm ${sipRealm}`); - + if (app) { + logger.debug({app}, `retrieved device calling app for realm ${sipRealm}`); + } else { + const calledAor = `${req.calledNumber}@${sipRealm}`; + const reg = await registrar.query(calledAor); + if (reg) { + logger.debug(`There is no device app found, called client is registered, + forwarding call to called client.`); + app = { + // Dummy hook to follow later feature server logic. + call_hook: { + url: 'https://jambonz.org', + method: 'GET' + }, + account_sid, + app_json: JSON.stringify( + [{ + verb: 'dial', + callerId: arr[1], + answerOnBridge: true, + target: [ + { + type: 'user', + name: calledAor + } + ] + }] + ) + }; + } + } } } else if (req.locals.msTeamsTenant) { @@ -257,7 +287,7 @@ module.exports = function(srf, logger) { app2.requestor = new HttpRequestor(logger, account_sid, app.call_hook, accountInfo.account.webhook_secret); if (app.call_status_hook) app2.notifier = new HttpRequestor(logger, account_sid, app.call_status_hook, accountInfo.account.webhook_secret); - else app2.notifier = {request: () => {}}; + else app2.notifier = {request: () => {}, close: () => {}}; } req.locals.application = app2; diff --git a/lib/utils/install-srf-locals.js b/lib/utils/install-srf-locals.js index 95eadbb3..bfd1812c 100644 --- a/lib/utils/install-srf-locals.js +++ b/lib/utils/install-srf-locals.js @@ -17,6 +17,7 @@ const { PORT, NODE_ENV, } = require('../config'); +const Registrar = require('@jambonz/mw-registrar'); const assert = require('assert'); function initMS(logger, wrapper, ms) { @@ -177,6 +178,7 @@ function installSrfLocals(srf, logger) { host: JAMBONES_REDIS_HOST, port: JAMBONES_REDIS_PORT || 6379 }, logger, tracer); + const registrar = new Registrar(logger, client); const { synthAudio, getNuanceAccessToken, @@ -204,6 +206,7 @@ function installSrfLocals(srf, logger) { srf.locals = {...srf.locals, dbHelpers: { client, + registrar, pool, lookupAppByPhoneNumber, lookupAppByRegex, diff --git a/package-lock.json b/package-lock.json index 0d1ae1c2..fb464f15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@aws-sdk/client-sns": "^3.360.0", "@jambonz/db-helpers": "^0.9.1", "@jambonz/http-health-check": "^0.0.1", + "@jambonz/mw-registrar": "^0.2.4", "@jambonz/realtimedb-helpers": "^0.8.6", "@jambonz/speech-utils": "^0.0.22", "@jambonz/stats-collector": "^0.1.9", @@ -3126,6 +3127,14 @@ "node": ">=14.x" } }, + "node_modules/@jambonz/mw-registrar": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@jambonz/mw-registrar/-/mw-registrar-0.2.4.tgz", + "integrity": "sha512-GHRlvHsDvwfQfqOUKh4Emt+CvoiODQ+Tny5zg8ZT5q5yz77qy3XIfOF072xO9gDSPrXjn0rH13W3iIKtNzcOIA==", + "dependencies": { + "debug": "^4.3.1" + } + }, "node_modules/@jambonz/realtimedb-helpers": { "version": "0.8.6", "resolved": "https://registry.npmjs.org/@jambonz/realtimedb-helpers/-/realtimedb-helpers-0.8.6.tgz", @@ -13212,6 +13221,14 @@ "express": "^4.17.2" } }, + "@jambonz/mw-registrar": { + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/@jambonz/mw-registrar/-/mw-registrar-0.2.4.tgz", + "integrity": "sha512-GHRlvHsDvwfQfqOUKh4Emt+CvoiODQ+Tny5zg8ZT5q5yz77qy3XIfOF072xO9gDSPrXjn0rH13W3iIKtNzcOIA==", + "requires": { + "debug": "^4.3.1" + } + }, "@jambonz/realtimedb-helpers": { "version": "0.8.6", "resolved": "https://registry.npmjs.org/@jambonz/realtimedb-helpers/-/realtimedb-helpers-0.8.6.tgz", diff --git a/package.json b/package.json index 82708710..e9895b1a 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@aws-sdk/client-sns": "^3.360.0", "@jambonz/db-helpers": "^0.9.1", "@jambonz/http-health-check": "^0.0.1", + "@jambonz/mw-registrar": "^0.2.4", "@jambonz/realtimedb-helpers": "^0.8.6", "@jambonz/speech-utils": "^0.0.22", "@jambonz/stats-collector": "^0.1.9",