|
| 1 | +/** |
| 2 | + * RemoteOpenTargets - resolves the SSH hostnames this environment advertises |
| 3 | + * for remote open-in-editor deep links (`vscode://vscode-remote/ssh-remote+…`). |
| 4 | + * |
| 5 | + * The server can only check itself: sshd listening locally, tailscaled |
| 6 | + * reporting a MagicDNS name, and the machine hostname for mDNS. Whether a |
| 7 | + * given name resolves from the viewer's machine is inherently client-side. |
| 8 | + * Targets are ordered most-reachable first (tailnet name works from anywhere |
| 9 | + * on the tailnet; `<hostname>.local` only on the same LAN). |
| 10 | + */ |
| 11 | +import { type RemoteOpenTarget } from "@t3tools/contracts"; |
| 12 | +import { HostProcessHostname } from "@t3tools/shared/hostProcess"; |
| 13 | +import * as NetService from "@t3tools/shared/Net"; |
| 14 | +import { readTailscaleStatus } from "@t3tools/tailscale"; |
| 15 | +import * as Context from "effect/Context"; |
| 16 | +import * as Effect from "effect/Effect"; |
| 17 | +import * as Layer from "effect/Layer"; |
| 18 | +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; |
| 19 | + |
| 20 | +const SSH_PORT = 22; |
| 21 | + |
| 22 | +export class RemoteOpenTargets extends Context.Service< |
| 23 | + RemoteOpenTargets, |
| 24 | + { |
| 25 | + readonly resolveTargets: () => Effect.Effect<ReadonlyArray<RemoteOpenTarget>>; |
| 26 | + } |
| 27 | +>()("t3/environment/RemoteOpenTargets") {} |
| 28 | + |
| 29 | +export const make = Effect.gen(function* () { |
| 30 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 31 | + const net = yield* NetService.NetService; |
| 32 | + |
| 33 | + const resolveTargets = Effect.gen(function* () { |
| 34 | + // No local sshd means no name can work; advertise nothing so clients |
| 35 | + // render a clear "no SSH route" state instead of links that hang. |
| 36 | + const sshdListening = yield* net.hasListenerOnHost(SSH_PORT, "127.0.0.1"); |
| 37 | + if (!sshdListening) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + const targets: Array<RemoteOpenTarget> = []; |
| 42 | + |
| 43 | + // Tailscale absent or down is the common case, not an error. |
| 44 | + const magicDnsName = yield* readTailscaleStatus.pipe( |
| 45 | + Effect.map((status) => status.magicDnsName), |
| 46 | + Effect.orElseSucceed(() => null), |
| 47 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 48 | + ); |
| 49 | + if (magicDnsName !== null) { |
| 50 | + targets.push({ kind: "tailscale", host: magicDnsName }); |
| 51 | + } |
| 52 | + |
| 53 | + // os.hostname() may already be an FQDN (macOS often reports |
| 54 | + // "Name.local"); mDNS names are always `<first-label>.local`. |
| 55 | + const hostname = yield* HostProcessHostname; |
| 56 | + const shortHostname = hostname.split(".")[0]?.trim(); |
| 57 | + if (shortHostname !== undefined && shortHostname.length > 0) { |
| 58 | + targets.push({ kind: "mdns", host: `${shortHostname}.local` }); |
| 59 | + } |
| 60 | + |
| 61 | + return targets; |
| 62 | + }); |
| 63 | + |
| 64 | + return RemoteOpenTargets.of({ resolveTargets: () => resolveTargets }); |
| 65 | +}); |
| 66 | + |
| 67 | +export const layer = Layer.effect(RemoteOpenTargets, make); |
0 commit comments