remove empheral gateways if regbot is stopped (#115)

* remove empheral gateways if regbot is stopped

* wip

* wip

* update realtime db version
This commit is contained in:
Hoan Luu Huu
2025-10-28 19:02:43 +07:00
committed by GitHub
parent e94ae431d8
commit 658cb6ae7d
5 changed files with 31 additions and 18 deletions

2
app.js
View File

@@ -87,6 +87,7 @@ const {
isMemberOfSet,
retrieveSet,
createEphemeralGateway,
deleteEphemeralGateway
} = require('@jambonz/realtimedb-helpers')({}, logger);
const interval = SBC_PUBLIC_ADDRESS_KEEP_ALIVE_IN_MILISECOND || 900000; // Default 15 minutes
@@ -119,6 +120,7 @@ srf.locals = {
retrieveKey,
retrieveSet,
createEphemeralGateway,
deleteEphemeralGateway
},
writeAlerts,
AlertType

View File

@@ -50,9 +50,19 @@ class Regbot {
this.register(srf);
}
stop() {
this.logger.info(`stopping regbot ${this.fromUser}@${this.sip_realm}`);
stop(srf) {
const { deleteEphemeralGateway } = srf.locals.realtimeDbHelpers;
clearTimeout(this.timer);
this.timer = null;
// remove any ephemeral gateways created for this regbot
if (this.addresses && this.addresses.length) {
this.addresses.forEach((ip) => {
deleteEphemeralGateway(ip, this.voip_carrier_sid).catch((err) => {
this.logger.error({err, ip}, 'Error deleting ephemeral gateway on regbot stop');
});
});
}
}
toJSON() {
@@ -136,7 +146,7 @@ class Regbot {
if (REGISTER_RESPONSE_REMOVE.includes(res.status)) {
const { updateCarrierBySid, lookupCarrierBySid } = srf.locals.dbHelpers;
await updateCarrierBySid(this.voip_carrier_sid, {requires_register: false});
this.stop(); //Remove the retry timer
this.stop(srf); //Remove the retry timer
const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
if (carrier) {
// eslint-disable-next-line max-len
@@ -194,25 +204,25 @@ class Regbot {
// for reg trunks, create ephemeral set of IP addresses for inbound gateways
if (this.trunk_type === 'reg') {
const addresses = [];
this.addresses = [];
if (this.port) {
const addrs = await dnsResolverA(this.logger, this.sip_realm);
addresses.push(...addrs);
this.addresses.push(...addrs);
}
else {
const addrs = await dnsResolverSrv(this.logger, this.sip_realm, this.transport);
addresses.push(...addrs);
this.addresses.push(...addrs);
}
if (addresses.length) {
if (this.addresses.length) {
try {
await Promise.all(
addresses.map((ip) => createEphemeralGateway(ip, this.voip_carrier_sid, expires))
this.addresses.map((ip) => createEphemeralGateway(ip, this.voip_carrier_sid, expires))
);
} catch (err) {
this.logger.error({addresses, err}, 'Error creating hash for reg-gateway');
this.logger.error({addresses: this.addresses, err}, 'Error creating hash for reg-gateway');
}
this.logger.debug({addresses},
this.logger.debug({addresses: this.addresses},
`Created ephemeral gateways for registration trunk ${this.voip_carrier_sid}, ${this.sip_realm}`);
}
}

View File

@@ -154,12 +154,12 @@ const checkStatus = async(logger, srf) => {
}
else if (token && token !== myToken) {
logger.info('Someone else grabbed the role! I need to stand down');
regbots.forEach((rb) => rb.stop());
regbots.forEach((rb) => rb.stop(srf));
regbots.length = 0;
}
else {
grabForTheWheel = true;
regbots.forEach((rb) => rb.stop());
regbots.forEach((rb) => rb.stop(srf));
regbots.length = 0;
}
}
@@ -230,7 +230,7 @@ const updateCarrierRegbots = async(logger, srf) => {
Array.prototype.push.apply(gateways, gws);
// stop / kill existing regbots
regbots.forEach((rb) => rb.stop());
regbots.forEach((rb) => rb.stop(srf));
regbots.length = 0;
// start new regbots

9
package-lock.json generated
View File

@@ -13,7 +13,7 @@
"@jambonz/db-helpers": "^0.9.18",
"@jambonz/digest-utils": "^0.0.6",
"@jambonz/mw-registrar": "^0.2.7",
"@jambonz/realtimedb-helpers": "^0.8.18",
"@jambonz/realtimedb-helpers": "^0.8.19",
"@jambonz/stats-collector": "^0.1.10",
"@jambonz/time-series": "^0.2.8",
"debug": "^4.3.4",
@@ -594,9 +594,10 @@
}
},
"node_modules/@jambonz/realtimedb-helpers": {
"version": "0.8.18",
"resolved": "https://registry.npmjs.org/@jambonz/realtimedb-helpers/-/realtimedb-helpers-0.8.18.tgz",
"integrity": "sha512-PfQRsOy/uKSA0ymRAEmTQnLxc1BZVRQWc+5mClLX6oB6vgvM/6YGgiuv5GSu26OtuKMTtS+RAxlFeT05iH/2rg==",
"version": "0.8.19",
"resolved": "https://registry.npmjs.org/@jambonz/realtimedb-helpers/-/realtimedb-helpers-0.8.19.tgz",
"integrity": "sha512-7ROvFyePQHTCw0IZgpPqeCrofShegdqXTDnTclVxo3x0SodWPd0m9mPS8MvsgzRB6Zh2ege/C7o570X+iXPwJg==",
"license": "MIT",
"dependencies": {
"debug": "^4.3.4",
"ioredis": "^5.3.2"

View File

@@ -31,7 +31,7 @@
"@jambonz/db-helpers": "^0.9.18",
"@jambonz/digest-utils": "^0.0.6",
"@jambonz/mw-registrar": "^0.2.7",
"@jambonz/realtimedb-helpers": "^0.8.18",
"@jambonz/realtimedb-helpers": "^0.8.19",
"@jambonz/stats-collector": "^0.1.10",
"@jambonz/time-series": "^0.2.8",
"debug": "^4.3.4",