From d2ebe3b85db5f7bd2bfb8fc5c03d001c0a05ef42 Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Fri, 9 Oct 2020 11:48:09 +0200 Subject: [PATCH] Add setting to disable auto forwarding of ports Fixes #108346 --- .../contrib/remote/browser/remote.ts | 37 +++++++++++++++---- .../electron-sandbox/remote.contribution.ts | 5 +++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/contrib/remote/browser/remote.ts b/src/vs/workbench/contrib/remote/browser/remote.ts index 37ae21c47ac..4f8fb0ba2cf 100644 --- a/src/vs/workbench/contrib/remote/browser/remote.ts +++ b/src/vs/workbench/contrib/remote/browser/remote.ts @@ -842,6 +842,8 @@ class RemoteAgentConnectionStatusListener implements IWorkbenchContribution { class AutomaticPortForwarding extends Disposable implements IWorkbenchContribution { private contextServiceListener?: IDisposable; + private urlFinder?: UrlFinder; + private static AUTO_FORWARD_SETTING = 'remote.autoForwardPorts'; constructor( @ITerminalService private readonly terminalService: ITerminalService, @@ -850,31 +852,43 @@ class AutomaticPortForwarding extends Disposable implements IWorkbenchContributi @IViewsService private readonly viewsService: IViewsService, @IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService, @IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService, - @IContextKeyService private readonly contextKeyService: IContextKeyService + @IContextKeyService private readonly contextKeyService: IContextKeyService, + @IConfigurationService private readonly configurationService: IConfigurationService ) { super(); + this._register(configurationService.onDidChangeConfiguration((e) => { + if (e.affectsConfiguration(AutomaticPortForwarding.AUTO_FORWARD_SETTING)) { + this.tryStartStopUrlFinder(); + } + })); if (this.environmentService.remoteAuthority) { - this.startUrlFinder(); + this.tryStartStopUrlFinder(); } else { this.contextServiceListener = this._register(this.contextKeyService.onDidChangeContext(e => { if (e.affectsSome(new Set(forwardedPortsViewEnabled.keys()))) { - this.startUrlFinder(); + this.tryStartStopUrlFinder(); } })); } } - private isStarted = false; + private tryStartStopUrlFinder() { + if (this.configurationService.getValue(AutomaticPortForwarding.AUTO_FORWARD_SETTING)) { + this.startUrlFinder(); + } else { + this.stopUrlFinder(); + } + } + private startUrlFinder() { - if (!this.isStarted && !forwardedPortsViewEnabled.getValue(this.contextKeyService)) { + if (!this.urlFinder && !forwardedPortsViewEnabled.getValue(this.contextKeyService)) { return; } if (this.contextServiceListener) { this.contextServiceListener.dispose(); } - this.isStarted = true; - const urlFinder = this._register(new UrlFinder(this.terminalService)); - this._register(urlFinder.onDidMatchLocalUrl(async (localUrl) => { + this.urlFinder = this._register(new UrlFinder(this.terminalService)); + this._register(this.urlFinder.onDidMatchLocalUrl(async (localUrl) => { if (mapHasTunnelLocalhostOrAllInterfaces(this.remoteExplorerService.tunnelModel.forwarded, localUrl.host, localUrl.port)) { return; } @@ -902,6 +916,13 @@ class AutomaticPortForwarding extends Disposable implements IWorkbenchContributi } })); } + + private stopUrlFinder() { + if (this.urlFinder) { + this.urlFinder.dispose(); + this.urlFinder = undefined; + } + } } const workbenchContributionsRegistry = Registry.as(WorkbenchExtensions.Workbench); diff --git a/src/vs/workbench/contrib/remote/electron-sandbox/remote.contribution.ts b/src/vs/workbench/contrib/remote/electron-sandbox/remote.contribution.ts index aa526471694..039ec302a61 100644 --- a/src/vs/workbench/contrib/remote/electron-sandbox/remote.contribution.ts +++ b/src/vs/workbench/contrib/remote/electron-sandbox/remote.contribution.ts @@ -172,6 +172,11 @@ Registry.as(ConfigurationExtensions.Configuration) type: 'boolean', markdownDescription: nls.localize('remote.restoreForwardedPorts', "Restores the ports you forwarded in a workspace."), default: false + }, + 'remote.autoForwardPorts': { + type: 'boolean', + markdownDescription: nls.localize('remote.autoForwardPorts', "When enabled, URLs with ports (ex. `http://127.0.0.1:3000`) that are printed to your terminals are automatically forwarded."), + default: true } } });