-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add ability to intercept websocket messages #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,5 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "6" | ||
- "8" | ||
script: | ||
- npm test | ||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) | ||
matrix: | ||
fast_finish: true | ||
notifications: | ||
email: | ||
- travis@nodejitsu.com | ||
irc: "irc.freenode.org#nodejitsu" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
const PerMessageDeflate = require('ws/lib/PerMessageDeflate'); | ||
const Extensions = require('ws/lib/Extensions'); | ||
const Receiver = require('ws/lib/Receiver'); | ||
const Sender = require('ws/lib/Sender'); | ||
|
||
const acceptExtensions = ({extenstions, isServer}) => { | ||
const {extensionName} = PerMessageDeflate; | ||
const extenstion = extenstions[extensionName]; | ||
|
||
if (!extenstion) { | ||
return {}; | ||
} | ||
|
||
const perMessageDeflate = new PerMessageDeflate({}, isServer); | ||
perMessageDeflate.accept(extenstion); | ||
|
||
return {[extensionName]: perMessageDeflate}; | ||
}; | ||
|
||
const getMsgHandler = ({interceptor, dataSender, binary}) => { | ||
return (data, flags) => { | ||
if (typeof interceptor !== 'function') { | ||
dataSender({data}); | ||
} | ||
|
||
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}) { | ||
this._socket = socket; | ||
this._options = options; | ||
this._proxyReq = proxyReq; | ||
this._proxyRes = proxyRes; | ||
this._proxySocket = proxySocket; | ||
|
||
this._configure(); | ||
} | ||
|
||
_configure() { | ||
const secWsExtensions = this._proxyRes.headers['sec-websocket-extensions']; | ||
const extenstions = Extensions.parse(secWsExtensions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this._isCompressed = secWsExtensions && secWsExtensions.indexOf('permessage-deflate') != -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему не |
||
|
||
// need both versions of extensions for each side of the proxy connection | ||
this._clientExtenstions = this._isCompressed ? acceptExtensions({extenstions, isServer: false}) : null; | ||
this._serverExtenstions = this._isCompressed ? acceptExtensions({extenstions, isServer: true}) : null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
_getDataSender({sender, event, options}) { | ||
return ({data, binary = false}) => { | ||
const opts = Object.assign({fin: true, compress: this._isCompressed, binary}, options); | ||
sender.send(data, opts); | ||
|
||
this._proxyReq.emit(event, {data, binary}); | ||
}; | ||
} | ||
|
||
_interceptServerMessages() { | ||
const receiver = new Receiver(this._clientExtenstions); | ||
const sender = new Sender(this._proxySocket, this._serverExtenstions); | ||
|
||
// frame must be masked when send from client to server - https://tools.ietf.org/html/rfc6455#section-5.3 | ||
const options = {mask: true}; | ||
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}); | ||
|
||
this._socket.on('data', (data) => receiver.add(data)); | ||
} | ||
|
||
_interceptClientMessages() { | ||
const receiver = new Receiver(this._serverExtenstions); | ||
const sender = new Sender(this._socket, this._clientExtenstions); | ||
|
||
const options = {mask: false}; | ||
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}); | ||
|
||
this._proxySocket.on('data', (data) => receiver.add(data)); | ||
} | ||
|
||
intercept() { | ||
this._interceptServerMessages(); | ||
this._interceptClientMessages(); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extension
иextensions[]
–t
лишняя