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

routes.test.ts

357 lines12.3 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Option from "effect/Option";
4​
5import {
6 BearerConnectionProfile,
7 type ConnectionCatalogEntry,
8 type ConnectionRoute,
9} from "./catalog.ts";
10import { gitHubRoutingConnectionKey } from "./githubRoutingPermissions.ts";
11import { BearerConnectionTarget, RelayConnectionTarget } from "./model.ts";
12import {
13 connectionRouteId,
14 connectionRouteKind,
15 connectionRouteLabel,
16 credentialConnectionId,
17 insertRoute,
18 entryWithRoutes,
19 mergeLearnedRoutes,
20 removedWithRelay,
21 routesAfterRemoving,
22 upsertRoute,
23} from "./routes.ts";
24​
25const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
26​
27function direct(id: string, httpBaseUrl: string): ConnectionRoute {
28 return {
29 target: new BearerConnectionTarget({
30 environmentId: ENVIRONMENT_ID,
31 label: "Desk",
32 connectionId: id,
33 }),
34 profile: Option.some(
35 new BearerConnectionProfile({
36 connectionId: id,
37 environmentId: ENVIRONMENT_ID,
38 label: "Desk",
39 httpBaseUrl,
40 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
41 }),
42 ),
43 };
45​
46const RELAY: ConnectionRoute = {
47 target: new RelayConnectionTarget({ environmentId: ENVIRONMENT_ID, label: "Desk" }),
48 profile: Option.none(),
49};
50const LAN = direct("lan", "http://192.168.1.10:3773/");
51const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
52const PUBLIC = direct("public", "https://desk.example.com/");
53​
54describe("connection routes", () => {
55 it("classifies direct routes by address", () => {
56 expect(connectionRouteKind(LAN)).toBe("lan");
57 // Tailscale, Cloudflare WARP and Mesh, and other VPNs share 100.64.0.0/10.
58 expect(connectionRouteKind(direct("ip", "http://100.101.102.103:3773/"))).toBe("vpn");
59 expect(connectionRouteLabel(direct("mesh", "http://100.96.0.1:3773/"))).toBe("VPN");
60 expect(connectionRouteKind(TAILNET)).toBe("tailnet");
61 expect(connectionRouteKind(PUBLIC)).toBe("public");
62 expect(connectionRouteKind(direct("lo", "http://127.0.0.1:3773/"))).toBe("loopback");
63 expect(connectionRouteKind(direct("ts6", "http://[fd7a:115c:a1e0::1]:3773/"))).toBe("tailnet");
64 expect(connectionRouteLabel(TAILNET)).toBe("Tailscale");
65 expect(connectionRouteLabel(RELAY)).toBe("T3 Connect");
66 });
67​
68 it("places a new route after faster kinds and ahead of T3 Connect", () => {
69 expect(insertRoute([RELAY], LAN)).toEqual([LAN, RELAY]);
70 expect(insertRoute([LAN, RELAY], TAILNET)).toEqual([LAN, TAILNET, RELAY]);
71 expect(insertRoute([TAILNET], LAN)).toEqual([LAN, TAILNET]);
72 expect(insertRoute([LAN, TAILNET], RELAY)).toEqual([LAN, TAILNET, RELAY]);
73 });
74​
75 it("keeps a user's order when a saved route is replaced", () => {
76 const repaired = direct("lan", "http://192.168.1.11:3773/");
77 expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
78 });
79});
80​
81describe("learned routes", () => {
82 const relayOnly: ConnectionCatalogEntry = {
83 target: RELAY.target,
84 profile: RELAY.profile,
85 enabled: true,
86 };
87 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
88 routes?.map((route) => connectionRouteId(route.target)) ?? null;
89 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
90​
91 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
92 const routes = mergeLearnedRoutes({
93 entry: relayOnly,
94 activeRoute: RELAY,
95 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
96 allowInsecure: true,
97 });
98 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
99 expect(profileOf(routes![0]!)).toMatchObject({
100 learned: true,
101 authorization: "t3-connect",
102 wsBaseUrl: "ws://192.168.1.10:3773/",
103 });
104 });
105​
106 it("replaces a learned LAN address when the server reports a new one", () => {
107 const first = mergeLearnedRoutes({
108 entry: relayOnly,
109 activeRoute: RELAY,
110 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
111 allowInsecure: true,
112 })!;
113 const entry: ConnectionCatalogEntry = {
114 ...relayOnly,
115 target: first[0]!.target,
116 profile: first[0]!.profile,
117 alternateRoutes: first.slice(1),
118 };
119 const moved = mergeLearnedRoutes({
120 entry,
121 activeRoute: RELAY,
122 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
123 allowInsecure: true,
124 });
125 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
126 });
127​
128 it("keeps a learned route where the user moved it while the server reports it", () => {
129 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
130 const first = mergeLearnedRoutes({
131 entry: relayOnly,
132 activeRoute: RELAY,
133 reported: [lan],
134 allowInsecure: true,
135 })!;
136 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
137 expect(
138 mergeLearnedRoutes({
139 entry: reordered,
140 activeRoute: RELAY,
141 reported: [lan],
142 allowInsecure: true,
143 }),
144 ).toBeNull();
145 const next = mergeLearnedRoutes({
146 entry: reordered,
147 activeRoute: RELAY,
148 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
149 allowInsecure: true,
150 });
151 expect(ids(next)).toEqual([
152 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
153 "relay",
154 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
155 ]);
156 });
157​
158 it("labels a learned address Tailscale only while the server reports it as tailnet", () => {
159 const tailscale = { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" };
160 const mesh = { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" };
161 const first = mergeLearnedRoutes({
162 entry: relayOnly,
163 activeRoute: RELAY,
164 reported: [tailscale, mesh],
165 allowInsecure: true,
166 })!;
167 expect(first.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
168​
169 const corrected = mergeLearnedRoutes({
170 entry: entryWithRoutes(relayOnly, first),
171 activeRoute: RELAY,
172 reported: [{ ...tailscale, kind: "lan" }, mesh],
173 allowInsecure: true,
174 })!;
175 expect(ids(corrected)).toEqual(ids(first));
176 expect(corrected.map(connectionRouteLabel)).toEqual(["VPN", "VPN", "T3 Connect"]);
177 expect(
178 mergeLearnedRoutes({
179 entry: entryWithRoutes(relayOnly, corrected),
180 activeRoute: RELAY,
181 reported: [{ ...tailscale, kind: "lan" }, mesh],
182 allowInsecure: true,
183 }),
184 ).toBeNull();
185 });
186​
187 it("labels a paired numeric Tailscale address Tailscale once the server confirms it", () => {
188 const paired = direct("paired", "http://100.101.102.103:3773/");
189 const other = direct("other", "http://100.96.0.1:3773/");
190 const entry = entryWithRoutes(relayOnly, [paired, other, RELAY]);
191 expect(connectionRouteLabel(paired)).toBe("VPN");
192 const confirmed = mergeLearnedRoutes({
193 entry,
194 activeRoute: paired,
195 reported: [{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }],
196 allowInsecure: true,
197 })!;
198 expect(ids(confirmed)).toEqual(["paired", "other", "relay"]);
199 expect(confirmed.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
200 });
201​
202 it("leaves user routes alone and does not learn an address already saved", () => {
203 const entry: ConnectionCatalogEntry = {
204 target: LAN.target,
205 profile: LAN.profile,
206 alternateRoutes: [RELAY],
207 enabled: true,
208 };
209 expect(
210 mergeLearnedRoutes({
211 entry,
212 activeRoute: RELAY,
213 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
214 allowInsecure: true,
215 }),
216 ).toBeNull();
217 expect(
218 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
219 ).toBeNull();
220 });
221​
222 it("borrows the paired token when learned over a bearer route", () => {
223 const entry: ConnectionCatalogEntry = {
224 ...relayOnly,
225 target: LAN.target,
226 profile: LAN.profile,
227 };
228 const routes = mergeLearnedRoutes({
229 entry,
230 activeRoute: LAN,
231 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
232 allowInsecure: true,
233 })!;
234 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
235 expect(profileOf(learned)).not.toHaveProperty("authorization");
236 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
237 });
238​
239 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
240 expect(
241 mergeLearnedRoutes({
242 entry: relayOnly,
243 activeRoute: RELAY,
244 reported: [
245 { httpBaseUrl: "http://192.168.1.10:3773/" },
246 { httpBaseUrl: "http://127.0.0.1:3773/" },
247 ],
248 allowInsecure: false,
249 }),
250 ).toBeNull();
251 });
252​
253 it("removes learned routes along with the route whose credential they borrow", () => {
254 const overRelay = mergeLearnedRoutes({
255 entry: relayOnly,
256 activeRoute: RELAY,
257 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
258 allowInsecure: true,
259 })!;
260 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
261​
262 const paired: ConnectionCatalogEntry = {
263 ...relayOnly,
264 target: LAN.target,
265 profile: LAN.profile,
266 alternateRoutes: [RELAY],
267 };
268 const overLan = mergeLearnedRoutes({
269 entry: paired,
270 activeRoute: LAN,
271 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
272 allowInsecure: true,
273 })!;
274 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
275 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
276 });
277​
278 it("counts an environment reached only through T3 Connect as removed with it", () => {
279 const learned = mergeLearnedRoutes({
280 entry: relayOnly,
281 activeRoute: RELAY,
282 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
283 allowInsecure: true,
284 })!;
285 expect(removedWithRelay(relayOnly)).toBe(true);
286 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
287 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
288 });
289​
290 it("keeps GitHub routing trust when a route is learned", () => {
291 const learned = mergeLearnedRoutes({
292 entry: relayOnly,
293 activeRoute: RELAY,
294 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
295 allowInsecure: true,
296 })!;
297 const withLearned: ConnectionCatalogEntry = {
298 ...relayOnly,
299 target: learned[0]!.target,
300 profile: learned[0]!.profile,
301 alternateRoutes: learned.slice(1),
302 };
303 expect(gitHubRoutingConnectionKey(withLearned)).toBe(gitHubRoutingConnectionKey(relayOnly));
304 });
305​
306 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
307 const first = mergeLearnedRoutes({
308 entry: relayOnly,
309 activeRoute: RELAY,
310 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
311 allowInsecure: true,
312 })!;
313 const learnedLan = first[0]!;
314 const entry: ConnectionCatalogEntry = {
315 ...relayOnly,
316 target: learnedLan.target,
317 profile: learnedLan.profile,
318 alternateRoutes: first.slice(1),
319 };
320 const next = mergeLearnedRoutes({
321 entry,
322 activeRoute: learnedLan,
323 reported: [
324 { httpBaseUrl: "http://192.168.1.10:3773/" },
325 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
326 ],
327 allowInsecure: true,
328 })!;
329 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
330 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
331 expect(connectionRouteId(route.target)).not.toContain("@");
332 }
333 });
334​
335 it("saves a scheme change on the same host as a new address", () => {
336 const first = mergeLearnedRoutes({
337 entry: relayOnly,
338 activeRoute: RELAY,
339 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
340 allowInsecure: true,
341 })!;
342 const entry: ConnectionCatalogEntry = {
343 ...relayOnly,
344 target: first[0]!.target,
345 profile: first[0]!.profile,
346 alternateRoutes: first.slice(1),
347 };
348 const moved = mergeLearnedRoutes({
349 entry,
350 activeRoute: RELAY,
351 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
352 allowInsecure: true,
353 });
354 expect(moved).not.toBeNull();
355 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
356 });
357});