mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2026-10-04 02:04:20 +00:00
send options ping (#48)
* send options ping * wip * wip * wip * wip * update blacklist sip gateway to redis * wip
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
const { addSipGatewayToBlacklist } = require('./utils');
|
||||
|
||||
const send_options_gateways = [];
|
||||
const send_options_bots = [];
|
||||
|
||||
class OptionsBot {
|
||||
constructor(logger, gateway) {
|
||||
this.logger = logger;
|
||||
this.sip_gateway_sid = gateway.sip_gateway_sid;
|
||||
this.voip_carrier_sid = gateway.voip_carrier_sid;
|
||||
this.ipv4 = gateway.ipv4;
|
||||
this.port = gateway.port;
|
||||
this.protocol = gateway.protocol;
|
||||
this.expiry = (process.env.SEND_OPTIONS_PING_INTERVAL || 60);
|
||||
|
||||
const isIPv4 = /[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/.test(gateway.ipv4);
|
||||
const transport = gateway.protocol.includes('/') ? gateway.protocol.substring(0, gateway.protocol.indexOf('/')) :
|
||||
gateway.protocol;
|
||||
this.proxy = `sip:${this.ipv4}${isIPv4 ? `:${this.port}` : ''};transport=${transport}`;
|
||||
this.uri = `sip${gateway.protocol.includes('tls') ?
|
||||
's' : ''}:${gateway.ipv4}${gateway.port && !gateway.protocol.includes('tls') ? `:${gateway.port}` : ''}`;
|
||||
}
|
||||
|
||||
async options(srf) {
|
||||
const { lookupCarrierBySid } = srf.locals.dbHelpers;
|
||||
const { writeAlerts, logger, realtimeDbHelpers } = srf.locals;
|
||||
try {
|
||||
const req = await srf.request({
|
||||
uri: this.uri,
|
||||
method: 'OPTIONS',
|
||||
proxy: this.proxy
|
||||
});
|
||||
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);
|
||||
const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
|
||||
if (carrier) {
|
||||
writeAlerts({
|
||||
account_sid: carrier.account_sid,
|
||||
service_provider_sid: carrier.service_provider_sid,
|
||||
message: `Options ping ${this.ipv4}${this.port ? `:${this.port}` : ''};transport=${this.protocol}
|
||||
unsuccessfully, received: ${res.status}`
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.error({ err }, `Error Options ping to ${this.uri}`);
|
||||
await addSipGatewayToBlacklist(realtimeDbHelpers.client, logger, this.sip_gateway_sid, this.expiry);
|
||||
const carrier = await lookupCarrierBySid(this.voip_carrier_sid);
|
||||
if (carrier) {
|
||||
writeAlerts({
|
||||
account_sid: carrier.account_sid,
|
||||
service_provider_sid: carrier.service_provider_sid,
|
||||
message: `Options ping ${this.ipv4}${this.port ? `:${this.port}` : ''};transport=${this.protocol}
|
||||
unsuccessfully, error: ${err}`
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = async(logger, srf) => {
|
||||
const updateSipGatewayOptsBot = async(logger, srf) => {
|
||||
try {
|
||||
|
||||
const { lookupSipGatewaysByFilters } = srf.locals.dbHelpers;
|
||||
const gws = await lookupSipGatewaysByFilters({send_options_ping: true, outbound: true, is_active: true});
|
||||
|
||||
logger.info(`updateSipGatewayOptsBot: start sending Options to ${gws.length} destinations`);
|
||||
send_options_gateways.length = 0;
|
||||
send_options_gateways.push(...gws);
|
||||
for (const g of send_options_gateways) {
|
||||
const optsBot = new OptionsBot(logger, g);
|
||||
send_options_bots.push(optsBot);
|
||||
optsBot.options(srf);
|
||||
}
|
||||
logger.debug(`updateSipGatewayOptsBot: we have started ${send_options_bots.length} optionsBots`);
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'updateSipGatewayOptsBot Error');
|
||||
}
|
||||
};
|
||||
|
||||
setInterval(updateSipGatewayOptsBot.bind(null, logger, srf), (process.env.SEND_OPTIONS_PING_INTERVAL || 60) * 1000);
|
||||
};
|
||||
@@ -19,6 +19,7 @@ const regbots = [];
|
||||
const carriers = [];
|
||||
const gateways = [];
|
||||
|
||||
|
||||
function pickRelevantCarrierProperties(c) {
|
||||
return {
|
||||
voip_carrier_sid: c.voip_carrier_sid,
|
||||
|
||||
@@ -14,8 +14,22 @@ function getSipProtocol(req) {
|
||||
if (req.getParsedHeader('Via')[0].protocol.toLowerCase().startsWith('udp')) return 'udp';
|
||||
}
|
||||
|
||||
function makeBlacklistGatewayKey(key) {
|
||||
return `blacklist-sip-gateway:${key}`;
|
||||
}
|
||||
|
||||
async function addSipGatewayToBlacklist(client, logger, sip_gateway_sid, expired) {
|
||||
try {
|
||||
await client.setex(makeBlacklistGatewayKey(sip_gateway_sid), expired, '');
|
||||
logger.info(`addSipGatewayToBlacklist: added ${sip_gateway_sid} to blacklist`);
|
||||
} catch (err) {
|
||||
logger.error({err}, `addSipGatewayToBlacklist: Error add ${sip_gateway_sid} to blacklist`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isUacBehindNat,
|
||||
getSipProtocol,
|
||||
addSipGatewayToBlacklist,
|
||||
NAT_EXPIRES: 30
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user