From 573f2e4bfdfcf469ceb4aaf7aae8028a9c1adae7 Mon Sep 17 00:00:00 2001 From: Kelvin Schoofs Date: Sun, 26 Sep 2021 22:41:55 +0200 Subject: [PATCH] Improve port forwarding for wildcard addresses --- src/portForwarding.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/portForwarding.ts b/src/portForwarding.ts index 0afdd1a..209c7b1 100644 --- a/src/portForwarding.ts +++ b/src/portForwarding.ts @@ -179,9 +179,9 @@ export function validatePortForwarding(forwarding: PortForwarding): PortForwardi async function createLocalForwarding(connection: Connection, forwarding: PortForwardingLocalRemote): Promise { validateLocalRemoteForwarding(forwarding); - const { localAddress, localPort, remoteAddress, remotePort } = forwarding; - if (forwarding.localAddress === '*') forwarding = { ...forwarding, localAddress: undefined }; + if (forwarding.localAddress === '') forwarding = { ...forwarding, localAddress: undefined }; if (forwarding.localAddress === '*') forwarding = { ...forwarding, localAddress: undefined }; + const { localAddress, localPort, remoteAddress, remotePort } = forwarding; const logging = Logging.scope(formatPortForwarding(forwarding)); logging.info(`Setting up local forwarding`); const { client } = connection; @@ -260,11 +260,12 @@ async function createRemoteForwarding(connection: Connection, forwarding: PortFo unlisten = () => connection.client.off('unix connection', listener); logging.info(`Listening on remote socket path: ${remoteAddress}`); } else { - const actualPort = await toPromise(cb => connection.client.forwardIn(remoteAddress || '', remotePort!, cb)); + const rAddr = remoteAddress === '*' ? '' : remoteAddress || ''; + const actualPort = await toPromise(cb => connection.client.forwardIn(rAddr, remotePort!, cb)); forwarding = { ...forwarding, remotePort: actualPort }; const listener = (details: TcpConnectionDetails, accept: () => ClientChannel) => { if (details.destPort !== actualPort) return; - if (details.destIP !== (remoteAddress || '')) return; + if (details.destIP !== rAddr) return; onSocket(accept()); }; connection.client.on('tcp connection', listener); @@ -455,17 +456,17 @@ export async function promptPortForwarding(config: FileSystemConfig): Promise