Skip to content

Commit

Permalink
fix: correctly close half-opened socket
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Jul 31, 2019
1 parent e9ef418 commit 518a718
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ module.exports = {
// The pipe below will end proxySocket if socket closes cleanly, but not
// if it errors (eg, vanishes from the net and starts returning
// EHOSTUNREACH). We need to do that explicitly.
socket.on('error', function () {
socket.on('error', function (err) {
console.warn('WARN: socket closed with error:', err);
proxySocket.end();
});

socket.on('end', () => socket.end());

common.setupSocket(proxySocket);

if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
Expand Down
26 changes: 16 additions & 10 deletions lib/http-proxy/ws/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ module.exports = class Interceptor {
this._proxyReq = proxyReq;
this._proxyRes = proxyRes;
this._proxySocket = proxySocket;
this._isSocketOpened = true;
this._isClientSocketOpened = true;
this._isServerSocketOpened = true;

this._configure();
}

_configure() {
this._proxySocket.on('close', () => {
this._isSocketOpened = false;
});
this._proxySocket.on('close', () => this._isClientSocketOpened = false);
this._socket.on('close', () => this._isServerSocketOpened = false);

const secWsExtensions = this._proxyRes.headers['sec-websocket-extensions'];
const extensions = Extensions.parse(secWsExtensions);
Expand All @@ -50,10 +50,12 @@ module.exports = class Interceptor {
this._serverExtensions = this._isCompressed ? acceptExtensions({extensions, isServer: true}) : null;
}

_getDataSender({sender, event, options}) {
_getDataSender({sender, type, event, options}) {
return ({data, binary = false}) => {
const opts = Object.assign({fin: true, compress: this._isCompressed, binary}, options);
sender.send(data, opts);
const condition = type === 'client' ? this._isClientSocketOpened : this._isServerSocketOpened;

condition && sender.send(data, opts);

this._proxyReq.emit(event, {data, binary});
};
Expand Down Expand Up @@ -82,11 +84,13 @@ module.exports = class Interceptor {

// 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: 'wsClientMsg', options});
const dataSender = this._getDataSender({sender, type: 'client', event: 'wsClientMsg', options});

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);
receiver.onclose = (code, msg, {masked: mask}) => {
this._isClientSocketOpened && sender.close(code, msg, mask);
};

this._socket.on('data', (data) => receiver.add(data));
}
Expand All @@ -97,11 +101,13 @@ module.exports = class Interceptor {
this._proxyReq.emit('serverSenderInited', sender);

const options = {mask: false};
const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options});
const dataSender = this._getDataSender({sender, type: 'server', event: 'wsServerMsg', options});

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);
receiver.onclose = (code, msg, {masked: mask}) => {
this._isServerSocketOpened && sender.close(code, msg, mask);
};

this._proxySocket.on('data', (data) => receiver.add(data));
}
Expand Down

0 comments on commit 518a718

Please sign in to comment.