From 3a770f839f1a07f9ba2a7d7fc915b3e1f53e85cc Mon Sep 17 00:00:00 2001 From: DudaGod Date: Mon, 15 Jul 2019 18:09:16 +0300 Subject: [PATCH] feat: pass request object to message interceptors --- README.md | 8 +++--- lib/http-proxy/passes/ws-incoming.js | 3 +- lib/http-proxy/ws/interceptor.js | 43 ++++++++++++++-------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index f6bda9932..d481845ee 100644 --- a/README.md +++ b/README.md @@ -395,21 +395,21 @@ proxyServer.listen(8015); }; ``` -* **wsInterceptClientMsg**: Is a handler which is called when a websocket message is intercepted on its way to the server from the client. It takes two arguments: `data` - is a websocket message and flags (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the client. +* **wsInterceptClientMsg**: Is a handler which is called when a websocket message is intercepted on its way to the server from the client. It takes two arguments: `data` - is a websocket message and `options` in which exists field `req` - websocket request object and `flags` (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the client. ``` const proxy = new HttpProxy({ ... - wsInterceptClientMsg: (data, flags) { + wsInterceptClientMsg: (data, options) { return typeof data === 'string ? data.toUpperCase() : data; } ... }) ``` -* **wsInterceptServerMsg**: Is a handler which is called when a websocket message is intercepted on its way to the client from the server. It takes two arguments: `data` - is a websocket message and flags (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the target server. +* **wsInterceptServerMsg**: Is a handler which is called when a websocket message is intercepted on its way to the client from the server. It takes two arguments: `data` - is a websocket message and `options` in which exist fields `req` - websocket request object and `flags` (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the target server. ``` const proxy = new HttpProxy({ ... - wsInterceptServerMsg: (data, flags) { + wsInterceptServerMsg: (data, options) { return typeof data === 'string ? data.toUpperCase() : data; } ... diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 5c481d683..8e510737b 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -145,7 +145,8 @@ module.exports = { // socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers)); - const wsInterceptor = WsInterceptor.create({socket, options, proxyReq, proxyRes, proxySocket}); + const wsInterceptor = WsInterceptor.create({socket, options, req, proxyReq, proxyRes, proxySocket}); + wsInterceptor.startDataTransfer(); server.emit('open', proxySocket); diff --git a/lib/http-proxy/ws/interceptor.js b/lib/http-proxy/ws/interceptor.js index fcf20cab9..10a6ffd9d 100644 --- a/lib/http-proxy/ws/interceptor.js +++ b/lib/http-proxy/ws/interceptor.js @@ -19,30 +19,15 @@ const acceptExtensions = ({extensions, isServer}) => { return {[extensionName]: perMessageDeflate}; }; -const getMsgHandler = ({interceptor, dataSender, binary}) => { - return (data, flags) => { - if (typeof interceptor !== 'function') { - dataSender({data}); - return; - } - - const modifiedData = interceptor(data, flags); - - // if interceptor does not return data then nothing will be sended to the server - if (modifiedData) { - dataSender({data: modifiedData, binary}); - } - } -}; - module.exports = class Interceptor { static create(opts = {}) { return new this(opts); } - constructor({socket, options, proxyReq, proxyRes, proxySocket}) { + constructor({socket, options, req, proxyReq, proxyRes, proxySocket}) { this._socket = socket; this._options = options; + this._req = req; this._proxyReq = proxyReq; this._proxyRes = proxyRes; this._proxySocket = proxySocket; @@ -74,6 +59,22 @@ module.exports = class Interceptor { }; } + _getMsgHandler({interceptor, dataSender, binary}) { + return (data, flags) => { + if (typeof interceptor !== 'function') { + dataSender({data}); + return; + } + + const modifiedData = interceptor(data, {req: this._req, flags}); + + // if interceptor does not return data then nothing will be sended to the server + if (modifiedData) { + dataSender({data: modifiedData, binary}); + } + } + } + _interceptClientMessages() { const receiver = new Receiver(this._clientExtensions); const sender = new Sender(this._proxySocket, this._serverExtensions); @@ -83,8 +84,8 @@ module.exports = class Interceptor { const options = {mask: true}; const dataSender = this._getDataSender({sender, event: 'wsClientMsg', options}); - receiver.ontext = getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: false}); - receiver.onbinary = getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: true}); + receiver.ontext = this._getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: false}); + receiver.onbinary = this._getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: true}); receiver.onclose = (code, msg, {masked: mask}) => this._isSocketOpened && sender.close(code, msg, mask); this._socket.on('data', (data) => receiver.add(data)); @@ -98,8 +99,8 @@ module.exports = class Interceptor { const options = {mask: false}; const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options}); - receiver.ontext = getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: false}); - receiver.onbinary = getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: true}); + receiver.ontext = this._getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: false}); + receiver.onbinary = this._getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: true}); receiver.onclose = (code, msg, {masked: mask}) => this._isSocketOpened && sender.close(code, msg, mask); this._proxySocket.on('data', (data) => receiver.add(data));