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

DirectEndpoints.test.ts

436 lines14.8 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { EnvironmentId } from "@t3tools/contracts";
3import { it as effectIt } from "@effect/vitest";
4import type * as NodeOS from "node:os";
5import * as Effect from "effect/Effect";
6import * as Deferred from "effect/Deferred";
7import * as Fiber from "effect/Fiber";
8import * as Ref from "effect/Ref";
9import * as Layer from "effect/Layer";
10import * as Sink from "effect/Sink";
11import * as Stream from "effect/Stream";
12import * as HttpClient from "effect/http/HttpClient";
13import type * as HttpClientRequest from "effect/http/HttpClientRequest";
14import * as HttpClientResponse from "effect/http/HttpClientResponse";
15import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16import { describe, expect, it } from "vite-plus/test";
17​
18import * as ServerConfig from "../config.ts";
19import * as DirectEndpoints from "./DirectEndpoints.ts";
20import * as ServerEnvironment from "./ServerEnvironment.ts";
21import { resolveBoundEndpoints } from "./DirectEndpoints.ts";
22​
23const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = {
24 lo0: [
25 {
26 address: "127.0.0.1",
27 netmask: "255.0.0.0",
28 family: "IPv4",
29 mac: "00:00:00:00:00:00",
30 internal: true,
31 cidr: "127.0.0.1/8",
32 },
33 ],
34 en0: [
35 {
36 address: "192.168.1.10",
37 netmask: "255.255.255.0",
38 family: "IPv4",
39 mac: "aa:bb:cc:dd:ee:ff",
40 internal: false,
41 cidr: "192.168.1.10/24",
42 },
43 {
44 address: "fe80::1",
45 netmask: "ffff:ffff:ffff:ffff::",
46 family: "IPv6",
47 mac: "aa:bb:cc:dd:ee:ff",
48 internal: false,
49 cidr: "fe80::1/64",
50 scopeid: 4,
51 },
52 ],
53 en1: [
54 {
55 address: "203.0.113.20",
56 netmask: "255.255.255.0",
57 family: "IPv4",
58 mac: "aa:bb:cc:dd:ee:00",
59 internal: false,
60 cidr: "203.0.113.20/24",
61 },
62 ],
63 utun4: [
64 {
65 address: "100.101.102.103",
66 netmask: "255.255.255.255",
67 family: "IPv4",
68 mac: "00:00:00:00:00:00",
69 internal: false,
70 cidr: "100.101.102.103/32",
71 },
72 {
73 address: "fd7a:115c:a1e0::1",
74 netmask: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
75 family: "IPv6",
76 mac: "00:00:00:00:00:00",
77 internal: false,
78 cidr: "fd7a:115c:a1e0::1/128",
79 scopeid: 0,
80 },
81 ],
82};
83​
84const vpnInterface = (address: string) => [
85 {
86 address,
87 netmask: "255.255.255.255",
88 family: "IPv4" as const,
89 mac: "00:00:00:00:00:00",
90 internal: false,
91 cidr: `${address}/32`,
92 },
93];
94​
95const virtualInterface = (address: string, mac = "02:42:ac:11:00:01") => [
96 {
97 address,
98 netmask: "255.255.0.0",
99 family: "IPv4" as const,
100 mac,
101 internal: false,
102 cidr: `${address}/16`,
103 },
104];
105​
106describe("resolveBoundEndpoints", () => {
107 it("lists every external IPv4 address for a wildcard bind, tagging the tailnet one", () => {
108 expect(
109 resolveBoundEndpoints({
110 platform: "linux",
111 host: "0.0.0.0",
112 port: 3773,
113 interfaces: INTERFACES,
114 }),
115 ).toEqual([
116 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
117 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
118 ]);
119 });
120​
121 it("skips container and VM networks, which only this machine reaches", () => {
122 const interfaces = {
123 ...INTERFACES,
124 docker0: virtualInterface("172.17.0.1"),
125 "br-3f2a1b": virtualInterface("172.18.0.1"),
126 virbr0: virtualInterface("192.168.122.1"),
127 "vEthernet (WSL)": virtualInterface("172.24.0.1"),
128 bridge100: virtualInterface("192.168.64.1"),
129 vmbr0: virtualInterface("192.168.1.20"),
130 };
131 expect(
132 resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }),
133 ).toEqual([
134 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
135 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
136 { kind: "lan", httpBaseUrl: "http://192.168.1.20:3773/" },
137 ]);
138 });
139​
140 it("skips NixOS container and systemd-nspawn veths, which only this machine reaches", () => {
141 const interfaces = {
142 ...INTERFACES,
143 "ve-jellyfin": virtualInterface("192.168.53.193"),
144 "ve-qbittorrent": virtualInterface("192.168.55.225"),
145 "vb-jellyfin": virtualInterface("192.168.100.1"),
146 "vz-machine": virtualInterface("192.168.101.1"),
147 };
148 expect(
149 resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }),
150 ).toEqual([
151 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
152 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
153 ]);
154 });
155​
156 it("keeps a Windows alias matching a Linux container prefix eligible", () => {
157 const interfaces = {
158 ...INTERFACES,
159 "ve-office": virtualInterface("192.168.1.30"),
160 };
161 expect(
162 resolveBoundEndpoints({ platform: "win32", host: "0.0.0.0", port: 3773, interfaces }),
163 ).toEqual([
164 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
165 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
166 { kind: "lan", httpBaseUrl: "http://192.168.1.30:3773/" },
167 ]);
168 });
169​
170 it("omits Windows host-only adapters without excluding guest NICs or explicit binds", () => {
171 const interfaces = {
172 ...INTERFACES,
173 "VirtualBox-Host-Only-Netzwerk": virtualInterface("192.168.56.1", "0A:00:27:00:01:01"),
174 "Ethernet 2": virtualInterface("192.168.150.1", "00:50:56:c0:00:01"),
175 "Renamed network": virtualInterface("192.168.151.1", "00:50:56:c0:00:08"),
176 guest1: virtualInterface("192.168.1.30", "08:00:27:5e:1a:22"),
177 guest2: virtualInterface("192.168.1.31", "00:50:56:2b:8c:01"),
178 guest3: virtualInterface("192.168.1.32", "00:0c:29:4f:8e:35"),
179 guest4: virtualInterface("192.168.1.33", "00:50:56:80:8c:01"),
180 };
181 const input = { host: "0.0.0.0", port: 3773, interfaces };
182 const retained = [
183 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
184 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
185 ...[30, 31, 32, 33].map((last) => ({
186 kind: "lan",
187 httpBaseUrl: `http://192.168.1.${last}:3773/`,
188 })),
189 ];
190 expect(resolveBoundEndpoints({ ...input, platform: "win32" })).toEqual(retained);
191 for (const platform of ["linux", "darwin"] as const) {
192 expect(resolveBoundEndpoints({ ...input, platform })).toHaveLength(retained.length + 3);
193 }
194 for (const host of ["192.168.56.1", "192.168.150.1", "192.168.151.1"]) {
195 expect(resolveBoundEndpoints({ ...input, platform: "win32", host })).toEqual([
196 { kind: "lan", httpBaseUrl: `http://${host}:3773/` },
197 ]);
198 }
199 });
200​
201 it("omits Windows hotspot adapters and 198.18.0.0/15 VPN tunnels from a wildcard bind", () => {
202 const interfaces = {
203 ...INTERFACES,
204 "Local Area Connection* 2": virtualInterface("192.168.137.1"),
205 "LAN-Verbindung* 10": virtualInterface("192.168.137.2"),
206 utun40: virtualInterface("198.19.2.2"),
207 };
208 expect(
209 resolveBoundEndpoints({ platform: "win32", host: "0.0.0.0", port: 3773, interfaces }),
210 ).toEqual([
211 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
212 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
213 ]);
214 expect(
215 resolveBoundEndpoints({ platform: "darwin", host: "198.19.2.2", port: 3773, interfaces }),
216 ).toEqual([{ kind: "lan", httpBaseUrl: "http://198.19.2.2:3773/" }]);
217 });
218​
219 it("tags only addresses on Tailscale's interface as tailnet, not other VPNs in its range", () => {
220 const interfaces = {
221 en0: INTERFACES.en0,
222 tailscale0: vpnInterface("100.70.1.2"),
223 CloudflareWARP: vpnInterface("100.96.0.1"),
224 utun7: vpnInterface("100.85.0.1"),
225 };
226 expect(
227 resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }),
228 ).toEqual([
229 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
230 { kind: "tailnet", httpBaseUrl: "http://100.70.1.2:3773/" },
231 { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" },
232 { kind: "lan", httpBaseUrl: "http://100.85.0.1:3773/" },
233 ]);
234 });
235​
236 it("never reports a host name, which can resolve to another machine per client", () => {
237 for (const host of ["server.local", "devbox", "devbox.home.arpa"]) {
238 expect(
239 resolveBoundEndpoints({ platform: "linux", host, port: 3773, interfaces: INTERFACES }),
240 ).toEqual([]);
241 }
242 });
243​
244 it("never reports a public address, which would carry the credential over plain HTTP", () => {
245 expect(
246 resolveBoundEndpoints({
247 platform: "linux",
248 host: "203.0.113.20",
249 port: 3773,
250 interfaces: INTERFACES,
251 }),
252 ).toEqual([]);
253 });
254​
255 it("reports only the tailnet address on a cloud VM, whose private network no client shares", () => {
256 expect(
257 resolveBoundEndpoints({
258 platform: "linux",
259 host: "0.0.0.0",
260 port: 3773,
261 interfaces: INTERFACES,
262 onCloudVm: true,
263 }),
264 ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
265 });
266​
267 it("reports an explicit bind to Tailscale's IPv6 address as the tailnet route", () => {
268 expect(
269 resolveBoundEndpoints({
270 platform: "linux",
271 host: "fd7a:115c:a1e0::1",
272 port: 3773,
273 interfaces: INTERFACES,
274 }),
275 ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://[fd7a:115c:a1e0::1]:3773/" }]);
276 });
277​
278 it("never reports an IPv4-mapped host, which can name the client's own loopback", () => {
279 expect(
280 resolveBoundEndpoints({
281 platform: "linux",
282 host: "::ffff:127.0.0.1",
283 port: 3773,
284 interfaces: INTERFACES,
285 }),
286 ).toEqual([]);
287 });
288});
289​
290const TAILSCALE_STATUS_JSON = JSON.stringify({
291 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
292});
293​
294const layerTailscaleUp = Layer.succeed(
295 ChildProcessSpawner.ChildProcessSpawner,
296 ChildProcessSpawner.make(() =>
297 Effect.succeed(
298 ChildProcessSpawner.makeHandle({
299 pid: ChildProcessSpawner.ProcessId(1),
300 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
301 isRunning: Effect.succeed(false),
302 kill: () => Effect.void,
303 unref: Effect.succeed(Effect.void),
304 stdin: Sink.drain,
305 stdout: Stream.make(new TextEncoder().encode(TAILSCALE_STATUS_JSON)),
306 stderr: Stream.empty,
307 all: Stream.empty,
308 getInputFd: () => Sink.drain,
309 getOutputFd: () => Stream.empty,
310 }),
311 ),
312 ),
313);
314​
315const T3_DESCRIPTOR_BODY = JSON.stringify({
316 environmentId: "serve-test",
317 label: "Serve test",
318 platform: { os: "darwin", arch: "arm64" },
319 serverVersion: "0.0.0-test",
320 capabilities: { repositoryIdentity: true },
321});
322​
323const serveProbeResponse = (request: HttpClientRequest.HttpClientRequest, status: number) =>
324 HttpClientResponse.fromWeb(
325 request,
326 new Response(status === 200 ? T3_DESCRIPTOR_BODY : null, { status }),
327 );
328​
329const layerServeProbe = (status: number) =>
330 Layer.succeed(
331 HttpClient.HttpClient,
332 HttpClient.make((request) => Effect.succeed(serveProbeResponse(request, status))),
333 );
334​
335const layerServeConfig = (host = "127.0.0.1") =>
336 Layer.effect(
337 ServerConfig.ServerConfig,
338 Effect.map(ServerConfig.ServerConfig, (config) => ({
339 ...config,
340 host,
341 port: 3773,
342 tailscaleServeEnabled: true,
343 tailscaleServePort: 443,
344 })),
345 ).pipe(
346 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
347 Layer.provide(NodeServices.layer),
348 Layer.merge(
349 Layer.succeed(ServerEnvironment.ServerEnvironment, {
350 getEnvironmentId: Effect.succeed(EnvironmentId.make("serve-test")),
351 getDescriptor: Effect.die("unused"),
352 }),
353 ),
354 );
355​
356const resolveWithServe = (probeStatus: number) =>
357 Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
358 Effect.provide(
359 DirectEndpoints.layer.pipe(
360 Layer.provide(
361 Layer.mergeAll(layerServeConfig(), layerTailscaleUp, layerServeProbe(probeStatus)),
362 ),
363 ),
364 ),
365 );
366​
367describe("DirectEndpoints Tailscale Serve", () => {
368 effectIt.effect("lists the tailnet name once Serve answers for this server", () =>
369 Effect.gen(function* () {
370 expect(yield* resolveWithServe(200)).toEqual([
371 { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
372 ]);
373 }),
374 );
375​
376 effectIt.effect(
377 "returns bound endpoints during validation and reuses only a completed Serve result",
378 () =>
379 Effect.gen(function* () {
380 const status = yield* Ref.make(200);
381 const finishProbe = yield* Deferred.make<void>();
382 const probeStarted = yield* Deferred.make<void>();
383 const revalidationStarted = yield* Deferred.make<void>();
384 const finishRevalidation = yield* Deferred.make<void>();
385 const service = yield* DirectEndpoints.make.pipe(
386 Effect.provide(
387 Layer.mergeAll(
388 layerServeConfig("192.168.1.10"),
389 layerTailscaleUp,
390 Layer.succeed(
391 HttpClient.HttpClient,
392 HttpClient.make((request) =>
393 Deferred.succeed(probeStarted, undefined).pipe(
394 Effect.andThen(Deferred.await(finishProbe)),
395 Effect.andThen(Ref.get(status)),
396 Effect.tap((status) =>
397 status === 502
398 ? Deferred.succeed(revalidationStarted, undefined).pipe(
399 Effect.andThen(Deferred.await(finishRevalidation)),
400 )
401 : Effect.void,
402 ),
403 Effect.map((status) => serveProbeResponse(request, status)),
404 ),
405 ),
406 ),
407 ),
408 ),
409 );
410 const resolving = yield* Effect.forkScoped(service.resolve());
411 yield* Deferred.await(probeStarted);
412 const bound = { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" };
413 expect(yield* service.resolveAvailable()).toEqual([bound]);
414 yield* Deferred.succeed(finishProbe, undefined);
415 const validated = yield* Fiber.join(resolving);
416 expect(validated).toEqual([
417 bound,
418 { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
419 ]);
420 expect(yield* service.resolveAvailable()).toEqual(validated);
421 yield* Ref.set(status, 502);
422 const revalidating = yield* Effect.forkScoped(service.resolve());
423 yield* Deferred.await(revalidationStarted);
424 expect(yield* service.resolveAvailable()).toEqual(validated);
425 yield* Deferred.succeed(finishRevalidation, undefined);
426 expect(yield* Fiber.join(revalidating)).toEqual([bound]);
427 expect(yield* service.resolveAvailable()).toEqual([bound]);
428 }),
429 );
430​
431 effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
432 Effect.gen(function* () {
433 expect(yield* resolveWithServe(502)).toEqual([]);
434 }),
435 );
436});