fix(server): stop re-spawning tailscale on every client connect on macOS #26103
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +35,21 @@35+ Object.values(NodeOS.networkInterfaces())36+ .flatMap((addresses) => addresses ?? [])37+ .filter(38+ (address) =>39+ !address.internal &&40+ address.family === "IPv4" &&41+ isTailscaleIpv4Address(address.address),42+ )43+ .map((address) => address.address),44+ ),45+ ]46+ .toSorted()47+ .join(",");48+49+const make = Effect.gen(function* () {50+ // Only macOS keeps reads: there each `tailscale` spawn can raise an "access data from other51+ // apps" prompt, so a success lives until the tailnet addresses change. Elsewhere every read is fresh.52+ const timeToLive =53+ (yield* HostProcess.Platform) === "darwin"54+ ? (exit: Exit.Exit<unknown, unknown>) =>55+ Exit.isSuccess(exit) ? Duration.infinity : FAILED_READ_TTL
juliusmarminge added labelsbugfix
juliusmarminge added labelspr:gate-failed
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -8,6 +14,21 @@814 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";15+import * as NodeDns from "node:dns";16+import * as NodeNet from "node:net";17+import * as NodeOS from "node:os";9181019 import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";11201221 const SSH_PORT = 22;1322 const TARGETS_CACHE_TTL = "60 seconds";23+const TAILNET_NAME_LOOKUP_TIMEOUT = "1500 millis";24+// Only the open-source tailscaled creates this; its CLI is not in an app bundle.25+const OPEN_SOURCE_TAILSCALED_SOCKET = "/var/run/tailscaled.socket";26+27+// MagicDNS answers reverse lookups for tailnet addresses through the system resolver.28+// Without an answer (e.g. accept-dns=false), the tailnet address is still reachable.29+const lookupTailnetHost = Effect.gen(function* () {30+ const address = Object.values(NodeOS.networkInterfaces())31+ .flatMap((addresses) => addresses ?? [])32+ .find(33+ (entry) =>34+ !entry.internal && entry.family === "IPv4" && isTailscaleIpv4Address(entry.address),
- @@ -9,5 +15,21 @@15+import * as NodeDns from "node:dns";16+import * as NodeNet from "node:net";17+import * as NodeOS from "node:os";9181019 import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";11201221 const SSH_PORT = 22;1322 const TARGETS_CACHE_TTL = "60 seconds";23+const TAILNET_NAME_LOOKUP_TIMEOUT = "1500 millis";24+// Only the open-source tailscaled creates this; its CLI is not in an app bundle.25+const OPEN_SOURCE_TAILSCALED_SOCKET = "/var/run/tailscaled.socket";26+27+// MagicDNS answers reverse lookups for tailnet addresses through the system resolver.28+// Without an answer (e.g. accept-dns=false), the tailnet address is still reachable.29+const lookupTailnetHost = Effect.gen(function* () {30+ const address = Object.values(NodeOS.networkInterfaces())31+ .flatMap((addresses) => addresses ?? [])32+ .find(33+ (entry) =>34+ !entry.internal && entry.family === "IPv4" && isTailscaleIpv4Address(entry.address),35+ )?.address;
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
juliusmarminge added labelstakeover:in-progress
juliusmarminge removed labelstakeover:blocked
Pushedb9bb43e
Pushed84ce8f7
- @@ -13,0 +23,21 @@23+const TAILNET_NAME_LOOKUP_TIMEOUT = "1500 millis";24+// Only the open-source tailscaled creates this; its CLI is not in an app bundle.25+const OPEN_SOURCE_TAILSCALED_SOCKET = "/var/run/tailscaled.socket";26+27+interface Tailnet {28+ readonly magicDnsName: string;29+ readonly ipv4Addresses: ReadonlyArray<string>;30+}31+32+// MagicDNS answers reverse lookups for tailnet addresses. Other VPNs also use the CGNAT33+// range, so an address without a MagicDNS name is not treated as a tailnet address.34+const lookupTailnetFromInterfaces = Effect.gen(function* () {35+ const address = Object.values(NodeOS.networkInterfaces())36+ .flatMap((addresses) => addresses ?? [])37+ .find(38+ (entry) =>39+ !entry.internal && entry.family === "IPv4" && isTailscaleIpv4Address(entry.address),40+ )?.address;41+ if (address === undefined) return null;42+ const magicDnsName = yield* Effect.promise(() =>43+ NodeDns.promises.lookupService(address, 0).then(
Pushedb42baf5
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -8,6 +14,21 @@814 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";15+import * as NodeDns from "node:dns";16+import * as NodeOS from "node:os";9171018 import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";11191220 const SSH_PORT = 22;1321 const TARGETS_CACHE_TTL = "60 seconds";22+const TAILNET_NAME_LOOKUP_TIMEOUT = "1500 millis";23+// Only the open-source tailscaled creates this; its CLI is not in an app bundle.24+const OPEN_SOURCE_TAILSCALED_SOCKET = "/var/run/tailscaled.socket";25+26+interface Tailnet {27+ readonly magicDnsName: string;28+ readonly ipv4Addresses: ReadonlyArray<string>;29+}30+31+// Other VPNs also use the CGNAT range, so only an address whose reverse lookup returns a32+// MagicDNS name (always under ts.net) is treated as this host's tailnet address.33+const lookupMagicDnsName = (address: string) =>34+ Effect.promise(() =>
Pushed26c120b
- @@ -0,0 +23,21 @@23+// name (always under ts.net) counts. Ask Tailscale's resolver directly: it answers even when24+// the Mac does not use Tailscale's DNS settings.25+const lookupMagicDnsName = (address: string) =>26+ Effect.promise(() => {27+ const resolver = new NodeDns.promises.Resolver({ tries: 1 });28+ resolver.setServers([TAILSCALE_RESOLVER]);29+ return resolver.reverse(address).then(30+ (names) =>31+ names32+ .map((name) => name.trim().replace(/\.$/u, "").toLowerCase())33+ .find((name) => name.endsWith(".ts.net")) ?? null,34+ () => null,35+ );36+ }).pipe(Effect.timeoutOption(TAILNET_NAME_LOOKUP_TIMEOUT), Effect.map(Option.getOrNull));37+38+const readTailnetFromInterfaces = Effect.gen(function* () {39+ const candidates = Object.values(NodeOS.networkInterfaces())40+ .flatMap((addresses) => addresses ?? [])41+ .filter(42+ (entry) =>43+ !entry.internal && entry.family === "IPv4" && isTailscaleIpv4Address(entry.address),
Pushed95b23cb
Pushed331b9c7
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
juliusmarminge added labelstakeover:in-progress
juliusmarminge removed labelstakeover:blocked
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed