Skip to content

Commit e779917

Browse files
committed
Break out port forward URL open to util
Felt like this could be broken out to reduce the component size. Also trying to figure out why it is causing the terminal to create multiple times.
1 parent a38737b commit e779917

File tree

2 files changed

+58
-44
lines changed

2 files changed

+58
-44
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { useDashboard } from "components/Dashboard/DashboardProvider";
1919
import { Region } from "api/typesGenerated";
2020
import { getLatencyColor } from "utils/latency";
2121
import { ProxyStatusLatency } from "components/ProxyStatusLatency/ProxyStatusLatency";
22-
import { portForwardURL } from "utils/portForward";
22+
import { openMaybePortForwardedURL } from "utils/portForward";
2323
import { terminalWebsocketUrl } from "utils/terminal";
2424
import { getMatchingAgentOrFirst } from "utils/workspace";
2525
import {
@@ -89,49 +89,13 @@ const TerminalPage: FC = () => {
8989
// handleWebLink handles opening of URLs in the terminal!
9090
const handleWebLink = useCallback(
9191
(uri: string) => {
92-
if (
93-
!workspaceAgent ||
94-
!workspace.data ||
95-
!username ||
96-
!proxy.preferredWildcardHostname
97-
) {
98-
return;
99-
}
100-
101-
const open = (uri: string) => {
102-
// Copied from: https://github.com/xtermjs/xterm.js/blob/master/addons/xterm-addon-web-links/src/WebLinksAddon.ts#L23
103-
const newWindow = window.open();
104-
if (newWindow) {
105-
try {
106-
newWindow.opener = null;
107-
} catch {
108-
// no-op, Electron can throw
109-
}
110-
newWindow.location.href = uri;
111-
} else {
112-
console.warn("Opening link blocked as opener could not be cleared");
113-
}
114-
};
115-
116-
try {
117-
const url = new URL(uri);
118-
const localHosts = ["0.0.0.0", "127.0.0.1", "localhost"];
119-
if (!localHosts.includes(url.hostname)) {
120-
open(uri);
121-
return;
122-
}
123-
open(
124-
portForwardURL(
125-
proxy.preferredWildcardHostname,
126-
parseInt(url.port),
127-
workspaceAgent.name,
128-
workspace.data.name,
129-
username,
130-
) + url.pathname,
131-
);
132-
} catch (ex) {
133-
open(uri);
134-
}
92+
openMaybePortForwardedURL(
93+
uri,
94+
proxy.preferredWildcardHostname,
95+
workspaceAgent?.name,
96+
workspace.data?.name,
97+
username,
98+
);
13599
},
136100
[workspaceAgent, workspace.data, username, proxy.preferredWildcardHostname],
137101
);

site/src/utils/portForward.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,53 @@ export const portForwardURL = (
1212
}--${agentName}--${workspaceName}--${username}`;
1313
return `${location.protocol}//${host}`.replace("*", subdomain);
1414
};
15+
16+
// openMaybePortForwardedURL tries to open the provided URI through the
17+
// port-forwarded URL if it is localhost, otherwise opens it normally.
18+
export const openMaybePortForwardedURL = (
19+
uri: string,
20+
proxyHost?: string,
21+
agentName?: string,
22+
workspaceName?: string,
23+
username?: string,
24+
) => {
25+
const open = (uri: string) => {
26+
// Copied from: https://github.com/xtermjs/xterm.js/blob/master/addons/xterm-addon-web-links/src/WebLinksAddon.ts#L23
27+
const newWindow = window.open();
28+
if (newWindow) {
29+
try {
30+
newWindow.opener = null;
31+
} catch {
32+
// no-op, Electron can throw
33+
}
34+
newWindow.location.href = uri;
35+
} else {
36+
console.warn("Opening link blocked as opener could not be cleared");
37+
}
38+
};
39+
40+
if (!agentName || !workspaceName || !username || !proxyHost) {
41+
open(uri);
42+
return;
43+
}
44+
45+
try {
46+
const url = new URL(uri);
47+
const localHosts = ["0.0.0.0", "127.0.0.1", "localhost"];
48+
if (!localHosts.includes(url.hostname)) {
49+
open(uri);
50+
return;
51+
}
52+
open(
53+
portForwardURL(
54+
proxyHost,
55+
parseInt(url.port),
56+
agentName,
57+
workspaceName,
58+
username,
59+
) + url.pathname,
60+
);
61+
} catch (ex) {
62+
open(uri);
63+
}
64+
};

0 commit comments

Comments
 (0)