From 563ea405e972045df7d3981238bc867ffdf46374 Mon Sep 17 00:00:00 2001 From: Quan HL Date: Tue, 24 Oct 2023 12:56:07 +0700 Subject: [PATCH] wip --- app.js | 3 +++ lib/call-session.js | 3 +++ lib/middleware.js | 13 ++++++++++++- package-lock.json | 17 +++++++++++++++++ package.json | 1 + 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index db46075..eea7198 100644 --- a/app.js +++ b/app.js @@ -67,6 +67,7 @@ const {LifeCycleEvents} = require('./lib/constants'); const setNameRtp = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:active-rtp`; const rtpServers = []; const setName = `${(process.env.JAMBONES_CLUSTER_ID || 'default')}:active-sip`; +const Registrar = require('@jambonz/mw-registrar'); const { pool, @@ -101,6 +102,7 @@ const { host: process.env.JAMBONES_REDIS_HOST, port: process.env.JAMBONES_REDIS_PORT || 6379 }, logger); +const registrar = new Registrar(logger, redisClient); const ngProtocol = process.env.JAMBONES_NG_PROTOCOL || 'udp'; const ngPort = process.env.RTPENGINE_PORT || ('udp' === ngProtocol ? 22222 : 8080); @@ -121,6 +123,7 @@ srf.locals = {...srf.locals, activeCallIds: new Map(), getRtpEngine, dbHelpers: { + registrar, pool, ping, lookupAuthHook, diff --git a/lib/call-session.js b/lib/call-session.js index b6a9815..73cc650 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -227,6 +227,9 @@ class CallSession extends Emitter { if (this.req.locals.queue_name) { Object.assign(headers, {'X-Queue-Name': this.req.locals.queue_name}); } + if (this.req.locals.called_user) { + Object.assign(headers, {'X-Called-User': this.req.locals.called_user}); + } if (this.req.authorization) { if (this.req.authorization.grant && this.req.authorization.grant.application_sid) { Object.assign(headers, {'X-Application-Sid': this.req.authorization.grant.application_sid}); diff --git a/lib/middleware.js b/lib/middleware.js index dcdac30..2850968 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -29,7 +29,8 @@ module.exports = function(srf, logger) { lookupAccountBySid, lookupAccountCapacitiesBySid, queryCallLimits, - lookupAppBySid + lookupAppBySid, + registrar } = srf.locals.dbHelpers; const {stats, writeCdrs} = srf.locals; @@ -198,6 +199,7 @@ module.exports = function(srf, logger) { return req.srf.endSession(req); } let deviceAppSid = null; + let called_user = null; const queue_name = uri.user.startsWith('queue-') ? uri.user.match(/queue-(.*)/)[1] : null; if (uri.user.startsWith('app-')) { // Call from registered device to test application. @@ -206,6 +208,14 @@ module.exports = function(srf, logger) { if (app && app.account_sid === account.account_sid) { deviceAppSid = app.application_sid; } + } else if (!queue_name) { + // check if call to registered user + const {realm} = this.req.authorization.challengeResponse; + const calledAor = `${req.calledNumber}@${realm}`; + const reg = await registrar.query(calledAor); + if (reg) { + called_user = calledAor; + } } req.locals = { service_provider_sid: account.service_provider_sid, @@ -213,6 +223,7 @@ module.exports = function(srf, logger) { account, application_sid: deviceAppSid || account.device_calling_application_sid, ...(queue_name && ({queue_name})), + ...(called_user && ({called_user})), webhook_secret: account.webhook_secret, realm: uri.host, ...(account.registration_hook && { diff --git a/package-lock.json b/package-lock.json index 17a3ce7..0971bbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@jambonz/db-helpers": "^0.9.1", "@jambonz/digest-utils": "^0.0.3", "@jambonz/http-health-check": "^0.0.1", + "@jambonz/mw-registrar": "^0.2.4", "@jambonz/realtimedb-helpers": "^0.8.6", "@jambonz/rtpengine-utils": "^0.4.3", "@jambonz/siprec-client-utils": "^0.2.6", @@ -1696,6 +1697,14 @@ "node": ">=14.x" } }, + "node_modules/@jambonz/mw-registrar": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/@jambonz/mw-registrar/-/mw-registrar-0.2.5.tgz", + "integrity": "sha512-+aBI2xpR6Ir140Hi7/ED+z5Hl7NgCalyVzTLNlgUVzTvsMLtyZZh6n9IQipKnuKvhh4nPM4aJ+JuFhi1UH9zEA==", + "dependencies": { + "debug": "^4.3.4" + } + }, "node_modules/@jambonz/realtimedb-helpers": { "version": "0.8.6", "resolved": "https://registry.npmjs.org/@jambonz/realtimedb-helpers/-/realtimedb-helpers-0.8.6.tgz", @@ -7664,6 +7673,14 @@ "express": "^4.17.2" } }, + "@jambonz/mw-registrar": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/@jambonz/mw-registrar/-/mw-registrar-0.2.5.tgz", + "integrity": "sha512-+aBI2xpR6Ir140Hi7/ED+z5Hl7NgCalyVzTLNlgUVzTvsMLtyZZh6n9IQipKnuKvhh4nPM4aJ+JuFhi1UH9zEA==", + "requires": { + "debug": "^4.3.4" + } + }, "@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 826de37..7cc29d7 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@jambonz/stats-collector": "^0.1.9", "@jambonz/time-series": "^0.2.5", "@jambonz/digest-utils": "^0.0.3", + "@jambonz/mw-registrar": "^0.2.4", "@aws-sdk/client-sns": "^3.360.0", "@aws-sdk/client-auto-scaling": "^3.360.0", "bent": "^7.3.12",