-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(desktop,host-service): forward remote workspace ports to the local machine #6858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ymirke
wants to merge
6
commits into
superset-sh:main
Choose a base branch
from
Ymirke:feat/remote-port-forwarding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17a81dc
feat(host-service): add /tcp/:port WebSocket bridge for port forwarding
Ymirke c89686e
feat(desktop): forward remote workspace ports over the relay
Ymirke 188839a
feat(desktop): auto-forward the selected remote workspace's ports
Ymirke 3b31008
feat(desktop,host-service): multiplex all of a workspace's forwards o…
saddlepaddle cca152f
Merge remote-tracking branch 'origin/main' into feat/remote-port-forw…
saddlepaddle e1291dd
fix(host-service): dial the scanner's detected address, not 127.0.0.1
saddlepaddle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { createPortForwardsRouter } from "./port-forwards"; |
48 changes: 48 additions & 0 deletions
48
apps/desktop/src/lib/trpc/routers/port-forwards/port-forwards.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { observable } from "@trpc/server/observable"; | ||
| import { portForwardManager, setRelayToken } from "main/lib/port-forward"; | ||
| import type { PortForward } from "shared/types"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../.."; | ||
|
|
||
| export const createPortForwardsRouter = () => { | ||
| return router({ | ||
| setRelayToken: publicProcedure | ||
| .input(z.object({ token: z.string().nullable() })) | ||
| .mutation(({ input }) => { | ||
| setRelayToken(input.token); | ||
| }), | ||
|
|
||
| list: publicProcedure.query((): PortForward[] => portForwardManager.list()), | ||
|
|
||
| subscribe: publicProcedure.subscription(() => { | ||
| return observable<PortForward[]>((emit) => { | ||
| const onChange = (forwards: PortForward[]) => emit.next(forwards); | ||
| portForwardManager.on("change", onChange); | ||
| emit.next(portForwardManager.list()); | ||
| return () => { | ||
| portForwardManager.off("change", onChange); | ||
| }; | ||
| }); | ||
| }), | ||
|
|
||
| sync: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| hostUrl: z.string(), | ||
| workspaceId: z.string(), | ||
| ports: z.array(z.number().int().positive()), | ||
| }), | ||
| ) | ||
| .mutation( | ||
| ({ input }): Promise<PortForward[]> => portForwardManager.sync(input), | ||
| ), | ||
|
|
||
| retryEphemeral: publicProcedure | ||
| .input(z.object({ id: z.string() })) | ||
| .mutation(({ input }) => portForwardManager.retryEphemeral(input.id)), | ||
|
|
||
| killLocalOwner: publicProcedure | ||
| .input(z.object({ id: z.string() })) | ||
| .mutation(({ input }) => portForwardManager.killLocalOwner(input.id)), | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { canBindPort } from "../host-service-utils"; | ||
| import { portManager } from "../terminal/port-manager"; | ||
| import { PortForwardManager } from "./port-forward-manager"; | ||
| import { RelayForwardTransport } from "./relay-forward-transport"; | ||
|
|
||
| export { PortForwardManager } from "./port-forward-manager"; | ||
| export { RelayForwardTransport } from "./relay-forward-transport"; | ||
| export type { ForwardTransport } from "./types"; | ||
|
|
||
| let relayToken: string | null = null; | ||
|
|
||
| export function setRelayToken(token: string | null): void { | ||
| relayToken = token; | ||
| } | ||
|
|
||
| export const portForwardManager = new PortForwardManager({ | ||
| transport: new RelayForwardTransport({ getToken: () => relayToken }), | ||
| getLocalPorts: () => portManager.getAllPorts(), | ||
| killLocalPort: ({ terminalId, workspaceId, port }) => | ||
| portManager.killPort({ terminalId, workspaceId, port }), | ||
| canBindPort, | ||
| }); |
233 changes: 233 additions & 0 deletions
233
apps/desktop/src/main/lib/port-forward/port-forward-manager.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import net from "node:net"; | ||
| import { type Duplex, PassThrough } from "node:stream"; | ||
| import type { DetectedPort } from "@superset/port-scanner"; | ||
| import type { ForwardTarget } from "shared/types"; | ||
| import { PortForwardManager } from "./port-forward-manager"; | ||
| import type { ForwardTransport } from "./types"; | ||
|
|
||
| const HOST = "https://relay.test/hosts/org:machine"; | ||
|
|
||
| async function startEcho(): Promise<{ port: number; close: () => void }> { | ||
| const server = net.createServer((s) => s.pipe(s)); | ||
| const port = await new Promise<number>((resolve) => | ||
| server.listen(0, "127.0.0.1", () => | ||
| resolve((server.address() as net.AddressInfo).port), | ||
| ), | ||
| ); | ||
| return { port, close: () => server.close() }; | ||
| } | ||
|
|
||
| async function freePort(): Promise<number> { | ||
| const server = net.createServer(); | ||
| return new Promise((resolve) => | ||
| server.listen(0, "127.0.0.1", () => { | ||
| const { port } = server.address() as net.AddressInfo; | ||
| server.close(() => resolve(port)); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** A transport that opens a TCP connection to an echo server on this machine. */ | ||
| function echoTransport(echoPort: number): ForwardTransport & { | ||
| opened: ForwardTarget[]; | ||
| } { | ||
| const opened: ForwardTarget[] = []; | ||
| return { | ||
| kind: "relay", | ||
| opened, | ||
| probe: async () => {}, | ||
| openStream: async (target) => { | ||
| opened.push(target); | ||
| const socket = net.connect({ host: "127.0.0.1", port: echoPort }); | ||
| await new Promise<void>((r) => socket.once("connect", () => r())); | ||
| return socket as Duplex; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function manager( | ||
| transport: ForwardTransport, | ||
| overrides: Partial<ConstructorParameters<typeof PortForwardManager>[0]> = {}, | ||
| ) { | ||
| return new PortForwardManager({ | ||
| transport, | ||
| getLocalPorts: () => [], | ||
| killLocalPort: async () => ({ success: true }), | ||
| canBindPort: async () => true, | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| async function roundTrip(port: number, payload: string): Promise<string> { | ||
| const socket = net.connect({ host: "127.0.0.1", port }); | ||
| await new Promise<void>((r) => socket.once("connect", () => r())); | ||
| socket.write(payload); | ||
| const data = await new Promise<Buffer>((r) => socket.once("data", r)); | ||
| socket.destroy(); | ||
| return data.toString(); | ||
| } | ||
|
|
||
| describe("PortForwardManager", () => { | ||
| test("sync listens on the requested port and bridges bytes", async () => { | ||
| const echo = await startEcho(); | ||
| const transport = echoTransport(echo.port); | ||
| const m = manager(transport); | ||
| const local = await freePort(); | ||
| const [fwd] = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| ports: [local], | ||
| }); | ||
| expect(fwd?.status).toEqual({ state: "active", localPort: local }); | ||
| expect(await roundTrip(local, "ping")).toBe("ping"); | ||
| expect(transport.opened[0]).toEqual({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| remotePort: local, | ||
| }); | ||
| m.stopAll(); | ||
| echo.close(); | ||
| }); | ||
|
|
||
| test("sync for another workspace stops the previous forwards", async () => { | ||
| const echo = await startEcho(); | ||
| const m = manager(echoTransport(echo.port)); | ||
| const a = await freePort(); | ||
| await m.sync({ hostUrl: HOST, workspaceId: "ws1", ports: [a] }); | ||
| const b = await freePort(); | ||
| const list = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws2", | ||
| ports: [b], | ||
| }); | ||
| expect(list.map((f) => f.target.workspaceId)).toEqual(["ws2"]); | ||
| expect(await freeToBind(a)).toBe(true); | ||
| m.stopAll(); | ||
| echo.close(); | ||
| }); | ||
|
|
||
| test("a busy local port reports busy with the owner when known", async () => { | ||
| const echo = await startEcho(); | ||
| const owner: DetectedPort = { | ||
| port: echo.port, | ||
| pid: 42, | ||
| processName: "node", | ||
| terminalId: "t1", | ||
| workspaceId: "local-ws", | ||
| detectedAt: 0, | ||
| address: "127.0.0.1", | ||
| }; | ||
| const m = manager(echoTransport(echo.port), { | ||
| getLocalPorts: () => [owner], | ||
| }); | ||
| const [fwd] = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| ports: [echo.port], | ||
| }); | ||
| expect(fwd?.status).toEqual({ | ||
| state: "busy", | ||
| localPort: echo.port, | ||
| localOwner: { | ||
| pid: 42, | ||
| processName: "node", | ||
| terminalId: "t1", | ||
| workspaceId: "local-ws", | ||
| }, | ||
| }); | ||
| m.stopAll(); | ||
| echo.close(); | ||
| }); | ||
|
|
||
| test("retryEphemeral moves a busy forward to another local port", async () => { | ||
| const echo = await startEcho(); | ||
| const m = manager(echoTransport(echo.port)); | ||
| const [fwd] = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| ports: [echo.port], | ||
| }); | ||
| expect(fwd?.status.state).toBe("busy"); | ||
| const retried = await m.retryEphemeral(fwd?.id ?? ""); | ||
| expect(retried?.status.state).toBe("active"); | ||
| if (retried?.status.state !== "active") throw new Error("not active"); | ||
| expect(retried.status.localPort).not.toBe(echo.port); | ||
| expect(await roundTrip(retried.status.localPort, "x")).toBe("x"); | ||
| m.stopAll(); | ||
| echo.close(); | ||
| }); | ||
|
|
||
| test("killLocalOwner restarts the forward once the port frees", async () => { | ||
| const echo = await startEcho(); | ||
| let killed = false; | ||
| const m = manager(echoTransport(echo.port), { | ||
| getLocalPorts: () => [ | ||
| { | ||
| port: echo.port, | ||
| pid: 1, | ||
| processName: "node", | ||
| terminalId: "t1", | ||
| workspaceId: "local-ws", | ||
| detectedAt: 0, | ||
| address: "127.0.0.1", | ||
| }, | ||
| ], | ||
| killLocalPort: async () => { | ||
| killed = true; | ||
| echo.close(); | ||
| return { success: true }; | ||
| }, | ||
| canBindPort: freeToBind, | ||
| }); | ||
| const [fwd] = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| ports: [echo.port], | ||
| }); | ||
| const result = await m.killLocalOwner(fwd?.id ?? ""); | ||
| expect(killed).toBe(true); | ||
| expect(result).toEqual({ success: true }); | ||
| expect(m.list()[0]?.status).toEqual({ | ||
| state: "active", | ||
| localPort: echo.port, | ||
| }); | ||
| m.stopAll(); | ||
| }); | ||
|
|
||
| test("a failed probe yields an error status", async () => { | ||
| const m = manager({ | ||
| kind: "relay", | ||
| probe: async () => { | ||
| throw new Error("protocol v1"); | ||
| }, | ||
| openStream: async () => new PassThrough(), | ||
| }); | ||
| const [fwd] = await m.sync({ | ||
| hostUrl: HOST, | ||
| workspaceId: "ws1", | ||
| ports: [await freePort()], | ||
| }); | ||
| expect(fwd?.status).toEqual({ state: "error", message: "protocol v1" }); | ||
| m.stopAll(); | ||
| }); | ||
|
|
||
| test("stopAll releases every listener", async () => { | ||
| const echo = await startEcho(); | ||
| const m = manager(echoTransport(echo.port)); | ||
| const local = await freePort(); | ||
| await m.sync({ hostUrl: HOST, workspaceId: "ws1", ports: [local] }); | ||
| m.stopAll(); | ||
| expect(m.list()).toEqual([]); | ||
| expect(await freeToBind(local)).toBe(true); | ||
| echo.close(); | ||
| }); | ||
| }); | ||
|
|
||
| function freeToBind(port: number): Promise<boolean> { | ||
| return new Promise((resolve) => { | ||
| const server = net.createServer(); | ||
| server.once("error", () => resolve(false)); | ||
| server.listen(port, "127.0.0.1", () => server.close(() => resolve(true))); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 18276
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 13163
Accumulate the complete TCP response before asserting its contents.
roundTripresolves on the first"data"event, whilePortForwardManagerforwards a TCP byte stream. TCP can split the echo response across chunks, so assertions can receive partial data and fail intermittently. Accumulate chunks until the expected byte length is received.🤖 Prompt for AI Agents