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

RemoteOpenTargets.ts

95 lines3.1 KB

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 magicDnsName = yield* readTailscaleStatus.pipe(
37 Effect.map((status) => status.magicDnsName),
38 Effect.orElseSucceed(() => null),
39 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
40 );
41​
42 const targets: Array<RemoteOpenTarget> = [];
43​
44 if (!sshdListening) {
45 // Tailscale SSH needs no local sshd; tailscaled alone does not prove an SSH route.
46 if (
47 magicDnsName !== null &&
48 (yield* readTailscaleSshEnabled.pipe(
49 Effect.orElseSucceed(() => false),
50 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
51 ))
52 ) {
53 targets.push({
54 kind: "tailscale",
55 host: magicDnsName,
56 ...(username === null ? {} : { username }),
57 });
58 }
59 return targets;
60 }
61​
62 if (magicDnsName !== null) {
63 targets.push({
64 kind: "tailscale",
65 host: magicDnsName,
66 ...(username === null ? {} : { username }),
67 });
68 }
69​
70 // hostname() may be an FQDN; mDNS always uses <first-label>.local.
71 const hostname = yield* HostProcess.Hostname;
72 const shortHostname = hostname.split(".")[0]?.trim();
73 if (shortHostname !== undefined && shortHostname.length > 0) {
74 targets.push({
75 kind: "mdns",
76 host: `${shortHostname}.local`,
77 ...(username === null ? {} : { username }),
78 });
79 }
80​
81 return targets;
82 });
83​
84 // Advertised on every client connect; probing sshd and tailscaled is slow
85 // on a loaded host, so serve the last result while it refreshes.
86 const resolveTargets = yield* makeStaleWhileRevalidate(discoverTargets, TARGETS_CACHE_TTL);
87​
88 return RemoteOpenTargets.of({
89 resolveTargets: Effect.fn("RemoteOpenTargets.resolveTargets")(function () {
90 return resolveTargets;
91 }),
92 });
93});
94​
95export const layer = Layer.effect(RemoteOpenTargets, make);