The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { |
| 2 | BrowserNavigationTarget, |
| 3 | EnvironmentId, |
| 4 | PreviewUrlResolution, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { isLoopbackHost, normalizePreviewUrl } from "@t3tools/shared/preview"; |
| 7 | import { isLocalLoopbackHost, isPrivateNetworkHost } from "@t3tools/shared/hostClassification"; |
| 8 | |
| 9 | import type { ConnectionTarget } from "@t3tools/client-runtime/connection"; |
| 10 | |
| 11 | import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; |
| 12 | import { isElectron } from "~/env"; |
| 13 | import { readPreparedConnection } from "~/state/session"; |
| 14 | |
| 15 | export { normalizeHostname, isLocalLoopbackHost } from "@t3tools/shared/hostClassification"; |
| 16 | |
| 17 | const readEnvironmentUrl = (environmentId: EnvironmentId): URL => { |
| 18 | const connection = readPreparedConnection(environmentId); |
| 19 | if (!connection) throw new Error(`Environment ${environmentId} is not connected.`); |
| 20 | return new URL(connection.httpBaseUrl); |
| 21 | }; |
| 22 | |
| 23 | const resolveEnvironmentPortTarget = ( |
| 24 | environmentId: EnvironmentId, |
| 25 | target: Extract<BrowserNavigationTarget, { readonly kind: "environment-port" }>, |
| 26 | environmentUrl: URL, |
| 27 | requestedUrl?: string, |
| 28 | sourceUrl?: URL, |
| 29 | ): PreviewUrlResolution => { |
| 30 | if (!isPrivateNetworkHost(environmentUrl.hostname)) { |
| 31 | throw new Error( |
| 32 | "This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.", |
| 33 | ); |
| 34 | } |
| 35 | const protocol = target.protocol ?? "http"; |
| 36 | const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`; |
| 37 | const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, ""); |
| 38 | // localhost lets Chromium reach loopback servers bound to either IPv4 or IPv6. |
| 39 | const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost) |
| 40 | ? "localhost" |
| 41 | : normalizedEnvironmentHost.includes(":") |
| 42 | ? `[${normalizedEnvironmentHost}]` |
| 43 | : normalizedEnvironmentHost; |
| 44 | const resolved = sourceUrl |
| 45 | ? new URL(sourceUrl) |
| 46 | : new URL(path, `${protocol}://${resolvedHost}:${target.port}`); |
| 47 | if (sourceUrl) { |
| 48 | resolved.hostname = resolvedHost; |
| 49 | resolved.port = String(target.port); |
| 50 | } |
| 51 | return { |
| 52 | requestedUrl: requestedUrl ?? `${protocol}://localhost:${target.port}${path}`, |
| 53 | resolvedUrl: resolved.toString(), |
| 54 | resolutionKind: isLocalLoopbackHost(normalizedEnvironmentHost) |
| 55 | ? "direct" |
| 56 | : "direct-private-network", |
| 57 | environmentId, |
| 58 | }; |
| 59 | }; |
| 60 | |
| 61 | // Loopback servers on the desktop's own primary and WSL backends answer on this machine's localhost. |
| 62 | const reachesEnvironmentLoopback = (target: ConnectionTarget): boolean => |
| 63 | isElectron && |
| 64 | (target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target)); |
| 65 | |
| 66 | /** Desktop carries every other environment's loopback traffic through the preview tunnel. */ |
| 67 | export const tunnelsEnvironmentLoopback = (target: ConnectionTarget | undefined): boolean => |
| 68 | isElectron && !(target !== undefined && reachesEnvironmentLoopback(target)); |
| 69 | |
| 70 | const resolveLoopbackUrl = ( |
| 71 | environmentId: EnvironmentId, |
| 72 | rawUrl: string, |
| 73 | tunnelled: boolean, |
| 74 | ): string => { |
| 75 | try { |
| 76 | const normalizedUrl = normalizePreviewUrl(rawUrl); |
| 77 | const parsed = new URL(normalizedUrl); |
| 78 | if (tunnelled || !isLoopbackHost(parsed.hostname)) return normalizedUrl; |
| 79 | const target = readPreparedConnection(environmentId)?.target; |
| 80 | if (target && reachesEnvironmentLoopback(target)) return normalizedUrl; |
| 81 | return resolveEnvironmentPortTarget( |
| 82 | environmentId, |
| 83 | { |
| 84 | kind: "environment-port", |
| 85 | port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)), |
| 86 | protocol: parsed.protocol === "https:" ? "https" : "http", |
| 87 | path: `${parsed.pathname}${parsed.search}${parsed.hash}`, |
| 88 | }, |
| 89 | readEnvironmentUrl(environmentId), |
| 90 | rawUrl, |
| 91 | parsed, |
| 92 | ).resolvedUrl; |
| 93 | } catch { |
| 94 | return rawUrl; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | /** The URL an in-app tab loads. A desktop webview keeps localhost; its partition reaches the environment. */ |
| 99 | export const resolveDiscoveredServerUrl = (environmentId: EnvironmentId, rawUrl: string): string => |
| 100 | resolveLoopbackUrl(environmentId, rawUrl, isElectron); |
| 101 | |
| 102 | /** The URL for the system browser, which has no tunnel and needs an address it can reach itself. */ |
| 103 | export const resolveExternalServerUrl = (environmentId: EnvironmentId, rawUrl: string): string => |
| 104 | resolveLoopbackUrl(environmentId, rawUrl, false); |