From 446bb06142031f349f8d2188e2de80aa5375e9d9 Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Thu, 3 Apr 2025 19:56:28 +0100 Subject: [PATCH] move lookupSystemInformation up to parent function and add a timer (#93) * move lookupSystemInformation up to parent function and add a timer * lint --- lib/regbot.js | 20 +++----------------- lib/sip-trunk-register.js | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/regbot.js b/lib/regbot.js index 8105419..514fcf9 100644 --- a/lib/regbot.js +++ b/lib/regbot.js @@ -36,22 +36,9 @@ class Regbot { } async start(srf) { - const { lookupSystemInformation } = srf.locals.dbHelpers; assert(!this.timer); this.logger.info(`starting regbot for ${this.fromUser}@${this.sip_realm}`); - try { - const info = await lookupSystemInformation(); - if (info) { - this.ourSipDomain = info.sip_domain_name; - this.logger.info(`lookup of sip domain from system_information: ${this.ourSipDomain}`); - } - else { - this.logger.info('no system_information found, we will use the realm or public ip as the domain'); - } - } catch (err) { - this.logger.info({ err }, 'Error looking up system information'); - } this.register(srf); } @@ -75,8 +62,7 @@ class Regbot { async register(srf) { const { updateVoipCarriersRegisterStatus } = srf.locals.dbHelpers; - const { writeAlerts } = srf.locals; - + const { writeAlerts, localSIPDomain } = srf.locals; try { // transport const transport = (this.protocol.includes('/') ? this.protocol.substring(0, this.protocol.indexOf('/')) : @@ -100,8 +86,8 @@ class Regbot { if (this.use_public_ip_in_contact) { contactAddress = `${this.fromUser}@${publicAddress}`; } - else if (this.ourSipDomain) { - contactAddress = `${this.fromUser}@${this.ourSipDomain}`; + else if (localSIPDomain) { + contactAddress = `${this.fromUser}@${localSIPDomain}`; } this.logger.debug(`sending REGISTER for ${this.aor}`); diff --git a/lib/sip-trunk-register.js b/lib/sip-trunk-register.js index 445fcb7..d6ff417 100644 --- a/lib/sip-trunk-register.js +++ b/lib/sip-trunk-register.js @@ -32,6 +32,24 @@ function pickRelevantCarrierProperties(c) { }; } +async function getLocalSIPDomain(logger, srf) { + const { lookupSystemInformation } = srf.locals.dbHelpers; + try { + const systemInfo = await lookupSystemInformation(); + if (systemInfo) { + logger.info(`lookup of sip domain from system_information: ${systemInfo.sip_domain_name}`); + srf.locals.localSIPDomain = systemInfo.sip_domain_name; + } + else { + logger.info('no system_information found, we will use the realm or public ip as the domain'); + return false; + } + } catch (err) { + logger.info({ err }, 'Error looking up system information'); + return false; + } +} + module.exports = async(logger, srf) => { if (initialized) return; initialized = true; @@ -42,6 +60,10 @@ module.exports = async(logger, srf) => { active: false }; + /* Set the Local SIP domain on srf.locals */ + await getLocalSIPDomain(logger, srf); // Initial Setup + setInterval(getLocalSIPDomain, 300000, logger, srf); //Refresh SIP Domain every 5 mins + /* sleep a random duration between 0 and MAX_INITIAL_DELAY seconds */ const ms = Math.floor(Math.random() * MAX_INITIAL_DELAY) * 1000; logger.info(`waiting ${ms}ms before attempting to claim regbot responsibility with token ${myToken}`); @@ -184,7 +206,7 @@ const updateCarrierRegbots = async(logger, srf) => { sip_realm: gw.carrier.register_sip_realm, from_user: gw.carrier.register_from_user, from_domain: gw.carrier.register_from_domain, - use_public_ip_in_contact: gw.carrier.register_public_ip_in_contact + use_public_ip_in_contact: gw.carrier.register_public_ip_in_contact, }); regbots.push(rb); // stagger the start of the regbots