Skip to content

fix: correctly close half-opened socket #6

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

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.error('ERROR: socket closed with:', err);
proxySocket.end();
});

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

common.setupSocket(proxySocket);

if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
Expand Down
23 changes: 13 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._isClientSocketOpen = true;
this._isServerSocketOpen = true;

this._configure();
}

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

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

_getDataSender({sender, event, options}) {
_getDataSender({sender, dataSendCond, event, options}) {
return ({data, binary = false}) => {
const opts = Object.assign({fin: true, compress: this._isCompressed, binary}, options);
sender.send(data, opts);

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

this._proxyReq.emit(event, {data, binary});
};
Expand Down Expand Up @@ -82,11 +83,12 @@ 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 dataSendCond = () => this._isClientSocketOpen;
const dataSender = this._getDataSender({sender, dataSendCond, 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._isClientSocketOpen && sender.close(code, msg, mask);

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

const options = {mask: false};
const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options});
const dataSendCond = () => this._isServerSocketOpen;
const dataSender = this._getDataSender({sender, dataSendCond, 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._isServerSocketOpen && sender.close(code, msg, mask);

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