-
Notifications
You must be signed in to change notification settings - Fork 28.6k
/
remoteExplorer.ts
781 lines (709 loc) · 35.4 KB
/
remoteExplorer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { Disposable, IDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Extensions, IViewContainersRegistry, IViewsRegistry, ViewContainer, ViewContainerLocation } from 'vs/workbench/common/views';
import { IRemoteExplorerService, PORT_AUTO_FALLBACK_SETTING, PORT_AUTO_FORWARD_SETTING, PORT_AUTO_SOURCE_SETTING, PORT_AUTO_SOURCE_SETTING_HYBRID, PORT_AUTO_SOURCE_SETTING_OUTPUT, PORT_AUTO_SOURCE_SETTING_PROCESS, TUNNEL_VIEW_CONTAINER_ID, TUNNEL_VIEW_ID } from 'vs/workbench/services/remote/common/remoteExplorerService';
import { Attributes, AutoTunnelSource, forwardedPortsViewEnabled, makeAddress, mapHasAddressLocalhostOrAllInterfaces, OnPortForward, Tunnel, TunnelCloseReason, TunnelSource } from 'vs/workbench/services/remote/common/tunnelModel';
import { ForwardPortAction, OpenPortInBrowserAction, TunnelPanel, TunnelPanelDescriptor, TunnelViewModel, OpenPortInPreviewAction, openPreviewEnabledContext } from 'vs/workbench/contrib/remote/browser/tunnelView';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { Registry } from 'vs/platform/registry/common/platform';
import { IStatusbarEntry, IStatusbarEntryAccessor, IStatusbarService, StatusbarAlignment } from 'vs/workbench/services/statusbar/browser/statusbar';
import { UrlFinder } from 'vs/workbench/contrib/remote/browser/urlFinder';
import Severity from 'vs/base/common/severity';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { INotificationHandle, INotificationService, IPromptChoice } from 'vs/platform/notification/common/notification';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { IDebugService } from 'vs/workbench/contrib/debug/common/debug';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
import { isWeb, OperatingSystem } from 'vs/base/common/platform';
import { ITunnelService, RemoteTunnel, TunnelPrivacyId } from 'vs/platform/tunnel/common/tunnel';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
import { portsViewIcon } from 'vs/workbench/contrib/remote/browser/remoteIcons';
import { Event } from 'vs/base/common/event';
import { IExternalUriOpenerService } from 'vs/workbench/contrib/externalUriOpener/common/externalUriOpenerService';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { ILogService } from 'vs/platform/log/common/log';
import { IWorkbenchConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
import { IRemoteAgentEnvironment } from 'vs/platform/remote/common/remoteAgentEnvironment';
import { Action } from 'vs/base/common/actions';
import { IPreferencesService } from 'vs/workbench/services/preferences/common/preferences';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
export const VIEWLET_ID = 'workbench.view.remote';
export class ForwardedPortsView extends Disposable implements IWorkbenchContribution {
private readonly contextKeyListener = this._register(new MutableDisposable<IDisposable>());
private readonly activityBadge = this._register(new MutableDisposable<IDisposable>());
private entryAccessor: IStatusbarEntryAccessor | undefined;
constructor(
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService,
@ITunnelService private readonly tunnelService: ITunnelService,
@IActivityService private readonly activityService: IActivityService,
@IStatusbarService private readonly statusbarService: IStatusbarService,
) {
super();
this._register(Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).registerViewWelcomeContent(TUNNEL_VIEW_ID, {
content: this.environmentService.remoteAuthority ? nls.localize('remoteNoPorts', "No forwarded ports. Forward a port to access your running services locally.\n[Forward a Port]({0})", `command:${ForwardPortAction.INLINE_ID}`)
: nls.localize('noRemoteNoPorts', "No forwarded ports. Forward a port to access your locally running services over the internet.\n[Forward a Port]({0})", `command:${ForwardPortAction.INLINE_ID}`),
}));
this.enableBadgeAndStatusBar();
this.enableForwardedPortsView();
}
private async getViewContainer(): Promise<ViewContainer | null> {
return Registry.as<IViewContainersRegistry>(Extensions.ViewContainersRegistry).registerViewContainer({
id: TUNNEL_VIEW_CONTAINER_ID,
title: nls.localize2('ports', "Ports"),
icon: portsViewIcon,
ctorDescriptor: new SyncDescriptor(ViewPaneContainer, [TUNNEL_VIEW_CONTAINER_ID, { mergeViewWithContainerWhenSingleView: true }]),
storageId: TUNNEL_VIEW_CONTAINER_ID,
hideIfEmpty: true,
order: 5
}, ViewContainerLocation.Panel);
}
private async enableForwardedPortsView() {
this.contextKeyListener.clear();
const viewEnabled: boolean = !!forwardedPortsViewEnabled.getValue(this.contextKeyService);
if (viewEnabled) {
const viewContainer = await this.getViewContainer();
const tunnelPanelDescriptor = new TunnelPanelDescriptor(new TunnelViewModel(this.remoteExplorerService, this.tunnelService), this.environmentService);
const viewsRegistry = Registry.as<IViewsRegistry>(Extensions.ViewsRegistry);
if (viewContainer) {
this.remoteExplorerService.enablePortsFeatures();
viewsRegistry.registerViews([tunnelPanelDescriptor], viewContainer);
}
} else {
this.contextKeyListener.value = this.contextKeyService.onDidChangeContext(e => {
if (e.affectsSome(new Set(forwardedPortsViewEnabled.keys()))) {
this.enableForwardedPortsView();
}
});
}
}
private enableBadgeAndStatusBar() {
const disposable = Registry.as<IViewsRegistry>(Extensions.ViewsRegistry).onViewsRegistered(e => {
if (e.find(view => view.views.find(viewDescriptor => viewDescriptor.id === TUNNEL_VIEW_ID))) {
this._register(Event.debounce(this.remoteExplorerService.tunnelModel.onForwardPort, (_last, e) => e, 50)(() => {
this.updateActivityBadge();
this.updateStatusBar();
}));
this._register(Event.debounce(this.remoteExplorerService.tunnelModel.onClosePort, (_last, e) => e, 50)(() => {
this.updateActivityBadge();
this.updateStatusBar();
}));
this.updateActivityBadge();
this.updateStatusBar();
disposable.dispose();
}
});
}
private async updateActivityBadge() {
if (this.remoteExplorerService.tunnelModel.forwarded.size > 0) {
this.activityBadge.value = this.activityService.showViewActivity(TUNNEL_VIEW_ID, {
badge: new NumberBadge(this.remoteExplorerService.tunnelModel.forwarded.size, n => n === 1 ? nls.localize('1forwardedPort', "1 forwarded port") : nls.localize('nForwardedPorts', "{0} forwarded ports", n))
});
}
}
private updateStatusBar() {
if (!this.entryAccessor) {
this._register(this.entryAccessor = this.statusbarService.addEntry(this.entry, 'status.forwardedPorts', StatusbarAlignment.LEFT, 40));
} else {
this.entryAccessor.update(this.entry);
}
}
private get entry(): IStatusbarEntry {
let tooltip: string;
const count = this.remoteExplorerService.tunnelModel.forwarded.size + this.remoteExplorerService.tunnelModel.detected.size;
const text = `${count}`;
if (count === 0) {
tooltip = nls.localize('remote.forwardedPorts.statusbarTextNone', "No Ports Forwarded");
} else {
const allTunnels = Array.from(this.remoteExplorerService.tunnelModel.forwarded.values());
allTunnels.push(...Array.from(this.remoteExplorerService.tunnelModel.detected.values()));
tooltip = nls.localize('remote.forwardedPorts.statusbarTooltip', "Forwarded Ports: {0}",
allTunnels.map(forwarded => forwarded.remotePort).join(', '));
}
return {
name: nls.localize('status.forwardedPorts', "Forwarded Ports"),
text: `$(radio-tower) ${text}`,
ariaLabel: tooltip,
tooltip,
command: `${TUNNEL_VIEW_ID}.focus`
};
}
}
export class PortRestore implements IWorkbenchContribution {
constructor(
@IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService,
@ILogService private readonly logService: ILogService
) {
if (!this.remoteExplorerService.tunnelModel.environmentTunnelsSet) {
Event.once(this.remoteExplorerService.tunnelModel.onEnvironmentTunnelsSet)(async () => {
await this.restore();
});
} else {
this.restore();
}
}
private async restore() {
this.logService.trace('ForwardedPorts: Doing first restore.');
return this.remoteExplorerService.restore();
}
}
export class AutomaticPortForwarding extends Disposable implements IWorkbenchContribution {
private procForwarder: ProcAutomaticPortForwarding | undefined;
private outputForwarder: OutputAutomaticPortForwarding | undefined;
private portListener: IDisposable | undefined;
constructor(
@ITerminalService private readonly terminalService: ITerminalService,
@INotificationService private readonly notificationService: INotificationService,
@IOpenerService private readonly openerService: IOpenerService,
@IExternalUriOpenerService private readonly externalOpenerService: IExternalUriOpenerService,
@IRemoteExplorerService private readonly remoteExplorerService: IRemoteExplorerService,
@IWorkbenchEnvironmentService readonly environmentService: IWorkbenchEnvironmentService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IWorkbenchConfigurationService private readonly configurationService: IWorkbenchConfigurationService,
@IDebugService private readonly debugService: IDebugService,
@IRemoteAgentService readonly remoteAgentService: IRemoteAgentService,
@ITunnelService private readonly tunnelService: ITunnelService,
@IHostService private readonly hostService: IHostService,
@ILogService private readonly logService: ILogService,
@IStorageService private readonly storageService: IStorageService,
@IPreferencesService private readonly preferencesService: IPreferencesService,
) {
super();
if (!environmentService.remoteAuthority) {
return;
}
configurationService.whenRemoteConfigurationLoaded().then(() => remoteAgentService.getEnvironment()).then(environment => {
this.setup(environment);
this._register(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(PORT_AUTO_SOURCE_SETTING)) {
this.setup(environment);
} else if (e.affectsConfiguration(PORT_AUTO_FALLBACK_SETTING) && !this.portListener) {
this.listenForPorts();
}
}));
});
if (!this.storageService.getBoolean('processPortForwardingFallback', StorageScope.WORKSPACE, true)) {
this.configurationService.updateValue(PORT_AUTO_FALLBACK_SETTING, 0, ConfigurationTarget.WORKSPACE);
}
}
private listenForPorts() {
let fallbackAt = this.configurationService.getValue<number>(PORT_AUTO_FALLBACK_SETTING);
if (fallbackAt === 0) {
this.portListener?.dispose();
return;
}
if (this.procForwarder && !this.portListener && (this.configurationService.getValue(PORT_AUTO_SOURCE_SETTING) === PORT_AUTO_SOURCE_SETTING_PROCESS)) {
this.portListener = this._register(this.remoteExplorerService.tunnelModel.onForwardPort(async () => {
fallbackAt = this.configurationService.getValue<number>(PORT_AUTO_FALLBACK_SETTING);
if (fallbackAt === 0) {
this.portListener?.dispose();
return;
}
if (Array.from(this.remoteExplorerService.tunnelModel.forwarded.values()).filter(tunnel => tunnel.source.source === TunnelSource.Auto).length > fallbackAt) {
await this.configurationService.updateValue(PORT_AUTO_SOURCE_SETTING, PORT_AUTO_SOURCE_SETTING_HYBRID);
this.notificationService.notify({
message: nls.localize('remote.autoForwardPortsSource.fallback', "Over 20 ports have been automatically forwarded. The `process` based automatic port forwarding has been switched to `hybrid` in settings. Some ports may no longer be detected."),
severity: Severity.Warning,
actions: {
primary: [
new Action('switchBack', nls.localize('remote.autoForwardPortsSource.fallback.switchBack', "Undo"), undefined, true, async () => {
await this.configurationService.updateValue(PORT_AUTO_SOURCE_SETTING, PORT_AUTO_SOURCE_SETTING_PROCESS);
await this.configurationService.updateValue(PORT_AUTO_FALLBACK_SETTING, 0, ConfigurationTarget.WORKSPACE);
this.portListener?.dispose();
this.portListener = undefined;
}),
new Action('showPortSourceSetting', nls.localize('remote.autoForwardPortsSource.fallback.showPortSourceSetting', "Show Setting"), undefined, true, async () => {
await this.preferencesService.openSettings({
query: 'remote.autoForwardPortsSource'
});
})
]
}
});
}
}));
} else {
this.portListener?.dispose();
this.portListener = undefined;
}
}
private setup(environment: IRemoteAgentEnvironment | null) {
const alreadyForwarded = this.procForwarder?.forwarded;
const isSwitch = this.outputForwarder || this.procForwarder;
this.procForwarder?.dispose();
this.procForwarder = undefined;
this.outputForwarder?.dispose();
this.outputForwarder = undefined;
if (environment?.os !== OperatingSystem.Linux) {
if (this.configurationService.inspect<string>(PORT_AUTO_SOURCE_SETTING).default?.value !== PORT_AUTO_SOURCE_SETTING_OUTPUT) {
Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration)
.registerDefaultConfigurations([{ overrides: { 'remote.autoForwardPortsSource': PORT_AUTO_SOURCE_SETTING_OUTPUT } }]);
}
this.outputForwarder = this._register(new OutputAutomaticPortForwarding(this.terminalService, this.notificationService, this.openerService, this.externalOpenerService,
this.remoteExplorerService, this.configurationService, this.debugService, this.tunnelService, this.hostService, this.logService, this.contextKeyService, () => false));
} else {
const useProc = () => (this.configurationService.getValue(PORT_AUTO_SOURCE_SETTING) === PORT_AUTO_SOURCE_SETTING_PROCESS);
if (useProc()) {
this.procForwarder = this._register(new ProcAutomaticPortForwarding(false, alreadyForwarded, !isSwitch, this.configurationService, this.remoteExplorerService, this.notificationService,
this.openerService, this.externalOpenerService, this.tunnelService, this.hostService, this.logService, this.contextKeyService));
} else if (this.configurationService.getValue(PORT_AUTO_SOURCE_SETTING) === PORT_AUTO_SOURCE_SETTING_HYBRID) {
this.procForwarder = this._register(new ProcAutomaticPortForwarding(true, alreadyForwarded, !isSwitch, this.configurationService, this.remoteExplorerService, this.notificationService,
this.openerService, this.externalOpenerService, this.tunnelService, this.hostService, this.logService, this.contextKeyService));
}
this.outputForwarder = this._register(new OutputAutomaticPortForwarding(this.terminalService, this.notificationService, this.openerService, this.externalOpenerService,
this.remoteExplorerService, this.configurationService, this.debugService, this.tunnelService, this.hostService, this.logService, this.contextKeyService, useProc));
}
this.listenForPorts();
}
}
class OnAutoForwardedAction extends Disposable {
private lastNotifyTime: Date;
private static NOTIFY_COOL_DOWN = 5000; // milliseconds
private lastNotification: INotificationHandle | undefined;
private lastShownPort: number | undefined;
private doActionTunnels: RemoteTunnel[] | undefined;
private alreadyOpenedOnce: Set<string> = new Set();
constructor(private readonly notificationService: INotificationService,
private readonly remoteExplorerService: IRemoteExplorerService,
private readonly openerService: IOpenerService,
private readonly externalOpenerService: IExternalUriOpenerService,
private readonly tunnelService: ITunnelService,
private readonly hostService: IHostService,
private readonly logService: ILogService,
private readonly contextKeyService: IContextKeyService) {
super();
this.lastNotifyTime = new Date();
this.lastNotifyTime.setFullYear(this.lastNotifyTime.getFullYear() - 1);
}
public async doAction(tunnels: RemoteTunnel[]): Promise<void> {
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Starting action for ${tunnels[0]?.tunnelRemotePort}`);
this.doActionTunnels = tunnels;
const tunnel = await this.portNumberHeuristicDelay();
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Heuristic chose ${tunnel?.tunnelRemotePort}`);
if (tunnel) {
const allAttributes = await this.remoteExplorerService.tunnelModel.getAttributes([{ port: tunnel.tunnelRemotePort, host: tunnel.tunnelRemoteHost }]);
const attributes = allAttributes?.get(tunnel.tunnelRemotePort)?.onAutoForward;
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) onAutoForward action is ${attributes}`);
switch (attributes) {
case OnPortForward.OpenBrowserOnce: {
if (this.alreadyOpenedOnce.has(tunnel.localAddress)) {
break;
}
this.alreadyOpenedOnce.add(tunnel.localAddress);
// Intentionally do not break so that the open browser path can be run.
}
case OnPortForward.OpenBrowser: {
const address = makeAddress(tunnel.tunnelRemoteHost, tunnel.tunnelRemotePort);
await OpenPortInBrowserAction.run(this.remoteExplorerService.tunnelModel, this.openerService, address);
break;
}
case OnPortForward.OpenPreview: {
const address = makeAddress(tunnel.tunnelRemoteHost, tunnel.tunnelRemotePort);
await OpenPortInPreviewAction.run(this.remoteExplorerService.tunnelModel, this.openerService, this.externalOpenerService, address);
break;
}
case OnPortForward.Silent: break;
default: {
const elapsed = new Date().getTime() - this.lastNotifyTime.getTime();
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) time elapsed since last notification ${elapsed} ms`);
if (elapsed > OnAutoForwardedAction.NOTIFY_COOL_DOWN) {
await this.showNotification(tunnel);
}
}
}
}
}
public hide(removedPorts: number[]) {
if (this.doActionTunnels) {
this.doActionTunnels = this.doActionTunnels.filter(value => !removedPorts.includes(value.tunnelRemotePort));
}
if (this.lastShownPort && removedPorts.indexOf(this.lastShownPort) >= 0) {
this.lastNotification?.close();
}
}
private newerTunnel: RemoteTunnel | undefined;
private async portNumberHeuristicDelay(): Promise<RemoteTunnel | undefined> {
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Starting heuristic delay`);
if (!this.doActionTunnels || this.doActionTunnels.length === 0) {
return;
}
this.doActionTunnels = this.doActionTunnels.sort((a, b) => a.tunnelRemotePort - b.tunnelRemotePort);
const firstTunnel = this.doActionTunnels.shift()!;
// Heuristic.
if (firstTunnel.tunnelRemotePort % 1000 === 0) {
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Heuristic chose tunnel because % 1000: ${firstTunnel.tunnelRemotePort}`);
this.newerTunnel = firstTunnel;
return firstTunnel;
// 9229 is the node inspect port
} else if (firstTunnel.tunnelRemotePort < 10000 && firstTunnel.tunnelRemotePort !== 9229) {
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Heuristic chose tunnel because < 10000: ${firstTunnel.tunnelRemotePort}`);
this.newerTunnel = firstTunnel;
return firstTunnel;
}
this.logService.trace(`ForwardedPorts: (OnAutoForwardedAction) Waiting for "better" tunnel than ${firstTunnel.tunnelRemotePort}`);
this.newerTunnel = undefined;
return new Promise(resolve => {
setTimeout(() => {
if (this.newerTunnel) {
resolve(undefined);
} else if (this.doActionTunnels?.includes(firstTunnel)) {
resolve(firstTunnel);
} else {
resolve(undefined);
}
}, 3000);
});
}
private async basicMessage(tunnel: RemoteTunnel) {
const properties = await this.remoteExplorerService.tunnelModel.getAttributes([{ host: tunnel.tunnelRemoteHost, port: tunnel.tunnelRemotePort }], false);
const label = properties?.get(tunnel.tunnelRemotePort)?.label;
return nls.localize('remote.tunnelsView.automaticForward', "Your application{0} running on port {1} is available. ",
label ? ` (${label})` : '',
tunnel.tunnelRemotePort);
}
private linkMessage() {
return nls.localize(
{ key: 'remote.tunnelsView.notificationLink2', comment: ['[See all forwarded ports]({0}) is a link. Only translate `See all forwarded ports`. Do not change brackets and parentheses or {0}'] },
"[See all forwarded ports]({0})", `command:${TunnelPanel.ID}.focus`);
}
private async showNotification(tunnel: RemoteTunnel) {
if (!await this.hostService.hadLastFocus()) {
return;
}
this.lastNotification?.close();
let message = await this.basicMessage(tunnel);
const choices = [this.openBrowserChoice(tunnel)];
if (!isWeb || openPreviewEnabledContext.getValue(this.contextKeyService)) {
choices.push(this.openPreviewChoice(tunnel));
}
if ((tunnel.tunnelLocalPort !== tunnel.tunnelRemotePort) && this.tunnelService.canElevate && this.tunnelService.isPortPrivileged(tunnel.tunnelRemotePort)) {
// Privileged ports are not on Windows, so it's safe to use "superuser"
message += nls.localize('remote.tunnelsView.elevationMessage', "You'll need to run as superuser to use port {0} locally. ", tunnel.tunnelRemotePort);
choices.unshift(this.elevateChoice(tunnel));
}
if (tunnel.privacy === TunnelPrivacyId.Private && isWeb && this.tunnelService.canChangePrivacy) {
choices.push(this.makePublicChoice(tunnel));
}
message += this.linkMessage();
this.lastNotification = this.notificationService.prompt(Severity.Info, message, choices, { neverShowAgain: { id: 'remote.tunnelsView.autoForwardNeverShow', isSecondary: true } });
this.lastShownPort = tunnel.tunnelRemotePort;
this.lastNotifyTime = new Date();
this.lastNotification.onDidClose(() => {
this.lastNotification = undefined;
this.lastShownPort = undefined;
});
}
private makePublicChoice(tunnel: RemoteTunnel): IPromptChoice {
return {
label: nls.localize('remote.tunnelsView.makePublic', "Make Public"),
run: async () => {
const oldTunnelDetails = mapHasAddressLocalhostOrAllInterfaces(this.remoteExplorerService.tunnelModel.forwarded, tunnel.tunnelRemoteHost, tunnel.tunnelRemotePort);
await this.remoteExplorerService.close({ host: tunnel.tunnelRemoteHost, port: tunnel.tunnelRemotePort }, TunnelCloseReason.Other);
return this.remoteExplorerService.forward({
remote: { host: tunnel.tunnelRemoteHost, port: tunnel.tunnelRemotePort },
local: tunnel.tunnelLocalPort,
name: oldTunnelDetails?.name,
elevateIfNeeded: true,
privacy: TunnelPrivacyId.Public,
source: oldTunnelDetails?.source
});
}
};
}
private openBrowserChoice(tunnel: RemoteTunnel): IPromptChoice {
const address = makeAddress(tunnel.tunnelRemoteHost, tunnel.tunnelRemotePort);
return {
label: OpenPortInBrowserAction.LABEL,
run: () => OpenPortInBrowserAction.run(this.remoteExplorerService.tunnelModel, this.openerService, address)
};
}
private openPreviewChoice(tunnel: RemoteTunnel): IPromptChoice {
const address = makeAddress(tunnel.tunnelRemoteHost, tunnel.tunnelRemotePort);
return {
label: OpenPortInPreviewAction.LABEL,
run: () => OpenPortInPreviewAction.run(this.remoteExplorerService.tunnelModel, this.openerService, this.externalOpenerService, address)
};
}
private elevateChoice(tunnel: RemoteTunnel): IPromptChoice {
return {
// Privileged ports are not on Windows, so it's ok to stick to just "sudo".
label: nls.localize('remote.tunnelsView.elevationButton', "Use Port {0} as Sudo...", tunnel.tunnelRemotePort),
run: async () => {
await this.remoteExplorerService.close({ host: tunnel.tunnelRemoteHost, port: tunnel.tunnelRemotePort }, TunnelCloseReason.Other);
const newTunnel = await this.remoteExplorerService.forward({
remote: { host: tunnel.tunnelRemoteHost, port: tunnel.tunnelRemotePort },
local: tunnel.tunnelRemotePort,
elevateIfNeeded: true,
source: AutoTunnelSource
});
if (!newTunnel || (typeof newTunnel === 'string')) {
return;
}
this.lastNotification?.close();
this.lastShownPort = newTunnel.tunnelRemotePort;
this.lastNotification = this.notificationService.prompt(Severity.Info,
await this.basicMessage(newTunnel) + this.linkMessage(),
[this.openBrowserChoice(newTunnel), this.openPreviewChoice(tunnel)],
{ neverShowAgain: { id: 'remote.tunnelsView.autoForwardNeverShow', isSecondary: true } });
this.lastNotification.onDidClose(() => {
this.lastNotification = undefined;
this.lastShownPort = undefined;
});
}
};
}
}
class OutputAutomaticPortForwarding extends Disposable {
private portsFeatures?: IDisposable;
private urlFinder?: UrlFinder;
private notifier: OnAutoForwardedAction;
constructor(
private readonly terminalService: ITerminalService,
readonly notificationService: INotificationService,
readonly openerService: IOpenerService,
readonly externalOpenerService: IExternalUriOpenerService,
private readonly remoteExplorerService: IRemoteExplorerService,
private readonly configurationService: IConfigurationService,
private readonly debugService: IDebugService,
readonly tunnelService: ITunnelService,
readonly hostService: IHostService,
readonly logService: ILogService,
readonly contextKeyService: IContextKeyService,
readonly privilegedOnly: () => boolean
) {
super();
this.notifier = new OnAutoForwardedAction(notificationService, remoteExplorerService, openerService, externalOpenerService, tunnelService, hostService, logService, contextKeyService);
this._register(configurationService.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(PORT_AUTO_FORWARD_SETTING)) {
this.tryStartStopUrlFinder();
}
}));
this.portsFeatures = this._register(this.remoteExplorerService.onEnabledPortsFeatures(() => {
this.tryStartStopUrlFinder();
}));
this.tryStartStopUrlFinder();
if (configurationService.getValue(PORT_AUTO_SOURCE_SETTING) === PORT_AUTO_SOURCE_SETTING_HYBRID) {
this._register(this.tunnelService.onTunnelClosed(tunnel => this.notifier.hide([tunnel.port])));
}
}
private tryStartStopUrlFinder() {
if (this.configurationService.getValue(PORT_AUTO_FORWARD_SETTING)) {
this.startUrlFinder();
} else {
this.stopUrlFinder();
}
}
private startUrlFinder() {
if (!this.urlFinder && !this.remoteExplorerService.portsFeaturesEnabled) {
return;
}
this.portsFeatures?.dispose();
this.urlFinder = this._register(new UrlFinder(this.terminalService, this.debugService));
this._register(this.urlFinder.onDidMatchLocalUrl(async (localUrl) => {
if (mapHasAddressLocalhostOrAllInterfaces(this.remoteExplorerService.tunnelModel.detected, localUrl.host, localUrl.port)) {
return;
}
const attributes = (await this.remoteExplorerService.tunnelModel.getAttributes([localUrl]))?.get(localUrl.port);
if (attributes?.onAutoForward === OnPortForward.Ignore) {
return;
}
if (this.privilegedOnly() && !this.tunnelService.isPortPrivileged(localUrl.port)) {
return;
}
const forwarded = await this.remoteExplorerService.forward({ remote: localUrl, source: AutoTunnelSource }, attributes ?? null);
if (forwarded && (typeof forwarded !== 'string')) {
this.notifier.doAction([forwarded]);
}
}));
}
private stopUrlFinder() {
if (this.urlFinder) {
this.urlFinder.dispose();
this.urlFinder = undefined;
}
}
}
class ProcAutomaticPortForwarding extends Disposable {
private candidateListener: IDisposable | undefined;
private autoForwarded: Set<string> = new Set();
private notifiedOnly: Set<string> = new Set();
private notifier: OnAutoForwardedAction;
private initialCandidates: Set<string> = new Set();
private portsFeatures: IDisposable | undefined;
constructor(
private readonly unforwardOnly: boolean,
readonly alreadyAutoForwarded: Set<string> | undefined,
private readonly needsInitialCandidates: boolean,
private readonly configurationService: IConfigurationService,
readonly remoteExplorerService: IRemoteExplorerService,
readonly notificationService: INotificationService,
readonly openerService: IOpenerService,
readonly externalOpenerService: IExternalUriOpenerService,
readonly tunnelService: ITunnelService,
readonly hostService: IHostService,
readonly logService: ILogService,
readonly contextKeyService: IContextKeyService,
) {
super();
this.notifier = new OnAutoForwardedAction(notificationService, remoteExplorerService, openerService, externalOpenerService, tunnelService, hostService, logService, contextKeyService);
alreadyAutoForwarded?.forEach(port => this.autoForwarded.add(port));
this.initialize();
}
get forwarded(): Set<string> {
return this.autoForwarded;
}
private async initialize() {
if (!this.remoteExplorerService.tunnelModel.environmentTunnelsSet) {
await new Promise<void>(resolve => this.remoteExplorerService.tunnelModel.onEnvironmentTunnelsSet(() => resolve()));
}
this._register(this.configurationService.onDidChangeConfiguration(async (e) => {
if (e.affectsConfiguration(PORT_AUTO_FORWARD_SETTING)) {
await this.startStopCandidateListener();
}
}));
this.portsFeatures = this._register(this.remoteExplorerService.onEnabledPortsFeatures(async () => {
await this.startStopCandidateListener();
}));
this.startStopCandidateListener();
}
private async startStopCandidateListener() {
if (this.configurationService.getValue(PORT_AUTO_FORWARD_SETTING)) {
await this.startCandidateListener();
} else {
this.stopCandidateListener();
}
}
private stopCandidateListener() {
if (this.candidateListener) {
this.candidateListener.dispose();
this.candidateListener = undefined;
}
}
private async startCandidateListener() {
if (this.candidateListener || !this.remoteExplorerService.portsFeaturesEnabled) {
return;
}
this.portsFeatures?.dispose();
// Capture list of starting candidates so we don't auto forward them later.
await this.setInitialCandidates();
// Need to check the setting again, since it may have changed while we waited for the initial candidates to be set.
if (this.configurationService.getValue(PORT_AUTO_FORWARD_SETTING)) {
this.candidateListener = this._register(this.remoteExplorerService.tunnelModel.onCandidatesChanged(this.handleCandidateUpdate, this));
}
}
private async setInitialCandidates() {
if (!this.needsInitialCandidates) {
this.logService.debug(`ForwardedPorts: (ProcForwarding) Not setting initial candidates`);
return;
}
let startingCandidates = this.remoteExplorerService.tunnelModel.candidatesOrUndefined;
if (!startingCandidates) {
await new Promise<void>(resolve => this.remoteExplorerService.tunnelModel.onCandidatesChanged(() => resolve()));
startingCandidates = this.remoteExplorerService.tunnelModel.candidates;
}
for (const value of startingCandidates) {
this.initialCandidates.add(makeAddress(value.host, value.port));
}
this.logService.debug(`ForwardedPorts: (ProcForwarding) Initial candidates set to ${startingCandidates.map(candidate => candidate.port).join(', ')}`);
}
private async forwardCandidates(): Promise<RemoteTunnel[] | undefined> {
let attributes: Map<number, Attributes> | undefined;
const allTunnels: RemoteTunnel[] = [];
this.logService.trace(`ForwardedPorts: (ProcForwarding) Attempting to forward ${this.remoteExplorerService.tunnelModel.candidates.length} candidates`);
for (const value of this.remoteExplorerService.tunnelModel.candidates) {
if (!value.detail) {
this.logService.trace(`ForwardedPorts: (ProcForwarding) Port ${value.port} missing detail`);
continue;
}
if (!attributes) {
attributes = await this.remoteExplorerService.tunnelModel.getAttributes(this.remoteExplorerService.tunnelModel.candidates);
}
const portAttributes = attributes?.get(value.port);
const address = makeAddress(value.host, value.port);
if (this.initialCandidates.has(address) && (portAttributes?.onAutoForward === undefined)) {
continue;
}
if (this.notifiedOnly.has(address) || this.autoForwarded.has(address)) {
continue;
}
const alreadyForwarded = mapHasAddressLocalhostOrAllInterfaces(this.remoteExplorerService.tunnelModel.forwarded, value.host, value.port);
if (mapHasAddressLocalhostOrAllInterfaces(this.remoteExplorerService.tunnelModel.detected, value.host, value.port)) {
continue;
}
if (portAttributes?.onAutoForward === OnPortForward.Ignore) {
this.logService.trace(`ForwardedPorts: (ProcForwarding) Port ${value.port} is ignored`);
continue;
}
const forwarded = await this.remoteExplorerService.forward({ remote: value, source: AutoTunnelSource }, portAttributes ?? null);
if (!alreadyForwarded && forwarded) {
this.logService.trace(`ForwardedPorts: (ProcForwarding) Port ${value.port} has been forwarded`);
this.autoForwarded.add(address);
} else if (forwarded) {
this.logService.trace(`ForwardedPorts: (ProcForwarding) Port ${value.port} has been notified`);
this.notifiedOnly.add(address);
}
if (forwarded && (typeof forwarded !== 'string')) {
allTunnels.push(forwarded);
}
}
this.logService.trace(`ForwardedPorts: (ProcForwarding) Forwarded ${allTunnels.length} candidates`);
if (allTunnels.length === 0) {
return undefined;
}
return allTunnels;
}
private async handleCandidateUpdate(removed: Map<string, { host: string; port: number }>) {
const removedPorts: number[] = [];
let autoForwarded: Map<string, string | Tunnel>;
if (this.unforwardOnly) {
autoForwarded = new Map();
for (const entry of this.remoteExplorerService.tunnelModel.forwarded.entries()) {
if (entry[1].source.source === TunnelSource.Auto) {
autoForwarded.set(entry[0], entry[1]);
}
}
} else {
autoForwarded = new Map(this.autoForwarded.entries());
}
for (const removedPort of removed) {
const key = removedPort[0];
let value = removedPort[1];
const forwardedValue = mapHasAddressLocalhostOrAllInterfaces(autoForwarded, value.host, value.port);
if (forwardedValue) {
if (typeof forwardedValue === 'string') {
this.autoForwarded.delete(key);
} else {
value = { host: forwardedValue.remoteHost, port: forwardedValue.remotePort };
}
await this.remoteExplorerService.close(value, TunnelCloseReason.AutoForwardEnd);
removedPorts.push(value.port);
} else if (this.notifiedOnly.has(key)) {
this.notifiedOnly.delete(key);
removedPorts.push(value.port);
} else if (this.initialCandidates.has(key)) {
this.initialCandidates.delete(key);
}
}
if (this.unforwardOnly) {
return;
}
if (removedPorts.length > 0) {
await this.notifier.hide(removedPorts);
}
const tunnels = await this.forwardCandidates();
if (tunnels) {
await this.notifier.doAction(tunnels);
}
}
}