mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2025-12-19 04:27:46 +00:00
Write options ping failure alert once (#88)
* updated realtimedb-helper to 0.8.13 * Do not write an alert for every failure if OPTIONS ping failure alrert is already written https://github.com/jambonz/sbc-sip-sidecar/issues/87
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const { addSipGatewayToBlacklist } = require('./utils');
|
||||
const { addSipGatewayToBlacklist, removeSipGatewayFromBlacklist, isSipGatewayBlacklisted } = require('./utils');
|
||||
|
||||
const send_options_gateways = [];
|
||||
const send_options_bots = [];
|
||||
@@ -12,6 +12,7 @@ class OptionsBot {
|
||||
this.port = gateway.port;
|
||||
this.protocol = gateway.protocol;
|
||||
this.expiry = (process.env.SEND_OPTIONS_PING_INTERVAL || 60);
|
||||
this.blacklist_expiry = (process.env.OPTIONS_PING_TTL || 300);
|
||||
|
||||
const useSipsScheme = gateway.protocol.includes('tls') && gateway.use_sips_scheme;
|
||||
const isIPv4 = /[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/.test(gateway.ipv4);
|
||||
@@ -34,7 +35,11 @@ class OptionsBot {
|
||||
req.on('response', async(res) => {
|
||||
if (res.status !== 200) {
|
||||
this.logger.info(`Received Options response ${res.status} for ${this.uri}`);
|
||||
await addSipGatewayToBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid, this.expiry);
|
||||
// Check if the SIP gateway is not already blacklisted
|
||||
if (!await isSipGatewayBlacklisted(realtimeDbHelpers.client, logger, this.sip_gateway_sid)) {
|
||||
// Add the gateway to the blacklist since it failed the OPTIONS ping
|
||||
await addSipGatewayToBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid,
|
||||
this.blacklist_expiry);
|
||||
const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
|
||||
if (carrier) {
|
||||
writeAlerts({
|
||||
@@ -45,10 +50,17 @@ class OptionsBot {
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If the gateway is blacklisted, remove it from the blacklist as we have successfully pinged it
|
||||
if (await isSipGatewayBlacklisted(realtimeDbHelpers.client, logger, this.sip_gateway_sid)) {
|
||||
await removeSipGatewayFromBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.error({ err }, `Error Options ping to ${this.uri}`);
|
||||
await addSipGatewayToBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid, this.expiry);
|
||||
if (!await isSipGatewayBlacklisted(realtimeDbHelpers.client, logger, this.sip_gateway_sid)) {
|
||||
await addSipGatewayToBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid, this.blacklist_expiry);
|
||||
const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
|
||||
if (carrier) {
|
||||
writeAlerts({
|
||||
@@ -61,6 +73,7 @@ class OptionsBot {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = async(logger, srf) => {
|
||||
const updateSipGatewayOptsBot = async(logger, srf) => {
|
||||
|
||||
22
lib/utils.js
22
lib/utils.js
@@ -20,13 +20,31 @@ function makeBlacklistGatewayKey(key) {
|
||||
|
||||
async function addSipGatewayToBlacklist(client, logger, sip_gateway_sid, expired) {
|
||||
try {
|
||||
await client.setex(makeBlacklistGatewayKey(sip_gateway_sid), expired, '');
|
||||
await client.setex(makeBlacklistGatewayKey(sip_gateway_sid), expired, '1');
|
||||
logger.info(`addSipGatewayToBlacklist: added ${sip_gateway_sid} to blacklist`);
|
||||
} catch (err) {
|
||||
logger.error({err}, `addSipGatewayToBlacklist: Error add ${sip_gateway_sid} to blacklist`);
|
||||
}
|
||||
}
|
||||
|
||||
async function removeSipGatewayFromBlacklist(client, logger, sip_gateway_sid) {
|
||||
try {
|
||||
await client.del(makeBlacklistGatewayKey(sip_gateway_sid));
|
||||
logger.info(`removeSipGatewayFromBlacklist: removed ${sip_gateway_sid} from blacklist`);
|
||||
} catch (err) {
|
||||
logger.error({err}, `removeSipGatewayFromBlacklist: Error removing ${sip_gateway_sid} from blacklist`);
|
||||
}
|
||||
}
|
||||
async function isSipGatewayBlacklisted(client, logger, sip_gateway_sid) {
|
||||
try {
|
||||
const exists = await client.get(makeBlacklistGatewayKey(sip_gateway_sid));
|
||||
return exists === '1';
|
||||
} catch (err) {
|
||||
logger.error({err}, `isSipGatewayBlacklisted: Error checking if ${sip_gateway_sid} is blacklisted`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Regex pattern to match valid IPv4 addresses (0.0.0.0 to 255.255.255.255) */
|
||||
const ipv4Pattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||
|
||||
@@ -58,6 +76,8 @@ module.exports = {
|
||||
isUacBehindNat,
|
||||
getSipProtocol,
|
||||
addSipGatewayToBlacklist,
|
||||
removeSipGatewayFromBlacklist,
|
||||
isSipGatewayBlacklisted,
|
||||
NAT_EXPIRES: 30,
|
||||
isValidIPv4,
|
||||
isValidDomainOrIP,
|
||||
|
||||
Reference in New Issue
Block a user