Skip to content

Commit 37dde5b

Browse files
committed
feat: automatically forward ports in remote debugging
Fixes #373
1 parent 98e6e2d commit 37dde5b

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/build/generate-contributions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,11 @@ const configurationSchema: ConfigurationAttributes<IConfigurationTypes> = {
846846
description: refString('configuration.suggestPrettyPrinting'),
847847
default: true,
848848
},
849+
[Configuration.AutoServerTunnelOpen]: {
850+
type: 'boolean',
851+
description: refString('configuration.automaticallyTunnelRemoteServer'),
852+
default: true,
853+
},
849854
};
850855

851856
process.stdout.write(

src/build/strings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ const strings = {
198198
'configuration.usePreview': 'Use the new in-preview JavaScript debugger for Node.js and Chrome.',
199199
'configuration.suggestPrettyPrinting':
200200
'Whether to suggest pretty printing JavaScript code that looks minified when you step into it.',
201+
'configuration.automaticallyTunnelRemoteServer':
202+
'When debugging a remote web app, configures whether to automatically tunnel the remote server to your local machine.',
201203
};
202204

203205
export default strings;

src/common/contributionUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const enum Configuration {
5555
WarnOnLongPrediction = 'debug.javascript.warnOnLongPrediction',
5656
TerminalDebugConfig = 'debug.javascript.terminalOptions',
5757
SuggestPrettyPrinting = 'debug.javascript.suggestPrettyPrinting',
58+
AutoServerTunnelOpen = 'debug.javascript.automaticallyTunnelRemoteServer',
5859
}
5960

6061
/**
@@ -66,6 +67,7 @@ export interface IConfigurationTypes {
6667
[Configuration.WarnOnLongPrediction]: boolean;
6768
[Configuration.TerminalDebugConfig]: Partial<ITerminalLaunchConfiguration>;
6869
[Configuration.SuggestPrettyPrinting]: boolean;
70+
[Configuration.AutoServerTunnelOpen]: boolean;
6971
}
7072

7173
export interface ICommandTypes {

src/ui/companionBrowserLaunch.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,63 @@
44

55
import * as vscode from 'vscode';
66
import Dap from '../dap/api';
7+
import { readConfig, Configuration } from '../common/contributionUtils';
8+
import { URL } from 'url';
79

810
const sessionTunnels = new Map<string, vscode.Tunnel>();
911

12+
const tunnelRemoteServerIfNecessary = async (args: Dap.LaunchBrowserInCompanionEventParams) => {
13+
const urlStr = (args.params as { url?: string }).url;
14+
if (!urlStr) {
15+
return;
16+
}
17+
18+
let url: URL;
19+
try {
20+
url = new URL(urlStr);
21+
} catch (e) {
22+
return;
23+
}
24+
25+
if (!readConfig(vscode.workspace.getConfiguration(), Configuration.AutoServerTunnelOpen)) {
26+
return;
27+
}
28+
29+
const port = Number(url.port) || 80;
30+
if ((await vscode.workspace.tunnels).some(t => t.localAddress.endsWith(`:${port}`))) {
31+
return;
32+
}
33+
34+
try {
35+
await vscode.workspace.openTunnel({
36+
remoteAddress: { port, host: 'localhost' },
37+
localAddressPort: port,
38+
});
39+
} catch {
40+
// throws if already forwarded by user or by us previously
41+
}
42+
};
43+
1044
const launchCompanionBrowser = async (
1145
session: vscode.DebugSession,
1246
args: Dap.LaunchBrowserInCompanionEventParams,
1347
) => {
1448
try {
15-
const tunnel = await vscode.workspace.openTunnel({
16-
remoteAddress: { port: args.serverPort, host: 'localhost' },
17-
localAddressPort: args.serverPort,
18-
label: 'Browser Debug Tunnel',
19-
});
49+
const [, tunnel] = await Promise.all([
50+
tunnelRemoteServerIfNecessary(args),
51+
vscode.workspace.openTunnel({
52+
remoteAddress: { port: args.serverPort, host: 'localhost' },
53+
localAddressPort: args.serverPort,
54+
label: 'Browser Debug Tunnel',
55+
}),
56+
]);
2057

2158
sessionTunnels.set(session.id, tunnel);
2259

2360
await vscode.commands.executeCommand('js-debug-companion.launchAndAttach', {
2461
proxyUri: tunnel
2562
? `${tunnel.remoteAddress.host}:${tunnel.remoteAddress.port}`
26-
: `127.0.01:${args.serverPort}`,
63+
: `127.0.0.1:${args.serverPort}`,
2764
...args,
2865
});
2966
} catch (e) {

0 commit comments

Comments
 (0)