The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createAdvertisedEndpoint } from "@t3tools/shared/advertisedEndpoint"; |
| 2 | import { |
| 3 | ExecutionEnvironmentDescriptor, |
| 4 | type AdvertisedEndpoint, |
| 5 | type AdvertisedEndpointProvider, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { |
| 8 | buildTailscaleHttpsBaseUrl, |
| 9 | isTailscaleIpv4Address, |
| 10 | parseTailscaleStatus, |
| 11 | probeTailscaleHttpsEndpoint, |
| 12 | readTailscaleStatus, |
| 13 | type TailscaleStatus, |
| 14 | } from "@t3tools/tailscale"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as Option from "effect/Option"; |
| 17 | import * as HttpClient from "effect/http/HttpClient"; |
| 18 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 19 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 20 | |
| 21 | import type { NetworkInterfaces } from "./DesktopNetworkInterfaces.ts"; |
| 22 | |
| 23 | const TAILSCALE_ENDPOINT_PROVIDER: AdvertisedEndpointProvider = { |
| 24 | id: "tailscale", |
| 25 | label: "Tailscale", |
| 26 | kind: "private-network", |
| 27 | isAddon: true, |
| 28 | }; |
| 29 | |
| 30 | function resolveTailscaleIpAdvertisedEndpoints(input: { |
| 31 | readonly port: number; |
| 32 | readonly networkInterfaces: NetworkInterfaces; |
| 33 | readonly status: TailscaleStatus | null; |
| 34 | }): readonly AdvertisedEndpoint[] { |
| 35 | const seen = new Set<string>(); |
| 36 | const endpoints: AdvertisedEndpoint[] = []; |
| 37 | |
| 38 | for (const [interfaceName, interfaceAddresses] of Object.entries(input.networkInterfaces)) { |
| 39 | if (!interfaceAddresses) continue; |
| 40 | |
| 41 | // CGNAT is shared with other VPNs; adapter identity works without current CLI status. |
| 42 | const isTailscaleInterface = |
| 43 | /^tailscale(?:\d+|ipv[46])?$/iu.test(interfaceName) || |
| 44 | interfaceAddresses.some( |
| 45 | (address) => |
| 46 | !address.internal && |
| 47 | address.family === "IPv6" && |
| 48 | /^fd7a:115c:a1e0:/iu.test(address.address), |
| 49 | ); |
| 50 | |
| 51 | for (const address of interfaceAddresses) { |
| 52 | if (address.internal) continue; |
| 53 | if (address.family !== "IPv4") continue; |
| 54 | if (!isTailscaleIpv4Address(address.address)) continue; |
| 55 | if (!isTailscaleInterface && !input.status?.tailnetIpv4Addresses.includes(address.address)) { |
| 56 | continue; |
| 57 | } |
| 58 | if (seen.has(address.address)) continue; |
| 59 | seen.add(address.address); |
| 60 | |
| 61 | endpoints.push( |
| 62 | createAdvertisedEndpoint({ |
| 63 | provider: TAILSCALE_ENDPOINT_PROVIDER, |
| 64 | source: "desktop-addon", |
| 65 | id: `tailscale-ip:http://${address.address}:${input.port}`, |
| 66 | label: "Tailscale IP", |
| 67 | httpBaseUrl: `http://${address.address}:${input.port}`, |
| 68 | reachability: "private-network", |
| 69 | status: "available", |
| 70 | description: "Reachable from devices on the same Tailnet.", |
| 71 | }), |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return endpoints; |
| 77 | } |
| 78 | |
| 79 | // The backend may run elsewhere (WSL), so ask it for its id instead of reading the state dir. |
| 80 | const probeServesThisBackend = (localHttpUrl: string) => (baseUrl: string) => |
| 81 | HttpClient.get(new URL("/.well-known/t3/environment", localHttpUrl)).pipe( |
| 82 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 83 | Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)), |
| 84 | Effect.flatMap(({ environmentId }) => probeTailscaleHttpsEndpoint({ baseUrl, environmentId })), |
| 85 | Effect.orElseSucceed(() => false), |
| 86 | ); |
| 87 | |
| 88 | const resolveTailscaleMagicDnsAdvertisedEndpoint = Effect.fn( |
| 89 | "resolveTailscaleMagicDnsAdvertisedEndpoint", |
| 90 | )(function* (input: { |
| 91 | readonly dnsName: string | null; |
| 92 | readonly localHttpUrl: string; |
| 93 | readonly serveEnabled: boolean; |
| 94 | readonly servePort?: number; |
| 95 | readonly probe?: (baseUrl: string) => Effect.Effect<boolean, never, HttpClient.HttpClient>; |
| 96 | }): Effect.fn.Return<Option.Option<AdvertisedEndpoint>, never, HttpClient.HttpClient> { |
| 97 | if (!input.dnsName) { |
| 98 | return Option.none(); |
| 99 | } |
| 100 | |
| 101 | const httpBaseUrl = buildTailscaleHttpsBaseUrl({ |
| 102 | magicDnsName: input.dnsName, |
| 103 | ...(input.servePort === undefined ? {} : { servePort: input.servePort }), |
| 104 | }); |
| 105 | const probe = (input.probe ?? probeServesThisBackend(input.localHttpUrl))(httpBaseUrl); |
| 106 | const isReachable = input.serveEnabled ? yield* probe : false; |
| 107 | |
| 108 | return Option.some( |
| 109 | createAdvertisedEndpoint({ |
| 110 | provider: TAILSCALE_ENDPOINT_PROVIDER, |
| 111 | source: "desktop-addon", |
| 112 | id: `tailscale-magicdns:${httpBaseUrl}`, |
| 113 | label: "Tailscale HTTPS", |
| 114 | httpBaseUrl, |
| 115 | reachability: "private-network", |
| 116 | hostedHttpsCompatibility: isReachable ? "compatible" : "requires-configuration", |
| 117 | status: isReachable ? "available" : "unavailable", |
| 118 | description: isReachable |
| 119 | ? "HTTPS endpoint served by Tailscale Serve." |
| 120 | : "MagicDNS hostname. Configure Tailscale Serve for HTTPS access.", |
| 121 | }), |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | export const resolveTailscaleAdvertisedEndpoints = Effect.fn("resolveTailscaleAdvertisedEndpoints")( |
| 126 | function* (input: { |
| 127 | readonly port: number; |
| 128 | readonly serveEnabled?: boolean; |
| 129 | readonly servePort?: number; |
| 130 | readonly networkInterfaces: NetworkInterfaces; |
| 131 | readonly statusJson?: string | null; |
| 132 | readonly readStatus?: Effect.Effect< |
| 133 | TailscaleStatus | null, |
| 134 | never, |
| 135 | ChildProcessSpawner.ChildProcessSpawner |
| 136 | >; |
| 137 | readonly probe?: (baseUrl: string) => Effect.Effect<boolean, never, HttpClient.HttpClient>; |
| 138 | }): Effect.fn.Return< |
| 139 | readonly AdvertisedEndpoint[], |
| 140 | never, |
| 141 | ChildProcessSpawner.ChildProcessSpawner | HttpClient.HttpClient |
| 142 | > { |
| 143 | const readStatus = |
| 144 | input.readStatus ?? readTailscaleStatus.pipe(Effect.orElseSucceed(() => null)); |
| 145 | const status = |
| 146 | input.statusJson === undefined |
| 147 | ? yield* readStatus |
| 148 | : input.statusJson |
| 149 | ? yield* parseTailscaleStatus(input.statusJson).pipe(Effect.orElseSucceed(() => null)) |
| 150 | : null; |
| 151 | const ipEndpoints = resolveTailscaleIpAdvertisedEndpoints({ ...input, status }); |
| 152 | const magicDnsEndpoint = yield* resolveTailscaleMagicDnsAdvertisedEndpoint({ |
| 153 | dnsName: status?.magicDnsName ?? null, |
| 154 | localHttpUrl: `http://127.0.0.1:${input.port}`, |
| 155 | serveEnabled: input.serveEnabled === true, |
| 156 | ...(input.servePort === undefined ? {} : { servePort: input.servePort }), |
| 157 | ...(input.probe === undefined ? {} : { probe: input.probe }), |
| 158 | }); |
| 159 | |
| 160 | return Option.match(magicDnsEndpoint, { |
| 161 | onNone: () => ipEndpoints, |
| 162 | onSome: (endpoint) => [...ipEndpoints, endpoint], |
| 163 | }); |
| 164 | }, |
| 165 | ); |