From 5b88065925da5d316951556d0348776c4b2ba120 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 30 Nov 2022 12:55:15 -0500 Subject: [PATCH] bugfix: allow users to fully specify a Refer-To uri without us overiding it --- lib/call-session.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/call-session.js b/lib/call-session.js index 6df1ec2..5655712 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -774,30 +774,38 @@ Duration=${payload.duration} ` const referredBy = req.getParsedHeader('Referred-By'); if (!referredBy) return res.send(400); const u = parseUri(referredBy.uri); - const isDotDecimal = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.test(uri.host); - let selectedGateway = false; - let e164 = false; - if (gateway && isDotDecimal) { - /* host of Refer-to to an outbound gateway */ - const gw = await this.srf.locals.getOutboundGatewayForRefer(gateway.voip_carrier_sid); - if (gw) { - selectedGateway = true; - e164 = gw.e164_leading_plus; - uri.host = gw.ipv4; - uri.port = gw.port; + const leaveReferToAlone = req.has('X-Refer-To-Leave-Untouched'); + if (leaveReferToAlone) { + this.logger.debug(`passing Refer-To header through untouched: ${referTo}`); + } + else { + const isDotDecimal = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.test(uri.host); + let selectedGateway = false; + let e164 = false; + if (gateway && isDotDecimal) { + /* host of Refer-to to an outbound gateway */ + const gw = await this.srf.locals.getOutboundGatewayForRefer(gateway.voip_carrier_sid); + if (gw) { + selectedGateway = true; + e164 = gw.e164_leading_plus; + uri.host = gw.ipv4; + uri.port = gw.port; + } + } + if (!selectedGateway && isDotDecimal) { + uri.host = this.req.source_address; + uri.port = this.req.source_port; + } + if (e164 && !uri.user.startsWith('+')) { + uri.user = `+${uri.user}`; } - } - if (!selectedGateway && isDotDecimal) { - uri.host = this.req.source_address; - uri.port = this.req.source_port; - } - if (e164 && !uri.user.startsWith('+')) { - uri.user = `+${uri.user}`; } // eslint-disable-next-line no-unused-vars const {via, from, to, 'call-id':callid, cseq, 'max-forwards':maxforwards, // eslint-disable-next-line no-unused-vars 'content-length':contentlength, 'refer-to':_referto, 'referred-by':_referredby, + // eslint-disable-next-line no-unused-vars + 'x-refer-to-leave-untouched': _leave, ...customHeaders } = req.headers;