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