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

tailscaleEndpointProvider.test.ts

136 lines5.2 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { HttpClient, HttpClientResponse } from "effect/http";
5import { ChildProcessSpawner } from "effect/process";
6​
7import { resolveTailscaleAdvertisedEndpoints } from "./tailscaleEndpointProvider.ts";
8​
9const layerUnusedTailscaleExternalServices = Layer.mergeAll(
10 Layer.succeed(
11 HttpClient.HttpClient,
12 HttpClient.make(() => Effect.die("unexpected Tailscale HTTPS probe")),
13 ),
14 Layer.succeed(
15 ChildProcessSpawner.ChildProcessSpawner,
16 ChildProcessSpawner.make(() => Effect.die("unexpected tailscale status process")),
17 ),
18);
19​
20const protonAndTailscaleInterfaces = {
21 pvpnksintrf1: [{ address: "100.85.0.1", family: "IPv4", internal: false }],
22 tailscale0: [{ address: "100.74.126.34", family: "IPv4", internal: false }],
23};
24​
25describe("tailscale endpoint provider", () => {
26 it.effect("does not advertise Proton's CGNAT address as a Tailscale pairing endpoint", () =>
27 Effect.gen(function* () {
28 const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
29 port: 3773,
30 networkInterfaces: protonAndTailscaleInterfaces,
31 statusJson: `{"Self":{"TailscaleIPs":["100.74.126.34"]}}`,
32 });
33 assert.deepEqual(
34 endpoints.map((endpoint) => endpoint.httpBaseUrl),
35 ["http://100.74.126.34:3773/"],
36 );
37 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
38 );
39​
40 it.effect("keeps native Tailscale interfaces with missing, malformed, or stale status", () =>
41 Effect.gen(function* () {
42 for (const interfaceName of ["tailscale0", "Tailscale"]) {
43 for (const statusJson of [null, "not-json", `{"Self":{"TailscaleIPs":[]}}`]) {
44 const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
45 port: 3773,
46 networkInterfaces: {
47 pvpnksintrf1: protonAndTailscaleInterfaces.pvpnksintrf1,
48 [interfaceName]: protonAndTailscaleInterfaces.tailscale0,
49 },
50 statusJson,
51 });
52 assert.deepEqual(
53 endpoints.map((endpoint) => endpoint.httpBaseUrl),
54 ["http://100.74.126.34:3773/"],
55 );
56 }
57 }
58 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
59 );
60​
61 it.effect("offers a VPN address when a stopped Tailscale still reports its last netmap", () =>
62 Effect.gen(function* () {
63 const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
64 port: 3773,
65 networkInterfaces: {
66 tailscale0: [{ address: "fe80::294c:83fd:4ef:892b", family: "IPv6", internal: false }],
67 utun100: [{ address: "100.81.22.33", family: "IPv4", internal: false }],
68 },
69 statusJson: `{"BackendState":"Stopped","Self":{"TailscaleIPs":["100.64.1.2"]}}`,
70 });
71 assert.deepEqual(
72 endpoints.map((endpoint) => endpoint.httpBaseUrl),
73 ["http://100.81.22.33:3773/"],
74 );
75 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
76 );
77​
78 it.effect(
79 "recognizes a macOS Tailscale tunnel by its IPv6 address when status is unavailable",
80 () =>
81 Effect.gen(function* () {
82 const endpoints = yield* resolveTailscaleAdvertisedEndpoints({
83 port: 3773,
84 networkInterfaces: {
85 utun3: [{ address: "100.85.0.1", family: "IPv4", internal: false }],
86 utun4: [
87 { address: "100.74.126.34", family: "IPv4", internal: false },
88 { address: "fd7a:115c:a1e0::1234", family: "IPv6", internal: false },
89 ],
90 },
91 statusJson: null,
92 });
93 assert.deepEqual(
94 endpoints.map((endpoint) => endpoint.httpBaseUrl),
95 ["http://100.74.126.34:3773/"],
96 );
97 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
98 );
99​
100 it.effect("offers Tailscale HTTPS only when Serve reaches this desktop's backend", () =>
101 Effect.gen(function* () {
102 const magicDnsStatus = (servedEnvironmentId: string) =>
103 resolveTailscaleAdvertisedEndpoints({
104 port: 3773,
105 serveEnabled: true,
106 networkInterfaces: {},
107 statusJson: `{"Self":{"DNSName":"desktop.example.ts.net.","TailscaleIPs":[]}}`,
108 }).pipe(
109 Effect.map((endpoints) => endpoints.map((endpoint) => endpoint.status)),
110 Effect.provideService(
111 HttpClient.HttpClient,
112 HttpClient.make((request) =>
113 Effect.succeed(
114 HttpClientResponse.fromWeb(
115 request,
116 Response.json({
117 environmentId:
118 new URL(request.url).hostname === "127.0.0.1"
119 ? "desktop-backend"
120 : servedEnvironmentId,
121 label: "T3 server",
122 platform: { os: "linux", arch: "x64" },
123 serverVersion: "0.1.9",
124 capabilities: { repositoryIdentity: true },
125 }),
126 ),
127 ),
128 ),
129 ),
130 );
131​
132 assert.deepEqual(yield* magicDnsStatus("desktop-backend"), ["available"]);
133 assert.deepEqual(yield* magicDnsStatus("another-server"), ["unavailable"]);
134 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
135 );
136});