From 381b99f6415128b1d721ab75926d2a347ab89fbc Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Fri, 15 Jan 2021 16:49:54 +0100 Subject: [PATCH] Also run the output based auto port forwarding (#114424) Fixes microsoft/vscode-internalbacklog#1736 --- src/vs/platform/remote/common/tunnel.ts | 10 +++++++--- .../contrib/remote/browser/remoteExplorer.ts | 19 +++++++++++++------ .../contrib/remote/browser/urlFinder.ts | 4 +++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/vs/platform/remote/common/tunnel.ts b/src/vs/platform/remote/common/tunnel.ts index f8a71a1f9be..29b9a895a46 100644 --- a/src/vs/platform/remote/common/tunnel.ts +++ b/src/vs/platform/remote/common/tunnel.ts @@ -5,7 +5,7 @@ import { Emitter, Event } from 'vs/base/common/event'; import { IDisposable } from 'vs/base/common/lifecycle'; -import { isWindows } from 'vs/base/common/platform'; +import { isWindows, OperatingSystem } from 'vs/base/common/platform'; import { URI } from 'vs/base/common/uri'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { ILogService } from 'vs/platform/log/common/log'; @@ -85,8 +85,12 @@ function getOtherLocalhost(host: string): string | undefined { return (host === 'localhost') ? '127.0.0.1' : ((host === '127.0.0.1') ? 'localhost' : undefined); } -export function isPortPrivileged(port: number): boolean { - return !isWindows && (port < 1024); +export function isPortPrivileged(port: number, os?: OperatingSystem): boolean { + if (os) { + return os !== OperatingSystem.Windows && (port < 1024); + } else { + return !isWindows && (port < 1024); + } } export abstract class AbstractTunnelService implements ITunnelService { diff --git a/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts b/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts index 9d3b53f3905..d009507ca72 100644 --- a/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts +++ b/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts @@ -212,10 +212,12 @@ export class AutomaticPortForwarding extends Disposable implements IWorkbenchCon remoteAgentService.getEnvironment().then(environment => { if (environment?.os === OperatingSystem.Windows) { - this._register(new WindowsAutomaticPortForwarding(terminalService, notificationService, openerService, - remoteExplorerService, configurationService, debugService, tunnelService)); + this._register(new OutputAutomaticPortForwarding(terminalService, notificationService, openerService, + remoteExplorerService, configurationService, debugService, tunnelService, remoteAgentService, false)); } else if (environment?.os === OperatingSystem.Linux) { - this._register(new LinuxAutomaticPortForwarding(configurationService, remoteExplorerService, notificationService, openerService, tunnelService)); + this._register(new ProcAutomaticPortForwarding(configurationService, remoteExplorerService, notificationService, openerService, tunnelService)); + this._register(new OutputAutomaticPortForwarding(terminalService, notificationService, openerService, + remoteExplorerService, configurationService, debugService, tunnelService, remoteAgentService, true)); } }); } @@ -355,7 +357,7 @@ class OnAutoForwardedAction extends Disposable { } } -class WindowsAutomaticPortForwarding extends Disposable { +class OutputAutomaticPortForwarding extends Disposable { private portsFeatures?: IDisposable; private urlFinder?: UrlFinder; private notifier: OnAutoForwardedAction; @@ -368,7 +370,9 @@ class WindowsAutomaticPortForwarding extends Disposable { private readonly remoteExplorerService: IRemoteExplorerService, private readonly configurationService: IConfigurationService, private readonly debugService: IDebugService, - readonly tunnelService: ITunnelService + readonly tunnelService: ITunnelService, + private readonly remoteAgentService: IRemoteAgentService, + readonly privilegedOnly: boolean ) { super(); this.portsAttributes = new PortsAttributes(configurationService); @@ -408,6 +412,9 @@ class WindowsAutomaticPortForwarding extends Disposable { if (this.portsAttributes.getAttributes(localUrl.port)?.onAutoForward === OnPortForward.Ignore) { return; } + if (this.privilegedOnly && !isPortPrivileged(localUrl.port, (await this.remoteAgentService.getEnvironment())?.os)) { + return; + } const forwarded = await this.remoteExplorerService.forward(localUrl); if (forwarded) { this.notifier.doAction([forwarded]); @@ -423,7 +430,7 @@ class WindowsAutomaticPortForwarding extends Disposable { } } -class LinuxAutomaticPortForwarding extends Disposable { +class ProcAutomaticPortForwarding extends Disposable { private candidateListener: IDisposable | undefined; private autoForwarded: Set = new Set(); private notifier: OnAutoForwardedAction; diff --git a/src/vs/workbench/contrib/remote/browser/urlFinder.ts b/src/vs/workbench/contrib/remote/browser/urlFinder.ts index e9c21167a3a..cb3c96dc2f1 100644 --- a/src/vs/workbench/contrib/remote/browser/urlFinder.ts +++ b/src/vs/workbench/contrib/remote/browser/urlFinder.ts @@ -18,6 +18,7 @@ export class UrlFinder extends Disposable { * http://0.0.0.0:4000 - Elixir Phoenix */ private static readonly localUrlRegex = /\b\w{2,20}:\/\/(?:localhost|127\.0\.0\.1|0\.0\.0\.0|:\d{2,5})[\w\-\.\~:\/\?\#[\]\@!\$&\(\)\*\+\,\;\=]*/gim; + private static readonly extractPortRegex = /(localhost|127\.0\.0\.1|0\.0\.0\.0):(\d{1,5})/; /** * https://github.com/microsoft/vscode-remote-release/issues/3949 */ @@ -106,7 +107,8 @@ export class UrlFinder extends Disposable { const serverUrl = new URL(match); if (serverUrl) { // check if the port is a valid integer value - const port = parseFloat(serverUrl.port!); + const portMatch = match.match(UrlFinder.extractPortRegex); + const port = parseFloat(serverUrl.port ? serverUrl.port : (portMatch ? portMatch[2] : 'NaN')); if (!isNaN(port) && Number.isInteger(port) && port > 0 && port <= 65535) { // normalize the host name let host = serverUrl.hostname; -- GitLab