Skip to content

feat(terminal): forward loopback links clicked in SSH terminals - #3664

Merged
bobleer merged 2 commits into
openBitFun:mainfrom
bobleer:bob/remote-terminal-link-forward
Nov 2, 2026
Merged

bobleer merged 2 commits into
openBitFun:mainfrom
bobleer:bob/remote-terminal-link-forward

Conversation

@bobleer

@bobleer bobleer commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When you Ctrl/Cmd+click a loopback link (127.x.y.z, localhost, *.localhost, 0.0.0.0, [::1], with an explicit port) in a remote SSH terminal, the remote port is now forwarded and the forwarded local URL opens, the same way VS Code does it.

Fixes #2285

Type and Areas

Type: Feature

Areas: web UI (terminal, ssh-remote), locales

Motivation / Impact

Previously, clicking http://127.0.0.1:5173 printed by a dev server on an SSH host opened the local URL. You reached whatever was listening on your own machine, or nothing.

Now:

  • loopbackLink.ts (pure, no IO): detects loopback http(s) links. A trailing host dot such as localhost. is stripped. The link is rewritten to the forward's bound address (always 127.0.0.1 unless the forward binds elsewhere), keeping the path, query and hash. localhost is not kept, because the browser could try ::1 first and reach a local server on the same port.
  • openTerminalLink.ts, for a terminal with a connectionId:
    • WSL / local Docker targets (saved connection with wsl or container.local): the link opens unchanged and the backend is not called. WSL2 forwards localhost by default, and these targets have no SSH channel.
    • Reuse: an existing forward is reused only if its remote host matches exactly. The one exception is that localhost and 127.0.0.1 count as the same host, so a [::1] link never reuses a 127.0.0.1 forward and a 127.1.2.3 link finds its own forward.
    • Forward with an error: an idle forward that recorded lastError is stopped and started again, keeping its local port, LAN exposure and label. Starting again reconnects the SSH session if needed.
    • New forward: started through sshApi.startPortForward, asking for the same local port first; the backend picks another if it is taken. A toast shows the mapping, then the rewritten URL opens.
    • Lost race: if a concurrent click started the same forward first, the existing forward is reported as reused, so only one toast appears. If the fallback re-list also fails, the original start error is shown.
  • No confirmation prompt: the click is the explicit user action. The Port Forwarding dialog intro (en-US / zh-CN / zh-TW) now says forwards are created only from the dialog or by clicking a localhost link in a remote terminal.
  • Loud degradation: in each of these cases the local URL is never opened silently, and the warning has an "Open link anyway" action:
    • a forward fails;
    • Peer Device Mode is active (the forward would bind on the peer, so nothing is attempted);
    • a loopback link in an SSH terminal has no explicit port.
  • Local terminals and non-loopback links are unchanged. Terminal.tsx gets an optional onLinkActivate prop. All calls go through the existing API adapters, not Tauri directly.

Verification

  • pnpm --dir src/web-ui run test:run src/features/ssh-remote src/tools/terminal: 22 files, 218 tests passed.
    • loopbackLink.test.ts
    • openTerminalLink.test.ts: local passthrough, WSL/Docker passthrough, exact-host reuse, 127.x reuse, [::1] versus 127.0.0.1, restarting a forward with an error, create, failure without opening locally, lost race reported as reuse, original error kept when the re-list fails, port-less notice, peer mode.
    • Terminal.linkActivation.test.tsx: component-level check that onLinkActivate is called and the external fallback still works.
  • tsc --noEmit in src/web-ui: passed. The ts-rs src/generated/api types were copied from an existing local generation, not regenerated.
  • pnpm run i18n:audit: passed, 0 warnings.
  • ESLint on the changed source files: clean.

Remote scenarios: only unit and component tests with mocked APIs were run. There was no live SSH, WSL, Docker or Peer Device Mode run. Remote control (mobile/IM) has no terminal link surface and is not affected.

Reviewer Notes

  • Forwards created this way appear in the Port Forwarding dialog with the label "Terminal link" and live as long as manual forwards do.
  • WSL/Docker detection uses the saved connection profile. If the profile lookup fails, forwarding is attempted, and the backend's existing rejection is shown as a warning.
  • No styling changes.

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above, or skipped checks are explained.
  • User-facing strings, docs, and locales are updated where applicable.

Ctrl/Cmd+clicking an http(s) loopback link with an explicit port in a
remote SSH terminal now reuses or starts a local port forward for that
remote port and opens the rewritten local URL. Forward failures, local
Docker/WSL targets, and Peer Device Mode show an explicit warning with an
'open anyway' action instead of silently opening the local URL.

Fixes openBitFun#2285
- Open links unchanged for WSL and local Docker targets instead of
  calling the SSH forward backend.
- Always rewrite to the forward's bound address so localhost cannot
  resolve to a local IPv6 server.
- Match forwards by exact remote host (plus localhost/127.0.0.1), so
  127.x and ::1 links reuse or create the right forward.
- Treat a lost start race as reuse and keep the original start error
  when the fallback re-list fails.
- Warn on port-less loopback links and strip a trailing host dot.
- Restart an idle forward that recorded an error instead of reusing it.
- Add Terminal onLinkActivate wiring test; keep terminal.json formatting.
@bobleer
bobleer merged commit 616c3f2 into openBitFun:main Nov 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 关于远程服务器的网页服务映射

1 participant