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

routes.test.ts

364 lines12.8 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 // The user preferred T3 Connect over the LAN; re-pairing the LAN keeps that.
77 const repaired = direct("lan", "http://192.168.1.11:3773/");
78 expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
79 });
80});
81​
82describe("learned routes", () => {
83 const relayOnly: ConnectionCatalogEntry = {
84 target: RELAY.target,
85 profile: RELAY.profile,
86 enabled: true,
87 };
88 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
89 routes?.map((route) => connectionRouteId(route.target)) ?? null;
90 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
91​
92 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
93 const routes = mergeLearnedRoutes({
94 entry: relayOnly,
95 activeRoute: RELAY,
96 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
97 allowInsecure: true,
98 });
99 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
100 expect(profileOf(routes![0]!)).toMatchObject({
101 learned: true,
102 authorization: "t3-connect",
103 wsBaseUrl: "ws://192.168.1.10:3773/",
104 });
105 });
106​
107 it("replaces a learned LAN address when the server reports a new one", () => {
108 const first = mergeLearnedRoutes({
109 entry: relayOnly,
110 activeRoute: RELAY,
111 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
112 allowInsecure: true,
113 })!;
114 const entry: ConnectionCatalogEntry = {
115 ...relayOnly,
116 target: first[0]!.target,
117 profile: first[0]!.profile,
118 alternateRoutes: first.slice(1),
119 };
120 const moved = mergeLearnedRoutes({
121 entry,
122 activeRoute: RELAY,
123 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
124 allowInsecure: true,
125 });
126 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
127 });
128​
129 it("keeps a learned route where the user moved it while the server reports it", () => {
130 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
131 const first = mergeLearnedRoutes({
132 entry: relayOnly,
133 activeRoute: RELAY,
134 reported: [lan],
135 allowInsecure: true,
136 })!;
137 // The user prefers T3 Connect over the learned LAN address.
138 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
139 expect(
140 mergeLearnedRoutes({
141 entry: reordered,
142 activeRoute: RELAY,
143 reported: [lan],
144 allowInsecure: true,
145 }),
146 ).toBeNull();
147 // A newly reported address is still placed by speed.
148 const next = mergeLearnedRoutes({
149 entry: reordered,
150 activeRoute: RELAY,
151 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
152 allowInsecure: true,
153 });
154 expect(ids(next)).toEqual([
155 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
156 "relay",
157 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
158 ]);
159 });
160​
161 it("labels a learned address Tailscale only while the server reports it as tailnet", () => {
162 const tailscale = { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" };
163 const mesh = { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" };
164 const first = mergeLearnedRoutes({
165 entry: relayOnly,
166 activeRoute: RELAY,
167 reported: [tailscale, mesh],
168 allowInsecure: true,
169 })!;
170 expect(first.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
171​
172 // The server later finds the address is not on its Tailscale interface.
173 const corrected = mergeLearnedRoutes({
174 entry: entryWithRoutes(relayOnly, first),
175 activeRoute: RELAY,
176 reported: [{ ...tailscale, kind: "lan" }, mesh],
177 allowInsecure: true,
178 })!;
179 expect(ids(corrected)).toEqual(ids(first));
180 expect(corrected.map(connectionRouteLabel)).toEqual(["VPN", "VPN", "T3 Connect"]);
181 expect(
182 mergeLearnedRoutes({
183 entry: entryWithRoutes(relayOnly, corrected),
184 activeRoute: RELAY,
185 reported: [{ ...tailscale, kind: "lan" }, mesh],
186 allowInsecure: true,
187 }),
188 ).toBeNull();
189 });
190​
191 it("labels a paired numeric Tailscale address Tailscale once the server confirms it", () => {
192 const paired = direct("paired", "http://100.101.102.103:3773/");
193 const other = direct("other", "http://100.96.0.1:3773/");
194 const entry = entryWithRoutes(relayOnly, [paired, other, RELAY]);
195 expect(connectionRouteLabel(paired)).toBe("VPN");
196 const confirmed = mergeLearnedRoutes({
197 entry,
198 activeRoute: paired,
199 reported: [{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }],
200 allowInsecure: true,
201 })!;
202 // Same routes in the same order; only the label of the confirmed one changes.
203 expect(ids(confirmed)).toEqual(["paired", "other", "relay"]);
204 expect(confirmed.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
205 });
206​
207 it("leaves user routes alone and does not learn an address already saved", () => {
208 const entry: ConnectionCatalogEntry = {
209 target: LAN.target,
210 profile: LAN.profile,
211 alternateRoutes: [RELAY],
212 enabled: true,
213 };
214 expect(
215 mergeLearnedRoutes({
216 entry,
217 activeRoute: RELAY,
218 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
219 allowInsecure: true,
220 }),
221 ).toBeNull();
222 // The server stops reporting the LAN address; the paired route stays.
223 expect(
224 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
225 ).toBeNull();
226 });
227​
228 it("borrows the paired token when learned over a bearer route", () => {
229 const entry: ConnectionCatalogEntry = {
230 ...relayOnly,
231 target: LAN.target,
232 profile: LAN.profile,
233 };
234 const routes = mergeLearnedRoutes({
235 entry,
236 activeRoute: LAN,
237 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
238 allowInsecure: true,
239 })!;
240 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
241 expect(profileOf(learned)).not.toHaveProperty("authorization");
242 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
243 });
244​
245 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
246 expect(
247 mergeLearnedRoutes({
248 entry: relayOnly,
249 activeRoute: RELAY,
250 reported: [
251 { httpBaseUrl: "http://192.168.1.10:3773/" },
252 { httpBaseUrl: "http://127.0.0.1:3773/" },
253 ],
254 allowInsecure: false,
255 }),
256 ).toBeNull();
257 });
258​
259 it("removes learned routes along with the route whose credential they borrow", () => {
260 const overRelay = mergeLearnedRoutes({
261 entry: relayOnly,
262 activeRoute: RELAY,
263 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
264 allowInsecure: true,
265 })!;
266 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
267​
268 const paired: ConnectionCatalogEntry = {
269 ...relayOnly,
270 target: LAN.target,
271 profile: LAN.profile,
272 alternateRoutes: [RELAY],
273 };
274 const overLan = mergeLearnedRoutes({
275 entry: paired,
276 activeRoute: LAN,
277 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
278 allowInsecure: true,
279 })!;
280 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
281 // Removing T3 Connect keeps the paired LAN and what it learned.
282 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
283 });
284​
285 it("counts an environment reached only through T3 Connect as removed with it", () => {
286 const learned = mergeLearnedRoutes({
287 entry: relayOnly,
288 activeRoute: RELAY,
289 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
290 allowInsecure: true,
291 })!;
292 expect(removedWithRelay(relayOnly)).toBe(true);
293 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
294 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
295 });
296​
297 it("keeps GitHub routing trust when a route is learned", () => {
298 const learned = mergeLearnedRoutes({
299 entry: relayOnly,
300 activeRoute: RELAY,
301 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
302 allowInsecure: true,
303 })!;
304 const withLearned: ConnectionCatalogEntry = {
305 ...relayOnly,
306 target: learned[0]!.target,
307 profile: learned[0]!.profile,
308 alternateRoutes: learned.slice(1),
309 };
310 expect(gitHubRoutingConnectionKey(withLearned)).toBe(gitHubRoutingConnectionKey(relayOnly));
311 });
312​
313 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
314 const first = mergeLearnedRoutes({
315 entry: relayOnly,
316 activeRoute: RELAY,
317 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
318 allowInsecure: true,
319 })!;
320 const learnedLan = first[0]!;
321 const entry: ConnectionCatalogEntry = {
322 ...relayOnly,
323 target: learnedLan.target,
324 profile: learnedLan.profile,
325 alternateRoutes: first.slice(1),
326 };
327 const next = mergeLearnedRoutes({
328 entry,
329 activeRoute: learnedLan,
330 reported: [
331 { httpBaseUrl: "http://192.168.1.10:3773/" },
332 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
333 ],
334 allowInsecure: true,
335 })!;
336 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
337 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
338 expect(connectionRouteId(route.target)).not.toContain("@");
339 }
340 });
341​
342 it("saves a scheme change on the same host as a new address", () => {
343 const first = mergeLearnedRoutes({
344 entry: relayOnly,
345 activeRoute: RELAY,
346 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
347 allowInsecure: true,
348 })!;
349 const entry: ConnectionCatalogEntry = {
350 ...relayOnly,
351 target: first[0]!.target,
352 profile: first[0]!.profile,
353 alternateRoutes: first.slice(1),
354 };
355 const moved = mergeLearnedRoutes({
356 entry,
357 activeRoute: RELAY,
358 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
359 allowInsecure: true,
360 });
361 expect(moved).not.toBeNull();
362 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
363 });
364});