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

DirectEndpoints.test.ts

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