Skip to content

Commit c730680

Browse files
committed
fixup: fix socks5 mutual stream destruction logic
1 parent 7827b7c commit c730680

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/devtools-proxy-support/src/agent.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,20 @@ export function createAgent(
145145

146146
export function useOrCreateAgent(
147147
proxyOptions: DevtoolsProxyOptions | AgentWithInitialize,
148-
target?: string
148+
target?: string,
149+
useTargetRegardlessOfExistingAgent = false
149150
): AgentWithInitialize | undefined {
150151
if ('createConnection' in proxyOptions) {
151-
return proxyOptions as AgentWithInitialize;
152+
const agent = proxyOptions as AgentWithInitialize;
153+
if (
154+
useTargetRegardlessOfExistingAgent &&
155+
target !== undefined &&
156+
agent.proxyOptions &&
157+
!proxyForUrl(agent.proxyOptions, target)
158+
) {
159+
return undefined;
160+
}
161+
return agent;
152162
} else {
153163
if (
154164
target !== undefined &&

packages/devtools-proxy-support/src/socks5.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,13 @@ class Socks5Server extends EventEmitter implements Tunnel {
331331
socket.on('error', forwardingErrorHandler);
332332

333333
socket.once('close', () => {
334+
if (!channel?.destroyed) channel.destroy();
334335
this.logger.emit('socks5:forwarded-socket-closed', { ...logMetadata });
335336
this.connections.delete(socket as Socket);
336337
});
338+
channel.once('close', () => {
339+
if (!socket?.destroyed) socket?.destroy();
340+
});
337341

338342
socket.pipe(channel).pipe(socket);
339343
} catch (err) {
@@ -392,7 +396,7 @@ export function createSocks5Tunnel(
392396
return new ExistingTunnel(socks5OnlyProxyOptions);
393397
}
394398

395-
const agent = useOrCreateAgent(proxyOptions, target);
399+
const agent = useOrCreateAgent(proxyOptions, target, true);
396400
if (!agent) return undefined;
397401

398402
let generateCredentials = false;

0 commit comments

Comments
 (0)