Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off -- replace the native OS lookup and sync its ESM bindings.
2import * as NodeModule from "node:module";
3import type * as NodeOS from "node:os";
4import { it } from "@effect/vitest";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6import * as NetService from "@t3tools/shared/Net";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Sink from "effect/Sink";
10import * as Stream from "effect/Stream";
11import * as ChildProcess from "effect/process/ChildProcess";
12import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
13import { afterEach, describe, expect, vi } from "vite-plus/test";
14
15import * as RemoteOpenTargets from "./RemoteOpenTargets.ts";
16
17const mutableOs = NodeModule.createRequire(import.meta.url)("node:os") as typeof NodeOS;
18
19afterEach(() => {
20 vi.restoreAllMocks();
21 NodeModule.syncBuiltinESMExports();
22});
23
24const encoder = new TextEncoder();
25
26const TAILSCALE_STATUS_JSON = JSON.stringify({
27 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
28});
29
30interface CommandResult {
31 readonly exitCode: number;
32 readonly stdout: string;
33}
34
35const layerSpawner = (input: { readonly status: CommandResult; readonly prefs: CommandResult }) =>
36 Layer.succeed(
37 ChildProcessSpawner.ChildProcessSpawner,
38 ChildProcessSpawner.make((command) => {
39 const args = ChildProcess.isStandardCommand(command) ? command.args : [];
40 const result = args[0] === "debug" ? input.prefs : input.status;
41 return Effect.succeed(
42 ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(1),
44 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
45 isRunning: Effect.succeed(false),
46 kill: () => Effect.void,
47 unref: Effect.succeed(Effect.void),
48 stdin: Sink.drain,
49 stdout: Stream.make(encoder.encode(result.stdout)),
50 stderr: Stream.empty,
51 all: Stream.empty,
52 getInputFd: () => Sink.drain,
53 getOutputFd: () => Stream.empty,
54 }),
55 );
56 }),
57 );
58
59const layerNet = (input: { readonly ipv4: boolean; readonly ipv6: boolean }) =>
60 Layer.succeed(NetService.NetService, {
61 canListenOnHost: () => Effect.succeed(true),
62 isPortAvailableOnLoopback: () => Effect.succeed(true),
63 hasListenerOnHost: (_port, host) => Effect.succeed(host === "::1" ? input.ipv6 : input.ipv4),
64 reserveLoopbackPort: () => Effect.succeed(40_000),
65 findAvailablePort: (preferred) => Effect.succeed(preferred),
66 });
67
68const resolveTargets = (input: {
69 readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean };
70 readonly tailscale: { readonly status: CommandResult; readonly prefs: CommandResult };
71 readonly hostname: string;
72 readonly username?: string | null;
73}) => {
74 const resolve = Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) =>
75 service.resolveTargets(),
76 ).pipe(
77 Effect.provideService(HostProcess.Hostname, input.hostname),
78 Effect.provide(
79 RemoteOpenTargets.layer.pipe(
80 Layer.provide(Layer.mergeAll(layerNet(input.sshd), layerSpawner(input.tailscale))),
81 ),
82 ),
83 );
84
85 return input.username === undefined
86 ? resolve
87 : resolve.pipe(Effect.provideService(HostProcess.Username, input.username));
88};
89
90const prefsJson = (prefs: { readonly RunSSH: boolean; readonly WantRunning: boolean }) => ({
91 exitCode: 0,
92 stdout: JSON.stringify(
93 { ControlURL: "https://controlplane.tailscale.com", ...prefs },
94 null,
95 "\t",
96 ),
97});
98
99const STATUS_UP = { exitCode: 0, stdout: TAILSCALE_STATUS_JSON };
100const TAILSCALE_UP = { status: STATUS_UP, prefs: prefsJson({ RunSSH: false, WantRunning: true }) };
101const TAILSCALE_SSH = { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: true }) };
102const TAILSCALE_DOWN = { status: { exitCode: 1, stdout: "" }, prefs: { exitCode: 1, stdout: "" } };
103
104describe("RemoteOpenTargets", () => {
105 it.effect("advertises nothing when no sshd accepts and Tailscale SSH is off", () =>
106 Effect.gen(function* () {
107 const targets = yield* resolveTargets({
108 sshd: { ipv4: false, ipv6: false },
109 tailscale: TAILSCALE_UP,
110 hostname: "bb-1",
111 username: "t3-user",
112 });
113 expect(targets).toEqual([]);
114 }),
115 );
116
117 it.effect("advertises only the tailnet name when Tailscale SSH serves without sshd", () =>
118 Effect.gen(function* () {
119 const targets = yield* resolveTargets({
120 sshd: { ipv4: false, ipv6: false },
121 tailscale: TAILSCALE_SSH,
122 hostname: "bb-1",
123 username: "t3-user",
124 });
125 expect(targets).toEqual([
126 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
127 ]);
128 }),
129 );
130
131 it.effect("ignores a stored RunSSH after `tailscale down`", () =>
132 Effect.gen(function* () {
133 const targets = yield* resolveTargets({
134 sshd: { ipv4: false, ipv6: false },
135 tailscale: { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: false }) },
136 hostname: "bb-1",
137 username: null,
138 });
139 expect(targets).toEqual([]);
140 }),
141 );
142
143 it.effect("orders the tailnet name before the mDNS name", () =>
144 Effect.gen(function* () {
145 const targets = yield* resolveTargets({
146 sshd: { ipv4: true, ipv6: true },
147 tailscale: TAILSCALE_UP,
148 hostname: "bb-1",
149 username: "t3-user",
150 });
151 expect(targets).toEqual([
152 { kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
153 { kind: "mdns", host: "bb-1.local", username: "t3-user" },
154 ]);
155 }),
156 );
157
158 it.effect("accepts an sshd bound to IPv6 loopback only", () =>
159 Effect.gen(function* () {
160 const targets = yield* resolveTargets({
161 sshd: { ipv4: false, ipv6: true },
162 tailscale: TAILSCALE_DOWN,
163 hostname: "bb-1",
164 username: "t3-user",
165 });
166 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
167 }),
168 );
169
170 it.effect("falls back to mDNS alone when tailscale is unavailable", () =>
171 Effect.gen(function* () {
172 const targets = yield* resolveTargets({
173 sshd: { ipv4: true, ipv6: false },
174 tailscale: TAILSCALE_DOWN,
175 hostname: "bb-1",
176 username: "t3-user",
177 });
178 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
179 }),
180 );
181
182 it.effect("shortens an FQDN hostname to its first label for mDNS", () =>
183 Effect.gen(function* () {
184 const targets = yield* resolveTargets({
185 sshd: { ipv4: true, ipv6: true },
186 tailscale: TAILSCALE_DOWN,
187 hostname: "bb-1.example.com",
188 username: "t3-user",
189 });
190 expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
191 }),
192 );
193
194 it.effect("still advertises hostname targets when the OS login lookup fails", () =>
195 Effect.gen(function* () {
196 const userInfo = vi.spyOn(mutableOs, "userInfo").mockImplementation(() => {
197 throw new Error("No passwd entry for this UID");
198 });
199 NodeModule.syncBuiltinESMExports();
200 const targets = yield* resolveTargets({
201 sshd: { ipv4: true, ipv6: true },
202 tailscale: TAILSCALE_UP,
203 hostname: "bb-1",
204 });
205 expect(userInfo).toHaveBeenCalled();
206 expect(targets).toEqual([
207 { kind: "tailscale", host: "bb-1.tail1234.ts.net" },
208 { kind: "mdns", host: "bb-1.local" },
209 ]);
210 }),
211 );
212});