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

View File

@@ -50,9 +50,19 @@ class Regbot {
this.register(srf); this.register(srf);
} }
stop() { stop(srf) {
this.logger.info(`stopping regbot ${this.fromUser}@${this.sip_realm}`); const { deleteEphemeralGateway } = srf.locals.realtimeDbHelpers;
clearTimeout(this.timer); 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() { toJSON() {
@@ -136,7 +146,7 @@ class Regbot {
if (REGISTER_RESPONSE_REMOVE.includes(res.status)) { if (REGISTER_RESPONSE_REMOVE.includes(res.status)) {
const { updateCarrierBySid, lookupCarrierBySid } = srf.locals.dbHelpers; const { updateCarrierBySid, lookupCarrierBySid } = srf.locals.dbHelpers;
await updateCarrierBySid(this.voip_carrier_sid, {requires_register: false}); 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); const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
if (carrier) { if (carrier) {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
@@ -194,25 +204,25 @@ class Regbot {
// for reg trunks, create ephemeral set of IP addresses for inbound gateways // for reg trunks, create ephemeral set of IP addresses for inbound gateways
if (this.trunk_type === 'reg') { if (this.trunk_type === 'reg') {
const addresses = []; this.addresses = [];
if (this.port) { if (this.port) {
const addrs = await dnsResolverA(this.logger, this.sip_realm); const addrs = await dnsResolverA(this.logger, this.sip_realm);
addresses.push(...addrs); this.addresses.push(...addrs);
} }
else { else {
const addrs = await dnsResolverSrv(this.logger, this.sip_realm, this.transport); 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 { try {
await Promise.all( 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) { } 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}`); `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) { else if (token && token !== myToken) {
logger.info('Someone else grabbed the role! I need to stand down'); 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; regbots.length = 0;
} }
else { else {
grabForTheWheel = true; grabForTheWheel = true;
regbots.forEach((rb) => rb.stop()); regbots.forEach((rb) => rb.stop(srf));
regbots.length = 0; regbots.length = 0;
} }
} }
@@ -230,7 +230,7 @@ const updateCarrierRegbots = async(logger, srf) => {
Array.prototype.push.apply(gateways, gws); Array.prototype.push.apply(gateways, gws);
// stop / kill existing regbots // stop / kill existing regbots
regbots.forEach((rb) => rb.stop()); regbots.forEach((rb) => rb.stop(srf));
regbots.length = 0; regbots.length = 0;
// start new regbots // start new regbots

9
package-lock.json generated
View File

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

View File

@@ -31,7 +31,7 @@
"@jambonz/db-helpers": "^0.9.18", "@jambonz/db-helpers": "^0.9.18",
"@jambonz/digest-utils": "^0.0.6", "@jambonz/digest-utils": "^0.0.6",
"@jambonz/mw-registrar": "^0.2.7", "@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/stats-collector": "^0.1.10",
"@jambonz/time-series": "^0.2.8", "@jambonz/time-series": "^0.2.8",
"debug": "^4.3.4", "debug": "^4.3.4",