diff --git a/packages/proxy/src/proxy.ts b/packages/proxy/src/proxy.ts index 1c2d55d3..97418914 100644 --- a/packages/proxy/src/proxy.ts +++ b/packages/proxy/src/proxy.ts @@ -367,6 +367,7 @@ async function onconnect( res = null; } + socket.on('end', () => target.destroy()); socket.pipe(target); target.pipe(socket); } diff --git a/packages/proxy/test/test.ts b/packages/proxy/test/test.ts index 7663e431..d90645bb 100644 --- a/packages/proxy/test/test.ts +++ b/packages/proxy/test/test.ts @@ -203,4 +203,27 @@ describe('proxy', () => { assert(0 == data.indexOf('HTTP/1.1 407')); }); }); + + it('should close both connections when client disconnects', async () => { + const proxySocketPromise = new Promise(resolve => proxy.on("connection", socket => resolve(socket))) + const targetRequestPromise = new Promise(resolve => server.on("connection", resolve)) + + const clientSocket = net.connect({ port: +proxyUrl.port }); + await once(clientSocket, 'connect'); + { + clientSocket.write('CONNECT ' + serverUrl.host + ' HTTP/1.1\r\n\r\n'); + clientSocket.setEncoding('utf8'); + const [data] = await once(clientSocket, 'data'); + assert(0 == data.indexOf('HTTP/1.1 200 Connection established\r\n')); + } + const serverSocket = await proxySocketPromise; + const targetRequest = await targetRequestPromise + await Promise.all([ + new Promise(resolve => serverSocket.on("end", resolve)), + new Promise(resolve => targetRequest.on("close", resolve)), + clientSocket.end() + ]) + expect(targetRequest.readyState).toEqual("closed") + expect(serverSocket.readyState).toEqual("closed") + }); });