Open History to see the last commit
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 Layer from "effect/Layer";
6import * as Sink from "effect/Sink";
7import * as Stream from "effect/Stream";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientResponse from "effect/http/HttpClientResponse";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11import { describe, expect, it } from "vite-plus/test";
12
13import * as ServerConfig from "../config.ts";
14import * as DirectEndpoints from "./DirectEndpoints.ts";
15import { resolveBoundEndpoints } from "./DirectEndpoints.ts";
16
17const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = {
18 lo0: [
19 {
20 address: "127.0.0.1",
21 netmask: "255.0.0.0",
22 family: "IPv4",
23 mac: "00:00:00:00:00:00",
24 internal: true,
25 cidr: "127.0.0.1/8",
26 },
27 ],
28 en0: [
29 {
30 address: "192.168.1.10",
31 netmask: "255.255.255.0",
32 family: "IPv4",
33 mac: "aa:bb:cc:dd:ee:ff",
34 internal: false,
35 cidr: "192.168.1.10/24",
36 },
37 {
38 address: "fe80::1",
39 netmask: "ffff:ffff:ffff:ffff::",
40 family: "IPv6",
41 mac: "aa:bb:cc:dd:ee:ff",
42 internal: false,
43 cidr: "fe80::1/64",
44 scopeid: 4,
45 },
46 ],
47 en1: [
48 {
49 address: "203.0.113.20",
50 netmask: "255.255.255.0",
51 family: "IPv4",
52 mac: "aa:bb:cc:dd:ee:00",
53 internal: false,
54 cidr: "203.0.113.20/24",
55 },
56 ],
57 utun4: [
58 {
59 address: "100.101.102.103",
60 netmask: "255.255.255.255",
61 family: "IPv4",
62 mac: "00:00:00:00:00:00",
63 internal: false,
64 cidr: "100.101.102.103/32",
65 },
66 ],
67};
68
69const virtualInterface = (address: string) => [
70 {
71 address,
72 netmask: "255.255.0.0",
73 family: "IPv4" as const,
74 mac: "02:42:ac:11:00:01",
75 internal: false,
76 cidr: `${address}/16`,
77 },
78];
79
80describe("resolveBoundEndpoints", () => {
81 it("lists nothing for a loopback-only server", () => {
82 expect(resolveBoundEndpoints({ host: undefined, port: 3773, interfaces: INTERFACES })).toEqual(
83 [],
84 );
85 expect(
86 resolveBoundEndpoints({ host: "127.0.0.1", port: 3773, interfaces: INTERFACES }),
87 ).toEqual([]);
88 });
89
90 it("lists every external IPv4 address for a wildcard bind, tagging the tailnet one", () => {
91 expect(resolveBoundEndpoints({ host: "0.0.0.0", port: 3773, interfaces: INTERFACES })).toEqual([
92 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
93 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
94 ]);
95 });
96
97 it("skips container and VM networks, which only this machine reaches", () => {
98 const interfaces = {
99 ...INTERFACES,
100 docker0: virtualInterface("172.17.0.1"),
101 "br-3f2a1b": virtualInterface("172.18.0.1"),
102 virbr0: virtualInterface("192.168.122.1"),
103 "vEthernet (WSL)": virtualInterface("172.24.0.1"),
104 bridge100: virtualInterface("192.168.64.1"),
105 vmbr0: virtualInterface("192.168.1.20"),
106 };
107 expect(resolveBoundEndpoints({ host: "0.0.0.0", port: 3773, interfaces })).toEqual([
108 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
109 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
110 { kind: "lan", httpBaseUrl: "http://192.168.1.20:3773/" },
111 ]);
112 });
113
114 it("lists only the bound address for a specific bind", () => {
115 expect(
116 resolveBoundEndpoints({ host: "100.101.102.103", port: 3773, interfaces: INTERFACES }),
117 ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
118 });
119
120 it("never reports a host name, which can resolve to another machine per client", () => {
121 for (const host of ["server.local", "devbox", "devbox.home.arpa"]) {
122 expect(resolveBoundEndpoints({ host, port: 3773, interfaces: INTERFACES })).toEqual([]);
123 }
124 });
125
126 it("never reports a public address, which would carry the credential over plain HTTP", () => {
127 expect(
128 resolveBoundEndpoints({ host: "203.0.113.20", port: 3773, interfaces: INTERFACES }),
129 ).toEqual([]);
130 });
131});
132
133const TAILSCALE_STATUS_JSON = JSON.stringify({
134 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
135});
136
137/** `tailscale status --json` reporting a MagicDNS name. */
138const layerTailscaleUp = Layer.succeed(
139 ChildProcessSpawner.ChildProcessSpawner,
140 ChildProcessSpawner.make(() =>
141 Effect.succeed(
142 ChildProcessSpawner.makeHandle({
143 pid: ChildProcessSpawner.ProcessId(1),
144 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
145 isRunning: Effect.succeed(false),
146 kill: () => Effect.void,
147 unref: Effect.succeed(Effect.void),
148 stdin: Sink.drain,
149 stdout: Stream.make(new TextEncoder().encode(TAILSCALE_STATUS_JSON)),
150 stderr: Stream.empty,
151 all: Stream.empty,
152 getInputFd: () => Sink.drain,
153 getOutputFd: () => Stream.empty,
154 }),
155 ),
156 ),
157);
158
159/** Answers the Serve probe with `status`. */
160const layerServeProbe = (status: number) =>
161 Layer.succeed(
162 HttpClient.HttpClient,
163 HttpClient.make((request) =>
164 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))),
165 ),
166 );
167
168/** A loopback-only server with Tailscale Serve on, so only the Serve name can be listed. */
169const layerServeConfig = Layer.effect(
170 ServerConfig.ServerConfig,
171 Effect.map(ServerConfig.ServerConfig, (config) => ({
172 ...config,
173 host: "127.0.0.1",
174 tailscaleServeEnabled: true,
175 tailscaleServePort: 443,
176 })),
177).pipe(
178 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
179 Layer.provide(NodeServices.layer),
180);
181
182const resolveWithServe = (probeStatus: number) =>
183 Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
184 Effect.provide(
185 DirectEndpoints.layer.pipe(
186 Layer.provide(
187 Layer.mergeAll(layerServeConfig, layerTailscaleUp, layerServeProbe(probeStatus)),
188 ),
189 ),
190 ),
191 );
192
193describe("DirectEndpoints Tailscale Serve", () => {
194 effectIt.effect("lists the tailnet name once Serve answers for this server", () =>
195 Effect.gen(function* () {
196 expect(yield* resolveWithServe(200)).toEqual([
197 { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
198 ]);
199 }),
200 );
201
202 effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
203 Effect.gen(function* () {
204 expect(yield* resolveWithServe(502)).toEqual([]);
205 }),
206 );
207});