From d077d184b261d35f818cd61fb1c52ae07262c8b3 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 23 Feb 2022 21:27:02 -0500 Subject: [PATCH] when using websockets, an action hook may still provide an absolute http url --- lib/utils/ws-requestor.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/utils/ws-requestor.js b/lib/utils/ws-requestor.js index 86884aea..5491d563 100644 --- a/lib/utils/ws-requestor.js +++ b/lib/utils/ws-requestor.js @@ -4,6 +4,7 @@ const short = require('short-uuid'); const {HookMsgTypes} = require('./constants.json'); const Websocket = require('ws'); const snakeCaseKeys = require('./snakecase-keys'); +const HttpRequestor = require('./http-requestor'); const MAX_RECONNECTS = 5; const RESPONSE_TIMEOUT_MS = process.env.JAMBONES_WS_API_MSG_RESPONSE_TIMEOUT || 5000; @@ -32,12 +33,20 @@ class WsRequestor extends BaseRequestor { */ async request(type, hook, params) { assert(HookMsgTypes.includes(type)); + const url = hook.url || hook; if (this.maliciousClient) { this.logger.info({url: this.url}, 'WsRequestor:request - discarding msg to malicious client'); return; } + /* if we have an absolute url, and it is http then do a standard webhook */ + if (this._isAbsoluteUrl(url) && url.startsWith('http')) { + this.logger.debug({hook}, 'WsRequestor: sending a webhook'); + const requestor = new HttpRequestor(this.logger, this.account_sid, hook, this.secret); + return requestor.request(type, hook, params); + } + /* connect if necessary */ if (!this.ws) { if (this.connections >= MAX_RECONNECTS) { @@ -57,8 +66,6 @@ class WsRequestor extends BaseRequestor { /* prepare and send message */ const payload = params ? snakeCaseKeys(params, ['customerData', 'sip']) : null; - const url = hook.url || hook; - assert.ok(url, 'WsRequestor:request url was not provided'); const msgid = short.generate();