@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 475 lines 17.3 KB
Raw
1 import { EnvironmentId } from "@t3tools/contracts";
2 import { describe, expect, it } from "@effect/vitest";
3 import * as Option from "effect/Option";
4
5 import {
6 BearerConnectionProfile,
7 type ConnectionCatalogEntry,
8 type ConnectionRoute,
9 } from "./catalog.ts";
10 import { pullRequestRoutingConnectionKey } from "./pullRequestRoutingPermissions.ts";
11 import { BearerConnectionTarget, RelayConnectionTarget } from "./model.ts";
12 import {
13 connectionRouteId,
14 connectionRouteKind,
15 connectionRouteLabel,
16 credentialConnectionId,
17 insertRoute,
18 entryWithRoutes,
19 isNearbyAddress,
20 mergeLearnedRoutes,
21 pendingRouteOrder,
22 removedWithRelay,
23 routeKeepsItsHost,
24 routesAfterRemoving,
25 sendsTokenInClear,
26 upsertRoute,
27 usableRoutes,
28 } from "./routes.ts";
29
30 const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
31
32 function direct(id: string, httpBaseUrl: string): ConnectionRoute {
33 return {
34 target: new BearerConnectionTarget({
35 environmentId: ENVIRONMENT_ID,
36 label: "Desk",
37 connectionId: id,
38 }),
39 profile: Option.some(
40 new BearerConnectionProfile({
41 connectionId: id,
42 environmentId: ENVIRONMENT_ID,
43 label: "Desk",
44 httpBaseUrl,
45 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
46 }),
47 ),
48 };
49 }
50
51 const RELAY: ConnectionRoute = {
52 target: new RelayConnectionTarget({ environmentId: ENVIRONMENT_ID, label: "Desk" }),
53 profile: Option.none(),
54 };
55 const LAN = direct("lan", "http://192.168.1.10:3773/");
56 const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
57 const PUBLIC = direct("public", "https://desk.example.com/");
58
59 function learned(id: string, httpBaseUrl: string): ConnectionRoute {
60 const route = direct(id, httpBaseUrl);
61 const profile = Option.getOrThrow(route.profile) as BearerConnectionProfile;
62 return {
63 target: route.target,
64 profile: Option.some(new BearerConnectionProfile({ ...profile, learned: true })),
65 };
66 }
67
68 describe("connection routes", () => {
69 it("classifies direct routes by address", () => {
70 expect(connectionRouteKind(LAN)).toBe("lan");
71 // Tailscale, Cloudflare WARP and Mesh, and other VPNs share 100.64.0.0/10.
72 expect(connectionRouteKind(direct("ip", "http://100.101.102.103:3773/"))).toBe("vpn");
73 expect(connectionRouteLabel(direct("mesh", "http://100.96.0.1:3773/"))).toBe("VPN");
74 expect(connectionRouteKind(TAILNET)).toBe("tailnet");
75 expect(connectionRouteKind(PUBLIC)).toBe("public");
76 expect(connectionRouteKind(direct("lo", "http://127.0.0.1:3773/"))).toBe("loopback");
77 expect(connectionRouteKind(direct("ts6", "http://[fd7a:115c:a1e0::1]:3773/"))).toBe("tailnet");
78 expect(connectionRouteLabel(direct("ts-ip", "http://[fd7a:115c:a1e0::1]:3773/"))).toBe(
79 "Tailscale",
80 );
81 expect(connectionRouteLabel(direct("ts-serve", "https://box.tail1234.ts.net/"))).toBe(
82 "Tailscale HTTPS",
83 );
84 expect(connectionRouteLabel(RELAY)).toBe("T3 Connect");
85 });
86
87 it("knows which addresses reach the same machine on every network", () => {
88 const tailnetIp = direct("ts-ip", "http://100.101.102.103:3773/");
89 const profile = Option.getOrThrow(tailnetIp.profile) as BearerConnectionProfile;
90 const marked = {
91 target: tailnetIp.target,
92 profile: Option.some(new BearerConnectionProfile({ ...profile, network: "tailscale" })),
93 };
94 expect(routeKeepsItsHost(PUBLIC)).toBe(true);
95 expect(routeKeepsItsHost(direct("ts-tls", "https://desk.tail1234.ts.net/"))).toBe(true);
96 expect(routeKeepsItsHost(direct("lo", "http://127.0.0.1:3773/"))).toBe(true);
97 // With Tailscale down, a plain http tailnet address can reach another host:
98 // carrier networks use 100.64.0.0/10 too, and nothing proves who answers.
99 expect(routeKeepsItsHost(direct("ts", "http://desk.tail1234.ts.net:3773/"))).toBe(false);
100 expect(routeKeepsItsHost(marked)).toBe(false);
101 expect(routeKeepsItsHost(tailnetIp)).toBe(false);
102 expect(routeKeepsItsHost(LAN)).toBe(false);
103 expect(routeKeepsItsHost(direct("http", "http://desk.example.com/"))).toBe(false);
104 expect(routeKeepsItsHost(RELAY)).toBe(false);
105 });
106
107 it("treats only loopback and LAN addresses as nearby", () => {
108 expect(isNearbyAddress("http://127.0.0.1:3773/")).toBe(true);
109 expect(isNearbyAddress("http://192.168.1.10:3773/")).toBe(true);
110 expect(isNearbyAddress("http://100.101.102.103:3773/")).toBe(false);
111 expect(isNearbyAddress("https://desk.tail1234.ts.net/")).toBe(false);
112 expect(isNearbyAddress("https://desk.example.com/")).toBe(false);
113 expect(isNearbyAddress("not a url")).toBe(false);
114 });
115
116 it("places a new route after faster kinds and ahead of T3 Connect", () => {
117 expect(insertRoute([RELAY], LAN)).toEqual([LAN, RELAY]);
118 expect(insertRoute([LAN, RELAY], TAILNET)).toEqual([LAN, TAILNET, RELAY]);
119 expect(insertRoute([TAILNET], LAN)).toEqual([LAN, TAILNET]);
120 expect(insertRoute([LAN, TAILNET], RELAY)).toEqual([LAN, TAILNET, RELAY]);
121 });
122
123 it("keeps a user's order when a saved route is replaced", () => {
124 const repaired = direct("lan", "http://192.168.1.11:3773/");
125 expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
126 });
127
128 it("puts a newly paired route ahead of saved routes of the same kind", () => {
129 const pasted = direct("lan-2", "http://192.168.1.11:3773/");
130 expect(upsertRoute([LAN, TAILNET], pasted)).toEqual([pasted, LAN, TAILNET]);
131 });
132
133 it("keeps a newly paired route behind a different kind of the same rank", () => {
134 const vpn = direct("vpn", "http://100.101.102.103:3773/");
135 const pasted = direct("ts-2", "http://[fd7a:115c:a1e0::2]:3773/");
136 expect(upsertRoute([vpn, TAILNET], pasted)).toEqual([vpn, pasted, TAILNET]);
137 });
138
139 it("shows a dragged order only until the saved routes catch up or change", () => {
140 const dragged = ["tailnet", "lan"];
141 expect(pendingRouteOrder(dragged, ["lan", "tailnet"])).toBe(dragged);
142 expect(pendingRouteOrder(dragged, ["tailnet", "lan"])).toBeNull();
143 expect(pendingRouteOrder(dragged, ["lan", "public"])).toBeNull();
144 expect(pendingRouteOrder(dragged, ["lan", "tailnet", "public"])).toBeNull();
145 });
146 });
147
148 describe("learned routes", () => {
149 const relayOnly: ConnectionCatalogEntry = {
150 target: RELAY.target,
151 profile: RELAY.profile,
152 enabled: true,
153 };
154 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
155 routes?.map((route) => connectionRouteId(route.target)) ?? null;
156 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
157
158 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
159 const routes = mergeLearnedRoutes({
160 entry: relayOnly,
161 activeRoute: RELAY,
162 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
163 allowInsecure: true,
164 });
165 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
166 expect(profileOf(routes![0]!)).toMatchObject({
167 learned: true,
168 authorization: "t3-connect",
169 wsBaseUrl: "ws://192.168.1.10:3773/",
170 });
171 });
172
173 it("replaces a learned LAN address when the server reports a new one", () => {
174 const first = mergeLearnedRoutes({
175 entry: relayOnly,
176 activeRoute: RELAY,
177 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
178 allowInsecure: true,
179 })!;
180 const entry: ConnectionCatalogEntry = {
181 ...relayOnly,
182 target: first[0]!.target,
183 profile: first[0]!.profile,
184 alternateRoutes: first.slice(1),
185 };
186 const moved = mergeLearnedRoutes({
187 entry,
188 activeRoute: RELAY,
189 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
190 allowInsecure: true,
191 });
192 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
193 });
194
195 it("keeps a learned route where the user moved it while the server reports it", () => {
196 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
197 const first = mergeLearnedRoutes({
198 entry: relayOnly,
199 activeRoute: RELAY,
200 reported: [lan],
201 allowInsecure: true,
202 })!;
203 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
204 expect(
205 mergeLearnedRoutes({
206 entry: reordered,
207 activeRoute: RELAY,
208 reported: [lan],
209 allowInsecure: true,
210 }),
211 ).toBeNull();
212 const next = mergeLearnedRoutes({
213 entry: reordered,
214 activeRoute: RELAY,
215 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
216 allowInsecure: true,
217 });
218 expect(ids(next)).toEqual([
219 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
220 "relay",
221 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
222 ]);
223 });
224
225 it("labels a learned address Tailscale only while the server reports it as tailnet", () => {
226 const tailscale = { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" };
227 const mesh = { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" };
228 const first = mergeLearnedRoutes({
229 entry: relayOnly,
230 activeRoute: RELAY,
231 reported: [tailscale, mesh],
232 allowInsecure: true,
233 })!;
234 expect(first.map(connectionRouteLabel)).toEqual(["Tailscale", "VPN", "T3 Connect"]);
235
236 const corrected = mergeLearnedRoutes({
237 entry: entryWithRoutes(relayOnly, first),
238 activeRoute: RELAY,
239 reported: [{ ...tailscale, kind: "lan" }, mesh],
240 allowInsecure: true,
241 })!;
242 expect(ids(corrected)).toEqual(ids(first));
243 expect(corrected.map(connectionRouteLabel)).toEqual(["VPN", "VPN", "T3 Connect"]);
244 expect(
245 mergeLearnedRoutes({
246 entry: entryWithRoutes(relayOnly, corrected),
247 activeRoute: RELAY,
248 reported: [{ ...tailscale, kind: "lan" }, mesh],
249 allowInsecure: true,
250 }),
251 ).toBeNull();
252 });
253
254 it("leaves user routes alone and does not learn an address already saved", () => {
255 const entry: ConnectionCatalogEntry = {
256 target: LAN.target,
257 profile: LAN.profile,
258 alternateRoutes: [RELAY],
259 enabled: true,
260 };
261 expect(
262 mergeLearnedRoutes({
263 entry,
264 activeRoute: RELAY,
265 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
266 allowInsecure: true,
267 }),
268 ).toBeNull();
269 expect(
270 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
271 ).toBeNull();
272 });
273
274 it("borrows the paired token when learned over a bearer route", () => {
275 const entry: ConnectionCatalogEntry = {
276 ...relayOnly,
277 target: LAN.target,
278 profile: LAN.profile,
279 };
280 const routes = mergeLearnedRoutes({
281 entry,
282 activeRoute: LAN,
283 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
284 allowInsecure: true,
285 })!;
286 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
287 expect(profileOf(learned)).not.toHaveProperty("authorization");
288 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
289 });
290
291 it("keeps a token paired over HTTPS off plain HTTP addresses outside the tailnet", () => {
292 const entry = entryWithRoutes(relayOnly, [PUBLIC, RELAY]);
293 const routes = mergeLearnedRoutes({
294 entry,
295 activeRoute: PUBLIC,
296 reported: [
297 { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" },
298 { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" },
299 { kind: "tailnet", httpBaseUrl: "https://desk.tail1234.ts.net/" },
300 ],
301 allowInsecure: true,
302 });
303 expect(ids(routes)).toEqual([
304 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773@public`,
305 `learned:${ENVIRONMENT_ID}:https://desk.tail1234.ts.net@public`,
306 "public",
307 "relay",
308 ]);
309 });
310
311 it("drops a stale plain HTTP route borrowing an HTTPS-paired token while another route is active", () => {
312 const lanOrigin = "http://10.0.0.42:3773";
313 const stale = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@public`, `${lanOrigin}/`);
314 const entry = entryWithRoutes(relayOnly, [stale, PUBLIC, RELAY]);
315 expect(sendsTokenInClear([stale, PUBLIC, RELAY], stale)).toBe(true);
316 expect(ids(usableRoutes(entry))).toEqual(["public", "relay"]);
317 const routes = mergeLearnedRoutes({
318 entry,
319 activeRoute: RELAY,
320 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
321 allowInsecure: true,
322 });
323 // The stale route is gone; the address is learned again with T3 Connect's credential.
324 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:${lanOrigin}`, "public", "relay"]);
325 expect(profileOf(routes![0]!)).toMatchObject({ authorization: "t3-connect" });
326 });
327
328 it("keeps plain HTTP routes that don't borrow the active HTTPS-paired token", () => {
329 const lanOrigin = "http://10.0.0.42:3773";
330 const overLan = learned(`learned:${ENVIRONMENT_ID}:${lanOrigin}@lan`, `${lanOrigin}/`);
331 const routes = mergeLearnedRoutes({
332 entry: entryWithRoutes(relayOnly, [overLan, LAN, PUBLIC, RELAY]),
333 activeRoute: PUBLIC,
334 reported: [{ kind: "lan", httpBaseUrl: `${lanOrigin}/` }],
335 allowInsecure: true,
336 });
337 expect(routes).toBeNull();
338 });
339
340 it("learns plain HTTP LAN addresses over a token paired over plain HTTP", () => {
341 const entry = entryWithRoutes(relayOnly, [LAN, RELAY]);
342 const routes = mergeLearnedRoutes({
343 entry,
344 activeRoute: LAN,
345 reported: [{ kind: "lan", httpBaseUrl: "http://10.0.0.42:3773/" }],
346 allowInsecure: true,
347 });
348 expect(ids(routes)).toEqual([
349 "lan",
350 `learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773@lan`,
351 "relay",
352 ]);
353 });
354
355 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
356 expect(
357 mergeLearnedRoutes({
358 entry: relayOnly,
359 activeRoute: RELAY,
360 reported: [
361 { httpBaseUrl: "http://192.168.1.10:3773/" },
362 { httpBaseUrl: "http://127.0.0.1:3773/" },
363 ],
364 allowInsecure: false,
365 }),
366 ).toBeNull();
367 });
368
369 it("removes learned routes along with the route whose credential they borrow", () => {
370 const overRelay = mergeLearnedRoutes({
371 entry: relayOnly,
372 activeRoute: RELAY,
373 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
374 allowInsecure: true,
375 })!;
376 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
377
378 const paired: ConnectionCatalogEntry = {
379 ...relayOnly,
380 target: LAN.target,
381 profile: LAN.profile,
382 alternateRoutes: [RELAY],
383 };
384 const overLan = mergeLearnedRoutes({
385 entry: paired,
386 activeRoute: LAN,
387 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
388 allowInsecure: true,
389 })!;
390 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
391 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
392 });
393
394 it("counts an environment reached only through T3 Connect as removed with it", () => {
395 const learned = mergeLearnedRoutes({
396 entry: relayOnly,
397 activeRoute: RELAY,
398 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
399 allowInsecure: true,
400 })!;
401 expect(removedWithRelay(relayOnly)).toBe(true);
402 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
403 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
404 });
405
406 it("keeps pull request routing trust when a route is learned", () => {
407 const learned = mergeLearnedRoutes({
408 entry: relayOnly,
409 activeRoute: RELAY,
410 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
411 allowInsecure: true,
412 })!;
413 const withLearned: ConnectionCatalogEntry = {
414 ...relayOnly,
415 target: learned[0]!.target,
416 profile: learned[0]!.profile,
417 alternateRoutes: learned.slice(1),
418 };
419 expect(pullRequestRoutingConnectionKey(withLearned)).toBe(
420 pullRequestRoutingConnectionKey(relayOnly),
421 );
422 });
423
424 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
425 const first = mergeLearnedRoutes({
426 entry: relayOnly,
427 activeRoute: RELAY,
428 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
429 allowInsecure: true,
430 })!;
431 const learnedLan = first[0]!;
432 const entry: ConnectionCatalogEntry = {
433 ...relayOnly,
434 target: learnedLan.target,
435 profile: learnedLan.profile,
436 alternateRoutes: first.slice(1),
437 };
438 const next = mergeLearnedRoutes({
439 entry,
440 activeRoute: learnedLan,
441 reported: [
442 { httpBaseUrl: "http://192.168.1.10:3773/" },
443 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
444 ],
445 allowInsecure: true,
446 })!;
447 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
448 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
449 expect(connectionRouteId(route.target)).not.toContain("@");
450 }
451 });
452
453 it("saves a scheme change on the same host as a new address", () => {
454 const first = mergeLearnedRoutes({
455 entry: relayOnly,
456 activeRoute: RELAY,
457 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
458 allowInsecure: true,
459 })!;
460 const entry: ConnectionCatalogEntry = {
461 ...relayOnly,
462 target: first[0]!.target,
463 profile: first[0]!.profile,
464 alternateRoutes: first.slice(1),
465 };
466 const moved = mergeLearnedRoutes({
467 entry,
468 activeRoute: RELAY,
469 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
470 allowInsecure: true,
471 });
472 expect(moved).not.toBeNull();
473 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
474 });
475 });