move lookupSystemInformation up to parent function and add a timer (#93)

* move lookupSystemInformation up to parent function and add a timer

* lint
This commit is contained in:
Sam Machin
2025-04-03 19:56:28 +01:00
committed by GitHub
parent a22ab38aab
commit 446bb06142
2 changed files with 26 additions and 18 deletions

View File

@@ -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}`);

View File

@@ -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