mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(remote): forward ports of a WSL project (#5140)
The Ports tool window existed only for SSH hosts and dev containers; a WSL project refused to forward. A WSL distribution shares this machine's loopback (as `forward_web_url` already assumes), so a port it listens on is reachable here on the same port without a tunnel. The route decision is now a pure `forward_route(target, host)` (SSH channel tunnel, container exec tunnel, or WSL's shared loopback; another host stays reachable from a dev container only), and `Forward::Wsl` reports the port unchanged and owns no resource. The Ports tool window registers for WSL sessions, remembered ports are scoped per distribution and folder (`wsl:<distro>:<root>`), the refusal and its string are gone, and the remote docs describe it. Co-authored-by: akbar1214 <43771058+akbar1214@users.noreply.github.com>
This commit is contained in:
@@ -16,13 +16,14 @@ import { closeConnectDialog, openConnectDialog } from "./model";
|
||||
import { restorePorts, usePorts } from "./ports";
|
||||
import { installReconnect, reconnectNow } from "./reconnect";
|
||||
|
||||
/** Forwarded ports are available while this window is connected to an SSH host or a dev container. */
|
||||
/** Forwarded ports are available while this window is connected to an SSH host, a WSL distribution or a dev container. */
|
||||
function trackPortsWindow(): () => void {
|
||||
let off: (() => void) | null = null;
|
||||
let session = useRemote.getState().session;
|
||||
const sync = () => {
|
||||
const next = useRemote.getState().session;
|
||||
const connected = next?.target.kind === "ssh" || next?.target.kind === "devContainer";
|
||||
const connected =
|
||||
next?.target.kind === "ssh" || next?.target.kind === "wsl" || next?.target.kind === "devContainer";
|
||||
if (connected && (next !== session || !off)) {
|
||||
usePorts.setState({ ports: [] });
|
||||
void restorePorts();
|
||||
|
||||
@@ -138,14 +138,53 @@ it("forwards and remembers ports of a dev container, per folder and configuratio
|
||||
useRemote.setState({ session: null });
|
||||
});
|
||||
|
||||
it("doesn't forward ports of a WSL project yet", async () => {
|
||||
it("forwards and remembers ports of a WSL distribution, per distro and folder", async () => {
|
||||
const { useRemote } = await import("@/platform/remote");
|
||||
ipc.remoteForwardPort.mockClear();
|
||||
useRemote.setState({
|
||||
session: { target: { kind: "wsl", distro: "u" }, root: "/home/u", host: "u", os: "linux", commands: [] },
|
||||
const { forwardPort, savedPorts } = await import("./ports");
|
||||
const wsl = (distro: string, root: string) => ({
|
||||
target: { kind: "wsl" as const, distro },
|
||||
root,
|
||||
host: distro,
|
||||
os: "linux",
|
||||
commands: [],
|
||||
});
|
||||
expect(await forwardPort(8080)).toBeNull();
|
||||
expect(ipc.remoteForwardPort).not.toHaveBeenCalled();
|
||||
ipc.remoteForwardPort.mockClear();
|
||||
useRemote.setState({ session: wsl("Ubuntu", "/home/u/app") });
|
||||
expect(await forwardPort(8080)).toEqual({ remotePort: 8080, localPort: 8080, host: null });
|
||||
expect(ipc.remoteForwardPort).toHaveBeenCalledWith(8080);
|
||||
expect(savedPorts()).toEqual([8080]);
|
||||
useRemote.setState({ session: wsl("Ubuntu", "/home/u/other") });
|
||||
expect(savedPorts()).toEqual([]);
|
||||
useRemote.setState({ session: wsl("Debian", "/home/u/app") });
|
||||
expect(savedPorts()).toEqual([]);
|
||||
// An SSH configuration of the same name has its own list.
|
||||
useRemote.setState({
|
||||
session: { target: { kind: "ssh", config: "Ubuntu" }, root: "/home/u/app", host: "h", os: "linux", commands: [] },
|
||||
});
|
||||
expect(savedPorts()).toEqual([]);
|
||||
useRemote.setState({ session: null });
|
||||
});
|
||||
|
||||
it("forwards the ports saved for a WSL distribution again after reconnecting", async () => {
|
||||
const { useRemote } = await import("@/platform/remote");
|
||||
const { forwardPort, restorePorts, savedPorts } = await import("./ports");
|
||||
useRemote.setState({
|
||||
session: {
|
||||
target: { kind: "wsl", distro: "Ubuntu" },
|
||||
root: "/home/u/app",
|
||||
host: "Ubuntu",
|
||||
os: "linux",
|
||||
commands: [],
|
||||
},
|
||||
});
|
||||
await forwardPort(3000);
|
||||
expect(savedPorts()).toEqual([3000]);
|
||||
// Reconnected: the new connection forwards nothing yet.
|
||||
forwarded.length = 0;
|
||||
ipc.remoteForwardPort.mockClear();
|
||||
await restorePorts();
|
||||
expect(ipc.remoteForwardPort).toHaveBeenCalledWith(3000);
|
||||
expect(usePorts.getState().ports).toEqual([{ remotePort: 3000, localPort: 53000, host: null }]);
|
||||
useRemote.setState({ session: null });
|
||||
});
|
||||
|
||||
@@ -199,16 +238,40 @@ it("does not display a previous session's delayed port response", async () => {
|
||||
useRemote.setState({ session: null });
|
||||
});
|
||||
|
||||
it("reloads ports when switching directly between connected sessions", async () => {
|
||||
it.each([
|
||||
[
|
||||
"SSH",
|
||||
(folder: string) => ({
|
||||
target: { kind: "ssh" as const, config: folder },
|
||||
root: "/srv/app",
|
||||
host: "h",
|
||||
os: "linux",
|
||||
commands: [],
|
||||
}),
|
||||
],
|
||||
[
|
||||
"WSL",
|
||||
(folder: string) => ({
|
||||
target: { kind: "wsl" as const, distro: folder },
|
||||
root: "/home/u/app",
|
||||
host: folder,
|
||||
os: "linux",
|
||||
commands: [],
|
||||
}),
|
||||
],
|
||||
[
|
||||
"dev container",
|
||||
(folder: string) => ({
|
||||
target: { kind: "devContainer" as const, folder, config: "" },
|
||||
root: "/workspaces/app",
|
||||
host: "app",
|
||||
os: "linux",
|
||||
commands: [],
|
||||
}),
|
||||
],
|
||||
])("reloads ports on connect and when switching between connected %s sessions", async (_kind, session) => {
|
||||
const { useRemote } = await import("@/platform/remote");
|
||||
const { activate } = await import("./index");
|
||||
const session = (folder: string) => ({
|
||||
target: { kind: "devContainer" as const, folder, config: "" },
|
||||
root: "/workspaces/app",
|
||||
host: "app",
|
||||
os: "linux",
|
||||
commands: [],
|
||||
});
|
||||
useRemote.setState({ session: session("/first") });
|
||||
forwarded.push({ remotePort: 8000, localPort: 8000, host: null });
|
||||
const dispose = activate();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Remote ports forwarded to this machine, remembered per host (or dev container) and folder and
|
||||
* forwarded again on reconnect. A dev container also forwards its `forwardPorts` on its own.
|
||||
* Remote ports forwarded to this machine, remembered per SSH host, WSL distribution or dev
|
||||
* container and folder, and forwarded again on reconnect. A dev container also forwards its
|
||||
* `forwardPorts` on its own.
|
||||
*/
|
||||
import { create } from "zustand";
|
||||
import type { ForwardedPort } from "@/bindings";
|
||||
@@ -16,6 +17,8 @@ function savedKey(): string | null {
|
||||
switch (session?.target.kind) {
|
||||
case "ssh":
|
||||
return `${SAVED_KEY}${session.target.config}:${session.root}`;
|
||||
case "wsl":
|
||||
return `${SAVED_KEY}wsl:${session.target.distro}:${session.root}`;
|
||||
case "devContainer":
|
||||
return `${SAVED_KEY}devcontainer:${JSON.stringify([session.target.folder, session.target.config])}`;
|
||||
default:
|
||||
@@ -67,14 +70,6 @@ export function parsePort(text: string): number | null {
|
||||
}
|
||||
|
||||
export async function forwardPort(port: number): Promise<ForwardedPort | null> {
|
||||
if (useRemote.getState().session?.target.kind === "wsl") {
|
||||
notifications().notify({
|
||||
group: "remote",
|
||||
level: "error",
|
||||
title: tx("Forwarding ports is not supported in a WSL project yet"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const forwarded = await call(commands.remoteForwardPort(port));
|
||||
await refreshPorts(true);
|
||||
|
||||
Reference in New Issue
Block a user