Open History to see the last commit
1import { type RemoteOpenTarget } from "@t3tools/contracts";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as NetService from "@t3tools/shared/Net";
4import { readTailscaleSshEnabled, readTailscaleStatus } from "@t3tools/tailscale";
5import * as Context from "effect/Context";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9
10import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";
11
12const SSH_PORT = 22;
13const TARGETS_CACHE_TTL = "60 seconds";
14
15export class RemoteOpenTargets extends Context.Service<
16 RemoteOpenTargets,
17 {
18 readonly resolveTargets: () => Effect.Effect<ReadonlyArray<RemoteOpenTarget>>;
19 }
20>()("t3/environment/RemoteOpenTargets") {}
21
22/** @public */
23export const make = Effect.gen(function* () {
24 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
25 const net = yield* NetService.NetService;
26
27 const discoverTargets = Effect.gen(function* () {
28 // Check both loopback families: sshd can be bound IPv6-only.
29 const sshdListening = yield* Effect.zipWith(
30 net.hasListenerOnHost(SSH_PORT, "127.0.0.1"),
31 net.hasListenerOnHost(SSH_PORT, "::1"),
32 (ipv4, ipv6) => ipv4 || ipv6,
33 );
34 const username = yield* HostProcess.Username;
35
36 const tailscaleStatus = yield* readTailscaleStatus.pipe(
37 Effect.orElseSucceed(() => null),
38 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
39 );
40 const magicDnsName = tailscaleStatus?.magicDnsName ?? null;
41
42 const targets: Array<RemoteOpenTarget> = [];
43
44 if (!sshdListening) {
45 // sshd may listen only on the tailnet address (ListenAddress), and Tailscale SSH
46 // needs no local sshd; tailscaled alone does not prove an SSH route.
47 if (magicDnsName === null) return targets;
48 const tailnetSshdListening = yield* Effect.forEach(
49 tailscaleStatus?.tailnetIpv4Addresses ?? [],
50 (address) => net.hasListenerOnHost(SSH_PORT, address),
51 ).pipe(Effect.map((listening) => listening.includes(true)));
52 const sshRoute =
53 tailnetSshdListening ||
54 (yield* readTailscaleSshEnabled.pipe(
55 Effect.orElseSucceed(() => false),
56 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
57 ));
58 if (sshRoute) {
59 targets.push({
60 kind: "tailscale",
61 host: magicDnsName,
62 ...(username === null ? {} : { username }),
63 });
64 }
65 return targets;
66 }
67
68 if (magicDnsName !== null) {
69 targets.push({
70 kind: "tailscale",
71 host: magicDnsName,
72 ...(username === null ? {} : { username }),
73 });
74 }
75
76 // hostname() may be an FQDN; mDNS always uses <first-label>.local.
77 const hostname = yield* HostProcess.Hostname;
78 const shortHostname = hostname.split(".")[0]?.trim();
79 if (shortHostname !== undefined && shortHostname.length > 0) {
80 targets.push({
81 kind: "mdns",
82 host: `${shortHostname}.local`,
83 ...(username === null ? {} : { username }),
84 });
85 }
86
87 return targets;
88 });
89
90 // Advertised on every client connect; probing sshd and tailscaled is slow
91 // on a loaded host, so serve the last result while it refreshes.
92 const resolveTargets = yield* makeStaleWhileRevalidate(discoverTargets, TARGETS_CACHE_TTL);
93
94 return RemoteOpenTargets.of({
95 resolveTargets: Effect.fn("RemoteOpenTargets.resolveTargets")(function () {
96 return resolveTargets;
97 }),
98 });
99});
100
101export const layer = Layer.effect(RemoteOpenTargets, make);