From 32837cf20728044f9c73c786e166e36210d4149a Mon Sep 17 00:00:00 2001 From: Pedro Sanders Date: Fri, 12 Mar 2021 09:28:45 -0500 Subject: [PATCH] Improved support for rfc5658 --- mod/core/processor/request_handler.js | 37 ++++++++++++++++------ mod/core/processor/request_utils.js | 44 ++++++++++++++++----------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/mod/core/processor/request_handler.js b/mod/core/processor/request_handler.js index e815334b3..2585c6982 100644 --- a/mod/core/processor/request_handler.js +++ b/mod/core/processor/request_handler.js @@ -105,22 +105,41 @@ class RequestHandler { async processRoute (transaction, request, route, routeInfo) { try { - // If the request autof dialog we wont have a route. Therefore, we get - // the transport from the request URI. - const transport = route + const lpTransport = request + .getHeader(ViaHeader.NAME) + .getTransport() + .toLowerCase() + const targetTransport = route ? route.transport - : request.getRequestURI().getParameter('transport') - const lp = this.sipProvider.getListeningPoint(transport) + : request + .getRequestURI() + .getParameter('transport') + .toLowerCase() + + const lp = this.sipProvider.getListeningPoint(lpTransport) const localAddr = { host: lp.getIPAddress().toString(), - port: lp.getPort() + port: lp.getPort(), + transport: lpTransport } - const advertisedAddr = getAdvertisedAddr(request, route, localAddr) + const advertisedAddr = getAdvertisedAddr( + request, + route, + localAddr, + targetTransport + ) + + LOG.debug( + `core.processor.RequestHandler.processRoute [targetTransport = ${targetTransport}]` + ) + LOG.debug( + `core.processor.RequestHandler.processRoute [lpTransport = ${lpTransport}]` + ) let requestOut = configureMaxForwards(request) requestOut = configureProxyAuthorization(requestOut) requestOut = configureRoute(requestOut, localAddr) - requestOut = configureVia(requestOut, advertisedAddr, transport) + requestOut = configureVia(requestOut, advertisedAddr, targetTransport) //requestOut = configureContact(requestOut) if (!isInDialog(request)) { @@ -128,7 +147,7 @@ class RequestHandler { requestOut = configurePrivacy(requestOut, routeInfo) requestOut = configureIdentity(requestOut, route) requestOut = configureXHeaders(requestOut, route) - requestOut = configureRecordRoute(requestOut, advertisedAddr, localAddr) + requestOut = configureRecordRoute(requestOut, localAddr, advertisedAddr) } if (routeInfo.getRoutingType() === RoutingType.DOMAIN_EGRESS_ROUTING) { diff --git a/mod/core/processor/request_utils.js b/mod/core/processor/request_utils.js index 08d455888..d57166328 100644 --- a/mod/core/processor/request_utils.js +++ b/mod/core/processor/request_utils.js @@ -34,7 +34,7 @@ const ownedAddresss = localAddr => } ] : [localAddr] -const getAdvertisedAddr = (request, route, localAddr) => { +const getAdvertisedAddr = (request, route, localAddr, targetTransport) => { // After the initial invite the route object will be null // and we need to the the target address from the request uri. // If the routing is type IDR the initial request uri will be a local @@ -44,9 +44,13 @@ const getAdvertisedAddr = (request, route, localAddr) => { ? LocatorUtils.aorAsObj(route.contactURI).getHost() : request.getRequestURI().getHost() const externAddr = config.spec.externAddr - return config.spec.externAddr && needsExternAddress(route, targetAddr) - ? { host: addrHost(externAddr), port: addrPort(externAddr, localAddr) } - : localAddr + return externAddr && needsExternAddress(route, targetAddr) + ? { + host: addrHost(externAddr), + port: addrPort(externAddr, localAddr), + transport: targetTransport + } + : { host: localAddr.host, port: localAddr.port, transport: targetTransport } } const getToUser = request => { const toHeader = request.getHeader(ToHeader.NAME) @@ -128,27 +132,31 @@ const configureVia = (request, advertisedAddr, transport) => { requestOut.addFirst(viaHeader) return requestOut } -const configureRecordRoute = (request, advertisedAddr, localAddr) => { + +// rfc5658 +const configureRecordRoute = (request, localAddr, advertisedAddr) => { const requestOut = request.clone() - if (config.spec.recordRoute) { + const viaHeader = request.getHeaders(ViaHeader.NAME).next() + const transport = viaHeader.getTransport().toLowerCase() + + if (config.spec.recordRoute || transport === 'ws' || transport === 'wss') { + // First we need the input interface from the top ViaHeader const p1 = addressFactory.createSipURI(null, localAddr.host) - p1.setLrParam() p1.setPort(localAddr.port) + p1.setTransportParam(localAddr.transport) + p1.setLrParam() const pa1 = addressFactory.createAddress(p1) const rr1 = headerFactory.createRecordRouteHeader(pa1) requestOut.addHeader(rr1) - if (config.spec.externAddr && isPublicAddress(advertisedAddr.host)) { - const p2 = addressFactory.createSipURI( - null, - addrHost(config.spec.externAddr) - ) - p2.setLrParam() - p2.setPort(addrPort(config.spec.externAddr, localAddr)) - const pa2 = addressFactory.createAddress(p2) - const rr2 = headerFactory.createRecordRouteHeader(pa2) - requestOut.addFirst(rr2) - } + // Then we get the advertisedAddr + const p2 = addressFactory.createSipURI(null, advertisedAddr.host) + p2.setLrParam() + p2.setTransportParam(advertisedAddr.transport) + p2.setPort(advertisedAddr.port) + const pa2 = addressFactory.createAddress(p2) + const rr2 = headerFactory.createRecordRouteHeader(pa2) + requestOut.addLast(rr2) } return requestOut }