This commit is contained in:
Dave Horton
2023-06-12 13:24:20 -04:00
parent 0ff8d8b7d0
commit eaa6fab994

View File

@@ -12,13 +12,26 @@ const regbots = [];
const carriers = [];
const gateways = [];
function pickRelevantCarrierProperties(c) {
return {
voip_carrier_sid: c.voip_carrier_sid,
requires_register: c.requires_register,
is_active: c.is_active,
register_username: c.register_username,
register_password: c.register_password,
register_sip_realm: c.register_sip_realm,
register_from_user: c.register_from_user,
register_from_domain: c.register_from_domain,
register_public_ip_in_contact: c.register_public_ip_in_contact
};
}
class Regbot {
constructor(logger, opts) {
this.logger = logger;
['ipv4', 'port', 'username', 'password', 'sip_realm'].forEach((prop) => this[prop] = opts[prop]);
logger.debug({ opts }, 'Regbot');
this.voip_carrier_sid = opts.voip_carrier_sid;
this.username = opts.username;
this.password = opts.password;
@@ -35,11 +48,13 @@ class Regbot {
}
start(srf) {
this.logger.info(`starting regbot ${this.username}@${this.sip_realm}`);
assert(!this.timer);
this.register(srf);
}
stop() {
this.logger.info(`stopping regbot ${this.username}@${this.sip_realm}`);
clearTimeout(this.timer);
}
@@ -60,6 +75,7 @@ class Regbot {
try {
const contactAddress = this.use_public_ip_in_contact ?
`${this.username}@${srf.locals.sbcPublicIpAddress}` : this.aor;
this.logger.debug(`sending REGISTER for ${this.aor}`);
const req = await srf.request(`sip:${this.aor}`, {
method: 'REGISTER',
proxy: `sip:${this.ipv4}:${this.port}`,
@@ -76,7 +92,7 @@ class Regbot {
req.on('response', (res) => {
if (res.status !== 200) {
this.status = 'fail';
this.logger.info(`Regbot: got ${res.status} registering to ${this.sip_realm} at ${this.ipv4}:${this.port}`);
this.logger.info(`${this.aor}: got ${res.status} registering to ${this.ipv4}:${this.port}`);
this.timer = setTimeout(this.register.bind(this, srf), 30 * 1000);
}
else {
@@ -101,7 +117,7 @@ class Regbot {
}));
});
} catch (err) {
this.logger.error({ err }, `Regbot Error registering to ${this.ipv4}:${this.port}`);
this.logger.error({ err }, `${this.aor}: Error registering to ${this.ipv4}:${this.port}`);
this.timer = setTimeout(this.register.bind(this, srf), 60 * 1000);
updateVoipCarriersRegisterStatus(this.voip_carrier_sid, JSON.stringify({
status: 'fail',
@@ -219,17 +235,18 @@ const updateCarrierRegbots = async(logger, srf) => {
let hasChanged = false;
const gws = [];
const cs = (await lookupAllVoipCarriers())
.filter((c) => c.requires_register);
.filter((c) => c.requires_register && c.is_active)
.map((c) => pickRelevantCarrierProperties(c));
if (JSON.stringify(cs) !== JSON.stringify(carriers)) hasChanged = true;
for (const c of cs) {
try {
const arr = (await lookupSipGatewaysByCarrier(c.voip_carrier_sid))
.filter((gw) => gw.outbound && gw.is_active)
.map((gw) => {
gw.carrier = c;
gw.carrier = pickRelevantCarrierProperties(c);
return gw;
});
Array.prototype.push.apply(gws, arr);
gws.push(...arr);
} catch (err) {
logger.error({ err }, 'updateCarrierRegbots Error retrieving gateways');
}
@@ -264,9 +281,8 @@ const updateCarrierRegbots = async(logger, srf) => {
});
regbots.push(rb);
rb.start(srf);
logger.info({ regbot: rb.toJSON() }, 'Starting regbot');
}
debug(`updateCarrierRegbots: we have ${regbots.length} regbots`);
logger.debug(`updateCarrierRegbots: we have started ${regbots.length} regbots`);
}
} catch (err) {
logger.error({ err }, 'updateCarrierRegbots Error');