diff --git a/apps/desktop/src/clientPersistence.test.ts b/apps/desktop/src/clientPersistence.test.ts index 192d7ac106..41994550ae 100644 --- a/apps/desktop/src/clientPersistence.test.ts +++ b/apps/desktop/src/clientPersistence.test.ts @@ -70,6 +70,12 @@ const savedRegistryRecord: PersistedSavedEnvironmentRecord = { wsBaseUrl: "wss://remote.example.com/", createdAt: "2026-04-09T00:00:00.000Z", lastConnectedAt: "2026-04-09T01:00:00.000Z", + desktopSsh: { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 22, + }, }; describe("clientPersistence", () => { diff --git a/apps/desktop/src/clientPersistence.ts b/apps/desktop/src/clientPersistence.ts index ad08a0036f..09a3494dbc 100644 --- a/apps/desktop/src/clientPersistence.ts +++ b/apps/desktop/src/clientPersistence.ts @@ -57,6 +57,12 @@ function isPersistedSavedEnvironmentStorageRecord( typeof value.wsBaseUrl === "string" && typeof value.createdAt === "string" && (value.lastConnectedAt === null || typeof value.lastConnectedAt === "string") && + (value.desktopSsh === undefined || + (Predicate.isObject(value.desktopSsh) && + typeof value.desktopSsh.alias === "string" && + typeof value.desktopSsh.hostname === "string" && + (value.desktopSsh.username === null || typeof value.desktopSsh.username === "string") && + (value.desktopSsh.port === null || typeof value.desktopSsh.port === "number"))) && (value.encryptedBearerToken === undefined || typeof value.encryptedBearerToken === "string") ); } @@ -77,7 +83,7 @@ function readSavedEnvironmentRegistryDocument(filePath: string): SavedEnvironmen function toPersistedSavedEnvironmentRecord( record: PersistedSavedEnvironmentStorageRecord, ): PersistedSavedEnvironmentRecord { - return { + const nextRecord = { environmentId: record.environmentId, label: record.label, httpBaseUrl: record.httpBaseUrl, @@ -85,6 +91,7 @@ function toPersistedSavedEnvironmentRecord( createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, }; + return record.desktopSsh ? { ...nextRecord, desktopSsh: record.desktopSsh } : nextRecord; } export function readClientSettings(settingsPath: string): ClientSettings | null { @@ -134,6 +141,7 @@ export function writeSavedEnvironmentRegistry( wsBaseUrl: record.wsBaseUrl, createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, + ...(record.desktopSsh ? { desktopSsh: record.desktopSsh } : {}), encryptedBearerToken, } : record; @@ -189,7 +197,7 @@ export function writeSavedEnvironmentSecret(input: { const encryptedBearerToken = input.secretStorage .encryptString(input.secret) .toString("base64"); - return { + const nextRecord = { environmentId: record.environmentId, label: record.label, httpBaseUrl: record.httpBaseUrl, @@ -197,7 +205,8 @@ export function writeSavedEnvironmentSecret(input: { createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, encryptedBearerToken, - } satisfies PersistedSavedEnvironmentStorageRecord; + }; + return record.desktopSsh ? { ...nextRecord, desktopSsh: record.desktopSsh } : nextRecord; }), } satisfies SavedEnvironmentRegistryDocument); return found; diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 9d4888b581..30e811ed74 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -20,7 +20,13 @@ import { } from "electron"; import type { MenuItemConstructorOptions, OpenDialogOptions } from "electron"; import type { + AuthBearerBootstrapResult, + AuthSessionState, + AuthWebSocketTokenResult, ClientSettings, + DesktopSshPasswordPromptRequest, + ExecutionEnvironmentDescriptor, + DesktopSshEnvironmentTarget, DesktopTheme, DesktopAppBranding, DesktopServerExposureMode, @@ -59,6 +65,7 @@ import { resolveDesktopCoreAdvertisedEndpoints, resolveDesktopServerExposure, } from "./serverExposure.ts"; +import { DesktopSshEnvironmentManager } from "./sshEnvironment.ts"; import { syncShellEnvironment } from "./syncShellEnvironment.ts"; import { waitForBackendStartupReady } from "./backendStartupReadiness.ts"; import { getAutoUpdateDisabledReason, shouldBroadcastDownloadProgress } from "./updateState.ts"; @@ -104,6 +111,14 @@ const SET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL = "desktop:set-saved-environment-re const GET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:get-saved-environment-secret"; const SET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:set-saved-environment-secret"; const REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:remove-saved-environment-secret"; +const DISCOVER_SSH_HOSTS_CHANNEL = "desktop:discover-ssh-hosts"; +const ENSURE_SSH_ENVIRONMENT_CHANNEL = "desktop:ensure-ssh-environment"; +const FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL = "desktop:fetch-ssh-environment-descriptor"; +const BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL = "desktop:bootstrap-ssh-bearer-session"; +const FETCH_SSH_SESSION_STATE_CHANNEL = "desktop:fetch-ssh-session-state"; +const ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL = "desktop:issue-ssh-websocket-token"; +const SSH_PASSWORD_PROMPT_CHANNEL = "desktop:ssh-password-prompt"; +const RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL = "desktop:resolve-ssh-password-prompt"; const GET_SERVER_EXPOSURE_STATE_CHANNEL = "desktop:get-server-exposure-state"; const SET_SERVER_EXPOSURE_MODE_CHANNEL = "desktop:set-server-exposure-mode"; const GET_ADVERTISED_ENDPOINTS_CHANNEL = "desktop:get-advertised-endpoints"; @@ -162,6 +177,7 @@ function resolvePickFolderDefaultPath(rawOptions: unknown): string | undefined { } const DESKTOP_LOOPBACK_HOST = "127.0.0.1"; const DESKTOP_REQUIRED_PORT_PROBE_HOSTS = ["0.0.0.0", "::"] as const; +const SSH_PASSWORD_PROMPT_TIMEOUT_MS = 3 * 60 * 1000; const TITLEBAR_HEIGHT = 40; const TITLEBAR_COLOR = "#01000000"; // #00000000 does not work correctly on Linux const TITLEBAR_LIGHT_SYMBOL_COLOR = "#1f2937"; @@ -206,6 +222,12 @@ type LinuxDesktopNamedApp = Electron.App & { setDesktopName?: (desktopName: string) => void; }; +interface PendingSshPasswordPrompt { + readonly resolve: (password: string | null) => void; + readonly reject: (error: Error) => void; + readonly timeout: ReturnType; +} + let mainWindow: BrowserWindow | null = null; let backendProcess: ChildProcess.ChildProcess | null = null; let backendPort = 0; @@ -229,6 +251,7 @@ let restoreStdIoCapture: (() => void) | null = null; let backendObservabilitySettings = readPersistedBackendObservabilitySettings(); let desktopSettings = readDesktopSettings(DESKTOP_SETTINGS_PATH, app.getVersion()); let desktopServerExposureMode: DesktopServerExposureMode = desktopSettings.serverExposureMode; +const pendingSshPasswordPrompts = new Map(); let destructiveMenuIconCache: Electron.NativeImage | null | undefined; const expectedBackendExitChildren = new WeakSet(); @@ -405,6 +428,7 @@ function relaunchDesktopApp(reason: string): void { `desktop relaunch backend shutdown warning message=${formatErrorMessage(error)}`, ); }) + .then(() => desktopSshEnvironmentManager.dispose().catch(() => undefined)) .finally(() => { restoreStdIoCapture?.(); if (isDevelopment) { @@ -467,6 +491,128 @@ function getSafeTheme(rawTheme: unknown): DesktopTheme | null { return null; } +function getSafeDesktopSshTarget(rawTarget: unknown): DesktopSshEnvironmentTarget | null { + if (typeof rawTarget !== "object" || rawTarget === null) { + return null; + } + + const target = rawTarget as Partial; + if (typeof target.alias !== "string" || typeof target.hostname !== "string") { + return null; + } + if ( + target.username !== null && + target.username !== undefined && + typeof target.username !== "string" + ) { + return null; + } + if (target.port !== null && target.port !== undefined && !Number.isInteger(target.port)) { + return null; + } + + return { + alias: target.alias.trim(), + hostname: target.hostname.trim(), + username: target.username?.trim() || null, + port: target.port ?? null, + }; +} + +function isLoopbackHostname(hostname: string): boolean { + const normalized = hostname + .trim() + .toLowerCase() + .replace(/^\[(.*)\]$/, "$1"); + return normalized === "127.0.0.1" || normalized === "::1" || normalized === "localhost"; +} + +function resolveLoopbackSshHttpUrl(rawHttpBaseUrl: unknown, pathname: string): URL { + if (typeof rawHttpBaseUrl !== "string" || rawHttpBaseUrl.trim().length === 0) { + throw new Error("Invalid SSH forwarded http base URL."); + } + + let baseUrl: URL; + try { + baseUrl = new URL(rawHttpBaseUrl); + } catch { + throw new Error("Invalid SSH forwarded http base URL."); + } + + if (!isLoopbackHostname(baseUrl.hostname)) { + throw new Error("SSH desktop bridge only supports loopback forwarded URLs."); + } + + const url = new URL(baseUrl.toString()); + url.pathname = pathname; + url.search = ""; + url.hash = ""; + return url; +} + +async function readRemoteFetchErrorMessage( + response: Response, + fallbackMessage: string, +): Promise { + const text = await response.text(); + if (!text) { + return fallbackMessage; + } + + try { + const parsed = JSON.parse(text) as { readonly error?: string }; + if (typeof parsed.error === "string" && parsed.error.trim().length > 0) { + return parsed.error; + } + } catch { + // Fall back to the raw text below. + } + + return text; +} + +async function fetchLoopbackSshJson(input: { + readonly httpBaseUrl: unknown; + readonly pathname: string; + readonly method?: "GET" | "POST"; + readonly bearerToken?: unknown; + readonly body?: unknown; +}): Promise { + const requestUrl = resolveLoopbackSshHttpUrl(input.httpBaseUrl, input.pathname).toString(); + const bearerToken = + typeof input.bearerToken === "string" && input.bearerToken.trim().length > 0 + ? input.bearerToken + : null; + + let response: Response; + try { + response = await fetch(requestUrl, { + method: input.method ?? "GET", + headers: { + ...(input.body !== undefined ? { "content-type": "application/json" } : {}), + ...(bearerToken ? { authorization: `Bearer ${bearerToken}` } : {}), + }, + ...(input.body !== undefined ? { body: JSON.stringify(input.body) } : {}), + }); + } catch (error) { + throw new Error( + `Failed to reach SSH forwarded endpoint ${requestUrl} (${error instanceof Error ? error.message : String(error)}).`, + { cause: error }, + ); + } + + if (!response.ok) { + throw new Error( + await readRemoteFetchErrorMessage( + response, + `SSH forwarded request failed (${response.status}).`, + ), + ); + } + + return (await response.json()) as T; +} + async function waitForBackendHttpReady( baseUrl: string, options?: Parameters[1], @@ -661,6 +807,56 @@ let updateInstallInFlight = false; let updaterConfigured = false; let updateState: DesktopUpdateState = initialUpdateState(); +function rejectPendingSshPasswordPrompts(message: string): void { + for (const [requestId, pending] of pendingSshPasswordPrompts) { + clearTimeout(pending.timeout); + pendingSshPasswordPrompts.delete(requestId); + pending.reject(new Error(message)); + } +} + +async function requestSshPasswordFromRenderer(input: { + readonly destination: string; + readonly username: string | null; + readonly prompt: string; +}): Promise { + const window = mainWindow; + if (!window || window.isDestroyed()) { + throw new Error("T3 Code window is not available for SSH authentication."); + } + + const request: DesktopSshPasswordPromptRequest = { + requestId: Crypto.randomUUID(), + destination: input.destination, + username: input.username, + prompt: input.prompt, + }; + + return await new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + pendingSshPasswordPrompts.delete(request.requestId); + reject(new Error(`SSH authentication timed out for ${input.destination}.`)); + }, SSH_PASSWORD_PROMPT_TIMEOUT_MS); + timeout.unref(); + + pendingSshPasswordPrompts.set(request.requestId, { + resolve, + reject, + timeout, + }); + + window.webContents.send(SSH_PASSWORD_PROMPT_CHANNEL, request); + if (window.isMinimized()) { + window.restore(); + } + window.focus(); + }); +} + +const desktopSshEnvironmentManager = new DesktopSshEnvironmentManager({ + passwordProvider: requestSshPasswordFromRenderer, +}); + function resolveUpdaterErrorContext(): DesktopUpdateErrorContext { if (updateInstallInFlight) return "install"; if (updateDownloadInFlight) return "download"; @@ -1679,6 +1875,101 @@ function registerIpcHandlers(): void { }, ); + ipcMain.removeHandler(DISCOVER_SSH_HOSTS_CHANNEL); + ipcMain.handle(DISCOVER_SSH_HOSTS_CHANNEL, async () => + desktopSshEnvironmentManager.discoverHosts(), + ); + + ipcMain.removeHandler(ENSURE_SSH_ENVIRONMENT_CHANNEL); + ipcMain.handle( + ENSURE_SSH_ENVIRONMENT_CHANNEL, + async (_event, rawTarget: unknown, rawOptions: unknown) => { + const target = getSafeDesktopSshTarget(rawTarget); + if (!target) { + throw new Error("Invalid desktop SSH target."); + } + + const issuePairingToken = + typeof rawOptions === "object" && + rawOptions !== null && + "issuePairingToken" in rawOptions && + (rawOptions as { issuePairingToken?: unknown }).issuePairingToken === true; + + return await desktopSshEnvironmentManager.ensureEnvironment(target, { + issuePairingToken, + }); + }, + ); + + ipcMain.removeHandler(FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL); + ipcMain.handle( + FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL, + async (_event, rawHttpBaseUrl: unknown) => + fetchLoopbackSshJson({ + httpBaseUrl: rawHttpBaseUrl, + pathname: "/.well-known/t3/environment", + }), + ); + + ipcMain.removeHandler(BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL); + ipcMain.handle( + BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL, + async (_event, rawHttpBaseUrl: unknown, rawCredential: unknown) => + fetchLoopbackSshJson({ + httpBaseUrl: rawHttpBaseUrl, + pathname: "/api/auth/bootstrap/bearer", + method: "POST", + body: { + credential: rawCredential, + }, + }), + ); + + ipcMain.removeHandler(FETCH_SSH_SESSION_STATE_CHANNEL); + ipcMain.handle( + FETCH_SSH_SESSION_STATE_CHANNEL, + async (_event, rawHttpBaseUrl: unknown, rawBearerToken: unknown) => + fetchLoopbackSshJson({ + httpBaseUrl: rawHttpBaseUrl, + pathname: "/api/auth/session", + bearerToken: rawBearerToken, + }), + ); + + ipcMain.removeHandler(ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL); + ipcMain.handle( + ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL, + async (_event, rawHttpBaseUrl: unknown, rawBearerToken: unknown) => + fetchLoopbackSshJson({ + httpBaseUrl: rawHttpBaseUrl, + pathname: "/api/auth/ws-token", + method: "POST", + bearerToken: rawBearerToken, + }), + ); + + ipcMain.removeHandler(RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL); + ipcMain.handle( + RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL, + async (_event, rawRequestId: unknown, rawPassword: unknown) => { + if (typeof rawRequestId !== "string" || rawRequestId.trim().length === 0) { + throw new Error("Invalid SSH password prompt id."); + } + if (rawPassword !== null && typeof rawPassword !== "string") { + throw new Error("Invalid SSH password prompt response."); + } + + const pending = pendingSshPasswordPrompts.get(rawRequestId); + if (!pending) { + throw new Error("SSH password prompt is no longer pending."); + } + + clearTimeout(pending.timeout); + pendingSshPasswordPrompts.delete(rawRequestId); + pending.resolve(rawPassword); + }, + ); + ipcMain.removeHandler(GET_SERVER_EXPOSURE_STATE_CHANNEL); ipcMain.handle(GET_SERVER_EXPOSURE_STATE_CHANNEL, async () => getDesktopServerExposureState()); @@ -2054,6 +2345,9 @@ function createWindow(): BrowserWindow { } window.on("closed", () => { + rejectPendingSshPasswordPrompts( + "SSH authentication was cancelled because the app window closed.", + ); if (mainWindow === window) { mainWindow = null; } @@ -2145,6 +2439,7 @@ app.on("before-quit", () => { clearUpdatePollTimer(); cancelBackendReadinessWait(); stopBackend(); + void desktopSshEnvironmentManager.dispose().catch(() => undefined); restoreStdIoCapture?.(); }); @@ -2194,6 +2489,7 @@ if (process.platform !== "win32") { clearUpdatePollTimer(); cancelBackendReadinessWait(); stopBackend(); + void desktopSshEnvironmentManager.dispose().catch(() => undefined); restoreStdIoCapture?.(); app.quit(); }); @@ -2204,6 +2500,7 @@ if (process.platform !== "win32") { writeDesktopLogHeader("SIGTERM received"); clearUpdatePollTimer(); stopBackend(); + void desktopSshEnvironmentManager.dispose().catch(() => undefined); restoreStdIoCapture?.(); app.quit(); }); diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index e918e782ab..fdfaf20813 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -22,6 +22,14 @@ const SET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL = "desktop:set-saved-environment-re const GET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:get-saved-environment-secret"; const SET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:set-saved-environment-secret"; const REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:remove-saved-environment-secret"; +const DISCOVER_SSH_HOSTS_CHANNEL = "desktop:discover-ssh-hosts"; +const ENSURE_SSH_ENVIRONMENT_CHANNEL = "desktop:ensure-ssh-environment"; +const FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL = "desktop:fetch-ssh-environment-descriptor"; +const BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL = "desktop:bootstrap-ssh-bearer-session"; +const FETCH_SSH_SESSION_STATE_CHANNEL = "desktop:fetch-ssh-session-state"; +const ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL = "desktop:issue-ssh-websocket-token"; +const SSH_PASSWORD_PROMPT_CHANNEL = "desktop:ssh-password-prompt"; +const RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL = "desktop:resolve-ssh-password-prompt"; const GET_SERVER_EXPOSURE_STATE_CHANNEL = "desktop:get-server-exposure-state"; const SET_SERVER_EXPOSURE_MODE_CHANNEL = "desktop:set-server-exposure-mode"; const GET_ADVERTISED_ENDPOINTS_CHANNEL = "desktop:get-advertised-endpoints"; @@ -52,6 +60,30 @@ contextBridge.exposeInMainWorld("desktopBridge", { ipcRenderer.invoke(SET_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId, secret), removeSavedEnvironmentSecret: (environmentId) => ipcRenderer.invoke(REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId), + discoverSshHosts: () => ipcRenderer.invoke(DISCOVER_SSH_HOSTS_CHANNEL), + ensureSshEnvironment: (target, options) => + ipcRenderer.invoke(ENSURE_SSH_ENVIRONMENT_CHANNEL, target, options), + fetchSshEnvironmentDescriptor: (httpBaseUrl) => + ipcRenderer.invoke(FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL, httpBaseUrl), + bootstrapSshBearerSession: (httpBaseUrl, credential) => + ipcRenderer.invoke(BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL, httpBaseUrl, credential), + fetchSshSessionState: (httpBaseUrl, bearerToken) => + ipcRenderer.invoke(FETCH_SSH_SESSION_STATE_CHANNEL, httpBaseUrl, bearerToken), + issueSshWebSocketToken: (httpBaseUrl, bearerToken) => + ipcRenderer.invoke(ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL, httpBaseUrl, bearerToken), + onSshPasswordPrompt: (listener) => { + const wrappedListener = (_event: Electron.IpcRendererEvent, request: unknown) => { + if (typeof request !== "object" || request === null) return; + listener(request as Parameters[0]); + }; + + ipcRenderer.on(SSH_PASSWORD_PROMPT_CHANNEL, wrappedListener); + return () => { + ipcRenderer.removeListener(SSH_PASSWORD_PROMPT_CHANNEL, wrappedListener); + }; + }, + resolveSshPasswordPrompt: (requestId, password) => + ipcRenderer.invoke(RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL, requestId, password), getServerExposureState: () => ipcRenderer.invoke(GET_SERVER_EXPOSURE_STATE_CHANNEL), setServerExposureMode: (mode) => ipcRenderer.invoke(SET_SERVER_EXPOSURE_MODE_CHANNEL, mode), getAdvertisedEndpoints: () => ipcRenderer.invoke(GET_ADVERTISED_ENDPOINTS_CHANNEL), diff --git a/apps/desktop/src/sshEnvironment.test.ts b/apps/desktop/src/sshEnvironment.test.ts new file mode 100644 index 0000000000..fa5bca6a0d --- /dev/null +++ b/apps/desktop/src/sshEnvironment.test.ts @@ -0,0 +1,196 @@ +import * as FS from "node:fs"; +import * as OS from "node:os"; +import * as Path from "node:path"; + +import { afterEach, describe, expect, it } from "vitest"; + +import { __test, discoverDesktopSshHosts } from "./sshEnvironment"; + +const tempDirectories: string[] = []; + +afterEach(() => { + for (const directory of tempDirectories.splice(0)) { + FS.rmSync(directory, { recursive: true, force: true }); + } +}); + +function makeTempHomeDir(): string { + const directory = FS.mkdtempSync(Path.join(OS.tmpdir(), "t3-ssh-env-test-")); + tempDirectories.push(directory); + return directory; +} + +describe("sshEnvironment", () => { + it("discovers ssh config hosts across included files", async () => { + const homeDir = makeTempHomeDir(); + const sshDir = Path.join(homeDir, ".ssh"); + FS.mkdirSync(Path.join(sshDir, "config.d"), { recursive: true }); + FS.writeFileSync( + Path.join(sshDir, "config"), + ["Host devbox", " HostName devbox.example.com", "Include config.d/*.conf", ""].join("\n"), + "utf8", + ); + FS.writeFileSync( + Path.join(sshDir, "config.d", "team.conf"), + [ + "Host staging", + " HostName staging.example.com", + "Host *", + " ServerAliveInterval 30", + "", + ].join("\n"), + "utf8", + ); + FS.writeFileSync( + Path.join(sshDir, "known_hosts"), + [ + "known.example.com ssh-ed25519 AAAA", + "|1|hashed|entry ssh-ed25519 AAAA", + "[bastion.example.com]:2222 ssh-ed25519 AAAA", + "", + ].join("\n"), + "utf8", + ); + + await expect(discoverDesktopSshHosts({ homeDir })).resolves.toEqual([ + { + alias: "bastion.example.com", + hostname: "bastion.example.com", + username: null, + port: null, + source: "known-hosts", + }, + { + alias: "devbox", + hostname: "devbox", + username: null, + port: null, + source: "ssh-config", + }, + { + alias: "known.example.com", + hostname: "known.example.com", + username: null, + port: null, + source: "known-hosts", + }, + { + alias: "staging", + hostname: "staging", + username: null, + port: null, + source: "ssh-config", + }, + ]); + }); + + it("parses known_hosts entries without returning hashed hosts", () => { + expect( + __test.parseKnownHostsHostnames( + [ + "github.com ssh-ed25519 AAAA", + "gitlab.com,gitlab-alias ssh-ed25519 BBBB", + "|1|hashed|entry ssh-ed25519 CCCC", + "@cert-authority *.example.com ssh-ed25519 DDDD", + "[ssh.example.com]:2200 ssh-ed25519 EEEE", + "", + ].join("\n"), + ), + ).toEqual(["github.com", "gitlab-alias", "gitlab.com", "ssh.example.com"]); + }); + + it("parses resolved ssh config output into a target", () => { + expect( + __test.parseSshResolveOutput( + "devbox", + ["hostname devbox.example.com", "user julius", "port 2222", ""].join("\n"), + ), + ).toEqual({ + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + }); + }); + + it("builds interactive ssh args without forcing batch mode", () => { + expect( + __test.baseSshArgs( + { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + }, + { batchMode: "no" }, + ), + ).toEqual(["-o", "BatchMode=no", "-o", "ConnectTimeout=10", "-p", "2222"]); + }); + + it("creates askpass env for desktop ssh prompts", () => { + const askpassDirectory = Path.join(makeTempHomeDir(), "askpass"); + const env = __test.buildSshChildEnvironment({ + authSecret: "super-secret", + interactiveAuth: true, + askpassDirectory, + platform: "linux", + baseEnv: {}, + }); + + expect(env.SSH_ASKPASS).toBe(Path.join(askpassDirectory, "ssh-askpass.sh")); + expect(env.SSH_ASKPASS_REQUIRE).toBe("force"); + expect(env.T3_SSH_AUTH_SECRET).toBe("super-secret"); + expect(env.DISPLAY).toBe("t3code"); + expect(FS.existsSync(Path.join(askpassDirectory, "ssh-askpass.sh"))).toBe(true); + expect(FS.readFileSync(Path.join(askpassDirectory, "ssh-askpass.sh"), "utf8")).toContain( + 'printf "%s\\n" "$T3_SSH_AUTH_SECRET"', + ); + }); + + it("builds a windows askpass launcher pair", () => { + const descriptor = __test.buildSshAskpassHelperDescriptor({ + directory: "C:\\temp\\t3code-ssh-askpass", + platform: "win32", + }); + + expect(descriptor.launcherPath).toBe("C:\\temp\\t3code-ssh-askpass\\ssh-askpass.cmd"); + expect(descriptor.files.map((file) => Path.win32.basename(file.path))).toEqual([ + "ssh-askpass.cmd", + "ssh-askpass.ps1", + ]); + }); + + it("builds a remote t3 runner with npx and npm fallbacks", () => { + const script = __test.buildRemoteT3RunnerScript(); + + expect(script).toContain('exec t3 "$@"'); + expect(script).toContain('exec npx --yes t3 "$@"'); + expect(script).toContain('exec npm exec --yes t3 -- "$@"'); + expect(script).toContain("could not find npx or npm on PATH"); + }); + + it("uses the remote t3 runner for launch and pairing scripts", () => { + const target = { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + } as const; + + expect(__test.buildRemoteLaunchScript()).toContain('"$RUNNER_FILE" serve --host 127.0.0.1'); + expect(__test.buildRemotePairingScript(target)).toContain( + '"$RUNNER_FILE" auth pairing create --base-dir "$SERVER_HOME" --json', + ); + }); + + it("detects ssh auth failures from common permission denied messages", () => { + expect( + __test.isSshAuthFailure( + new Error( + "julius@100.65.180.100: Permission denied (publickey,password,keyboard-interactive).", + ), + ), + ).toBe(true); + expect(__test.isSshAuthFailure(new Error("Connection timed out"))).toBe(false); + }); +}); diff --git a/apps/desktop/src/sshEnvironment.ts b/apps/desktop/src/sshEnvironment.ts new file mode 100644 index 0000000000..e197645fd9 --- /dev/null +++ b/apps/desktop/src/sshEnvironment.ts @@ -0,0 +1,1013 @@ +import * as ChildProcess from "node:child_process"; +import * as Crypto from "node:crypto"; +import * as FS from "node:fs"; +import * as OS from "node:os"; +import * as Path from "node:path"; +import * as Net from "node:net"; + +import type { + DesktopDiscoveredSshHost, + DesktopSshEnvironmentBootstrap, + DesktopSshEnvironmentTarget, +} from "@t3tools/contracts"; + +import { waitForHttpReady } from "./backendReadiness"; + +const DEFAULT_REMOTE_PORT = 3773; +const REMOTE_PORT_SCAN_WINDOW = 200; +const SSH_ASKPASS_DIR_NAME = "t3code-ssh-askpass"; +const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000; +const SSH_READY_TIMEOUT_MS = 20_000; + +interface SshTunnelEntry { + readonly key: string; + readonly target: DesktopSshEnvironmentTarget; + readonly remotePort: number; + readonly localPort: number; + readonly httpBaseUrl: string; + readonly wsBaseUrl: string; + readonly process: ChildProcess.ChildProcess; +} + +interface SshCommandResult { + readonly stdout: string; + readonly stderr: string; +} + +interface SshAskpassFile { + readonly path: string; + readonly contents: string; + readonly mode?: number; +} + +interface SshAskpassHelperDescriptor { + readonly launcherPath: string; + readonly files: ReadonlyArray; +} + +interface SshAuthOptions { + readonly authSecret?: string | null; + readonly batchMode?: "yes" | "no"; + readonly interactiveAuth?: boolean; +} + +interface DesktopSshPasswordRequest { + readonly destination: string; + readonly username: string | null; + readonly prompt: string; + readonly attempt: number; +} + +interface DesktopSshEnvironmentManagerOptions { + readonly passwordProvider?: (request: DesktopSshPasswordRequest) => Promise; +} + +const NO_HOSTS = [] as const; + +function stripInlineComment(line: string): string { + const hashIndex = line.indexOf("#"); + return (hashIndex >= 0 ? line.slice(0, hashIndex) : line).trim(); +} + +function splitDirectiveArgs(value: string): ReadonlyArray { + return value + .trim() + .split(/\s+/u) + .map((entry) => entry.trim()) + .filter((entry) => entry.length > 0); +} + +function hasSshPattern(value: string): boolean { + return value.includes("*") || value.includes("?") || value.startsWith("!"); +} + +function escapeRegex(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); +} + +function globToRegExp(pattern: string): RegExp { + return new RegExp( + `^${escapeRegex(pattern).replace(/\\\*/gu, ".*").replace(/\\\?/gu, ".")}$`, + "u", + ); +} + +function expandGlob(pattern: string): ReadonlyArray { + if (!pattern.includes("*") && !pattern.includes("?")) { + return FS.existsSync(pattern) ? [pattern] : NO_HOSTS; + } + + const directory = Path.dirname(pattern); + const basePattern = Path.basename(pattern); + if (!FS.existsSync(directory)) { + return NO_HOSTS; + } + + const matcher = globToRegExp(basePattern); + return FS.readdirSync(directory) + .filter((entry) => matcher.test(entry)) + .map((entry) => Path.join(directory, entry)) + .filter((entry) => FS.existsSync(entry)) + .toSorted((left, right) => left.localeCompare(right)); +} + +function collectSshConfigAliasesFromFile( + filePath: string, + visited = new Set(), +): ReadonlyArray { + const resolvedPath = Path.resolve(filePath); + if (visited.has(resolvedPath) || !FS.existsSync(resolvedPath)) { + return NO_HOSTS; + } + visited.add(resolvedPath); + + const aliases = new Set(); + const directory = Path.dirname(resolvedPath); + const raw = FS.readFileSync(resolvedPath, "utf8"); + + for (const line of raw.split(/\r?\n/u)) { + const stripped = stripInlineComment(line); + if (stripped.length === 0) { + continue; + } + + const [directive = "", ...rawArgs] = splitDirectiveArgs(stripped); + const normalizedDirective = directive.toLowerCase(); + if (normalizedDirective === "include") { + for (const includePattern of rawArgs) { + const resolvedPattern = Path.isAbsolute(includePattern) + ? includePattern + : Path.resolve(directory, includePattern); + for (const includedPath of expandGlob(resolvedPattern)) { + for (const alias of collectSshConfigAliasesFromFile(includedPath, visited)) { + aliases.add(alias); + } + } + } + continue; + } + + if (normalizedDirective !== "host") { + continue; + } + + for (const alias of rawArgs) { + if (alias.length === 0 || hasSshPattern(alias)) { + continue; + } + aliases.add(alias); + } + } + + return [...aliases].toSorted((left, right) => left.localeCompare(right)); +} + +function parseKnownHostsHostnames(raw: string): ReadonlyArray { + const hostnames = new Set(); + + for (const line of raw.split(/\r?\n/u)) { + const trimmed = line.trim(); + if (trimmed.length === 0 || trimmed.startsWith("#")) { + continue; + } + + const withoutMarker = trimmed.startsWith("@") + ? trimmed.split(/\s+/u).slice(1).join(" ") + : trimmed; + const [hostField = ""] = withoutMarker.split(/\s+/u); + if (hostField.length === 0 || hostField.startsWith("|")) { + continue; + } + + for (const rawHost of hostField.split(",")) { + const host = + /^\[([^\]]+)\]:(\d+)$/u.exec(rawHost)?.[1] ?? rawHost.replace(/:.*$/u, "").trim(); + if (host.length === 0 || hasSshPattern(host)) { + continue; + } + hostnames.add(host); + } + } + + return [...hostnames].toSorted((left, right) => left.localeCompare(right)); +} + +function readKnownHostsHostnames(filePath: string): ReadonlyArray { + if (!FS.existsSync(filePath)) { + return NO_HOSTS; + } + + return parseKnownHostsHostnames(FS.readFileSync(filePath, "utf8")); +} + +function parseSshResolveOutput(alias: string, stdout: string): DesktopSshEnvironmentTarget { + const values = new Map(); + for (const line of stdout.split(/\r?\n/u)) { + const trimmed = line.trim(); + if (trimmed.length === 0) { + continue; + } + const [key, ...rest] = trimmed.split(/\s+/u); + if (!key || rest.length === 0 || values.has(key)) { + continue; + } + values.set(key, rest.join(" ").trim()); + } + + const hostname = values.get("hostname")?.trim() || alias; + const username = values.get("user")?.trim() || null; + const rawPort = values.get("port")?.trim() ?? ""; + const parsedPort = Number.parseInt(rawPort, 10); + + return { + alias, + hostname, + username, + port: Number.isInteger(parsedPort) ? parsedPort : null, + }; +} + +async function findAvailableLocalPort(): Promise { + return await new Promise((resolve, reject) => { + const server = Net.createServer(); + server.unref(); + server.once("error", reject); + server.listen(0, "127.0.0.1", () => { + const address = server.address(); + if (!address || typeof address === "string") { + server.close(() => reject(new Error("Unable to allocate a local tunnel port."))); + return; + } + const { port } = address; + server.close((error) => { + if (error) { + reject(error); + return; + } + resolve(port); + }); + }); + }); +} + +function targetConnectionKey(target: DesktopSshEnvironmentTarget): string { + return `${target.alias}\u0000${target.hostname}\u0000${target.username ?? ""}\u0000${target.port ?? ""}`; +} + +function remoteStateKey(target: DesktopSshEnvironmentTarget): string { + return Crypto.createHash("sha256").update(targetConnectionKey(target)).digest("hex").slice(0, 16); +} + +function buildSshHostSpec(target: DesktopSshEnvironmentTarget): string { + const destination = target.alias.trim() || target.hostname.trim(); + if (destination.length === 0) { + throw new Error("SSH target is missing its alias/hostname."); + } + return target.username ? `${target.username}@${destination}` : destination; +} + +function getDefaultSshAskpassDirectory(): string { + return Path.join(OS.tmpdir(), SSH_ASKPASS_DIR_NAME); +} + +function buildPosixSshAskpassScript(): string { + return [ + "#!/bin/sh", + "set -eu", + 'PROMPT="${1:-SSH authentication}"', + 'if [ "${T3_SSH_AUTH_SECRET+x}" = "x" ]; then', + ' printf "%s\\n" "$T3_SSH_AUTH_SECRET"', + " exit 0", + "fi", + "if command -v osascript >/dev/null 2>&1; then", + " T3_SSH_ASKPASS_PROMPT=\"$PROMPT\" /usr/bin/osascript <<'APPLESCRIPT'", + 'set promptText to system attribute "T3_SSH_ASKPASS_PROMPT"', + "try", + ' set dialogResult to display dialog promptText default answer "" with hidden answer buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel"', + " text returned of dialogResult", + "on error number -128", + " error number -128", + "end try", + "APPLESCRIPT", + " exit $?", + "fi", + "if command -v zenity >/dev/null 2>&1; then", + ' zenity --password --title="SSH authentication" --text="$PROMPT"', + " exit $?", + "fi", + "if command -v kdialog >/dev/null 2>&1; then", + ' kdialog --title "SSH authentication" --password "$PROMPT"', + " exit $?", + "fi", + "if command -v ssh-askpass >/dev/null 2>&1; then", + ' ssh-askpass "$PROMPT"', + " exit $?", + "fi", + "printf 'Unable to open an SSH password prompt on this desktop.\\n' >&2", + "exit 1", + "", + ].join("\n"); +} + +function buildWindowsSshAskpassScript(): string { + return [ + "if ($env:T3_SSH_AUTH_SECRET -ne $null) {", + " [Console]::Out.WriteLine($env:T3_SSH_AUTH_SECRET)", + " exit 0", + "}", + "Add-Type -AssemblyName System.Windows.Forms", + "[System.Windows.Forms.Application]::EnableVisualStyles()", + '$prompt = if ($args.Length -gt 0 -and $args[0]) { $args[0] } else { "SSH authentication" }', + "$form = New-Object System.Windows.Forms.Form", + '$form.Text = "SSH authentication"', + "$form.Width = 420", + "$form.Height = 185", + '$form.StartPosition = "CenterScreen"', + '$form.FormBorderStyle = "FixedDialog"', + "$form.MaximizeBox = $false", + "$form.MinimizeBox = $false", + "$form.TopMost = $true", + "$label = New-Object System.Windows.Forms.Label", + "$label.Left = 16", + "$label.Top = 16", + "$label.Width = 372", + "$label.Height = 34", + "$label.Text = $prompt", + "$textbox = New-Object System.Windows.Forms.TextBox", + "$textbox.Left = 16", + "$textbox.Top = 60", + "$textbox.Width = 372", + "$textbox.UseSystemPasswordChar = $true", + "$okButton = New-Object System.Windows.Forms.Button", + '$okButton.Text = "OK"', + "$okButton.Left = 232", + "$okButton.Top = 100", + "$okButton.Width = 75", + "$cancelButton = New-Object System.Windows.Forms.Button", + '$cancelButton.Text = "Cancel"', + "$cancelButton.Left = 313", + "$cancelButton.Top = 100", + "$cancelButton.Width = 75", + "$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK", + "$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel", + "$form.AcceptButton = $okButton", + "$form.CancelButton = $cancelButton", + "$form.Controls.Add($label)", + "$form.Controls.Add($textbox)", + "$form.Controls.Add($okButton)", + "$form.Controls.Add($cancelButton)", + "$result = $form.ShowDialog()", + "if ($result -ne [System.Windows.Forms.DialogResult]::OK) { exit 1 }", + "[Console]::Out.WriteLine($textbox.Text)", + "", + ].join("\r\n"); +} + +function buildSshAskpassHelperDescriptor(input?: { + readonly directory?: string; + readonly platform?: NodeJS.Platform; +}): SshAskpassHelperDescriptor { + const platform = input?.platform ?? process.platform; + const directory = input?.directory ?? getDefaultSshAskpassDirectory(); + const pathModule = platform === "win32" ? Path.win32 : Path.posix; + + if (platform === "win32") { + const powershellPath = pathModule.join(directory, "ssh-askpass.ps1"); + return { + launcherPath: pathModule.join(directory, "ssh-askpass.cmd"), + files: [ + { + path: pathModule.join(directory, "ssh-askpass.cmd"), + contents: [ + "@echo off", + 'powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0ssh-askpass.ps1" %*', + "", + ].join("\r\n"), + }, + { + path: powershellPath, + contents: buildWindowsSshAskpassScript(), + }, + ], + }; + } + + return { + launcherPath: pathModule.join(directory, "ssh-askpass.sh"), + files: [ + { + path: pathModule.join(directory, "ssh-askpass.sh"), + contents: buildPosixSshAskpassScript(), + mode: 0o700, + }, + ], + }; +} + +function ensureSshAskpassHelpers(input?: { + readonly directory?: string; + readonly platform?: NodeJS.Platform; +}): string { + const descriptor = buildSshAskpassHelperDescriptor(input); + const platform = input?.platform ?? process.platform; + FS.mkdirSync(Path.dirname(descriptor.launcherPath), { recursive: true }); + + for (const file of descriptor.files) { + const current = + FS.existsSync(file.path) && FS.statSync(file.path).isFile() + ? FS.readFileSync(file.path, "utf8") + : null; + if (current !== file.contents) { + FS.writeFileSync(file.path, file.contents, "utf8"); + } + if (file.mode !== undefined && platform !== "win32") { + FS.chmodSync(file.path, file.mode); + } + } + + return descriptor.launcherPath; +} + +function buildSshChildEnvironment(input?: { + readonly interactiveAuth?: boolean; + readonly baseEnv?: NodeJS.ProcessEnv; + readonly askpassDirectory?: string; + readonly authSecret?: string | null; + readonly platform?: NodeJS.Platform; +}): NodeJS.ProcessEnv { + const baseEnv = { ...(input?.baseEnv ?? process.env) }; + if (!input?.interactiveAuth) { + return baseEnv; + } + + const platform = input?.platform ?? process.platform; + const askpassInput = + input?.askpassDirectory === undefined + ? { platform } + : { + directory: input.askpassDirectory, + platform, + }; + return { + ...baseEnv, + SSH_ASKPASS: ensureSshAskpassHelpers(askpassInput), + SSH_ASKPASS_REQUIRE: "force", + ...(input?.authSecret === undefined ? {} : { T3_SSH_AUTH_SECRET: input.authSecret ?? "" }), + ...(platform === "win32" || baseEnv.DISPLAY ? {} : { DISPLAY: "t3code" }), + }; +} + +function baseSshArgs( + target: DesktopSshEnvironmentTarget, + input?: { readonly batchMode?: "yes" | "no" }, +): string[] { + return [ + "-o", + `BatchMode=${input?.batchMode ?? "no"}`, + "-o", + "ConnectTimeout=10", + ...(target.port !== null ? ["-p", String(target.port)] : []), + ]; +} + +function normalizeSshErrorMessage(stderr: string, fallbackMessage: string): string { + const cleaned = stderr.trim(); + return cleaned.length > 0 ? cleaned : fallbackMessage; +} + +function isSshAuthFailure(error: unknown): boolean { + const message = error instanceof Error ? error.message : String(error); + return /permission denied|authentication failed|keyboard-interactive/u.test( + message.toLowerCase(), + ); +} + +async function runSshCommand( + target: DesktopSshEnvironmentTarget, + input?: { + readonly preHostArgs?: ReadonlyArray; + readonly remoteCommandArgs?: ReadonlyArray; + readonly stdin?: string; + readonly signal?: AbortSignal; + readonly authSecret?: string | null; + readonly batchMode?: "yes" | "no"; + readonly interactiveAuth?: boolean; + }, +): Promise { + const hostSpec = buildSshHostSpec(target); + + return await new Promise((resolve, reject) => { + const childEnvironment = + input?.interactiveAuth === undefined + ? buildSshChildEnvironment() + : buildSshChildEnvironment({ + interactiveAuth: input.interactiveAuth, + ...(input.authSecret === undefined ? {} : { authSecret: input.authSecret }), + }); + const child = ChildProcess.spawn( + "ssh", + [ + ...baseSshArgs(target, { + batchMode: input?.batchMode ?? (input?.interactiveAuth ? "no" : "yes"), + }), + ...(input?.preHostArgs ?? []), + hostSpec, + ...(input?.remoteCommandArgs ?? []), + ], + { + env: childEnvironment, + stdio: "pipe", + }, + ); + + let stdout = ""; + let stderr = ""; + + const onAbort = () => { + child.kill("SIGTERM"); + reject(new Error(`SSH command aborted for ${hostSpec}.`)); + }; + + input?.signal?.addEventListener("abort", onAbort, { once: true }); + child.stdout?.setEncoding("utf8"); + child.stderr?.setEncoding("utf8"); + child.stdout?.on("data", (chunk: string) => { + stdout += chunk; + }); + child.stderr?.on("data", (chunk: string) => { + stderr += chunk; + }); + child.once("error", (error) => { + input?.signal?.removeEventListener("abort", onAbort); + reject(error); + }); + child.once("close", (code) => { + input?.signal?.removeEventListener("abort", onAbort); + if (code === 0) { + resolve({ stdout, stderr }); + return; + } + reject( + new Error( + normalizeSshErrorMessage(stderr, `SSH command failed for ${hostSpec} (exit ${code}).`), + ), + ); + }); + + if (input?.stdin !== undefined) { + child.stdin?.end(input.stdin); + return; + } + child.stdin?.end(); + }); +} + +async function resolveDesktopSshTarget(alias: string): Promise { + const trimmedAlias = alias.trim(); + if (trimmedAlias.length === 0) { + throw new Error("SSH host alias is required."); + } + + try { + const result = await runSshCommand( + { + alias: trimmedAlias, + hostname: trimmedAlias, + username: null, + port: null, + }, + { preHostArgs: ["-G"] }, + ); + return parseSshResolveOutput(trimmedAlias, result.stdout); + } catch { + return { + alias: trimmedAlias, + hostname: trimmedAlias, + username: null, + port: null, + }; + } +} + +function buildRemoteLaunchScript(): string { + const runnerScript = buildRemoteT3RunnerScript(); + return ` +set -eu +STATE_KEY="$1" +STATE_DIR="$HOME/.t3/ssh-launch/$STATE_KEY" +SERVER_HOME="$STATE_DIR/server-home" +PORT_FILE="$STATE_DIR/port" +PID_FILE="$STATE_DIR/pid" +LOG_FILE="$STATE_DIR/server.log" +RUNNER_FILE="$STATE_DIR/run-t3.sh" +mkdir -p "$STATE_DIR" "$SERVER_HOME" +cat >"$RUNNER_FILE" <<'SH' +${runnerScript} +SH +chmod 700 "$RUNNER_FILE" +pick_port() { + node - "$PORT_FILE" <<'NODE' +const fs = require("node:fs"); +const net = require("node:net"); +const filePath = process.argv[2]; +const raw = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8").trim() : ""; +const preferred = Number.parseInt(raw, 10); +const start = Number.isInteger(preferred) ? preferred : ${DEFAULT_REMOTE_PORT}; +const end = start + ${REMOTE_PORT_SCAN_WINDOW}; + +function tryPort(port) { + return new Promise((resolve) => { + const server = net.createServer(); + server.unref(); + server.once("error", () => resolve(false)); + server.listen(port, "127.0.0.1", () => { + server.close((error) => resolve(error ? false : port)); + }); + }); +} + +(async () => { + for (let port = start; port < end; port += 1) { + const available = await tryPort(port); + if (available) { + process.stdout.write(String(port)); + return; + } + } + process.exit(1); +})().catch(() => process.exit(1)); +NODE +} +REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)" +REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)" +if [ -n "$REMOTE_PID" ] && kill -0 "$REMOTE_PID" 2>/dev/null; then + : +else + REMOTE_PORT="$(pick_port)" + nohup env T3CODE_NO_BROWSER=1 "$RUNNER_FILE" serve --host 127.0.0.1 --port "$REMOTE_PORT" --base-dir "$SERVER_HOME" >>"$LOG_FILE" 2>&1 < /dev/null & + REMOTE_PID="$!" + printf '%s\\n' "$REMOTE_PID" >"$PID_FILE" + printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE" +fi +printf '{"remotePort":%s}\\n' "$REMOTE_PORT" +`.trimStart(); +} + +function buildRemoteT3RunnerScript(): string { + return [ + "#!/bin/sh", + "set -eu", + "if command -v t3 >/dev/null 2>&1; then", + ' exec t3 "$@"', + "fi", + "if command -v npx >/dev/null 2>&1; then", + ' exec npx --yes t3 "$@"', + "fi", + "if command -v npm >/dev/null 2>&1; then", + ' exec npm exec --yes t3 -- "$@"', + "fi", + "printf 'Remote host is missing the t3 CLI and could not find npx or npm on PATH.\\n' >&2", + "exit 1", + ].join("\n"); +} + +function buildRemotePairingScript(target: DesktopSshEnvironmentTarget): string { + const runnerScript = buildRemoteT3RunnerScript(); + return ` +set -eu +STATE_DIR="$HOME/.t3/ssh-launch/${remoteStateKey(target)}" +SERVER_HOME="$STATE_DIR/server-home" +RUNNER_FILE="$STATE_DIR/run-t3.sh" +mkdir -p "$STATE_DIR" "$SERVER_HOME" +cat >"$RUNNER_FILE" <<'SH' +${runnerScript} +SH +chmod 700 "$RUNNER_FILE" +"$RUNNER_FILE" auth pairing create --base-dir "$SERVER_HOME" --json +`.trimStart(); +} + +async function launchOrReuseRemoteServer( + target: DesktopSshEnvironmentTarget, + input?: SshAuthOptions, +): Promise { + const result = await runSshCommand(target, { + remoteCommandArgs: ["sh", "-s", "--", remoteStateKey(target)], + stdin: buildRemoteLaunchScript(), + ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }), + ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }), + ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }), + }); + const line = result.stdout + .trim() + .split(/\r?\n/u) + .map((entry) => entry.trim()) + .findLast((entry) => entry.length > 0); + if (!line) { + throw new Error("SSH launch did not return a remote port."); + } + + const parsed = JSON.parse(line) as { remotePort?: unknown }; + if (typeof parsed.remotePort !== "number" || !Number.isInteger(parsed.remotePort)) { + throw new Error("SSH launch returned an invalid remote port."); + } + return parsed.remotePort; +} + +async function issueRemotePairingToken( + target: DesktopSshEnvironmentTarget, + input?: SshAuthOptions, +): Promise { + const result = await runSshCommand(target, { + remoteCommandArgs: ["sh", "-s"], + stdin: buildRemotePairingScript(target), + ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }), + ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }), + ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }), + }); + const parsed = JSON.parse(result.stdout) as { credential?: unknown }; + if (typeof parsed.credential !== "string" || parsed.credential.trim().length === 0) { + throw new Error("SSH pairing command returned an invalid credential."); + } + return parsed.credential; +} + +async function stopTunnel(entry: SshTunnelEntry): Promise { + const child = entry.process; + if (child.exitCode !== null || child.signalCode !== null) { + return; + } + + await new Promise((resolve) => { + let settled = false; + let forceKillTimer: ReturnType | null = null; + + const settle = () => { + if (settled) { + return; + } + settled = true; + child.off("exit", onExit); + if (forceKillTimer) { + clearTimeout(forceKillTimer); + } + resolve(); + }; + + const onExit = () => { + settle(); + }; + + child.once("exit", onExit); + child.kill("SIGTERM"); + forceKillTimer = setTimeout(() => { + if (child.exitCode === null && child.signalCode === null) { + child.kill("SIGKILL"); + } + }, TUNNEL_SHUTDOWN_TIMEOUT_MS); + forceKillTimer.unref(); + }); +} + +export async function discoverDesktopSshHosts(input?: { + readonly homeDir?: string; +}): Promise { + const sshDirectory = Path.join(input?.homeDir ?? OS.homedir(), ".ssh"); + const configAliases = collectSshConfigAliasesFromFile(Path.join(sshDirectory, "config")); + const knownHosts = readKnownHostsHostnames(Path.join(sshDirectory, "known_hosts")); + const discovered = new Map(); + + for (const alias of configAliases) { + discovered.set(alias, { + alias, + hostname: alias, + username: null, + port: null, + source: "ssh-config", + }); + } + + for (const hostname of knownHosts) { + if (discovered.has(hostname)) { + continue; + } + discovered.set(hostname, { + alias: hostname, + hostname, + username: null, + port: null, + source: "known-hosts", + }); + } + + return [...discovered.values()].toSorted((left, right) => left.alias.localeCompare(right.alias)); +} + +export class DesktopSshEnvironmentManager { + private readonly tunnels = new Map(); + private readonly authSecrets = new Map(); + + constructor(private readonly options: DesktopSshEnvironmentManagerOptions = {}) {} + + private async promptForPassword( + target: DesktopSshEnvironmentTarget, + attempt: number, + ): Promise { + const passwordProvider = this.options.passwordProvider; + if (!passwordProvider) { + throw new Error(`SSH authentication failed for ${buildSshHostSpec(target)}.`); + } + + const password = await passwordProvider({ + attempt, + destination: target.alias.trim() || target.hostname.trim(), + username: target.username, + prompt: `Enter the SSH password for ${buildSshHostSpec(target)}.`, + }); + if (password === null) { + throw new Error(`SSH authentication cancelled for ${buildSshHostSpec(target)}.`); + } + return password; + } + + private async runWithSshAuth( + key: string, + target: DesktopSshEnvironmentTarget, + operation: (authOptions: SshAuthOptions) => Promise, + ): Promise { + let authSecret = this.authSecrets.get(key) ?? null; + let promptCount = 0; + + while (true) { + try { + return await operation( + authSecret === null + ? { + batchMode: this.options.passwordProvider ? "yes" : "no", + interactiveAuth: !this.options.passwordProvider, + } + : { + authSecret, + batchMode: "no", + interactiveAuth: true, + }, + ); + } catch (error) { + if (!isSshAuthFailure(error)) { + throw error; + } + + if (!this.options.passwordProvider) { + throw error; + } + + if (authSecret !== null) { + this.authSecrets.delete(key); + } + if (promptCount >= 2) { + throw error; + } + + promptCount += 1; + authSecret = await this.promptForPassword(target, promptCount); + this.authSecrets.set(key, authSecret); + } + } + } + + async discoverHosts(): Promise { + return await discoverDesktopSshHosts(); + } + + async ensureEnvironment( + target: DesktopSshEnvironmentTarget, + options?: { readonly issuePairingToken?: boolean }, + ): Promise { + const resolvedTarget = await resolveDesktopSshTarget(target.alias || target.hostname); + const key = targetConnectionKey(resolvedTarget); + let entry = this.tunnels.get(key) ?? null; + + if (entry !== null) { + try { + await waitForHttpReady(entry.httpBaseUrl, { timeoutMs: 2_000 }); + } catch { + await stopTunnel(entry).catch(() => undefined); + this.tunnels.delete(key); + entry = null; + } + } + + if (entry === null) { + const remotePort = await this.runWithSshAuth(key, resolvedTarget, (authOptions) => + launchOrReuseRemoteServer(resolvedTarget, authOptions), + ); + const localPort = await findAvailableLocalPort(); + const httpBaseUrl = `http://127.0.0.1:${localPort}/`; + const wsBaseUrl = `ws://127.0.0.1:${localPort}/`; + entry = await this.runWithSshAuth(key, resolvedTarget, async (authOptions) => { + const process = ChildProcess.spawn( + "ssh", + [ + ...baseSshArgs(resolvedTarget, { batchMode: authOptions.batchMode ?? "no" }), + "-o", + "ExitOnForwardFailure=yes", + "-o", + "ServerAliveInterval=15", + "-o", + "ServerAliveCountMax=3", + "-N", + "-L", + `${localPort}:127.0.0.1:${remotePort}`, + buildSshHostSpec(resolvedTarget), + ], + { + env: buildSshChildEnvironment({ + ...(authOptions.authSecret === undefined + ? {} + : { authSecret: authOptions.authSecret }), + ...(authOptions.interactiveAuth === undefined + ? {} + : { interactiveAuth: authOptions.interactiveAuth }), + }), + stdio: "pipe", + }, + ); + const nextEntry: SshTunnelEntry = { + key, + target: resolvedTarget, + remotePort, + localPort, + httpBaseUrl, + wsBaseUrl, + process, + }; + const tunnelReady = new Promise((resolve, reject) => { + let stderr = ""; + process.stderr?.setEncoding("utf8"); + process.stderr?.on("data", (chunk: string) => { + stderr += chunk; + }); + process.once("error", (error) => { + this.tunnels.delete(key); + reject(error); + }); + process.once("exit", (code) => { + this.tunnels.delete(key); + reject( + new Error( + normalizeSshErrorMessage( + stderr, + `SSH tunnel exited unexpectedly for ${resolvedTarget.alias} (exit ${code ?? "unknown"}).`, + ), + ), + ); + }); + waitForHttpReady(httpBaseUrl, { timeoutMs: SSH_READY_TIMEOUT_MS }) + .then(() => resolve()) + .catch((error) => reject(error)); + }); + this.tunnels.set(key, nextEntry); + try { + await tunnelReady; + return nextEntry; + } catch (error) { + await stopTunnel(nextEntry).catch(() => undefined); + this.tunnels.delete(key); + throw error; + } + }); + } + + const pairingToken = options?.issuePairingToken + ? await this.runWithSshAuth(key, entry.target, (authOptions) => + issueRemotePairingToken(entry.target, authOptions), + ) + : null; + + return { + target: entry.target, + httpBaseUrl: entry.httpBaseUrl, + wsBaseUrl: entry.wsBaseUrl, + pairingToken, + }; + } + + async dispose(): Promise { + const entries = [...this.tunnels.values()]; + this.tunnels.clear(); + await Promise.all(entries.map((entry) => stopTunnel(entry).catch(() => undefined))); + } +} + +export const __test = { + baseSshArgs, + buildRemoteLaunchScript, + buildRemotePairingScript, + buildRemoteT3RunnerScript, + buildSshAskpassHelperDescriptor, + buildSshChildEnvironment, + isSshAuthFailure, + collectSshConfigAliasesFromFile, + parseKnownHostsHostnames, + parseSshResolveOutput, +}; diff --git a/apps/web/src/clientPersistenceStorage.test.ts b/apps/web/src/clientPersistenceStorage.test.ts index a74ce18ac3..e02168e09b 100644 --- a/apps/web/src/clientPersistenceStorage.test.ts +++ b/apps/web/src/clientPersistenceStorage.test.ts @@ -10,6 +10,12 @@ const savedRegistryRecord: PersistedSavedEnvironmentRecord = { wsBaseUrl: "wss://remote.example.com/", createdAt: "2026-04-09T00:00:00.000Z", lastConnectedAt: null, + desktopSsh: { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 22, + }, }; function createLocalStorageStub(): Storage { diff --git a/apps/web/src/clientPersistenceStorage.ts b/apps/web/src/clientPersistenceStorage.ts index 70f51d5c30..30c949b37a 100644 --- a/apps/web/src/clientPersistenceStorage.ts +++ b/apps/web/src/clientPersistenceStorage.ts @@ -19,6 +19,14 @@ const BrowserSavedEnvironmentRecordSchema = Schema.Struct({ wsBaseUrl: Schema.String, createdAt: Schema.String, lastConnectedAt: Schema.NullOr(Schema.String), + desktopSsh: Schema.optionalKey( + Schema.Struct({ + alias: Schema.String, + hostname: Schema.String, + username: Schema.NullOr(Schema.String), + port: Schema.NullOr(Schema.Number), + }), + ), bearerToken: Schema.optionalKey(Schema.String), }); type BrowserSavedEnvironmentRecord = typeof BrowserSavedEnvironmentRecordSchema.Type; @@ -37,7 +45,7 @@ function hasWindow(): boolean { function toPersistedSavedEnvironmentRecord( record: PersistedSavedEnvironmentRecord, ): PersistedSavedEnvironmentRecord { - return { + const nextRecord = { environmentId: record.environmentId, label: record.label, httpBaseUrl: record.httpBaseUrl, @@ -45,6 +53,7 @@ function toPersistedSavedEnvironmentRecord( createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, }; + return record.desktopSsh ? { ...nextRecord, desktopSsh: record.desktopSsh } : nextRecord; } export function readBrowserClientSettings(): ClientSettings | null { @@ -135,6 +144,7 @@ export function writeBrowserSavedEnvironmentRegistry( wsBaseUrl: record.wsBaseUrl, createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, + ...(record.desktopSsh ? { desktopSsh: record.desktopSsh } : {}), bearerToken, } : toPersistedSavedEnvironmentRecord(record); @@ -166,7 +176,7 @@ export function writeBrowserSavedEnvironmentSecret( return record; } found = true; - return { + const nextRecord = { environmentId: record.environmentId, label: record.label, httpBaseUrl: record.httpBaseUrl, @@ -174,7 +184,8 @@ export function writeBrowserSavedEnvironmentSecret( createdAt: record.createdAt, lastConnectedAt: record.lastConnectedAt, bearerToken: secret, - } satisfies BrowserSavedEnvironmentRecord; + }; + return record.desktopSsh ? { ...nextRecord, desktopSsh: record.desktopSsh } : nextRecord; }), }); return found; diff --git a/apps/web/src/components/desktop/SshPasswordPromptDialog.tsx b/apps/web/src/components/desktop/SshPasswordPromptDialog.tsx new file mode 100644 index 0000000000..4066544c63 --- /dev/null +++ b/apps/web/src/components/desktop/SshPasswordPromptDialog.tsx @@ -0,0 +1,114 @@ +import type { DesktopSshPasswordPromptRequest } from "@t3tools/contracts"; +import { useEffect, useRef, useState } from "react"; + +import { Button } from "../ui/button"; +import { + Dialog, + DialogDescription, + DialogFooter, + DialogHeader, + DialogPanel, + DialogPopup, + DialogTitle, +} from "../ui/dialog"; +import { Input } from "../ui/input"; + +function describeSshTarget(request: DesktopSshPasswordPromptRequest): string { + return request.username ? `${request.username}@${request.destination}` : request.destination; +} + +export function SshPasswordPromptDialog() { + const [queue, setQueue] = useState([]); + const [password, setPassword] = useState(""); + const currentRequest = queue[0] ?? null; + const inputRef = useRef(null); + + useEffect(() => { + const bridge = window.desktopBridge; + if (!bridge?.onSshPasswordPrompt) { + return; + } + + return bridge.onSshPasswordPrompt((request) => { + setQueue((currentQueue) => [...currentQueue, request]); + }); + }, []); + + useEffect(() => { + setPassword(""); + if (!currentRequest) { + return; + } + + const frame = window.requestAnimationFrame(() => { + inputRef.current?.focus(); + inputRef.current?.select(); + }); + return () => { + window.cancelAnimationFrame(frame); + }; + }, [currentRequest]); + + const respond = async (nextPassword: string | null) => { + if (!currentRequest) { + return; + } + + const requestId = currentRequest.requestId; + setQueue((currentQueue) => currentQueue.slice(1)); + setPassword(""); + try { + await window.desktopBridge?.resolveSshPasswordPrompt(requestId, nextPassword); + } catch (error) { + console.error("Failed to resolve SSH password prompt.", error); + } + }; + + const target = currentRequest ? describeSshTarget(currentRequest) : null; + + return ( + { + if (!open) { + void respond(null); + } + }} + > + + + SSH Password Required + + T3 needs your SSH password to connect to{" "} + {target ? {target} : "the remote host"}. The password is passed to the + local SSH process for this connection attempt and is not saved by T3 Code. + + + +
+

{currentRequest?.prompt}

+ setPassword(event.target.value)} + /> +
+

+ Use SSH keys to avoid repeated password prompts on new SSH sessions. +

+
+ + + + +
+
+ ); +} diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 3125378193..061feb4add 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -1,9 +1,11 @@ -import { ChevronDownIcon, PlusIcon, QrCodeIcon } from "lucide-react"; +import { ChevronDownIcon, PlusIcon, QrCodeIcon, RefreshCwIcon } from "lucide-react"; import { type ReactNode, memo, useCallback, useEffect, useMemo, useState } from "react"; import { type AuthClientSession, type AuthPairingLink, type AdvertisedEndpoint, + type DesktopDiscoveredSshHost, + type DesktopSshEnvironmentTarget, type DesktopServerExposureState, type EnvironmentId, } from "@t3tools/contracts"; @@ -30,6 +32,7 @@ import { DialogTitle, DialogTrigger, } from "../ui/dialog"; +import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../ui/collapsible"; import { AlertDialog, AlertDialogClose, @@ -67,6 +70,7 @@ import { useSavedEnvironmentRegistryStore, useSavedEnvironmentRuntimeStore, addSavedEnvironment, + connectDesktopSshEnvironment, getPrimaryEnvironmentConnection, reconnectSavedEnvironment, removeSavedEnvironment, @@ -165,6 +169,69 @@ function getSavedBackendStatusTooltip( : "Not connected yet."; } +function formatDesktopSshTarget(target: NonNullable): string { + const authority = target.username ? `${target.username}@${target.hostname}` : target.hostname; + return target.port ? `${authority}:${target.port}` : authority; +} + +function parseManualDesktopSshTarget(input: { + readonly host: string; + readonly username: string; + readonly port: string; +}): DesktopSshEnvironmentTarget { + const rawHost = input.host.trim(); + if (rawHost.length === 0) { + throw new Error("SSH host or alias is required."); + } + + let hostname = rawHost; + let username = input.username.trim() || null; + let port: number | null = null; + + const atIndex = hostname.lastIndexOf("@"); + if (atIndex > 0) { + const inlineUsername = hostname.slice(0, atIndex).trim(); + hostname = hostname.slice(atIndex + 1).trim(); + if (!username && inlineUsername.length > 0) { + username = inlineUsername; + } + } + + const bracketedHostMatch = /^\[([^\]]+)\](?::(\d+))?$/u.exec(hostname); + if (bracketedHostMatch) { + hostname = bracketedHostMatch[1]!.trim(); + if (bracketedHostMatch[2]) { + port = Number.parseInt(bracketedHostMatch[2], 10); + } + } else { + const colonSegments = hostname.split(":"); + if (colonSegments.length === 2 && /^\d+$/u.test(colonSegments[1] ?? "")) { + hostname = colonSegments[0]!.trim(); + port = Number.parseInt(colonSegments[1]!, 10); + } + } + + const rawPort = input.port.trim(); + if (rawPort.length > 0) { + port = Number.parseInt(rawPort, 10); + } + + if (hostname.length === 0) { + throw new Error("SSH host or alias is required."); + } + + if (port !== null && (!Number.isInteger(port) || port <= 0 || port > 65_535)) { + throw new Error("SSH port must be between 1 and 65535."); + } + + return { + alias: hostname, + hostname, + username, + port, + }; +} + /** Direct row in the card – same pattern as the Provider / ACP-agent list rows. */ const ITEM_ROW_CLASSNAME = "border-t border-border/60 px-4 py-4 first:border-t-0 sm:px-5"; const ENDPOINT_ROW_CLASSNAME = "border-t border-border/60 px-4 py-2.5 first:border-t-0 sm:px-5"; @@ -933,6 +1000,7 @@ function SavedBackendListRow({ const descriptorLabel = runtime?.descriptor?.label ?? null; const statusTooltip = getSavedBackendStatusTooltip(runtime, record, nowMs); const metadataBits = [ + record.desktopSsh ? `SSH ${formatDesktopSshTarget(record.desktopSsh)}` : null, roleLabel, record.lastConnectedAt ? `Last connected ${formatAccessTimestamp(record.lastConnectedAt)}` @@ -983,6 +1051,59 @@ function SavedBackendListRow({ ); } +interface DesktopSshHostRowProps { + target: DesktopDiscoveredSshHost; + savedRecord: SavedEnvironmentRecord | null; + connectingHostAlias: string | null; + onConnect: (target: DesktopDiscoveredSshHost) => void; +} + +const DesktopSshHostRow = memo(function DesktopSshHostRow({ + target, + savedRecord, + connectingHostAlias, + onConnect, +}: DesktopSshHostRowProps) { + const address = formatDesktopSshTarget(target); + const secondaryBits = [target.source === "ssh-config" ? "SSH config" : "Known hosts", address]; + const buttonLabel = + connectingHostAlias === target.alias ? "Connecting…" : savedRecord ? "Reconnect" : "Connect"; + + return ( +
+
+
+
+

{target.alias}

+
+

{secondaryBits.join(" · ")}

+ {savedRecord ? ( +

+ Saved as {savedRecord.label} + {savedRecord.lastConnectedAt + ? ` · Last connected ${formatAccessTimestamp(savedRecord.lastConnectedAt)}` + : ""} +

+ ) : null} +
+
+ +
+
+
+ ); +}); + export function ConnectionsSettings() { const desktopBridge = window.desktopBridge; const [currentSessionRole, setCurrentSessionRole] = useState<"owner" | "client" | null>( @@ -999,6 +1120,26 @@ export function ConnectionsSettings() { .map((record) => record.environmentId), [savedEnvironmentsById], ); + const savedDesktopSshEnvironmentsByAlias = useMemo( + () => + Object.values(savedEnvironmentsById).reduce>( + (accumulator, record) => { + if (record.desktopSsh?.alias) { + accumulator[record.desktopSsh.alias] = record; + } + return accumulator; + }, + {}, + ), + [savedEnvironmentsById], + ); + const [discoveredSshHosts, setDiscoveredSshHosts] = useState< + ReadonlyArray + >([]); + const [hasLoadedDiscoveredSshHosts, setHasLoadedDiscoveredSshHosts] = useState(false); + const [isLoadingDiscoveredSshHosts, setIsLoadingDiscoveredSshHosts] = useState(false); + const [discoveredSshHostsError, setDiscoveredSshHostsError] = useState(null); + const [connectingSshHostAlias, setConnectingSshHostAlias] = useState(null); const [desktopServerExposureState, setDesktopServerExposureState] = useState(null); @@ -1024,13 +1165,17 @@ export function ConnectionsSettings() { >(null); const [isRevokingOtherDesktopClients, setIsRevokingOtherDesktopClients] = useState(false); const [addBackendDialogOpen, setAddBackendDialogOpen] = useState(false); - const [savedBackendMode, setSavedBackendMode] = useState<"pairing-url" | "host-code">( + const [savedBackendMode, setSavedBackendMode] = useState<"pairing-url" | "host-code" | "ssh">( "pairing-url", ); const [savedBackendLabel, setSavedBackendLabel] = useState(""); const [savedBackendPairingUrl, setSavedBackendPairingUrl] = useState(""); const [savedBackendHost, setSavedBackendHost] = useState(""); const [savedBackendPairingCode, setSavedBackendPairingCode] = useState(""); + const [savedBackendSshHost, setSavedBackendSshHost] = useState(""); + const [savedBackendSshUsername, setSavedBackendSshUsername] = useState(""); + const [savedBackendSshPort, setSavedBackendSshPort] = useState(""); + const [isManualSshFormOpen, setIsManualSshFormOpen] = useState(false); const [savedBackendError, setSavedBackendError] = useState(null); const [isAddingSavedBackend, setIsAddingSavedBackend] = useState(false); const [reconnectingSavedEnvironmentId, setReconnectingSavedEnvironmentId] = @@ -1158,6 +1303,47 @@ export function ConnectionsSettings() { }, []); const handleAddSavedBackend = useCallback(async () => { + if (savedBackendMode === "ssh") { + setIsAddingSavedBackend(true); + setSavedBackendError(null); + try { + const target = parseManualDesktopSshTarget({ + host: savedBackendSshHost, + username: savedBackendSshUsername, + port: savedBackendSshPort, + }); + const record = await connectDesktopSshEnvironment(target, { + label: savedBackendLabel, + }); + setSavedBackendLabel(""); + setSavedBackendPairingUrl(""); + setSavedBackendHost(""); + setSavedBackendPairingCode(""); + setSavedBackendSshHost(""); + setSavedBackendSshUsername(""); + setSavedBackendSshPort(""); + setIsManualSshFormOpen(false); + setAddBackendDialogOpen(false); + toastManager.add({ + type: "success", + title: "Environment connected", + description: `${record.label} is ready over an SSH-managed tunnel.`, + }); + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to connect SSH host."; + setIsManualSshFormOpen(true); + setSavedBackendError(message); + toastManager.add({ + type: "error", + title: "Could not connect SSH host", + description: message, + }); + } finally { + setIsAddingSavedBackend(false); + } + return; + } + setIsAddingSavedBackend(true); setSavedBackendError(null); try { @@ -1174,6 +1360,10 @@ export function ConnectionsSettings() { setSavedBackendPairingUrl(""); setSavedBackendHost(""); setSavedBackendPairingCode(""); + setSavedBackendSshHost(""); + setSavedBackendSshUsername(""); + setSavedBackendSshPort(""); + setIsManualSshFormOpen(false); setAddBackendDialogOpen(false); toastManager.add({ type: "success", @@ -1199,6 +1389,9 @@ export function ConnectionsSettings() { savedBackendMode, savedBackendPairingCode, savedBackendPairingUrl, + savedBackendSshHost, + savedBackendSshPort, + savedBackendSshUsername, ]); const handleReconnectSavedBackend = useCallback(async (environmentId: EnvironmentId) => { @@ -1241,6 +1434,90 @@ export function ConnectionsSettings() { } }, []); + const loadDiscoveredSshHosts = useCallback(async () => { + if (!desktopBridge) { + setDiscoveredSshHosts([]); + setHasLoadedDiscoveredSshHosts(false); + setDiscoveredSshHostsError(null); + return; + } + + setIsLoadingDiscoveredSshHosts(true); + setDiscoveredSshHostsError(null); + try { + const hosts = await desktopBridge.discoverSshHosts(); + setDiscoveredSshHosts(hosts); + setHasLoadedDiscoveredSshHosts(true); + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to discover SSH hosts."; + setDiscoveredSshHostsError(message); + setHasLoadedDiscoveredSshHosts(true); + } finally { + setIsLoadingDiscoveredSshHosts(false); + } + }, [desktopBridge]); + + const handleConnectSshHost = useCallback( + async (target: DesktopSshEnvironmentTarget, label?: string) => { + setConnectingSshHostAlias(target.alias); + if (savedBackendMode === "ssh") { + setSavedBackendError(null); + } else { + setDiscoveredSshHostsError(null); + } + try { + const record = await connectDesktopSshEnvironment( + target, + label === undefined ? undefined : { label }, + ); + setSavedBackendLabel(""); + setSavedBackendSshHost(""); + setSavedBackendSshUsername(""); + setSavedBackendSshPort(""); + setAddBackendDialogOpen(false); + toastManager.add({ + type: "success", + title: savedDesktopSshEnvironmentsByAlias[target.alias] + ? "Environment reconnected" + : "Environment connected", + description: `${record.label} is ready over an SSH-managed tunnel.`, + }); + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to connect SSH host."; + if (savedBackendMode === "ssh") { + setSavedBackendError(message); + } else { + setDiscoveredSshHostsError(message); + } + toastManager.add({ + type: "error", + title: "Could not connect SSH host", + description: message, + }); + } finally { + setConnectingSshHostAlias(null); + } + }, + [savedBackendMode, savedDesktopSshEnvironmentsByAlias], + ); + + useEffect(() => { + if (!desktopBridge || !addBackendDialogOpen || savedBackendMode !== "ssh") { + return; + } + if (hasLoadedDiscoveredSshHosts || isLoadingDiscoveredSshHosts) { + return; + } + void loadDiscoveredSshHosts(); + }, [ + addBackendDialogOpen, + desktopBridge, + hasLoadedDiscoveredSshHosts, + isLoadingDiscoveredSshHosts, + loadDiscoveredSshHosts, + savedBackendMode, + ]); + useEffect(() => { if (desktopBridge) { setCurrentSessionRole("owner"); @@ -1598,6 +1875,7 @@ export function ConnectionsSettings() { setAddBackendDialogOpen(open); if (!open) { setSavedBackendError(null); + setIsManualSshFormOpen(false); } }} > @@ -1609,7 +1887,7 @@ export function ConnectionsSettings() { } /> - + Add Environment Pair another environment to this client. @@ -1640,6 +1918,21 @@ export function ConnectionsSettings() { > Host + code + {desktopBridge ? ( + + ) : null} @@ -1648,25 +1941,30 @@ export function ConnectionsSettings() {

Enter the full pairing URL from the environment you want to connect to.

- ) : ( + ) : savedBackendMode === "host-code" ? (

Enter the backend host and pairing code separately.

+ ) : ( +

+ Connect over SSH using an existing alias from ~/.ssh/config or + enter a host directly. T3 uses your current SSH agent and config. +

)} -
- - {savedBackendMode === "pairing-url" ? ( + {savedBackendMode === "pairing-url" ? ( +
+ - ) : ( - <> -
+ ) : savedBackendMode === "host-code" ? ( +
+ + + +
+ ) : ( +
+
+
+

Discovered hosts

+

+ From ~/.ssh/config and known_hosts. +

+
+ +
+ {discoveredSshHostsError ? ( +

{discoveredSshHostsError}

+ ) : null} +
+ {discoveredSshHosts.map((target) => ( + void handleConnectSshHost(nextTarget)} /> - -
+ {savedBackendError ? ( +

{savedBackendError}

+ ) : null} + + +
+

+ Enter a host manually +

+

+ Use an alias or enter host, username, and port directly. +

+
+ - - - )} -
- {savedBackendError ? ( + + +
+ + +
+ + +
+ + Uses your existing SSH keys, agent, and config. Password and + keyboard-interactive prompts open through your system SSH dialog when + needed. + + +
+
+ +
+ )} + {savedBackendMode !== "ssh" && savedBackendError ? (

{savedBackendError}

) : null} - + {savedBackendMode !== "ssh" ? ( + + ) : null}
diff --git a/apps/web/src/components/settings/SettingsPanels.browser.tsx b/apps/web/src/components/settings/SettingsPanels.browser.tsx index 92c5917889..6a70003405 100644 --- a/apps/web/src/components/settings/SettingsPanels.browser.tsx +++ b/apps/web/src/components/settings/SettingsPanels.browser.tsx @@ -114,6 +114,8 @@ const authAccessHarness = vi.hoisted(() => { }; }); +const mockConnectDesktopSshEnvironment = vi.fn(); + vi.mock("../../environments/runtime", () => { const primaryConnection = { kind: "primary" as const, @@ -151,6 +153,7 @@ vi.mock("../../environments/runtime", () => { new URL(path, "http://localhost:3000").toString(), waitForSavedEnvironmentRegistryHydration: async () => undefined, addSavedEnvironment: vi.fn(), + connectDesktopSshEnvironment: mockConnectDesktopSshEnvironment, disconnectSavedEnvironment: vi.fn(), ensureEnvironmentConnectionBootstrapped: async () => undefined, getPrimaryEnvironmentConnection: () => primaryConnection, @@ -258,6 +261,7 @@ function makeClientSession(input: { } const createDesktopBridgeStub = (overrides?: { + readonly discoverSshHosts?: DesktopBridge["discoverSshHosts"]; readonly serverExposureState?: Awaited>; readonly advertisedEndpoints?: Awaited>; readonly setServerExposureMode?: DesktopBridge["setServerExposureMode"]; @@ -295,6 +299,50 @@ const createDesktopBridgeStub = (overrides?: { getSavedEnvironmentSecret: vi.fn().mockResolvedValue(null), setSavedEnvironmentSecret: vi.fn().mockResolvedValue(true), removeSavedEnvironmentSecret: vi.fn().mockResolvedValue(undefined), + discoverSshHosts: overrides?.discoverSshHosts ?? vi.fn().mockResolvedValue([]), + ensureSshEnvironment: vi.fn().mockImplementation(async (target) => ({ + target, + httpBaseUrl: "http://127.0.0.1:3774/", + wsBaseUrl: "ws://127.0.0.1:3774/", + pairingToken: "ssh-pairing-token", + })), + fetchSshEnvironmentDescriptor: vi.fn().mockResolvedValue({ + environmentId: "environment-ssh", + label: "SSH environment", + platform: { + os: "linux", + arch: "x64", + }, + serverVersion: "0.0.0-test", + capabilities: { + repositoryIdentity: true, + }, + }), + bootstrapSshBearerSession: vi.fn().mockResolvedValue({ + authenticated: true, + role: "owner", + sessionMethod: "bearer-session-token", + expiresAt: "2026-05-01T12:00:00.000Z", + sessionToken: "ssh-bearer-token", + }), + fetchSshSessionState: vi.fn().mockResolvedValue({ + authenticated: true, + auth: { + policy: "remote-reachable", + bootstrapMethods: ["one-time-token"], + sessionMethods: ["browser-session-cookie", "bearer-session-token"], + sessionCookieName: "t3_session", + }, + role: "owner", + sessionMethod: "bearer-session-token", + expiresAt: "2026-05-01T12:00:00.000Z", + }), + issueSshWebSocketToken: vi.fn().mockResolvedValue({ + token: "ssh-ws-token", + expiresAt: "2026-05-01T12:05:00.000Z", + }), + onSshPasswordPrompt: vi.fn(() => () => {}), + resolveSshPasswordPrompt: vi.fn().mockResolvedValue(undefined), getServerExposureState: vi.fn().mockResolvedValue( overrides?.serverExposureState ?? { mode: "local-only", @@ -348,6 +396,7 @@ describe("GeneralSettingsPanel observability", () => { localStorage.clear(); useUiStateStore.setState({ defaultAdvertisedEndpointKey: null }); authAccessHarness.reset(); + mockConnectDesktopSshEnvironment.mockReset(); }); afterEach(async () => { @@ -852,6 +901,70 @@ describe("GeneralSettingsPanel observability", () => { .toBeInTheDocument(); }); + it("adds desktop ssh environments from the add-environment dialog", async () => { + const discoverSshHosts = vi.fn().mockResolvedValue([ + { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 22, + source: "ssh-config" as const, + }, + ]); + window.desktopBridge = createDesktopBridgeStub({ + discoverSshHosts, + }); + mockConnectDesktopSshEnvironment.mockResolvedValue({ + environmentId: EnvironmentId.make("environment-devbox"), + label: "Build box", + wsBaseUrl: "ws://127.0.0.1:3774/", + httpBaseUrl: "http://127.0.0.1:3774/", + createdAt: "2036-04-07T00:00:00.000Z", + lastConnectedAt: "2036-04-07T00:00:00.000Z", + desktopSsh: { + alias: "devbox.example.com", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + }, + }); + + setServerConfigSnapshot(createBaseServerConfig()); + + mounted = await render( + + + , + ); + + await page.getByRole("button", { name: "Add environment", exact: true }).click(); + await expect.element(page.getByText("Add Environment")).toBeInTheDocument(); + await page.getByRole("button", { name: "SSH", exact: true }).click(); + await vi.waitFor(() => { + expect(discoverSshHosts).toHaveBeenCalledTimes(1); + }); + await expect.element(page.getByText("devbox")).toBeInTheDocument(); + + await page.getByText("Enter a host manually").click(); + await page.getByLabelText("Label").fill("Build box"); + await page.getByLabelText("SSH host or alias").fill("devbox.example.com"); + await page.getByLabelText("Username").fill("julius"); + await page.getByLabelText("Port").fill("2222"); + await page.getByRole("button", { name: "Connect SSH host", exact: true }).click(); + + await vi.waitFor(() => { + expect(mockConnectDesktopSshEnvironment).toHaveBeenCalledWith( + { + alias: "devbox.example.com", + hostname: "devbox.example.com", + username: "julius", + port: 2222, + }, + { label: "Build box" }, + ); + }); + }); + it("opens the logs folder in the preferred editor", async () => { const openInEditor = vi.fn().mockResolvedValue(undefined); window.nativeApi = { diff --git a/apps/web/src/components/ui/dialog.tsx b/apps/web/src/components/ui/dialog.tsx index 080ac80997..bebe1b0ee0 100644 --- a/apps/web/src/components/ui/dialog.tsx +++ b/apps/web/src/components/ui/dialog.tsx @@ -24,7 +24,7 @@ function DialogBackdrop({ className, ...props }: DialogPrimitive.Backdrop.Props) return ( ({ resolveRemotePairingTarget: mockResolveRemotePairingTarget, @@ -18,6 +22,7 @@ vi.mock("../remote/api", () => ({ bootstrapRemoteBearerSession: mockBootstrapRemoteBearerSession, fetchRemoteEnvironmentDescriptor: mockFetchRemoteEnvironmentDescriptor, fetchRemoteSessionState: vi.fn(), + isRemoteEnvironmentAuthHttpError: vi.fn(() => false), resolveRemoteWebSocketConnectionUrl: vi.fn(), })); @@ -55,18 +60,37 @@ vi.mock("./catalog", () => ({ })); vi.mock("./connection", () => ({ - createEnvironmentConnection: vi.fn(), + createEnvironmentConnection: mockCreateEnvironmentConnection, })); describe("addSavedEnvironment", () => { beforeEach(() => { vi.resetModules(); vi.clearAllMocks(); - mockResolveRemotePairingTarget.mockReturnValue({ - httpBaseUrl: "https://remote.example.com/", - wsBaseUrl: "wss://remote.example.com/", - credential: "pairing-code", + vi.stubGlobal("window", { + desktopBridge: { + ensureSshEnvironment: mockEnsureSshEnvironment, + fetchSshEnvironmentDescriptor: mockFetchSshEnvironmentDescriptor, + bootstrapSshBearerSession: mockBootstrapSshBearerSession, + fetchSshSessionState: vi.fn(), + issueSshWebSocketToken: vi.fn(), + }, }); + mockResolveRemotePairingTarget.mockImplementation( + (input: { host?: string; pairingCode?: string }) => ({ + httpBaseUrl: input.host + ? input.host.endsWith("/") + ? input.host + : `${input.host}/` + : "https://remote.example.com/", + wsBaseUrl: input.host + ? input.host.replace(/^http/u, "ws").endsWith("/") + ? input.host.replace(/^http/u, "ws") + : `${input.host.replace(/^http/u, "ws")}/` + : "wss://remote.example.com/", + credential: input.pairingCode ?? "pairing-code", + }), + ); mockFetchRemoteEnvironmentDescriptor.mockResolvedValue({ environmentId: EnvironmentId.make("environment-1"), label: "Remote environment", @@ -75,10 +99,40 @@ describe("addSavedEnvironment", () => { sessionToken: "bearer-token", role: "owner", }); + mockFetchSshEnvironmentDescriptor.mockResolvedValue({ + environmentId: EnvironmentId.make("environment-1"), + label: "Remote environment", + }); + mockBootstrapSshBearerSession.mockResolvedValue({ + sessionToken: "ssh-bearer-token", + role: "owner", + }); mockPersistSavedEnvironmentRecord.mockResolvedValue(undefined); mockWriteSavedEnvironmentBearerToken.mockResolvedValue(false); mockSetSavedEnvironmentRegistry.mockResolvedValue(undefined); mockListSavedEnvironmentRecords.mockReturnValue([]); + mockCreateEnvironmentConnection.mockImplementation( + (input: { knownEnvironment: { environmentId: EnvironmentId }; client: unknown }) => ({ + kind: "saved", + environmentId: input.knownEnvironment.environmentId, + knownEnvironment: input.knownEnvironment, + client: input.client, + ensureBootstrapped: async () => undefined, + reconnect: async () => undefined, + dispose: async () => undefined, + }), + ); + mockEnsureSshEnvironment.mockResolvedValue({ + target: { + alias: "devbox", + hostname: "devbox.example.com", + username: "julius", + port: 22, + }, + httpBaseUrl: "http://127.0.0.1:3774/", + wsBaseUrl: "ws://127.0.0.1:3774/", + pairingToken: "ssh-pairing-code", + }); }); it("rolls back persisted metadata when bearer token persistence fails", async () => { @@ -102,4 +156,46 @@ describe("addSavedEnvironment", () => { await resetEnvironmentServiceForTests(); }); + + it("bootstraps a desktop ssh environment through the desktop bridge", async () => { + mockWriteSavedEnvironmentBearerToken.mockResolvedValue(true); + + const { connectDesktopSshEnvironment, resetEnvironmentServiceForTests } = + await import("./service"); + + await expect( + connectDesktopSshEnvironment({ + alias: "devbox", + hostname: "devbox", + username: null, + port: null, + }), + ).rejects.toThrow(); + + expect(mockEnsureSshEnvironment).toHaveBeenCalledWith( + { + alias: "devbox", + hostname: "devbox", + username: null, + port: null, + }, + { issuePairingToken: true }, + ); + expect(mockResolveRemotePairingTarget).toHaveBeenCalledWith({ + host: "http://127.0.0.1:3774/", + pairingCode: "ssh-pairing-code", + }); + expect(mockFetchSshEnvironmentDescriptor).toHaveBeenCalledWith("http://127.0.0.1:3774/"); + expect(mockBootstrapSshBearerSession).toHaveBeenCalledWith( + "http://127.0.0.1:3774/", + "ssh-pairing-code", + ); + expect(mockFetchRemoteEnvironmentDescriptor).not.toHaveBeenCalled(); + expect(mockBootstrapRemoteBearerSession).not.toHaveBeenCalled(); + expect(mockUpsert.mock.invocationCallOrder[0]).toBeLessThan( + mockCreateEnvironmentConnection.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY, + ); + + await resetEnvironmentServiceForTests(); + }); }); diff --git a/apps/web/src/environments/runtime/service.ts b/apps/web/src/environments/runtime/service.ts index 6c58f9e8a5..be20cbd1fd 100644 --- a/apps/web/src/environments/runtime/service.ts +++ b/apps/web/src/environments/runtime/service.ts @@ -1,5 +1,7 @@ import { type AuthSessionRole, + type DesktopSshEnvironmentBootstrap, + type DesktopSshEnvironmentTarget, type EnvironmentId, type OrchestrationEvent, type OrchestrationShellSnapshot, @@ -33,6 +35,7 @@ import { bootstrapRemoteBearerSession, fetchRemoteEnvironmentDescriptor, fetchRemoteSessionState, + isRemoteEnvironmentAuthHttpError, resolveRemoteWebSocketConnectionUrl, } from "../remote/api"; import { resolveRemotePairingTarget } from "../remote/target"; @@ -85,6 +88,7 @@ type ThreadDetailSubscriptionEntry = { }; const environmentConnections = new Map(); +const pendingSavedEnvironmentConnections = new Map>(); const environmentConnectionListeners = new Set<() => void>(); const threadDetailSubscriptions = new Map(); const lastAppliedProjectionVersionByEnvironment = new Map< @@ -491,6 +495,163 @@ function isoNow(): string { return new Date().toISOString(); } +function serializeSavedEnvironmentRecord(record: SavedEnvironmentRecord) { + return { + environmentId: record.environmentId, + label: record.label, + httpBaseUrl: record.httpBaseUrl, + wsBaseUrl: record.wsBaseUrl, + createdAt: record.createdAt, + lastConnectedAt: record.lastConnectedAt, + ...(record.desktopSsh ? { desktopSsh: record.desktopSsh } : {}), + } as const; +} + +function isDesktopSshTargetEqual( + left: DesktopSshEnvironmentTarget | undefined, + right: DesktopSshEnvironmentTarget | undefined, +): boolean { + if (!left || !right) { + return false; + } + + return ( + left.alias === right.alias && + left.hostname === right.hostname && + left.username === right.username && + left.port === right.port + ); +} + +function findSavedEnvironmentRecordByDesktopSshTarget( + target: DesktopSshEnvironmentTarget | undefined, +): SavedEnvironmentRecord | null { + if (!target) { + return null; + } + + return ( + listSavedEnvironmentRecords().find((record) => + isDesktopSshTargetEqual(record.desktopSsh, target), + ) ?? null + ); +} + +async function persistSavedEnvironmentRegistryRollback(): Promise { + await ensureLocalApi().persistence.setSavedEnvironmentRegistry( + listSavedEnvironmentRecords().map((entry) => serializeSavedEnvironmentRecord(entry)), + ); +} + +async function resolveDesktopSshEnvironmentBootstrap( + target: DesktopSshEnvironmentTarget, + options?: { readonly issuePairingToken?: boolean }, +): Promise { + const desktopBridge = window.desktopBridge; + if (!desktopBridge) { + throw new Error("SSH launch is only available in the desktop app."); + } + + return await desktopBridge.ensureSshEnvironment(target, options); +} + +function getDesktopSshBridge() { + const desktopBridge = window.desktopBridge; + if (!desktopBridge) { + throw new Error("SSH launch is only available in the desktop app."); + } + return desktopBridge; +} + +async function fetchDesktopSshEnvironmentDescriptor(httpBaseUrl: string) { + return await getDesktopSshBridge().fetchSshEnvironmentDescriptor(httpBaseUrl); +} + +async function bootstrapDesktopSshBearerSession(httpBaseUrl: string, credential: string) { + return await getDesktopSshBridge().bootstrapSshBearerSession(httpBaseUrl, credential); +} + +async function fetchDesktopSshSessionState(httpBaseUrl: string, bearerToken: string) { + return await getDesktopSshBridge().fetchSshSessionState(httpBaseUrl, bearerToken); +} + +async function resolveDesktopSshWebSocketConnectionUrl( + wsBaseUrl: string, + httpBaseUrl: string, + bearerToken: string, +) { + const issued = await getDesktopSshBridge().issueSshWebSocketToken(httpBaseUrl, bearerToken); + const url = new URL(wsBaseUrl, window.location.origin); + url.searchParams.set("wsToken", issued.token); + return url.toString(); +} + +async function prepareSavedEnvironmentRecordForConnection( + record: SavedEnvironmentRecord, + options?: { readonly issuePairingToken?: boolean }, +): Promise<{ + readonly record: SavedEnvironmentRecord; + readonly pairingToken: string | null; +}> { + if (!record.desktopSsh) { + return { record, pairingToken: null }; + } + + const bootstrap = await resolveDesktopSshEnvironmentBootstrap(record.desktopSsh, options); + const nextRecord: SavedEnvironmentRecord = { + ...record, + httpBaseUrl: bootstrap.httpBaseUrl, + wsBaseUrl: bootstrap.wsBaseUrl, + desktopSsh: bootstrap.target, + }; + + if ( + nextRecord.httpBaseUrl !== record.httpBaseUrl || + nextRecord.wsBaseUrl !== record.wsBaseUrl || + !isDesktopSshTargetEqual(nextRecord.desktopSsh, record.desktopSsh) + ) { + await persistSavedEnvironmentRecord(nextRecord); + useSavedEnvironmentRegistryStore.getState().upsert(nextRecord); + } + + return { + record: nextRecord, + pairingToken: bootstrap.pairingToken, + }; +} + +async function issueDesktopSshBearerSession(record: SavedEnvironmentRecord): Promise<{ + readonly record: SavedEnvironmentRecord; + readonly bearerToken: string; + readonly role: AuthSessionRole | null; +}> { + const prepared = await prepareSavedEnvironmentRecordForConnection(record, { + issuePairingToken: true, + }); + if (!prepared.pairingToken) { + throw new Error("Desktop SSH launch did not return a pairing token."); + } + + const bearerSession = await bootstrapDesktopSshBearerSession( + prepared.record.httpBaseUrl, + prepared.pairingToken, + ); + const didPersistBearerToken = await writeSavedEnvironmentBearerToken( + prepared.record.environmentId, + bearerSession.sessionToken, + ); + if (!didPersistBearerToken) { + await persistSavedEnvironmentRegistryRollback(); + throw new Error("Unable to persist saved environment credentials."); + } + + return { + record: prepared.record, + bearerToken: bearerSession.sessionToken, + role: bearerSession.role ?? null, + }; +} + function setRuntimeConnecting(environmentId: EnvironmentId) { useSavedEnvironmentRuntimeStore.getState().patch(environmentId, { connectionState: "connecting", @@ -802,35 +963,46 @@ function createPrimaryEnvironmentClient( } function createSavedEnvironmentClient( - record: SavedEnvironmentRecord, + environmentId: EnvironmentId, bearerToken: string, ): WsRpcClient { - useSavedEnvironmentRuntimeStore.getState().ensure(record.environmentId); + useSavedEnvironmentRuntimeStore.getState().ensure(environmentId); return createWsRpcClient( new WsTransport( - () => - resolveRemoteWebSocketConnectionUrl({ - wsBaseUrl: record.wsBaseUrl, - httpBaseUrl: record.httpBaseUrl, - bearerToken, - }), + async () => { + const record = getSavedEnvironmentRecord(environmentId); + if (!record) { + throw new Error(`Saved environment ${environmentId} not found.`); + } + return record.desktopSsh + ? await resolveDesktopSshWebSocketConnectionUrl( + record.wsBaseUrl, + record.httpBaseUrl, + bearerToken, + ) + : await resolveRemoteWebSocketConnectionUrl({ + wsBaseUrl: record.wsBaseUrl, + httpBaseUrl: record.httpBaseUrl, + bearerToken, + }); + }, { onAttempt: () => { - setRuntimeConnecting(record.environmentId); + setRuntimeConnecting(environmentId); }, onOpen: () => { - setRuntimeConnected(record.environmentId); + setRuntimeConnected(environmentId); }, onError: (message: string) => { - useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { + useSavedEnvironmentRuntimeStore.getState().patch(environmentId, { connectionState: "error", lastError: message, lastErrorAt: isoNow(), }); }, onClose: (details: { readonly code: number; readonly reason: string }) => { - setRuntimeDisconnected(record.environmentId, details.reason); + setRuntimeDisconnected(environmentId, details.reason); }, }, ), @@ -838,18 +1010,25 @@ function createSavedEnvironmentClient( } async function refreshSavedEnvironmentMetadata( - record: SavedEnvironmentRecord, + environmentId: EnvironmentId, bearerToken: string, client: WsRpcClient, roleHint?: AuthSessionRole | null, configHint?: ServerConfig | null, ): Promise { + const record = getSavedEnvironmentRecord(environmentId); + if (!record) { + throw new Error(`Saved environment ${environmentId} not found.`); + } + const [serverConfig, sessionState] = await Promise.all([ configHint ? Promise.resolve(configHint) : client.server.getConfig(), - fetchRemoteSessionState({ - httpBaseUrl: record.httpBaseUrl, - bearerToken, - }), + record.desktopSsh + ? fetchDesktopSshSessionState(record.httpBaseUrl, bearerToken) + : fetchRemoteSessionState({ + httpBaseUrl: record.httpBaseUrl, + bearerToken, + }), ]); useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { @@ -923,69 +1102,124 @@ async function ensureSavedEnvironmentConnection( return existing; } - const bearerToken = - options?.bearerToken ?? (await readSavedEnvironmentBearerToken(record.environmentId)); - if (!bearerToken) { - useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { - authState: "requires-auth", - role: null, - connectionState: "disconnected", - lastError: "Saved environment is missing its saved credential. Pair it again.", - lastErrorAt: isoNow(), + const pending = pendingSavedEnvironmentConnections.get(record.environmentId); + if (pending) { + return pending; + } + + const nextConnection = (async () => { + let activeRecord = record; + let roleHint = options?.role ?? null; + let bearerToken = + options?.bearerToken ?? (await readSavedEnvironmentBearerToken(record.environmentId)); + if (!bearerToken) { + if (record.desktopSsh) { + const issued = await issueDesktopSshBearerSession(record); + activeRecord = issued.record; + bearerToken = issued.bearerToken; + roleHint = issued.role; + } else { + useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { + authState: "requires-auth", + role: null, + connectionState: "disconnected", + lastError: "Saved environment is missing its saved credential. Pair it again.", + lastErrorAt: isoNow(), + }); + throw new Error("Saved environment is missing its saved credential."); + } + } else { + const prepared = await prepareSavedEnvironmentRecordForConnection(record); + activeRecord = prepared.record; + } + + const activeBearerToken = bearerToken; + const client = + options?.client ?? + createSavedEnvironmentClient(activeRecord.environmentId, activeBearerToken); + const knownEnvironment = createKnownEnvironment({ + id: activeRecord.environmentId, + label: activeRecord.label, + source: "manual", + target: { + httpBaseUrl: activeRecord.httpBaseUrl, + wsBaseUrl: activeRecord.wsBaseUrl, + }, }); - throw new Error("Saved environment is missing its saved credential."); - } - - const client = options?.client ?? createSavedEnvironmentClient(record, bearerToken); - const knownEnvironment = createKnownEnvironment({ - id: record.environmentId, - label: record.label, - source: "manual", - target: { - httpBaseUrl: record.httpBaseUrl, - wsBaseUrl: record.wsBaseUrl, - }, - }); - const connection = createEnvironmentConnection({ - kind: "saved", - knownEnvironment: { - ...knownEnvironment, - environmentId: record.environmentId, - }, - client, - refreshMetadata: async () => { - await refreshSavedEnvironmentMetadata(record, bearerToken, client); - }, - onConfigSnapshot: (config) => { - useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { - descriptor: config.environment, - serverConfig: config, - }); - }, - onWelcome: (payload) => { - useSavedEnvironmentRuntimeStore.getState().patch(record.environmentId, { - descriptor: payload.environment, - }); - }, - ...createEnvironmentConnectionHandlers(), - }); - - registerConnection(connection); - - try { - await refreshSavedEnvironmentMetadata( - record, - bearerToken, + const connection = createEnvironmentConnection({ + kind: "saved", + knownEnvironment: { + ...knownEnvironment, + environmentId: activeRecord.environmentId, + }, client, - options?.role ?? null, - options?.serverConfig ?? null, - ); - return connection; - } catch (error) { - setRuntimeError(record.environmentId, error); - await removeConnection(record.environmentId).catch(() => false); - throw error; - } + refreshMetadata: async () => { + await refreshSavedEnvironmentMetadata( + activeRecord.environmentId, + activeBearerToken, + client, + ); + }, + onConfigSnapshot: (config) => { + useSavedEnvironmentRuntimeStore.getState().patch(activeRecord.environmentId, { + descriptor: config.environment, + serverConfig: config, + }); + }, + onWelcome: (payload) => { + useSavedEnvironmentRuntimeStore.getState().patch(activeRecord.environmentId, { + descriptor: payload.environment, + }); + }, + ...createEnvironmentConnectionHandlers(), + }); + + registerConnection(connection); + + try { + try { + await refreshSavedEnvironmentMetadata( + activeRecord.environmentId, + activeBearerToken, + client, + roleHint, + options?.serverConfig ?? null, + ); + } catch (error) { + if ( + !record.desktopSsh || + !isRemoteEnvironmentAuthHttpError(error) || + error.status !== 401 + ) { + throw error; + } + + const issued = await issueDesktopSshBearerSession(activeRecord); + activeRecord = issued.record; + bearerToken = issued.bearerToken; + roleHint = issued.role; + await removeConnection(activeRecord.environmentId).catch(() => false); + pendingSavedEnvironmentConnections.delete(activeRecord.environmentId); + return await ensureSavedEnvironmentConnection(activeRecord, { + bearerToken, + role: roleHint, + serverConfig: options?.serverConfig ?? null, + }); + } + return connection; + } catch (error) { + setRuntimeError(activeRecord.environmentId, error); + await removeConnection(activeRecord.environmentId).catch(() => false); + throw error; + } + })(); + + pendingSavedEnvironmentConnections.set(record.environmentId, nextConnection); + return await nextConnection.finally(() => { + if (pendingSavedEnvironmentConnections.get(record.environmentId) === nextConnection) { + pendingSavedEnvironmentConnections.delete(record.environmentId); + } + }); } async function syncSavedEnvironmentConnections( @@ -1063,8 +1297,22 @@ export async function reconnectSavedEnvironment(environmentId: EnvironmentId): P setRuntimeConnecting(environmentId); try { + if (record.desktopSsh) { + await prepareSavedEnvironmentRecordForConnection(record); + } await connection.reconnect(); } catch (error) { + if (record.desktopSsh) { + const issued = await issueDesktopSshBearerSession( + getSavedEnvironmentRecord(environmentId) ?? record, + ); + await removeConnection(environmentId).catch(() => false); + await ensureSavedEnvironmentConnection(issued.record, { + bearerToken: issued.bearerToken, + role: issued.role, + }); + return; + } setRuntimeError(environmentId, error); throw error; } @@ -1081,33 +1329,40 @@ export async function addSavedEnvironment(input: { readonly pairingUrl?: string; readonly host?: string; readonly pairingCode?: string; + readonly desktopSsh?: DesktopSshEnvironmentTarget; }): Promise { const resolvedTarget = resolveRemotePairingTarget({ ...(input.pairingUrl !== undefined ? { pairingUrl: input.pairingUrl } : {}), ...(input.host !== undefined ? { host: input.host } : {}), ...(input.pairingCode !== undefined ? { pairingCode: input.pairingCode } : {}), }); - const descriptor = await fetchRemoteEnvironmentDescriptor({ - httpBaseUrl: resolvedTarget.httpBaseUrl, - }); + const descriptor = input.desktopSsh + ? await fetchDesktopSshEnvironmentDescriptor(resolvedTarget.httpBaseUrl) + : await fetchRemoteEnvironmentDescriptor({ + httpBaseUrl: resolvedTarget.httpBaseUrl, + }); const environmentId = descriptor.environmentId; + const existingRecord = + getSavedEnvironmentRecord(environmentId) ?? + findSavedEnvironmentRecordByDesktopSshTarget(input.desktopSsh); - if (environmentConnections.has(environmentId)) { - throw new Error("This environment is already connected."); - } - - const bearerSession = await bootstrapRemoteBearerSession({ - httpBaseUrl: resolvedTarget.httpBaseUrl, - credential: resolvedTarget.credential, - }); + const bearerSession = input.desktopSsh + ? await bootstrapDesktopSshBearerSession(resolvedTarget.httpBaseUrl, resolvedTarget.credential) + : await bootstrapRemoteBearerSession({ + httpBaseUrl: resolvedTarget.httpBaseUrl, + credential: resolvedTarget.credential, + }); const record: SavedEnvironmentRecord = { environmentId, - label: input.label.trim() || descriptor.label, + label: input.label.trim() || existingRecord?.label || descriptor.label, wsBaseUrl: resolvedTarget.wsBaseUrl, httpBaseUrl: resolvedTarget.httpBaseUrl, - createdAt: isoNow(), + createdAt: existingRecord?.createdAt ?? isoNow(), lastConnectedAt: isoNow(), + ...((input.desktopSsh ?? existingRecord?.desktopSsh) + ? { desktopSsh: input.desktopSsh ?? existingRecord?.desktopSsh } + : {}), }; await persistSavedEnvironmentRecord(record); @@ -1116,26 +1371,37 @@ export async function addSavedEnvironment(input: { bearerSession.sessionToken, ); if (!didPersistBearerToken) { - await ensureLocalApi().persistence.setSavedEnvironmentRegistry( - listSavedEnvironmentRecords().map((entry) => ({ - environmentId: entry.environmentId, - label: entry.label, - httpBaseUrl: entry.httpBaseUrl, - wsBaseUrl: entry.wsBaseUrl, - createdAt: entry.createdAt, - lastConnectedAt: entry.lastConnectedAt, - })), - ); + await persistSavedEnvironmentRegistryRollback(); throw new Error("Unable to persist saved environment credentials."); } + useSavedEnvironmentRegistryStore.getState().upsert(record); + await removeConnection(environmentId).catch(() => false); await ensureSavedEnvironmentConnection(record, { bearerToken: bearerSession.sessionToken, role: bearerSession.role, }); - useSavedEnvironmentRegistryStore.getState().upsert(record); return record; } +export async function connectDesktopSshEnvironment( + target: DesktopSshEnvironmentTarget, + options?: { label?: string }, +): Promise { + const bootstrap = await resolveDesktopSshEnvironmentBootstrap(target, { + issuePairingToken: true, + }); + if (!bootstrap.pairingToken) { + throw new Error("Desktop SSH launch did not return a pairing token."); + } + + return await addSavedEnvironment({ + label: options?.label?.trim() || bootstrap.target.alias, + host: bootstrap.httpBaseUrl, + pairingCode: bootstrap.pairingToken, + desktopSsh: bootstrap.target, + }); +} + export async function ensureEnvironmentConnectionBootstrapped( environmentId: EnvironmentId, ): Promise { @@ -1211,6 +1477,7 @@ export function startEnvironmentConnectionService(queryClient: QueryClient): () export async function resetEnvironmentServiceForTests(): Promise { stopActiveService(); lastAppliedProjectionVersionByEnvironment.clear(); + pendingSavedEnvironmentConnections.clear(); for (const key of Array.from(threadDetailSubscriptions.keys())) { disposeThreadDetailSubscriptionByKey(key); } diff --git a/apps/web/src/localApi.test.ts b/apps/web/src/localApi.test.ts index de5f057875..a02241315f 100644 --- a/apps/web/src/localApi.test.ts +++ b/apps/web/src/localApi.test.ts @@ -169,6 +169,24 @@ function makeDesktopBridge(overrides: Partial = {}): DesktopBridg getSavedEnvironmentSecret: async () => null, setSavedEnvironmentSecret: async () => true, removeSavedEnvironmentSecret: async () => undefined, + discoverSshHosts: async () => [], + ensureSshEnvironment: async () => { + throw new Error("ensureSshEnvironment not implemented in test"); + }, + fetchSshEnvironmentDescriptor: async () => { + throw new Error("fetchSshEnvironmentDescriptor not implemented in test"); + }, + bootstrapSshBearerSession: async () => { + throw new Error("bootstrapSshBearerSession not implemented in test"); + }, + fetchSshSessionState: async () => { + throw new Error("fetchSshSessionState not implemented in test"); + }, + issueSshWebSocketToken: async () => { + throw new Error("issueSshWebSocketToken not implemented in test"); + }, + onSshPasswordPrompt: () => () => undefined, + resolveSshPasswordPrompt: async () => undefined, getServerExposureState: async () => ({ mode: "local-only", endpointUrl: null, diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index df62be29df..7c38d77853 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -13,6 +13,7 @@ import { QueryClient, useQueryClient } from "@tanstack/react-query"; import { APP_DISPLAY_NAME } from "../branding"; import { AppSidebarLayout } from "../components/AppSidebarLayout"; import { CommandPalette } from "../components/CommandPalette"; +import { SshPasswordPromptDialog } from "../components/desktop/SshPasswordPromptDialog"; import { SlowRpcAckToastCoordinator, WebSocketConnectionCoordinator, @@ -145,6 +146,7 @@ function RootRouteView() { {primaryEnvironmentAuthenticated ? : null} {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? : null} {primaryEnvironmentAuthenticated ? : null} diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 1aeaf02a45..fc0413e468 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -52,9 +52,11 @@ import type { OrchestrationThreadStreamItem, } from "./orchestration.ts"; import type { EnvironmentId } from "./baseSchemas.ts"; +import type { AuthBearerBootstrapResult, AuthSessionState, AuthWebSocketTokenResult } from "./auth.ts"; import type { AdvertisedEndpoint } from "./remoteAccess.ts"; import { EditorId } from "./editor.ts"; -import { ServerSettings, type ClientSettings, type ServerSettingsPatch } from "./settings.ts"; +import type { ExecutionEnvironmentDescriptor } from "./environment.ts"; +import { ClientSettings, ServerSettings, ServerSettingsPatch } from "./settings.ts"; export interface ContextMenuItem { id: T; @@ -126,6 +128,33 @@ export interface DesktopEnvironmentBootstrap { bootstrapToken?: string; } +export interface DesktopSshEnvironmentTarget { + alias: string; + hostname: string; + username: string | null; + port: number | null; +} + +export type DesktopSshHostSource = "ssh-config" | "known-hosts"; + +export interface DesktopDiscoveredSshHost extends DesktopSshEnvironmentTarget { + source: DesktopSshHostSource; +} + +export interface DesktopSshEnvironmentBootstrap { + target: DesktopSshEnvironmentTarget; + httpBaseUrl: string; + wsBaseUrl: string; + pairingToken: string | null; +} + +export interface DesktopSshPasswordPromptRequest { + requestId: string; + destination: string; + username: string | null; + prompt: string; +} + export interface PersistedSavedEnvironmentRecord { environmentId: EnvironmentId; label: string; @@ -133,6 +162,7 @@ export interface PersistedSavedEnvironmentRecord { httpBaseUrl: string; createdAt: string; lastConnectedAt: string | null; + desktopSsh?: DesktopSshEnvironmentTarget; } export type DesktopServerExposureMode = "local-only" | "network-accessible"; @@ -159,6 +189,23 @@ export interface DesktopBridge { getSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise; setSavedEnvironmentSecret: (environmentId: EnvironmentId, secret: string) => Promise; removeSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise; + discoverSshHosts: () => Promise; + ensureSshEnvironment: ( + target: DesktopSshEnvironmentTarget, + options?: { issuePairingToken?: boolean }, + ) => Promise; + fetchSshEnvironmentDescriptor: (httpBaseUrl: string) => Promise; + bootstrapSshBearerSession: ( + httpBaseUrl: string, + credential: string, + ) => Promise; + fetchSshSessionState: (httpBaseUrl: string, bearerToken: string) => Promise; + issueSshWebSocketToken: ( + httpBaseUrl: string, + bearerToken: string, + ) => Promise; + onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => () => void; + resolveSshPasswordPrompt: (requestId: string, password: string | null) => Promise; getServerExposureState: () => Promise; setServerExposureMode: (mode: DesktopServerExposureMode) => Promise; getAdvertisedEndpoints: () => Promise;