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
+20 -10
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}`);
}
}
+3 -3
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