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
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { it as effectIt } from "@effect/vitest"; |
| 4 | import type * as NodeOS from "node:os"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Deferred from "effect/Deferred"; |
| 7 | import * as Fiber from "effect/Fiber"; |
| 8 | import * as Ref from "effect/Ref"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Sink from "effect/Sink"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import * as HttpClient from "effect/http/HttpClient"; |
| 13 | import type * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 14 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 15 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 16 | import { describe, expect, it } from "vite-plus/test"; |
| 17 | |
| 18 | import * as ServerConfig from "../config.ts"; |
| 19 | import * as DirectEndpoints from "./DirectEndpoints.ts"; |
| 20 | import * as ServerEnvironment from "./ServerEnvironment.ts"; |
| 21 | import { resolveBoundEndpoints } from "./DirectEndpoints.ts"; |
| 22 | |
| 23 | const INTERFACES: ReturnType<typeof NodeOS.networkInterfaces> = { |
| 24 | lo0: [ |
| 25 | { |
| 26 | address: "127.0.0.1", |
| 27 | netmask: "255.0.0.0", |
| 28 | family: "IPv4", |
| 29 | mac: "00:00:00:00:00:00", |
| 30 | internal: true, |
| 31 | cidr: "127.0.0.1/8", |
| 32 | }, |
| 33 | ], |
| 34 | en0: [ |
| 35 | { |
| 36 | address: "192.168.1.10", |
| 37 | netmask: "255.255.255.0", |
| 38 | family: "IPv4", |
| 39 | mac: "aa:bb:cc:dd:ee:ff", |
| 40 | internal: false, |
| 41 | cidr: "192.168.1.10/24", |
| 42 | }, |
| 43 | { |
| 44 | address: "fe80::1", |
| 45 | netmask: "ffff:ffff:ffff:ffff::", |
| 46 | family: "IPv6", |
| 47 | mac: "aa:bb:cc:dd:ee:ff", |
| 48 | internal: false, |
| 49 | cidr: "fe80::1/64", |
| 50 | scopeid: 4, |
| 51 | }, |
| 52 | ], |
| 53 | en1: [ |
| 54 | { |
| 55 | address: "203.0.113.20", |
| 56 | netmask: "255.255.255.0", |
| 57 | family: "IPv4", |
| 58 | mac: "aa:bb:cc:dd:ee:00", |
| 59 | internal: false, |
| 60 | cidr: "203.0.113.20/24", |
| 61 | }, |
| 62 | ], |
| 63 | utun4: [ |
| 64 | { |
| 65 | address: "100.101.102.103", |
| 66 | netmask: "255.255.255.255", |
| 67 | family: "IPv4", |
| 68 | mac: "00:00:00:00:00:00", |
| 69 | internal: false, |
| 70 | cidr: "100.101.102.103/32", |
| 71 | }, |
| 72 | { |
| 73 | address: "fd7a:115c:a1e0::1", |
| 74 | netmask: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", |
| 75 | family: "IPv6", |
| 76 | mac: "00:00:00:00:00:00", |
| 77 | internal: false, |
| 78 | cidr: "fd7a:115c:a1e0::1/128", |
| 79 | scopeid: 0, |
| 80 | }, |
| 81 | ], |
| 82 | }; |
| 83 | |
| 84 | const vpnInterface = (address: string) => [ |
| 85 | { |
| 86 | address, |
| 87 | netmask: "255.255.255.255", |
| 88 | family: "IPv4" as const, |
| 89 | mac: "00:00:00:00:00:00", |
| 90 | internal: false, |
| 91 | cidr: `${address}/32`, |
| 92 | }, |
| 93 | ]; |
| 94 | |
| 95 | const virtualInterface = (address: string, mac = "02:42:ac:11:00:01") => [ |
| 96 | { |
| 97 | address, |
| 98 | netmask: "255.255.0.0", |
| 99 | family: "IPv4" as const, |
| 100 | mac, |
| 101 | internal: false, |
| 102 | cidr: `${address}/16`, |
| 103 | }, |
| 104 | ]; |
| 105 | |
| 106 | describe("resolveBoundEndpoints", () => { |
| 107 | it("lists every external IPv4 address for a wildcard bind, tagging the tailnet one", () => { |
| 108 | expect( |
| 109 | resolveBoundEndpoints({ |
| 110 | platform: "linux", |
| 111 | host: "0.0.0.0", |
| 112 | port: 3773, |
| 113 | interfaces: INTERFACES, |
| 114 | }), |
| 115 | ).toEqual([ |
| 116 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 117 | { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, |
| 118 | ]); |
| 119 | }); |
| 120 | |
| 121 | it("skips container and VM networks, which only this machine reaches", () => { |
| 122 | const interfaces = { |
| 123 | ...INTERFACES, |
| 124 | docker0: virtualInterface("172.17.0.1"), |
| 125 | "br-3f2a1b": virtualInterface("172.18.0.1"), |
| 126 | virbr0: virtualInterface("192.168.122.1"), |
| 127 | "vEthernet (WSL)": virtualInterface("172.24.0.1"), |
| 128 | bridge100: virtualInterface("192.168.64.1"), |
| 129 | vmbr0: virtualInterface("192.168.1.20"), |
| 130 | }; |
| 131 | expect( |
| 132 | resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }), |
| 133 | ).toEqual([ |
| 134 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 135 | { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, |
| 136 | { kind: "lan", httpBaseUrl: "http://192.168.1.20:3773/" }, |
| 137 | ]); |
| 138 | }); |
| 139 | |
| 140 | it("skips NixOS container and systemd-nspawn veths, which only this machine reaches", () => { |
| 141 | const interfaces = { |
| 142 | ...INTERFACES, |
| 143 | "ve-jellyfin": virtualInterface("192.168.53.193"), |
| 144 | "ve-qbittorrent": virtualInterface("192.168.55.225"), |
| 145 | "vb-jellyfin": virtualInterface("192.168.100.1"), |
| 146 | "vz-machine": virtualInterface("192.168.101.1"), |
| 147 | }; |
| 148 | expect( |
| 149 | resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }), |
| 150 | ).toEqual([ |
| 151 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 152 | { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, |
| 153 | ]); |
| 154 | }); |
| 155 | |
| 156 | it("keeps a Windows alias matching a Linux container prefix eligible", () => { |
| 157 | const interfaces = { |
| 158 | ...INTERFACES, |
| 159 | "ve-office": virtualInterface("192.168.1.30"), |
| 160 | }; |
| 161 | expect( |
| 162 | resolveBoundEndpoints({ platform: "win32", host: "0.0.0.0", port: 3773, interfaces }), |
| 163 | ).toEqual([ |
| 164 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 165 | { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, |
| 166 | { kind: "lan", httpBaseUrl: "http://192.168.1.30:3773/" }, |
| 167 | ]); |
| 168 | }); |
| 169 | |
| 170 | it("omits Windows host-only adapters without excluding guest NICs or explicit binds", () => { |
| 171 | const interfaces = { |
| 172 | ...INTERFACES, |
| 173 | "VirtualBox-Host-Only-Netzwerk": virtualInterface("192.168.56.1", "0A:00:27:00:01:01"), |
| 174 | "Ethernet 2": virtualInterface("192.168.150.1", "00:50:56:c0:00:01"), |
| 175 | "Renamed network": virtualInterface("192.168.151.1", "00:50:56:c0:00:08"), |
| 176 | guest1: virtualInterface("192.168.1.30", "08:00:27:5e:1a:22"), |
| 177 | guest2: virtualInterface("192.168.1.31", "00:50:56:2b:8c:01"), |
| 178 | guest3: virtualInterface("192.168.1.32", "00:0c:29:4f:8e:35"), |
| 179 | guest4: virtualInterface("192.168.1.33", "00:50:56:80:8c:01"), |
| 180 | }; |
| 181 | const input = { host: "0.0.0.0", port: 3773, interfaces }; |
| 182 | const retained = [ |
| 183 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 184 | { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, |
| 185 | ...[30, 31, 32, 33].map((last) => ({ |
| 186 | kind: "lan", |
| 187 | httpBaseUrl: `http://192.168.1.${last}:3773/`, |
| 188 | })), |
| 189 | ]; |
| 190 | expect(resolveBoundEndpoints({ ...input, platform: "win32" })).toEqual(retained); |
| 191 | for (const platform of ["linux", "darwin"] as const) { |
| 192 | expect(resolveBoundEndpoints({ ...input, platform })).toHaveLength(retained.length + 3); |
| 193 | } |
| 194 | for (const host of ["192.168.56.1", "192.168.150.1", "192.168.151.1"]) { |
| 195 | expect(resolveBoundEndpoints({ ...input, platform: "win32", host })).toEqual([ |
| 196 | { kind: "lan", httpBaseUrl: `http://${host}:3773/` }, |
| 197 | ]); |
| 198 | } |
| 199 | }); |
| 200 | |
| 201 | it("tags only addresses on Tailscale's interface as tailnet, not other VPNs in its range", () => { |
| 202 | const interfaces = { |
| 203 | en0: INTERFACES.en0, |
| 204 | tailscale0: vpnInterface("100.70.1.2"), |
| 205 | CloudflareWARP: vpnInterface("100.96.0.1"), |
| 206 | utun7: vpnInterface("100.85.0.1"), |
| 207 | }; |
| 208 | expect( |
| 209 | resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }), |
| 210 | ).toEqual([ |
| 211 | { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, |
| 212 | { kind: "tailnet", httpBaseUrl: "http://100.70.1.2:3773/" }, |
| 213 | { kind: "lan", httpBaseUrl: "http://100.96.0.1:3773/" }, |
| 214 | { kind: "lan", httpBaseUrl: "http://100.85.0.1:3773/" }, |
| 215 | ]); |
| 216 | }); |
| 217 | |
| 218 | it("never reports a host name, which can resolve to another machine per client", () => { |
| 219 | for (const host of ["server.local", "devbox", "devbox.home.arpa"]) { |
| 220 | expect( |
| 221 | resolveBoundEndpoints({ platform: "linux", host, port: 3773, interfaces: INTERFACES }), |
| 222 | ).toEqual([]); |
| 223 | } |
| 224 | }); |
| 225 | |
| 226 | it("never reports a public address, which would carry the credential over plain HTTP", () => { |
| 227 | expect( |
| 228 | resolveBoundEndpoints({ |
| 229 | platform: "linux", |
| 230 | host: "203.0.113.20", |
| 231 | port: 3773, |
| 232 | interfaces: INTERFACES, |
| 233 | }), |
| 234 | ).toEqual([]); |
| 235 | }); |
| 236 | |
| 237 | it("reports only the tailnet address on a cloud VM, whose private network no client shares", () => { |
| 238 | expect( |
| 239 | resolveBoundEndpoints({ |
| 240 | platform: "linux", |
| 241 | host: "0.0.0.0", |
| 242 | port: 3773, |
| 243 | interfaces: INTERFACES, |
| 244 | onCloudVm: true, |
| 245 | }), |
| 246 | ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]); |
| 247 | }); |
| 248 | |
| 249 | it("reports an explicit bind to Tailscale's IPv6 address as the tailnet route", () => { |
| 250 | expect( |
| 251 | resolveBoundEndpoints({ |
| 252 | platform: "linux", |
| 253 | host: "fd7a:115c:a1e0::1", |
| 254 | port: 3773, |
| 255 | interfaces: INTERFACES, |
| 256 | }), |
| 257 | ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://[fd7a:115c:a1e0::1]:3773/" }]); |
| 258 | }); |
| 259 | |
| 260 | it("never reports an IPv4-mapped host, which can name the client's own loopback", () => { |
| 261 | expect( |
| 262 | resolveBoundEndpoints({ |
| 263 | platform: "linux", |
| 264 | host: "::ffff:127.0.0.1", |
| 265 | port: 3773, |
| 266 | interfaces: INTERFACES, |
| 267 | }), |
| 268 | ).toEqual([]); |
| 269 | }); |
| 270 | }); |
| 271 | |
| 272 | const TAILSCALE_STATUS_JSON = JSON.stringify({ |
| 273 | Self: { DNSName: "bb-1.tail1234.ts.net.", TailscaleIPs: ["100.64.1.2"] }, |
| 274 | }); |
| 275 | |
| 276 | const layerTailscaleUp = Layer.succeed( |
| 277 | ChildProcessSpawner.ChildProcessSpawner, |
| 278 | ChildProcessSpawner.make(() => |
| 279 | Effect.succeed( |
| 280 | ChildProcessSpawner.makeHandle({ |
| 281 | pid: ChildProcessSpawner.ProcessId(1), |
| 282 | exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 283 | isRunning: Effect.succeed(false), |
| 284 | kill: () => Effect.void, |
| 285 | unref: Effect.succeed(Effect.void), |
| 286 | stdin: Sink.drain, |
| 287 | stdout: Stream.make(new TextEncoder().encode(TAILSCALE_STATUS_JSON)), |
| 288 | stderr: Stream.empty, |
| 289 | all: Stream.empty, |
| 290 | getInputFd: () => Sink.drain, |
| 291 | getOutputFd: () => Stream.empty, |
| 292 | }), |
| 293 | ), |
| 294 | ), |
| 295 | ); |
| 296 | |
| 297 | const T3_DESCRIPTOR_BODY = JSON.stringify({ |
| 298 | environmentId: "serve-test", |
| 299 | label: "Serve test", |
| 300 | platform: { os: "darwin", arch: "arm64" }, |
| 301 | serverVersion: "0.0.0-test", |
| 302 | capabilities: { repositoryIdentity: true }, |
| 303 | }); |
| 304 | |
| 305 | const serveProbeResponse = (request: HttpClientRequest.HttpClientRequest, status: number) => |
| 306 | HttpClientResponse.fromWeb( |
| 307 | request, |
| 308 | new Response(status === 200 ? T3_DESCRIPTOR_BODY : null, { status }), |
| 309 | ); |
| 310 | |
| 311 | const layerServeProbe = (status: number) => |
| 312 | Layer.succeed( |
| 313 | HttpClient.HttpClient, |
| 314 | HttpClient.make((request) => Effect.succeed(serveProbeResponse(request, status))), |
| 315 | ); |
| 316 | |
| 317 | const layerServeConfig = (host = "127.0.0.1") => |
| 318 | Layer.effect( |
| 319 | ServerConfig.ServerConfig, |
| 320 | Effect.map(ServerConfig.ServerConfig, (config) => ({ |
| 321 | ...config, |
| 322 | host, |
| 323 | port: 3773, |
| 324 | tailscaleServeEnabled: true, |
| 325 | tailscaleServePort: 443, |
| 326 | })), |
| 327 | ).pipe( |
| 328 | Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })), |
| 329 | Layer.provide(NodeServices.layer), |
| 330 | Layer.merge( |
| 331 | Layer.succeed(ServerEnvironment.ServerEnvironment, { |
| 332 | getEnvironmentId: Effect.succeed(EnvironmentId.make("serve-test")), |
| 333 | getDescriptor: Effect.die("unused"), |
| 334 | }), |
| 335 | ), |
| 336 | ); |
| 337 | |
| 338 | const resolveWithServe = (probeStatus: number) => |
| 339 | Effect.flatMap(DirectEndpoints.DirectEndpoints, (service) => service.resolve()).pipe( |
| 340 | Effect.provide( |
| 341 | DirectEndpoints.layer.pipe( |
| 342 | Layer.provide( |
| 343 | Layer.mergeAll(layerServeConfig(), layerTailscaleUp, layerServeProbe(probeStatus)), |
| 344 | ), |
| 345 | ), |
| 346 | ), |
| 347 | ); |
| 348 | |
| 349 | describe("DirectEndpoints Tailscale Serve", () => { |
| 350 | effectIt.effect("lists the tailnet name once Serve answers for this server", () => |
| 351 | Effect.gen(function* () { |
| 352 | expect(yield* resolveWithServe(200)).toEqual([ |
| 353 | { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" }, |
| 354 | ]); |
| 355 | }), |
| 356 | ); |
| 357 | |
| 358 | effectIt.effect( |
| 359 | "returns bound endpoints during validation and reuses only a completed Serve result", |
| 360 | () => |
| 361 | Effect.gen(function* () { |
| 362 | const status = yield* Ref.make(200); |
| 363 | const finishProbe = yield* Deferred.make<void>(); |
| 364 | const probeStarted = yield* Deferred.make<void>(); |
| 365 | const revalidationStarted = yield* Deferred.make<void>(); |
| 366 | const finishRevalidation = yield* Deferred.make<void>(); |
| 367 | const service = yield* DirectEndpoints.make.pipe( |
| 368 | Effect.provide( |
| 369 | Layer.mergeAll( |
| 370 | layerServeConfig("192.168.1.10"), |
| 371 | layerTailscaleUp, |
| 372 | Layer.succeed( |
| 373 | HttpClient.HttpClient, |
| 374 | HttpClient.make((request) => |
| 375 | Deferred.succeed(probeStarted, undefined).pipe( |
| 376 | Effect.andThen(Deferred.await(finishProbe)), |
| 377 | Effect.andThen(Ref.get(status)), |
| 378 | Effect.tap((status) => |
| 379 | status === 502 |
| 380 | ? Deferred.succeed(revalidationStarted, undefined).pipe( |
| 381 | Effect.andThen(Deferred.await(finishRevalidation)), |
| 382 | ) |
| 383 | : Effect.void, |
| 384 | ), |
| 385 | Effect.map((status) => serveProbeResponse(request, status)), |
| 386 | ), |
| 387 | ), |
| 388 | ), |
| 389 | ), |
| 390 | ), |
| 391 | ); |
| 392 | const resolving = yield* Effect.forkScoped(service.resolve()); |
| 393 | yield* Deferred.await(probeStarted); |
| 394 | const bound = { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }; |
| 395 | expect(yield* service.resolveAvailable()).toEqual([bound]); |
| 396 | yield* Deferred.succeed(finishProbe, undefined); |
| 397 | const validated = yield* Fiber.join(resolving); |
| 398 | expect(validated).toEqual([ |
| 399 | bound, |
| 400 | { kind: "tailnet", httpBaseUrl: "https://bb-1.tail1234.ts.net/" }, |
| 401 | ]); |
| 402 | expect(yield* service.resolveAvailable()).toEqual(validated); |
| 403 | yield* Ref.set(status, 502); |
| 404 | const revalidating = yield* Effect.forkScoped(service.resolve()); |
| 405 | yield* Deferred.await(revalidationStarted); |
| 406 | expect(yield* service.resolveAvailable()).toEqual(validated); |
| 407 | yield* Deferred.succeed(finishRevalidation, undefined); |
| 408 | expect(yield* Fiber.join(revalidating)).toEqual([bound]); |
| 409 | expect(yield* service.resolveAvailable()).toEqual([bound]); |
| 410 | }), |
| 411 | ); |
| 412 | |
| 413 | effectIt.effect("leaves the tailnet name out when Serve is not forwarding", () => |
| 414 | Effect.gen(function* () { |
| 415 | expect(yield* resolveWithServe(502)).toEqual([]); |
| 416 | }), |
| 417 | ); |
| 418 | }); |