@devries-cameron20 / t3code Mirror · read-only / commits / db63178ff7
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, 19:45 UTC

fix(desktop): Tailscale HTTPS counts as on only when Serve reaches this backend (#35544)

Julius Marminge committed Nov 12, 2026 at 11:25 UTC db63178ff7a2065e9c9f835aac19e049321b334c
7 files changed +104 -12
apps/desktop/src/backend/tailscaleEndpointProvider.test.ts
+38 -1
@@ -1,7 +1,7 @@
1 import { assert, describe, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 -import { HttpClient } from "effect/http";
4 +import { HttpClient, HttpClientResponse } from "effect/http";
5 import { ChildProcessSpawner } from "effect/process";
6
7 import { resolveTailscaleAdvertisedEndpoints } from "./tailscaleEndpointProvider.ts";
@@ -79,4 +79,41 @@ describe("tailscale endpoint provider", () => {
79 );
80 }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
81 );
82 +
83 + it.effect("offers Tailscale HTTPS only when Serve reaches this desktop's backend", () =>
84 + Effect.gen(function* () {
85 + const magicDnsStatus = (servedEnvironmentId: string) =>
86 + resolveTailscaleAdvertisedEndpoints({
87 + port: 3773,
88 + serveEnabled: true,
89 + networkInterfaces: {},
90 + statusJson: `{"Self":{"DNSName":"desktop.example.ts.net.","TailscaleIPs":[]}}`,
91 + }).pipe(
92 + Effect.map((endpoints) => endpoints.map((endpoint) => endpoint.status)),
93 + Effect.provideService(
94 + HttpClient.HttpClient,
95 + HttpClient.make((request) =>
96 + Effect.succeed(
97 + HttpClientResponse.fromWeb(
98 + request,
99 + Response.json({
100 + environmentId:
101 + new URL(request.url).hostname === "127.0.0.1"
102 + ? "desktop-backend"
103 + : servedEnvironmentId,
104 + label: "T3 server",
105 + platform: { os: "linux", arch: "x64" },
106 + serverVersion: "0.1.9",
107 + capabilities: { repositoryIdentity: true },
108 + }),
109 + ),
110 + ),
111 + ),
112 + ),
113 + );
114 +
115 + assert.deepEqual(yield* magicDnsStatus("desktop-backend"), ["available"]);
116 + assert.deepEqual(yield* magicDnsStatus("another-server"), ["unavailable"]);
117 + }).pipe(Effect.provide(layerUnusedTailscaleExternalServices)),
118 + );
119 });
apps/desktop/src/backend/tailscaleEndpointProvider.ts
+18 -6
@@ -1,5 +1,9 @@
1 import { createAdvertisedEndpoint } from "@t3tools/shared/advertisedEndpoint";
2 -import type { AdvertisedEndpoint, AdvertisedEndpointProvider } from "@t3tools/contracts";
2 +import {
3 + ExecutionEnvironmentDescriptor,
4 + type AdvertisedEndpoint,
5 + type AdvertisedEndpointProvider,
6 +} from "@t3tools/contracts";
7 import {
8 buildTailscaleHttpsBaseUrl,
9 isTailscaleIpv4Address,
@@ -11,6 +15,7 @@ import {
15 import * as Effect from "effect/Effect";
16 import * as Option from "effect/Option";
17 import * as HttpClient from "effect/http/HttpClient";
18 +import * as HttpClientResponse from "effect/http/HttpClientResponse";
19 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
20
21 import type { NetworkInterfaces } from "./DesktopNetworkInterfaces.ts";
@@ -71,10 +76,20 @@ function resolveTailscaleIpAdvertisedEndpoints(input: {
76 return endpoints;
77 }
78
79 +// The backend may run elsewhere (WSL), so ask it for its id instead of reading the state dir.
80 +const probeServesThisBackend = (localHttpUrl: string) => (baseUrl: string) =>
81 + HttpClient.get(new URL("/.well-known/t3/environment", localHttpUrl)).pipe(
82 + Effect.flatMap(HttpClientResponse.filterStatusOk),
83 + Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)),
84 + Effect.flatMap(({ environmentId }) => probeTailscaleHttpsEndpoint({ baseUrl, environmentId })),
85 + Effect.orElseSucceed(() => false),
86 + );
87 +
88 const resolveTailscaleMagicDnsAdvertisedEndpoint = Effect.fn(
89 "resolveTailscaleMagicDnsAdvertisedEndpoint",
90 )(function* (input: {
91 readonly dnsName: string | null;
92 + readonly localHttpUrl: string;
93 readonly serveEnabled: boolean;
94 readonly servePort?: number;
95 readonly probe?: (baseUrl: string) => Effect.Effect<boolean, never, HttpClient.HttpClient>;
@@ -87,11 +102,7 @@ const resolveTailscaleMagicDnsAdvertisedEndpoint = Effect.fn(
102 magicDnsName: input.dnsName,
103 ...(input.servePort === undefined ? {} : { servePort: input.servePort }),
104 });
90 - const probe =
91 - input.probe?.(httpBaseUrl) ??
92 - probeTailscaleHttpsEndpoint({
93 - baseUrl: httpBaseUrl,
94 - });
105 + const probe = (input.probe ?? probeServesThisBackend(input.localHttpUrl))(httpBaseUrl);
106 const isReachable = input.serveEnabled ? yield* probe : false;
107
108 return Option.some(
@@ -140,6 +151,7 @@ export const resolveTailscaleAdvertisedEndpoints = Effect.fn("resolveTailscaleAd
151 const ipEndpoints = resolveTailscaleIpAdvertisedEndpoints({ ...input, status });
152 const magicDnsEndpoint = yield* resolveTailscaleMagicDnsAdvertisedEndpoint({
153 dnsName: status?.magicDnsName ?? null,
154 + localHttpUrl: `http://127.0.0.1:${input.port}`,
155 serveEnabled: input.serveEnabled === true,
156 ...(input.servePort === undefined ? {} : { servePort: input.servePort }),
157 ...(input.probe === undefined ? {} : { probe: input.probe }),
apps/server/src/environment/DirectEndpoints.test.ts
+8
@@ -1,4 +1,5 @@
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";
@@ -16,6 +17,7 @@ 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> = {
@@ -284,6 +286,12 @@ const layerServeConfig = (host = "127.0.0.1") =>
286 ).pipe(
287 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-direct-" })),
288 Layer.provide(NodeServices.layer),
289 + Layer.merge(
290 + Layer.succeed(ServerEnvironment.ServerEnvironment, {
291 + getEnvironmentId: Effect.succeed(EnvironmentId.make("serve-test")),
292 + getDescriptor: Effect.die("unused"),
293 + }),
294 + ),
295 );
296
297 const resolveWithServe = (probeStatus: number) =>
apps/server/src/environment/DirectEndpoints.ts
+3 -1
@@ -23,6 +23,7 @@ import {
23 } from "@t3tools/shared/hostClassification";
24
25 import * as ServerConfig from "../config.ts";
26 +import * as ServerEnvironment from "./ServerEnvironment.ts";
27 import { isLoopbackHost, isPrivateNetworkAddress, VIRTUAL_INTERFACE } from "../startupAccess.ts";
28
29 type NetworkInterfacesMap = ReturnType<typeof NodeOS.networkInterfaces>;
@@ -120,6 +121,7 @@ export const make = Effect.gen(function* () {
121 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
122 const httpClient = yield* HttpClient.HttpClient;
123 const platform = yield* HostProcess.Platform;
124 + const environmentId = yield* (yield* ServerEnvironment.ServerEnvironment).getEnvironmentId;
125 // Only the service default listens on every interface without being asked,
126 // so only it checks once whether it runs in a cloud. The check starts now so
127 // the first client does not wait for its timeout.
@@ -155,7 +157,7 @@ export const make = Effect.gen(function* () {
157 magicDnsName,
158 servePort: config.tailscaleServePort,
159 });
158 - return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl }).pipe(
160 + return probeTailscaleHttpsEndpoint({ baseUrl: httpBaseUrl, environmentId }).pipe(
161 Effect.map((reachable) => (reachable ? httpBaseUrl : null)),
162 );
163 }),
apps/server/src/server.ts
+1 -1
@@ -773,7 +773,7 @@ const layerRuntimeDependencies = layerRuntimeCoreDependencies.pipe(
773 Layer.provideMerge(AnalyticsService.layer.pipe(Layer.provide(ManagedPolicy.layer))),
774 Layer.provideMerge(ExternalLauncher.layer),
775 Layer.provideMerge(RemoteOpenTargets.layer),
776 - Layer.provideMerge(DirectEndpoints.layer),
776 + Layer.provideMerge(DirectEndpoints.layer.pipe(Layer.provide(layerServerEnvironment))),
777 Layer.provideMerge(ServerLifecycleEvents.layer),
778 Layer.provide(NetService.layer),
779 );
packages/tailscale/src/tailscale.test.ts
+29
@@ -110,6 +110,7 @@ describe("tailscale", () => {
110 ] as const) {
111 const reachable = yield* probeTailscaleHttpsEndpoint({
112 baseUrl: "https://desktop.example.ts.net",
113 + environmentId: "this-environment",
114 }).pipe(
115 Effect.provideService(
116 HttpClient.HttpClient,
@@ -123,6 +124,34 @@ describe("tailscale", () => {
124 }),
125 );
126
127 + it.effect("does not treat another T3 server behind the Serve URL as this server", () =>
128 + Effect.gen(function* () {
129 + const answer = (environmentId: string) =>
130 + HttpClient.make((request) =>
131 + Effect.succeed(
132 + HttpClientResponse.fromWeb(
133 + request,
134 + Response.json({
135 + environmentId,
136 + label: "T3 server",
137 + platform: { os: "linux", arch: "x64" },
138 + serverVersion: "0.1.9",
139 + capabilities: { repositoryIdentity: true },
140 + }),
141 + ),
142 + ),
143 + );
144 + const probe = (served: string) =>
145 + probeTailscaleHttpsEndpoint({
146 + baseUrl: "https://desktop.example.ts.net",
147 + environmentId: "this-environment",
148 + }).pipe(Effect.provideService(HttpClient.HttpClient, answer(served)));
149 +
150 + assert.isTrue(yield* probe("this-environment"));
151 + assert.isFalse(yield* probe("another-environment"));
152 + }),
153 + );
154 +
155 it("finds the Serve routes that forward to the bound address, and only those", () => {
156 const status = JSON.stringify({
157 TCP: {
packages/tailscale/src/tailscale.ts
+7 -3
@@ -760,18 +760,22 @@ export const readTailscaleServeRootHolder = Effect.fn("tailscale.readTailscaleSe
760 );
761
762 export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")(
763 - function* (input: { readonly baseUrl: string; readonly timeout?: Duration.Input }) {
763 + function* (input: {
764 + readonly baseUrl: string;
765 + readonly environmentId: string;
766 + readonly timeout?: Duration.Input;
767 + }) {
768 const client = yield* HttpClient.HttpClient;
769 const descriptor = yield* Effect.gen(function* () {
770 const url = new URL("/.well-known/t3/environment", input.baseUrl);
771 const response = yield* client
772 .execute(HttpClientRequest.get(url.toString()))
773 .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
770 - // A Serve mapping to another app also answers 2xx; only a T3 descriptor proves this server.
774 + // Another app or another T3 server can hold the Serve port; only our own descriptor counts.
775 return yield* HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)(response);
776 }).pipe(Effect.timeoutOption(input.timeout ?? TAILSCALE_PROBE_TIMEOUT));
777
774 - return Option.isSome(descriptor);
778 + return Option.isSome(descriptor) && descriptor.value.environmentId === input.environmentId;
779 },
780 Effect.orElseSucceed(() => false),
781 );