Add setting to disable auto forwarding of ports

Fixes #108346
This commit is contained in:
Alex Ross
2020-10-09 11:48:30 +02:00
parent f0f95fbc43
commit d2ebe3b85d
2 changed files with 34 additions and 8 deletions
@@ -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<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
@@ -172,6 +172,11 @@ Registry.as<IConfigurationRegistry>(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
}
}
});