Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RemoteOpenTargets.ts

72 lines2.8 KB

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 */
11import { type RemoteOpenTarget } from "@t3tools/contracts";
12import { HostProcessHostname } from "@t3tools/shared/hostProcess";
13import * as NetService from "@t3tools/shared/Net";
14import { readTailscaleStatus } from "@t3tools/tailscale";
15import * as Context from "effect/Context";
16import * as Effect from "effect/Effect";
17import * as Layer from "effect/Layer";
18import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
19​
20const SSH_PORT = 22;
21​
22export class RemoteOpenTargets extends Context.Service<
23 RemoteOpenTargets,
24 {
25 readonly resolveTargets: () => Effect.Effect<ReadonlyArray<RemoteOpenTarget>>;
26 }
27>()("t3/environment/RemoteOpenTargets") {}
28​
29export 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 // Check both loopback families: sshd can be bound IPv6-only.
37 const sshdListening = yield* Effect.zipWith(
38 net.hasListenerOnHost(SSH_PORT, "127.0.0.1"),
39 net.hasListenerOnHost(SSH_PORT, "::1"),
40 (ipv4, ipv6) => ipv4 || ipv6,
41 );
42 if (!sshdListening) {
43 return [];
44 }
45​
46 const targets: Array<RemoteOpenTarget> = [];
47​
48 // Tailscale absent or down is the common case, not an error.
49 const magicDnsName = yield* readTailscaleStatus.pipe(
50 Effect.map((status) => status.magicDnsName),
51 Effect.orElseSucceed(() => null),
52 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
53 );
54 if (magicDnsName !== null) {
55 targets.push({ kind: "tailscale", host: magicDnsName });
56 }
57​
58 // os.hostname() may already be an FQDN (macOS often reports
59 // "Name.local"); mDNS names are always `<first-label>.local`.
60 const hostname = yield* HostProcessHostname;
61 const shortHostname = hostname.split(".")[0]?.trim();
62 if (shortHostname !== undefined && shortHostname.length > 0) {
63 targets.push({ kind: "mdns", host: `${shortHostname}.local` });
64 }
65​
66 return targets;
67 });
68​
69 return RemoteOpenTargets.of({ resolveTargets: () => resolveTargets });
70});
71​
72export const layer = Layer.effect(RemoteOpenTargets, make);