mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2026-01-24 22:27:52 +00:00
Merge pull request #19 from jambonz/feat/carrier_register_status
feat: update voip carriers register status
This commit is contained in:
6
app.js
6
app.js
@@ -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,
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ 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.voip_carrier_sid = opts.voip_carrier_sid;
|
||||||
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;
|
||||||
@@ -44,6 +45,7 @@ class Regbot {
|
|||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
return {
|
return {
|
||||||
|
voip_carrier_sid: this.voip_carrier_sid,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
sip_realm: this.sip_realm,
|
sip_realm: this.sip_realm,
|
||||||
ipv4: this.ipv4,
|
ipv4: this.ipv4,
|
||||||
@@ -54,6 +56,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 +93,29 @@ 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(this.voip_carrier_sid, JSON.stringify({
|
||||||
|
status: res.status === 200 ? 'ok' : 'fail',
|
||||||
|
reason: `${res.status} ${res.reason}`,
|
||||||
|
cseq: req.get('Cseq'),
|
||||||
|
callId: req.get('Call-Id')
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
} 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(this.voip_carrier_sid, JSON.stringify({
|
||||||
|
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,7 +144,7 @@ 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');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,10 +152,10 @@ module.exports = async(logger, srf) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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 +166,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 +197,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,7 +206,7 @@ const checkStatus = async(logger, srf) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error({err}, 'checkStatus: ERROR');
|
logger.error({ err }, 'checkStatus: ERROR');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -238,6 +252,7 @@ const updateCarrierRegbots = async(logger, srf) => {
|
|||||||
// start new regbots
|
// start new regbots
|
||||||
for (const gw of gateways) {
|
for (const gw of gateways) {
|
||||||
const rb = new Regbot(logger, {
|
const rb = new Regbot(logger, {
|
||||||
|
voip_carrier_sid: gw.carrier.voip_carrier_sid,
|
||||||
ipv4: gw.ipv4,
|
ipv4: gw.ipv4,
|
||||||
port: gw.port,
|
port: gw.port,
|
||||||
username: gw.carrier.register_username,
|
username: gw.carrier.register_username,
|
||||||
|
|||||||
18
package-lock.json
generated
18
package-lock.json
generated
@@ -9,7 +9,7 @@
|
|||||||
"version": "0.8.1",
|
"version": "0.8.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jambonz/db-helpers": "^0.7.3",
|
"@jambonz/db-helpers": "^0.7.5",
|
||||||
"@jambonz/http-authenticator": "^0.2.2",
|
"@jambonz/http-authenticator": "^0.2.2",
|
||||||
"@jambonz/mw-registrar": "^0.2.3",
|
"@jambonz/mw-registrar": "^0.2.3",
|
||||||
"@jambonz/realtimedb-helpers": "^0.4.34",
|
"@jambonz/realtimedb-helpers": "^0.4.34",
|
||||||
@@ -711,12 +711,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@jambonz/db-helpers": {
|
"node_modules/@jambonz/db-helpers": {
|
||||||
"version": "0.7.3",
|
"version": "0.7.5",
|
||||||
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.7.3.tgz",
|
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.7.5.tgz",
|
||||||
"integrity": "sha512-FXQPnl5AGV3P6RhvdSKfjIOdEXShYe7aKJayJjaPARPTCTGqLTDhV2tfsWirCrpQhn+vanJHAiOyzOTmbDIfzA==",
|
"integrity": "sha512-Zqq4LSsW76VxQiqXMGeVCkSqXCNwUyLN86PD0ejWQO5x+pA2GaBhLX+SHTdCiHhQ3t29SCyOoSi5MJ96LZwI4w==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cidr-matcher": "^2.1.1",
|
"cidr-matcher": "^2.1.1",
|
||||||
"debug": "^4.3.3",
|
"debug": "^4.3.4",
|
||||||
"mysql2": "^2.3.3",
|
"mysql2": "^2.3.3",
|
||||||
"node-object-hash": "^2.3.10",
|
"node-object-hash": "^2.3.10",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2"
|
||||||
@@ -5913,12 +5913,12 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@jambonz/db-helpers": {
|
"@jambonz/db-helpers": {
|
||||||
"version": "0.7.3",
|
"version": "0.7.5",
|
||||||
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.7.3.tgz",
|
"resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.7.5.tgz",
|
||||||
"integrity": "sha512-FXQPnl5AGV3P6RhvdSKfjIOdEXShYe7aKJayJjaPARPTCTGqLTDhV2tfsWirCrpQhn+vanJHAiOyzOTmbDIfzA==",
|
"integrity": "sha512-Zqq4LSsW76VxQiqXMGeVCkSqXCNwUyLN86PD0ejWQO5x+pA2GaBhLX+SHTdCiHhQ3t29SCyOoSi5MJ96LZwI4w==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"cidr-matcher": "^2.1.1",
|
"cidr-matcher": "^2.1.1",
|
||||||
"debug": "^4.3.3",
|
"debug": "^4.3.4",
|
||||||
"mysql2": "^2.3.3",
|
"mysql2": "^2.3.3",
|
||||||
"node-object-hash": "^2.3.10",
|
"node-object-hash": "^2.3.10",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2"
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/xquanluu/sbc-outbound-handler#readme",
|
"homepage": "https://github.com/xquanluu/sbc-outbound-handler#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@jambonz/db-helpers": "^0.7.3",
|
"@jambonz/db-helpers": "^0.7.5",
|
||||||
"@jambonz/http-authenticator": "^0.2.2",
|
"@jambonz/http-authenticator": "^0.2.2",
|
||||||
"@jambonz/mw-registrar": "^0.2.3",
|
"@jambonz/mw-registrar": "^0.2.3",
|
||||||
"@jambonz/realtimedb-helpers": "^0.4.34",
|
"@jambonz/realtimedb-helpers": "^0.4.34",
|
||||||
|
|||||||
Reference in New Issue
Block a user