Improve port forwarding for wildcard addresses

feature/forwarding
Kelvin Schoofs 3 years ago
parent f03e1754ba
commit 573f2e4bfd

@ -179,9 +179,9 @@ export function validatePortForwarding(forwarding: PortForwarding): PortForwardi
async function createLocalForwarding(connection: Connection, forwarding: PortForwardingLocalRemote): Promise<ActivePortForwarding> { async function createLocalForwarding(connection: Connection, forwarding: PortForwardingLocalRemote): Promise<ActivePortForwarding> {
validateLocalRemoteForwarding(forwarding); 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 }; if (forwarding.localAddress === '*') forwarding = { ...forwarding, localAddress: undefined };
const { localAddress, localPort, remoteAddress, remotePort } = forwarding;
const logging = Logging.scope(formatPortForwarding(forwarding)); const logging = Logging.scope(formatPortForwarding(forwarding));
logging.info(`Setting up local forwarding`); logging.info(`Setting up local forwarding`);
const { client } = connection; const { client } = connection;
@ -260,11 +260,12 @@ async function createRemoteForwarding(connection: Connection, forwarding: PortFo
unlisten = () => connection.client.off('unix connection', listener); unlisten = () => connection.client.off('unix connection', listener);
logging.info(`Listening on remote socket path: ${remoteAddress}`); logging.info(`Listening on remote socket path: ${remoteAddress}`);
} else { } else {
const actualPort = await toPromise<number>(cb => connection.client.forwardIn(remoteAddress || '', remotePort!, cb)); const rAddr = remoteAddress === '*' ? '' : remoteAddress || '';
const actualPort = await toPromise<number>(cb => connection.client.forwardIn(rAddr, remotePort!, cb));
forwarding = { ...forwarding, remotePort: actualPort }; forwarding = { ...forwarding, remotePort: actualPort };
const listener = (details: TcpConnectionDetails, accept: () => ClientChannel) => { const listener = (details: TcpConnectionDetails, accept: () => ClientChannel) => {
if (details.destPort !== actualPort) return; if (details.destPort !== actualPort) return;
if (details.destIP !== (remoteAddress || '')) return; if (details.destIP !== rAddr) return;
onSocket(accept()); onSocket(accept());
}; };
connection.client.on('tcp connection', listener); connection.client.on('tcp connection', listener);
@ -455,17 +456,17 @@ export async function promptPortForwarding(config: FileSystemConfig): Promise<Po
]; ];
} else if (picker.value) { } else if (picker.value) {
const type = picker.value.toLowerCase().trimLeft().match(/^[a-zA-Z]*/)![0].replace(/Forward$/, ''); const type = picker.value.toLowerCase().trimLeft().match(/^[a-zA-Z]*/)![0].replace(/Forward$/, '');
let detail: string; let detail: string;
if (type === 'l' || type === 'local') { if (type === 'l' || type === 'local') {
detail = 'Local [localAddress]:localPort remoteAddress:remotePort'; detail = 'Local [localAddress]:localPort remoteAddress:remotePort';
} else if (type === 'r' || type === 'remote') { } else if (type === 'r' || type === 'remote') {
detail = 'Remote [localAddress:localPort] [remoteAddress]:remotePort'; detail = 'Remote [localAddress:localPort] [remoteAddress]:remotePort';
} else if (type === 'd' || type === 'dynamic') { } else if (type === 'd' || type === 'dynamic') {
detail = 'Dynamic localAddress:localPort'; detail = 'Dynamic localAddress:localPort';
} else { } else {
detail = 'Select or type a port forwarding type'; detail = 'Select or type a port forwarding type';
items = [...ITEMS]; items = [...ITEMS];
} }
try { try {
const forward = parsePortForwarding(picker.value, 'throw')!; const forward = parsePortForwarding(picker.value, 'throw')!;
suggested.push(formatPF(forward)); suggested.push(formatPF(forward));
@ -503,11 +504,11 @@ export async function promptPortForwarding(config: FileSystemConfig): Promise<Po
if (picker.value === 'examples') { if (picker.value === 'examples') {
// Looking at examples, don't actually accept but copy the value // Looking at examples, don't actually accept but copy the value
picker.value = formatPortForwardingConfig(item); picker.value = formatPortForwardingConfig(item);
} else { } else {
resolve(item); resolve(item);
picker.hide(); picker.hide();
return; return;
} }
} }
updateItems(); updateItems();
}); });

Loading…
Cancel
Save