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

DirectEndpoints.test.ts

358 lines11.9 KB

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