From a22ab38aabf6324f27972285356b5e916ec800fc Mon Sep 17 00:00:00 2001 From: Sam Machin Date: Wed, 2 Apr 2025 13:22:52 +0100 Subject: [PATCH] Disable Options and Registration on certain failures (#90) * disable options ping on defined errors * correct sid * disable reg on error * fix path * fixes from testing * fixes from testing * lint * update dbhelpers dep * remove whitespace in CONFIG strings * lint * this seems like a better way of convering and matching status codes --- app.js | 6 ++++-- lib/config.js | 6 +++++- lib/regbot.js | 22 ++++++++++++++++++++-- lib/sip-trunk-options-ping.js | 17 +++++++++++++++++ package-lock.json | 9 +++++---- package.json | 2 +- 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index ab79d06..19922c4 100644 --- a/app.js +++ b/app.js @@ -56,7 +56,8 @@ const { lookupSipGatewaysByFilters, updateSipGatewayBySid, lookupCarrierBySid, - lookupSystemInformation + lookupSystemInformation, + updateCarrierBySid, } = require('@jambonz/db-helpers')({ host: JAMBONES_MYSQL_HOST, user: JAMBONES_MYSQL_USER, @@ -104,7 +105,8 @@ srf.locals = { lookupSipGatewaysByFilters, updateSipGatewayBySid, lookupCarrierBySid, - lookupSystemInformation + lookupSystemInformation, + updateCarrierBySid }, realtimeDbHelpers: { client, diff --git a/lib/config.js b/lib/config.js index 5143777..f607d53 100644 --- a/lib/config.js +++ b/lib/config.js @@ -31,6 +31,8 @@ const JAMBONES_REGBOT_CONTACT_USE_IP = process.env.JAMBONES_REGBOT_CONTACT_USE_I const JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL = process.env.JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL; const JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL = process.env.JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL; const JAMBONES_REGBOT_INITIAL_DELAY_MS = process.env.JAMBONES_REGBOT_INITIAL_DELAY_MS || 250; +const OPTIONS_RESPONSE_REMOVE = process.env.OPTIONS_RESPONSE_REMOVE?.split(',').map(Number) || []; +const REGISTER_RESPONSE_REMOVE = process.env.REGISTER_RESPONSE_REMOVE?.split(',').map(Number) || []; module.exports = { JAMBONES_MYSQL_HOST, @@ -57,5 +59,7 @@ module.exports = { JAMBONES_REGBOT_CONTACT_USE_IP, JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL, JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL, - JAMBONES_REGBOT_INITIAL_DELAY_MS + JAMBONES_REGBOT_INITIAL_DELAY_MS, + OPTIONS_RESPONSE_REMOVE, + REGISTER_RESPONSE_REMOVE }; diff --git a/lib/regbot.js b/lib/regbot.js index 9bed1d2..8105419 100644 --- a/lib/regbot.js +++ b/lib/regbot.js @@ -1,7 +1,8 @@ const { JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL, JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL, - JAMBONES_REGBOT_CONTACT_USE_IP + JAMBONES_REGBOT_CONTACT_USE_IP, + REGISTER_RESPONSE_REMOVE } = require('./config'); const debug = require('debug')('jambonz:sbc-registrar'); const {isValidIPv4, isValidDomainOrIP} = require('./utils'); @@ -74,6 +75,8 @@ class Regbot { async register(srf) { const { updateVoipCarriersRegisterStatus } = srf.locals.dbHelpers; + const { writeAlerts } = srf.locals; + try { // transport const transport = (this.protocol.includes('/') ? this.protocol.substring(0, this.protocol.indexOf('/')) : @@ -120,11 +123,26 @@ class Regbot { password: this.password } }); - req.on('response', (res) => { + req.on('response', async(res) => { if (res.status !== 200) { this.status = 'fail'; this.logger.info(`${this.aor}: got ${res.status} registering to ${this.ipv4}:${this.port}`); this.timer = setTimeout(this.register.bind(this, srf), 30 * 1000); + if (REGISTER_RESPONSE_REMOVE.includes(res.status)) { + const { updateCarrierBySid, lookupCarrierBySid } = srf.locals.dbHelpers; + await updateCarrierBySid(this.voip_carrier_sid, {requires_register: false}); + this.stop(); //Remove the retry timer + const carrier = await lookupCarrierBySid(this.voip_carrier_sid); + if (carrier) { + // eslint-disable-next-line max-len + this.logger.info(`Disabling Outbound Registration for carrier ${carrier.name} (sid:${carrier.voip_carrier_sid})`); + writeAlerts({ + account_sid: carrier.account_sid, + service_provider_sid: carrier.service_provider_sid, + message: `Disabling Outbound Registration for carrier ${carrier.name} (sid:${carrier.voip_carrier_sid})` + }); + } + } } else { diff --git a/lib/sip-trunk-options-ping.js b/lib/sip-trunk-options-ping.js index e518b21..684829d 100644 --- a/lib/sip-trunk-options-ping.js +++ b/lib/sip-trunk-options-ping.js @@ -1,4 +1,5 @@ const { addSipGatewayToBlacklist, removeSipGatewayFromBlacklist, isSipGatewayBlacklisted } = require('./utils'); +const {OPTIONS_RESPONSE_REMOVE} = require('./config'); const send_options_gateways = []; const send_options_bots = []; @@ -50,6 +51,22 @@ class OptionsBot { }); } } + if (OPTIONS_RESPONSE_REMOVE.includes(res.status)) { + const { updateSipGatewayBySid } = srf.locals.dbHelpers; + await updateSipGatewayBySid(this.sip_gateway_sid, {send_options_ping: false}); + const carrier = await lookupCarrierBySid(this.voip_carrier_sid); + if (carrier) { + // eslint-disable-next-line max-len + this.logger.info(`Disabling Options ping for ${this.ipv4} in carrier ${carrier.name} (sid:${carrier.voip_carrier_sid})`); + writeAlerts({ + account_sid: carrier.account_sid, + service_provider_sid: carrier.service_provider_sid, + // eslint-disable-next-line max-len + message: `Disabling Options ping for ${this.ipv4} in carrier ${carrier.name} (sid:${carrier.voip_carrier_sid})` + }); + } + + } } 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)) { diff --git a/package-lock.json b/package-lock.json index b2a92cd..49ccbbf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.9.3", "license": "MIT", "dependencies": { - "@jambonz/db-helpers": "^0.9.4", + "@jambonz/db-helpers": "^0.9.10", "@jambonz/digest-utils": "^0.0.5", "@jambonz/mw-registrar": "^0.2.7", "@jambonz/realtimedb-helpers": "^0.8.13", @@ -499,9 +499,10 @@ } }, "node_modules/@jambonz/db-helpers": { - "version": "0.9.7", - "resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.9.7.tgz", - "integrity": "sha512-5qN/CJZJXpbMkMn+8gFn8PpQ0ZImZxp1EjKyxLUlmMn+xgjeNb29c3pjeVt/6EQnBB65jAax6TNsVzVIfpvE2w==", + "version": "0.9.10", + "resolved": "https://registry.npmjs.org/@jambonz/db-helpers/-/db-helpers-0.9.10.tgz", + "integrity": "sha512-yV2pQal/unHmsj+vFgzQXcrKZ/CrtNDhMzL7+Oj5Y+/KJ2gKQR2QtanC5RkUNyCThYhqN18UdJu0PR+J1BL2dg==", + "license": "MIT", "dependencies": { "cidr-matcher": "^2.1.1", "debug": "^4.3.4", diff --git a/package.json b/package.json index e65eb5b..12c5c0e 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "homepage": "https://github.com/jambonz/sbc-sip-sidecar#readme", "dependencies": { - "@jambonz/db-helpers": "^0.9.4", + "@jambonz/db-helpers": "^0.9.10", "@jambonz/mw-registrar": "^0.2.7", "@jambonz/realtimedb-helpers": "^0.8.13", "@jambonz/stats-collector": "^0.1.10",