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

DirectEndpoints.test.ts

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