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

DirectEndpoints.test.ts

296 lines9.2 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 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, mac = "02:42:ac:11:00:01") => [
90 {
91 address,
92 netmask: "255.255.0.0",
93 family: "IPv4" as const,
94 mac,
95 internal: false,
96 cidr: `${address}/16`,
97 },
98];
99​
100describe("resolveBoundEndpoints", () => {
101 it("lists every external IPv4 address for a wildcard bind, tagging the tailnet one", () => {
102 expect(
103 resolveBoundEndpoints({
104 platform: "linux",
105 host: "0.0.0.0",
106 port: 3773,
107 interfaces: INTERFACES,
108 }),
109 ).toEqual([
110 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
111 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
112 ]);
113 });
114​
115 it("skips container and VM networks, which only this machine reaches", () => {
116 const interfaces = {
117 ...INTERFACES,
118 docker0: virtualInterface("172.17.0.1"),
119 "br-3f2a1b": virtualInterface("172.18.0.1"),
120 virbr0: virtualInterface("192.168.122.1"),
121 "vEthernet (WSL)": virtualInterface("172.24.0.1"),
122 bridge100: virtualInterface("192.168.64.1"),
123 vmbr0: virtualInterface("192.168.1.20"),
124 };
125 expect(
126 resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }),
127 ).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("omits Windows host-only adapters without excluding guest NICs or explicit binds", () => {
135 const interfaces = {
136 ...INTERFACES,
137 "VirtualBox-Host-Only-Netzwerk": virtualInterface("192.168.56.1", "0A:00:27:00:01:01"),
138 "Ethernet 2": virtualInterface("192.168.150.1", "00:50:56:c0:00:01"),
139 "Renamed network": virtualInterface("192.168.151.1", "00:50:56:c0:00:08"),
140 guest1: virtualInterface("192.168.1.30", "08:00:27:5e:1a:22"),
141 guest2: virtualInterface("192.168.1.31", "00:50:56:2b:8c:01"),
142 guest3: virtualInterface("192.168.1.32", "00:0c:29:4f:8e:35"),
143 guest4: virtualInterface("192.168.1.33", "00:50:56:80:8c:01"),
144 };
145 const input = { host: "0.0.0.0", port: 3773, interfaces };
146 const retained = [
147 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
148 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
149 ...[30, 31, 32, 33].map((last) => ({
150 kind: "lan",
151 httpBaseUrl: `http://192.168.1.${last}:3773/`,
152 })),
153 ];
154 expect(resolveBoundEndpoints({ ...input, platform: "win32" })).toEqual(retained);
155 for (const platform of ["linux", "darwin"] as const) {
156 expect(resolveBoundEndpoints({ ...input, platform })).toHaveLength(retained.length + 3);
157 }
158 for (const host of ["192.168.56.1", "192.168.150.1", "192.168.151.1"]) {
159 expect(resolveBoundEndpoints({ ...input, platform: "win32", host })).toEqual([
160 { kind: "lan", httpBaseUrl: `http://${host}:3773/` },
161 ]);
162 }
163 });
164​
165 it("tags only addresses on Tailscale's interface as tailnet, not other VPNs in its range", () => {
166 const interfaces = {
167 en0: INTERFACES.en0,
168 tailscale0: vpnInterface("100.70.1.2"),
169 CloudflareWARP: vpnInterface("100.96.0.1"),
170 utun7: vpnInterface("100.85.0.1"),
171 };
172 expect(
173 resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }),
174 ).toEqual([
175 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
176 { kind: "tailnet", httpBaseUrl: "http://100.70.1.2:3773/" },
177 { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" },
178 { kind: "lan", httpBaseUrl: "http://100.85.0.1:3773/" },
179 ]);
180 });
181​
182 it("never reports a host name, which can resolve to another machine per client", () => {
183 for (const host of ["server.local", "devbox", "devbox.home.arpa"]) {
184 expect(
185 resolveBoundEndpoints({ platform: "linux", host, port: 3773, interfaces: INTERFACES }),
186 ).toEqual([]);
187 }
188 });
189​
190 it("never reports a public address, which would carry the credential over plain HTTP", () => {
191 expect(
192 resolveBoundEndpoints({
193 platform: "linux",
194 host: "203.0.113.20",
195 port: 3773,
196 interfaces: INTERFACES,
197 }),
198 ).toEqual([]);
199 });
200​
201 it("reports only the tailnet address on a cloud VM, whose private network no client shares", () => {
202 expect(
203 resolveBoundEndpoints({
204 platform: "linux",
205 host: "0.0.0.0",
206 port: 3773,
207 interfaces: INTERFACES,
208 onCloudVm: true,
209 }),
210 ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]);
211 });
212​
213 it("never reports an IPv4-mapped host, which can name the client's own loopback", () => {
214 expect(
215 resolveBoundEndpoints({
216 platform: "linux",
217 host: "::ffff:127.0.0.1",
218 port: 3773,
219 interfaces: INTERFACES,
220 }),
221 ).toEqual([]);
222 });
223});
224​
225const TAILSCALE_STATUS_JSON = JSON.stringify({
226 Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] },
227});
228​
229const layerTailscaleUp = Layer.succeed(
230 ChildProcessSpawner.ChildProcessSpawner,
231 ChildProcessSpawner.make(() =>
232 Effect.succeed(
233 ChildProcessSpawner.makeHandle({
234 pid: ChildProcessSpawner.ProcessId(1),
235 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
236 isRunning: Effect.succeed(false),
237 kill: () => Effect.void,
238 unref: Effect.succeed(Effect.void),
239 stdin: Sink.drain,
240 stdout: Stream.make(new TextEncoder().encode(TAILSCALE_STATUS_JSON)),
241 stderr: Stream.empty,
242 all: Stream.empty,
243 getInputFd: () => Sink.drain,
244 getOutputFd: () => Stream.empty,
245 }),
246 ),
247 ),
248);
249​
250const layerServeProbe = (status: number) =>
251 Layer.succeed(
252 HttpClient.HttpClient,
253 HttpClient.make((request) =>
254 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))),
255 ),
256 );
257​
258const layerServeConfig = Layer.effect(
259 ServerConfig.ServerConfig,
260 Effect.map(ServerConfig.ServerConfig, (config) => ({
261 ...config,
262 host: "127.0.0.1",
263 tailscaleServeEnabled: true,
264 tailscaleServePort: 443,
265 })),
266).pipe(
267 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
268 Layer.provide(NodeServices.layer),
269);
270​
271const resolveWithServe = (probeStatus: number) =>
272 Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe(
273 Effect.provide(
274 DirectEndpoints.layer.pipe(
275 Layer.provide(
276 Layer.mergeAll(layerServeConfig, layerTailscaleUp, layerServeProbe(probeStatus)),
277 ),
278 ),
279 ),
280 );
281​
282describe("DirectEndpoints Tailscale Serve", () => {
283 effectIt.effect("lists the tailnet name once Serve answers for this server", () =>
284 Effect.gen(function* () {
285 expect(yield* resolveWithServe(200)).toEqual([
286 { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" },
287 ]);
288 }),
289 );
290​
291 effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () =>
292 Effect.gen(function* () {
293 expect(yield* resolveWithServe(502)).toEqual([]);
294 }),
295 );
296});