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 address: "fd7a:115c:a1e0::1",
68 netmask: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
69 family: "IPv6",
70 mac: "00:00:00:00:00:00",
71 internal: false,
72 cidr: "fd7a:115c:a1e0::1/128",
73 scopeid: 0,
74 },
75 ],
76};
77
78const vpnInterface = (address: string) => [
79 {
80 address,
81 netmask: "255.255.255.255",
82 family: "IPv4" as const,
83 mac: "00:00:00:00:00:00",
84 internal: false,
85 cidr: `${address}/32`,
86 },
87];
88
89const virtualInterface = (address: string) => [
90 {
91 address,
92 netmask: "255.255.0.0",
93 family: "IPv4" as const,
94 mac: "02:42:ac:11:00:01",
95 internal: false,
96 cidr: `${address}/16`,
97 },
98];
99
100describe("resolveBoundEndpoints", () => {
101 it("lists nothing for a loopback-only server", () => {
102 expect(resolveBoundEndpoints({ host: undefined, port: 3773, interfaces: INTERFACES })).toEqual(
103 [],
104 );
105 expect(
106 resolveBoundEndpoints({ host: "127.0.0.1", port: 3773, interfaces: INTERFACES }),
107 ).toEqual([]);
108 });
109
110 it("lists every external IPv4 address for a wildcard bind, tagging the tailnet one", () => {
111 expect(resolveBoundEndpoints({ host: "0.0.0.0", port: 3773, interfaces: INTERFACES })).toEqual([
112 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
113 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
114 ]);
115 });
116
117 it("skips container and VM networks, which only this machine reaches", () => {
118 const interfaces = {
119 ...INTERFACES,
120 docker0: virtualInterface("172.17.0.1"),
121 "br-3f2a1b": virtualInterface("172.18.0.1"),
122 virbr0: virtualInterface("192.168.122.1"),
123 "vEthernet (WSL)": virtualInterface("172.24.0.1"),
124 bridge100: virtualInterface("192.168.64.1"),
125 vmbr0: virtualInterface("192.168.1.20"),
126 };
127 expect(resolveBoundEndpoints({ host: "0.0.0.0", port: 3773, interfaces })).toEqual([
128 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
129 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
130 { kind: "lan", httpBaseUrl: "http://192.168.1.20:3773/" },
131 ]);
132 });
133
134 it("tags only addresses on Tailscale's interface as tailnet, not other VPNs in its range", () => {
135 const interfaces = {
136 en0: INTERFACES.en0,
137 tailscale0: vpnInterface("100.70.1.2"),
138 CloudflareWARP: vpnInterface("100.96.0.1"),
139 utun7: vpnInterface("100.85.0.1"),
140 };
141 expect(resolveBoundEndpoints({ host: "0.0.0.0", port: 3773, interfaces })).toEqual([
142 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
143 { kind: "tailnet", httpBaseUrl: "http://100.70.1.2:3773/" },
144 { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" },
145 { kind: "lan", httpBaseUrl: "http://100.85.0.1:3773/" },
146 ]);
147 });
148
149 it("lists only the bound address for a specific bind", () => {
150 expect(
151 resolveBoundEndpoints({ host: "100.101.102.103", port: 3773, interfaces: INTERFACES }),
152 ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
153 });
154
155 it("never reports a host name, which can resolve to another machine per client", () => {
156 for (const host of ["server.local", "devbox", "devbox.home.arpa"]) {
157 expect(resolveBoundEndpoints({ host, port: 3773, interfaces: INTERFACES })).toEqual([]);
158 }
159 });
160
161 it("never reports a public address, which would carry the credential over plain HTTP", () => {
162 expect(
163 resolveBoundEndpoints({ host: "203.0.113.20", port: 3773, interfaces: INTERFACES }),
164 ).toEqual([]);
165 });
166});
167
168const TAILSCALE_STATUS_JSON = JSON.stringify({
169 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
170});
171
172const layerTailscaleUp = Layer.succeed(
173 ChildProcessSpawner.ChildProcessSpawner,
174 ChildProcessSpawner.make(() =>
175 Effect.succeed(
176 ChildProcessSpawner.makeHandle({
177 pid: ChildProcessSpawner.ProcessId(1),
178 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
179 isRunning: Effect.succeed(false),
180 kill: () => Effect.void,
181 unref: Effect.succeed(Effect.void),
182 stdin: Sink.drain,
183 stdout: Stream.make(new TextEncoder().encode(TAILSCALE_STATUS_JSON)),
184 stderr: Stream.empty,
185 all: Stream.empty,
186 getInputFd: () => Sink.drain,
187 getOutputFd: () => Stream.empty,
188 }),
189 ),
190 ),
191);
192
193const layerServeProbe = (status: number) =>
194 Layer.succeed(
195 HttpClient.HttpClient,
196 HttpClient.make((request) =>
197 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))),
198 ),
199 );
200
201const layerServeConfig = Layer.effect(
202 ServerConfig.ServerConfig,
203 Effect.map(ServerConfig.ServerConfig, (config) => ({
204 ...config,
205 host: "127.0.0.1",
206 tailscaleServeEnabled: true,
207 tailscaleServePort: 443,
208 })),
209).pipe(
210 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
211 Layer.provide(NodeServices.layer),
212);
213
214const resolveWithServe = (probeStatus: number) =>
215 Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
216 Effect.provide(
217 DirectEndpoints.layer.pipe(
218 Layer.provide(
219 Layer.mergeAll(layerServeConfig, layerTailscaleUp, layerServeProbe(probeStatus)),
220 ),
221 ),
222 ),
223 );
224
225describe("DirectEndpoints Tailscale Serve", () => {
226 effectIt.effect("lists the tailnet name once Serve answers for this server", () =>
227 Effect.gen(function* () {
228 expect(yield* resolveWithServe(200)).toEqual([
229 { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
230 ]);
231 }),
232 );
233
234 effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
235 Effect.gen(function* () {
236 expect(yield* resolveWithServe(502)).toEqual([]);
237 }),
238 );
239});