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

RemoteOpenTargets.ts

99 lines3.4 KB

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