if invite is sent with a domain name in request uri then assume the intention is for it to go out that way

This commit is contained in:
Dave Horton
2023-11-13 13:01:21 -05:00
parent 5a6a9b34d3
commit a8ee1db8da

View File

@@ -7,6 +7,9 @@ const {SipError, stringifyUri, parseUri} = require('drachtio-srf');
const debug = require('debug')('jambonz:sbc-outbound'); const debug = require('debug')('jambonz:sbc-outbound');
const makeInviteInProgressKey = (callid) => `sbc-out-iip${callid}`; const makeInviteInProgressKey = (callid) => `sbc-out-iip${callid}`;
const isDotDecimal = (host) => /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.test(host);
/** /**
* this is to make sure the outgoing From has the number in the incoming From * this is to make sure the outgoing From has the number in the incoming From
* and not the incoming PAI * and not the incoming PAI
@@ -25,9 +28,11 @@ const createBLegFromHeader = (req, teams) => {
} }
return `sip:${user}@${host}`; return `sip:${user}@${host}`;
}; };
const createBLegToHeader = (req, teams) => { const createBLegToHeader = (req, teams, retainHostName) => {
const to = req.getParsedHeader('To'); const to = req.getParsedHeader('To');
const host = teams ? req.get('X-MS-Teams-Tenant-FQDN') : 'localhost'; let host = 'localhost';
if (teams) host = req.get('X-MS-Teams-Tenant-FQDN');
else if (retainHostName) host = to.host;
const uri = parseUri(to.uri); const uri = parseUri(to.uri);
if (uri && uri.user) return `sip:${uri.user}@${host}`; if (uri && uri.user) return `sip:${uri.user}@${host}`;
return `sip:anonymous@${host}`; return `sip:anonymous@${host}`;
@@ -185,11 +190,18 @@ class CallSession extends Emitter {
try { try {
// determine where to send the call // determine where to send the call
debug(`connecting call: ${JSON.stringify(this.req.locals)}`); debug(`connecting call: ${JSON.stringify(this.req.locals)}`);
/**
* if we receive an INVITE with a host part that is not a dot-decimal ipv4
* default to retaining that in the INVITE we send
* */
const parsedIncomingUri = parseUri(this.req.uri);
const retainHostName = !isDotDecimal(parsedIncomingUri.host);
let headers = { let headers = {
'From': createBLegFromHeader(this.req, teams), 'From': createBLegFromHeader(this.req, teams),
'Contact': createBLegFromHeader(this.req, teams), 'Contact': createBLegFromHeader(this.req, teams),
'To': createBLegToHeader(this.req, teams), 'To': createBLegToHeader(this.req, teams), retainHostName,
Allow: 'INVITE, ACK, OPTIONS, CANCEL, BYE, NOTIFY, UPDATE, PRACK', Allow: 'INVITE, ACK, OPTIONS, CANCEL, BYE, NOTIFY, UPDATE, PRACK, REFER, INFO',
'X-Account-Sid': this.account_sid 'X-Account-Sid': this.account_sid
}; };
@@ -262,21 +274,28 @@ class CallSession extends Emitter {
if (gws.length) { if (gws.length) {
uris = []; uris = [];
gws.forEach((o) => { gws.forEach((o) => {
let proxy;
const calledNumber = this.req.calledNumber.startsWith('+') ? const calledNumber = this.req.calledNumber.startsWith('+') ?
this.req.calledNumber.slice(1) : this.req.calledNumber.slice(1) :
this.req.calledNumber; this.req.calledNumber;
const prefix = vc.tech_prefix || ''; const prefix = vc.tech_prefix || '';
const protocol = o.protocol?.startsWith('tls') ? 'tls' : (o.protocol || 'udp'); const protocol = o.protocol?.startsWith('tls') ? 'tls' : (o.protocol || 'udp');
const hostport = !o.port || 5060 === o.port ? o.ipv4 : `${o.ipv4}:${o.port}`; const host = retainHostName ? parsedIncomingUri.host : o.ipv4;
const hostport = !o.port || 5060 === o.port ? host : `${host}:${o.port}`;
const prependPlus = vc.e164_leading_plus && !this.req.calledNumber.startsWith('0') ? '+' : ''; const prependPlus = vc.e164_leading_plus && !this.req.calledNumber.startsWith('0') ? '+' : '';
const transport = `transport=${protocol}`; const transport = `transport=${protocol}`;
const scheme = protocol === 'tls' ? 'sips' : 'sip'; const scheme = protocol === 'tls' ? 'sips' : 'sip';
const u = `${scheme}:${prefix}${prependPlus}${calledNumber}@${hostport};${transport}`; const u = `${scheme}:${prefix}${prependPlus}${calledNumber}@${hostport};${transport}`;
if (retainHostName) {
const hostport = !o.port || 5060 === o.port ? o.ipv4 : `${o.ipv4}:${o.port}`;
proxy = `${scheme}:${hostport};transport=${transport}`;
}
const obj = { const obj = {
name: vc.name, name: vc.name,
diversion: vc.diversion, diversion: vc.diversion,
hostport, hostport,
protocol: o.protocol protocol: o.protocol,
...(proxy && {proxy})
}; };
if (vc.register_username && vc.register_password) { if (vc.register_username && vc.register_password) {
obj.auth = { obj.auth = {
@@ -355,7 +374,6 @@ class CallSession extends Emitter {
// INVITE request line and To header should be the same. // INVITE request line and To header should be the same.
hdrs = {...hdrs, 'To': uri}; hdrs = {...hdrs, 'To': uri};
if (gw) { if (gw) {
this.logger.info({gw}, `sending INVITE to ${uri} via carrier ${gw.name}`);
if (gw.diversion) { if (gw.diversion) {
let div = gw.diversion; let div = gw.diversion;
if (div.startsWith('+')) { if (div.startsWith('+')) {
@@ -367,6 +385,8 @@ class CallSession extends Emitter {
'Diversion': div 'Diversion': div
}; };
} }
proxy = gw.proxy;
this.logger.info({gw}, `sending INVITE to ${uri}`);
} }
else this.logger.info(`sending INVITE to ${uri} via proxy ${proxy})`); else this.logger.info(`sending INVITE to ${uri} via proxy ${proxy})`);
try { try {