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

RemoteOpenTargets.ts

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