mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2026-01-24 22:27:52 +00:00
feat: update voip carriers register status
This commit is contained in:
6
app.js
6
app.js
@@ -25,7 +25,8 @@ const {
|
||||
lookupSipGatewaysByCarrier,
|
||||
lookupAccountBySipRealm,
|
||||
lookupAccountCapacitiesBySid,
|
||||
addSbcAddress
|
||||
addSbcAddress,
|
||||
updateVoipCarriersRegisterStatus
|
||||
} = require('@jambonz/db-helpers')({
|
||||
host: process.env.JAMBONES_MYSQL_HOST,
|
||||
user: process.env.JAMBONES_MYSQL_USER,
|
||||
@@ -69,7 +70,8 @@ srf.locals = {
|
||||
lookupAllVoipCarriers,
|
||||
lookupSipGatewaysByCarrier,
|
||||
lookupAccountBySipRealm,
|
||||
lookupAccountCapacitiesBySid
|
||||
lookupAccountCapacitiesBySid,
|
||||
updateVoipCarriersRegisterStatus
|
||||
},
|
||||
realtimeDbHelpers: {
|
||||
addKey,
|
||||
|
||||
@@ -18,7 +18,7 @@ class Regbot {
|
||||
|
||||
['ipv4', 'port', 'username', 'password', 'sip_realm'].forEach((prop) => this[prop] = opts[prop]);
|
||||
|
||||
logger.debug({opts}, 'Regbot');
|
||||
logger.debug({ opts }, 'Regbot');
|
||||
this.username = opts.username;
|
||||
this.password = opts.password;
|
||||
this.sip_realm = opts.sip_realm || opts.ipv4;
|
||||
@@ -54,6 +54,7 @@ class Regbot {
|
||||
}
|
||||
|
||||
async register(srf) {
|
||||
const { updateVoipCarriersRegisterStatus } = srf.locals.dbHelpers;
|
||||
try {
|
||||
const contactAddress = this.use_public_ip_in_contact ?
|
||||
`${this.username}@${srf.locals.sbcPublicIpAddress}` : this.aor;
|
||||
@@ -90,18 +91,27 @@ class Regbot {
|
||||
debug(`setting timer for next register to ${expires} seconds`);
|
||||
this.timer = setTimeout(this.register.bind(this, srf), (expires - 5) * 1000);
|
||||
}
|
||||
updateVoipCarriersRegisterStatus({
|
||||
status: res.status === 200 ? 'ok' : 'fail',
|
||||
reason: `${res.status} ${res.reason}`
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.error({ err }, `Regbot Error registering to ${this.ipv4}:${this.port}`);
|
||||
this.timer = setTimeout(this.register.bind(this, srf), 60 * 1000);
|
||||
updateVoipCarriersRegisterStatus({
|
||||
status: 'fail',
|
||||
reason: err
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = async(logger, srf) => {
|
||||
module.exports = async (logger, srf) => {
|
||||
if (initialized) return;
|
||||
initialized = true;
|
||||
const {addKeyNx} = srf.locals.realtimeDbHelpers;
|
||||
const { addKeyNx } = srf.locals.realtimeDbHelpers;
|
||||
const myToken = short.generate();
|
||||
srf.locals.regbot = {
|
||||
myToken,
|
||||
@@ -130,18 +140,18 @@ module.exports = async(logger, srf) => {
|
||||
if (srf.locals.regbot.active) {
|
||||
updateCarrierRegbots(logger, srf)
|
||||
.catch((err) => {
|
||||
logger.error({err}, 'updateCarrierRegbots failure');
|
||||
logger.error({ err }, 'updateCarrierRegbots failure');
|
||||
});
|
||||
}
|
||||
|
||||
return srf.locals.regbot.active;
|
||||
};
|
||||
|
||||
const checkStatus = async(logger, srf) => {
|
||||
const {addKeyNx, addKey, retrieveKey} = srf.locals.realtimeDbHelpers;
|
||||
const {myToken, active} = srf.locals.regbot;
|
||||
const checkStatus = async (logger, srf) => {
|
||||
const { addKeyNx, addKey, retrieveKey } = srf.locals.realtimeDbHelpers;
|
||||
const { myToken, active } = srf.locals.regbot;
|
||||
|
||||
logger.info({active, myToken}, 'checking in on regbot status');
|
||||
logger.info({ active, myToken }, 'checking in on regbot status');
|
||||
try {
|
||||
const token = await retrieveKey(regbotKey);
|
||||
let grabForTheWheel = false;
|
||||
@@ -152,7 +162,7 @@ const checkStatus = async(logger, srf) => {
|
||||
addKey(regbotKey, myToken, REGBOT_STATUS_CHECK_INTERVAL + 10)
|
||||
.then(updateCarrierRegbots.bind(null, logger, srf))
|
||||
.catch((err) => {
|
||||
logger.error({err}, 'updateCarrierRegbots failure');
|
||||
logger.error({ err }, 'updateCarrierRegbots failure');
|
||||
});
|
||||
}
|
||||
else if (token && token !== myToken) {
|
||||
@@ -183,7 +193,7 @@ const checkStatus = async(logger, srf) => {
|
||||
logger.info(`successfully claimed regbot responsibility with token ${myToken}`);
|
||||
updateCarrierRegbots(logger, srf)
|
||||
.catch((err) => {
|
||||
logger.error({err}, 'updateCarrierRegbots failure');
|
||||
logger.error({ err }, 'updateCarrierRegbots failure');
|
||||
});
|
||||
}
|
||||
else {
|
||||
@@ -192,11 +202,11 @@ const checkStatus = async(logger, srf) => {
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({err}, 'checkStatus: ERROR');
|
||||
logger.error({ err }, 'checkStatus: ERROR');
|
||||
}
|
||||
};
|
||||
|
||||
const updateCarrierRegbots = async(logger, srf) => {
|
||||
const updateCarrierRegbots = async (logger, srf) => {
|
||||
// Check if We are
|
||||
const { lookupAllVoipCarriers, lookupSipGatewaysByCarrier } = srf.locals.dbHelpers;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user