Open History to see the last commit
1import type { RelayManagedEndpointRuntimeConfig } from "@t3tools/contracts/relay";
2import { RotatingFileSink, RotatingFileSinkError } from "@t3tools/shared/logging";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as RelayClient from "@t3tools/shared/relayClient";
5import * as Clock from "effect/Clock";
6import * as Config from "effect/Config";
7import * as Context from "effect/Context";
8import * as Deferred from "effect/Deferred";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Exit from "effect/Exit";
12import * as Layer from "effect/Layer";
13import * as Logger from "effect/Logger";
14import * as Option from "effect/Option";
15import * as Path from "effect/Path";
16import * as Queue from "effect/Queue";
17import * as Ref from "effect/Ref";
18import * as Result from "effect/Result";
19import * as Schema from "effect/Schema";
20import * as Semaphore from "effect/Semaphore";
21import * as Scope from "effect/Scope";
22import * as Stream from "effect/Stream";
23import { HttpClient, HttpClientResponse } from "effect/http";
24import * as ChildProcess from "effect/process/ChildProcess";
25import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
26
27import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
28import * as ServerConfig from "../config.ts";
29import * as ManagedLimits from "../policy/managedLimits.ts";
30import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
31
32export type CloudManagedEndpointRuntimeStatus =
33 | {
34 readonly status: "disabled";
35 }
36 | {
37 readonly status: "failed";
38 readonly providerKind: RelayManagedEndpointRuntimeConfig["providerKind"];
39 readonly failure: "unsupported-platform" | "not-installed" | "spawn-failed";
40 readonly reason: string;
41 readonly tunnelId?: string;
42 readonly tunnelName?: string;
43 }
44 | {
45 readonly status: "running";
46 readonly providerKind: "cloudflare_tunnel";
47 readonly pid: number;
48 readonly tunnelId?: string;
49 readonly tunnelName?: string;
50 }
51 | {
52 readonly status: "unsupported";
53 readonly providerKind: RelayManagedEndpointRuntimeConfig["providerKind"];
54 };
55
56/** What the running cloudflared reports about itself. `null` fields are not known yet. */
57export interface ConnectorFacts {
58 readonly pid: number;
59 readonly readyConnections: number | null;
60 /** The public tunnel hostname from the remote config cloudflared receives once it connects. */
61 readonly hostname: string | null;
62 readonly protocol: string | null;
63}
64
65export class CloudManagedEndpointRuntime extends Context.Service<
66 CloudManagedEndpointRuntime,
67 {
68 readonly applyConfig: (
69 config: RelayManagedEndpointRuntimeConfig | null,
70 ) => Effect.Effect<CloudManagedEndpointRuntimeStatus>;
71 /** `null` when no connector is running. */
72 readonly inspectConnector: Effect.Effect<ConnectorFacts | null>;
73 /** Emits when a connector registers a connection or its readiness changes. */
74 readonly readinessChanged: Stream.Stream<void>;
75 readonly recoveryRequests: Stream.Stream<RelayManagedEndpointRuntimeConfig>;
76 readonly requestRecovery: (config: RelayManagedEndpointRuntimeConfig) => Effect.Effect<void>;
77 /**
78 * Restarts the connector if it still runs as `pid` and `stillDead` holds once it has
79 * the lock. Returns whether it sent the restart.
80 */
81 readonly restartConnector: (
82 pid: number,
83 stillDead: Effect.Effect<boolean>,
84 ) => Effect.Effect<boolean>;
85 readonly tunnelConnected: Stream.Stream<void>;
86 readonly withLinkStateLock: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
87 }
88>()("t3/cloud/ManagedEndpointRuntime/CloudManagedEndpointRuntime") {}
89
90interface ActiveConnector {
91 readonly child: ChildProcessSpawner.ChildProcessHandle;
92 readonly executable: RelayClient.AvailableRelayClient;
93 readonly scope: Scope.Closeable;
94 readonly configKey: string;
95 readonly config: RelayManagedEndpointRuntimeConfig;
96 readonly startedAtMillis: number;
97 readonly registered: Deferred.Deferred<void>;
98 readonly metricsAddress: Deferred.Deferred<string>;
99 readonly protocol: Ref.Ref<string | null>;
100}
101
102// An instantly failing relay client otherwise respawns ~100 times/sec and exhausts V8 via tracing.
103const isRotatingFileSinkError = Schema.is(RotatingFileSinkError);
104
105const RELAY_RESTART_STABLE_UPTIME_MS = 30_000;
106const RELAY_RESTART_BACKOFF_BASE_MS = 1_000;
107const RELAY_RESTART_BACKOFF_MAX_MS = 60_000;
108// Newly created tunnels can fail authorization briefly while Cloudflare propagates their token.
109const TUNNEL_AUTHORIZATION_FAILURES_BEFORE_RECOVERY = 4;
110// Unknown rejection wording can leave a dead tunnel unconnected; periodically request recovery.
111const CONNECTOR_REGISTRATION_TIMEOUT = Duration.minutes(3);
112// cloudflared resolves edges only at startup and can keep dialing stale addresses after a network change.
113const CONNECTOR_READINESS_INTERVAL = Duration.seconds(30);
114const CONNECTOR_READINESS_REQUEST_TIMEOUT = Duration.seconds(5);
115const CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART = 6;
116const CONNECTOR_NOT_READY_CHECKS_MAX = 60;
117const RELAY_CLIENT_METRICS_LINE = /\bStarting metrics server on (127\.0\.0\.1:\d+)\/metrics\b/u;
118const RELAY_CLIENT_PROTOCOL = /\bprotocol=(\w+)/u;
119// cloudflared's metrics server: `/ready` answers 200 or 503 with this body, and
120// `/config` holds the ingress rules the edge pushed after the first connection.
121const ConnectorReadyBody = Schema.Struct({ readyConnections: Schema.Number });
122const ConnectorConfigBody = Schema.Struct({
123 config: Schema.Struct({
124 ingress: Schema.optional(
125 Schema.NullOr(Schema.Array(Schema.Struct({ hostname: Schema.optional(Schema.String) }))),
126 ),
127 }),
128});
129// Registration resets cloudflared's own fallback, even when QUIC stalls again
130// immediately. Keep that evidence across registrations and connector restarts.
131const QUIC_STALLS_BEFORE_HTTP2 = 30;
132const QUIC_STALL_WINDOW_MS = 5 * 60_000;
133// Retry QUIC on a later startup after transient failures or network changes; reuse never renews this hint.
134const HTTP2_FALLBACK_TTL_MS = 24 * 60 * 60_000;
135const HTTP2_FALLBACK_STATE = "cloud-endpoint-http2-fallback";
136const Http2Fallback = Schema.fromJsonString(Schema.Struct({ fellBackAt: Schema.Number }));
137const decodeHttp2Fallback = Schema.decodeUnknownOption(Http2Fallback);
138const encodeHttp2Fallback = Schema.encodeSync(Http2Fallback);
139// A failed background install of the pinned relay client is retried no sooner
140// than this, so a crash-looping connector or an offline host does not redownload
141// on every reconcile.
142const RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS = 10 * 60_000;
143export const RELAY_CLIENT_LOG_FILE = "relay-client.ndjson";
144
145/** Preserve user-chosen PATH binaries and overrides; CLI downloads require consent. */
146function needsPinnedRelayClient(executable: RelayClient.RelayClientStatus): boolean {
147 return (
148 executable.status === "missing" ||
149 (executable.status === "available" &&
150 executable.source === "managed" &&
151 !RelayClient.isPinnedManagedRelayClient(executable))
152 );
153}
154
155export function classifyRelayClientOutput(
156 line: string,
157): "connected" | "unregistered" | "warning" | "debug" {
158 if (/\bRegistered tunnel connection\b/iu.test(line)) {
159 return "connected";
160 }
161 // cloudflared uses zerolog FTL/PNC for fatal/panic; classify them above debug too.
162 if (/\b(?:ERR|WRN|FTL|PNC)\b/u.test(line)) {
163 return "warning";
164 }
165 return /\bINF\b.*\bUnregistered tunnel connection\b/u.test(line) ? "unregistered" : "debug";
166}
167
168/** Edge rejection wording varies; any Unauthorized registration error counts. */
169export function isRejectedRelayClientTunnelOutput(line: string): boolean {
170 return (
171 /\bRegister tunnel error from server side\b/iu.test(line) &&
172 /error="(?:Unauthorized:[^"]*|Failed to get tunnel|Tunnel not found|Record for tunnel not found|Invalid tunnel secret)"/iu.test(
173 line,
174 )
175 );
176}
177
178export function isRetryableManagedEndpointRuntimeStatus(status: unknown): boolean {
179 if (typeof status !== "object" || status === null || !("status" in status)) {
180 return false;
181 }
182 if (status.status !== "failed" || !("failure" in status)) {
183 return false;
184 }
185 return status.failure === "not-installed" || status.failure === "spawn-failed";
186}
187
188function runtimeConfigKey(config: RelayManagedEndpointRuntimeConfig): string {
189 return JSON.stringify({
190 providerKind: config.providerKind,
191 connectorToken: config.connectorToken,
192 tunnelId: config.tunnelId ?? null,
193 tunnelName: config.tunnelName ?? null,
194 });
195}
196
197const stopConnector = (connector: ActiveConnector | null) =>
198 connector
199 ? Scope.close(connector.scope, Exit.void).pipe(
200 Effect.tap(() =>
201 Effect.logInfo("Relay client stopped", {
202 pid: Number(connector.child.pid),
203 }),
204 ),
205 Effect.ignore,
206 )
207 : Effect.void;
208
209/** @public */
210export const make = Effect.gen(function* () {
211 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
212 const relayClient = yield* RelayClient.RelayClient;
213 const secrets = yield* ServerSecretStore.ServerSecretStore;
214 const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
215 // Without T3 Connect no connector ever runs, whoever asks for one.
216 const connectAllowed = ManagedLimits.allowsConnect(yield* ManagedPolicy.ManagedPolicy);
217 const activeRef = yield* Ref.make<ActiveConnector | null>(null);
218 const desiredConfigRef = yield* Ref.make<RelayManagedEndpointRuntimeConfig | null>(null);
219 const recoveryRequests = yield* Queue.sliding<RelayManagedEndpointRuntimeConfig>(1);
220 const tunnelConnections = yield* Queue.sliding<void>(1);
221 const readinessChanges = yield* Queue.sliding<void>(1);
222 const reconcileSemaphore = yield* Semaphore.make(1);
223 const restartDelayRef = yield* Ref.make(0);
224 const linkStateSemaphore = yield* Semaphore.make(1);
225 const runtimeScope = yield* Effect.scope;
226 const installInFlightRef = yield* Ref.make(false);
227 const lastInstallFailureAtRef = yield* Ref.make<number | null>(null);
228 const prunedRef = yield* Ref.make(false);
229 const retryLoopRunningRef = yield* Ref.make(false);
230 const notReadyChecksBeforeRestartRef = yield* Ref.make(CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART);
231 // The relay client shares the server's process group, so it exits on these signals before the finalizer runs.
232 let shutdownSignalReceived = false;
233 const onShutdownSignal = () => {
234 shutdownSignalReceived = true;
235 };
236 yield* Effect.acquireRelease(
237 Effect.sync(() => {
238 process.on("SIGINT", onShutdownSignal);
239 process.on("SIGTERM", onShutdownSignal);
240 }),
241 () =>
242 Effect.sync(() => {
243 process.off("SIGINT", onShutdownSignal);
244 process.off("SIGTERM", onShutdownSignal);
245 }),
246 );
247 // An explicit cloudflared transport in the environment is the user's choice.
248 const env = yield* HostProcess.Environment;
249 const transportChosenByUser = Option.isSome(
250 yield* Config.String("TUNNEL_TRANSPORT_PROTOCOL").pipe(Config.option),
251 );
252 const previousFallback = transportChosenByUser
253 ? Option.none()
254 : yield* secrets.get(HTTP2_FALLBACK_STATE).pipe(
255 Effect.map(Option.flatMap((bytes) => decodeHttp2Fallback(new TextDecoder().decode(bytes)))),
256 Effect.orElseSucceed(() => Option.none()),
257 );
258 const now = yield* Clock.currentTimeMillis;
259 const http2Ref = yield* Ref.make(
260 Option.isSome(previousFallback) &&
261 now >= previousFallback.value.fellBackAt &&
262 now - previousFallback.value.fellBackAt < HTTP2_FALLBACK_TTL_MS,
263 );
264 const quicStallsRef = yield* Ref.make<ReadonlyArray<number>>([]);
265 let reconcileConfig: CloudManagedEndpointRuntime["Service"]["applyConfig"];
266
267 const { logsDir } = yield* ServerConfig.ServerConfig;
268 const path = yield* Path.Path;
269 const relayClientLogPath = path.join(logsDir, RELAY_CLIENT_LOG_FILE);
270 // Synchronous appends avoid a flush timer for this low-volume log.
271 const fallbackLoggers = yield* Effect.service(Logger.CurrentLoggers);
272 let writeFailureReported = false;
273 const relayClientLog = yield* Effect.try({
274 try: () =>
275 new RotatingFileSink({
276 filePath: relayClientLogPath,
277 maxBytes: 1024 * 1024,
278 maxFiles: 2,
279 throwOnError: true,
280 }),
281 catch: (cause) =>
282 isRotatingFileSinkError(cause)
283 ? cause
284 : new RotatingFileSinkError({
285 operation: "initialize",
286 filePath: relayClientLogPath,
287 cause,
288 }),
289 }).pipe(
290 Effect.map((sink) =>
291 Logger.make((options) => {
292 try {
293 const entry = Logger.formatJson.log(options);
294 const bytes = Buffer.byteLength(entry) + 1;
295 sink.write(
296 bytes <= 1024 * 1024
297 ? `${entry}\n`
298 : `${JSON.stringify({
299 timestamp: options.date.toISOString(),
300 level: options.logLevel.toUpperCase(),
301 message: ["Relay client log entry omitted: exceeds 1 MiB", { bytes }],
302 })}\n`,
303 );
304 } catch {
305 if (!writeFailureReported) {
306 writeFailureReported = true;
307 for (const logger of fallbackLoggers) {
308 logger.log({
309 ...options,
310 logLevel: "Warn",
311 message: ["Relay client log unavailable", { filePath: relayClientLogPath }],
312 });
313 }
314 }
315 }
316 }),
317 ),
318 Effect.catch((error) =>
319 Effect.logWarning("Relay client log unavailable", {
320 filePath: relayClientLogPath,
321 cause: error,
322 }).pipe(Effect.as(undefined)),
323 ),
324 );
325 // Forked connector fibers inherit this logger after the applyConfig span ends.
326 const withRelayClientLog = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
327 relayClientLog ? Effect.withLogger(effect, relayClientLog) : effect;
328
329 const stopActive = Effect.gen(function* () {
330 const active = yield* Ref.getAndSet(activeRef, null);
331 yield* stopConnector(active);
332 });
333
334 // Lives until connector exit and starts replacements; avoid nested lifetime spans.
335 const superviseConnector = Effect.fnUntraced(
336 function* (connector: ActiveConnector) {
337 const result = yield* Effect.result(connector.child.exitCode);
338 const activeAtExit = yield* Ref.get(activeRef);
339 if (
340 activeAtExit?.child.pid !== connector.child.pid ||
341 activeAtExit.configKey !== connector.configKey
342 ) {
343 return;
344 }
345 const uptimeMillis = (yield* Clock.currentTimeMillis) - connector.startedAtMillis;
346 // Back off outside the semaphore so config changes remain responsive; recheck config afterwards.
347 const restartDelayMillis = yield* Ref.modify(restartDelayRef, (current) => {
348 if (uptimeMillis >= RELAY_RESTART_STABLE_UPTIME_MS) {
349 return [0, 0];
350 }
351 return [
352 current,
353 current === 0
354 ? RELAY_RESTART_BACKOFF_BASE_MS
355 : Math.min(current * 2, RELAY_RESTART_BACKOFF_MAX_MS),
356 ];
357 });
358 if (restartDelayMillis > 0) {
359 yield* Effect.logWarning("Relay client is crash-looping; delaying restart", {
360 pid: Number(connector.child.pid),
361 uptimeMillis,
362 restartDelayMillis,
363 tunnelId: connector.config.tunnelId,
364 tunnelName: connector.config.tunnelName,
365 });
366 yield* Effect.sleep(Duration.millis(restartDelayMillis));
367 }
368 yield* reconcileSemaphore.withPermits(1)(
369 Effect.gen(function* () {
370 const active = yield* Ref.get(activeRef);
371 if (
372 active?.child.pid !== connector.child.pid ||
373 active.configKey !== connector.configKey
374 ) {
375 return;
376 }
377 yield* Ref.set(activeRef, null);
378 yield* stopConnector(connector);
379
380 const desiredConfig = yield* Ref.get(desiredConfigRef);
381 if (
382 shutdownSignalReceived ||
383 !desiredConfig ||
384 desiredConfig.providerKind !== "cloudflare_tunnel" ||
385 runtimeConfigKey(desiredConfig) !== connector.configKey
386 ) {
387 return;
388 }
389
390 yield* Effect.logWarning("Relay client exited; restarting", {
391 pid: Number(connector.child.pid),
392 ...(Result.isSuccess(result)
393 ? { exitCode: Number(result.success) }
394 : { cause: result.failure }),
395 tunnelId: connector.config.tunnelId,
396 tunnelName: connector.config.tunnelName,
397 });
398 yield* Queue.offer(recoveryRequests, connector.config);
399 yield* reconcileConfig(desiredConfig);
400 }),
401 );
402 },
403 Effect.catchCause((cause) => Effect.logWarning("Relay client supervisor failed", { cause })),
404 );
405
406 // Keep the old connector until its replacement registers, preserving the relay connection.
407 const installPinnedRelayClient = Effect.gen(function* () {
408 if (yield* Ref.getAndSet(installInFlightRef, true)) return;
409 yield* Effect.logInfo("Installing the pinned relay client", {
410 version: RelayClient.CLOUDFLARED_VERSION,
411 });
412 yield* relayClient.install.pipe(
413 Effect.tap(() => Ref.set(lastInstallFailureAtRef, null)),
414 Effect.flatMap((installed) =>
415 reconcileSemaphore.withPermits(1)(
416 Effect.gen(function* () {
417 const desiredConfig = yield* Ref.get(desiredConfigRef);
418 if (!desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel") return;
419 const active = yield* Ref.get(activeRef);
420 // Self-updated binaries share the pinned path; compare versions to detect replacements.
421 if (
422 active?.executable.executablePath === installed.executablePath &&
423 active.executable.version === installed.version
424 ) {
425 return;
426 }
427 // A scanner can block a fresh binary's probe; retry instead of restarting onto the older binary.
428 const resolved = yield* relayClient.resolve;
429 if (
430 resolved.status !== "available" ||
431 resolved.executablePath !== installed.executablePath ||
432 resolved.version !== installed.version
433 ) {
434 return;
435 }
436 yield* Effect.logInfo("Relay client installed; moving the connector to it", {
437 version: installed.version,
438 previousVersion: active?.executable.version,
439 });
440 const previous = yield* Ref.getAndSet(activeRef, null);
441 const status = yield* reconcileConfig(desiredConfig);
442 const next = yield* Ref.get(activeRef);
443 if (!next) {
444 yield* Effect.logWarning("Relay client did not start after the update", status);
445 // The supervisor skips exits while detached; request recovery explicitly.
446 const previousRunning = previous
447 ? yield* previous.child.isRunning.pipe(Effect.orElseSucceed(() => false))
448 : false;
449 if (previous && previousRunning) {
450 yield* Ref.set(activeRef, previous);
451 } else {
452 yield* stopConnector(previous);
453 yield* Queue.offer(recoveryRequests, desiredConfig);
454 }
455 return;
456 }
457 if (previous) {
458 yield* Deferred.await(next.registered).pipe(
459 Effect.ensuring(stopConnector(previous)),
460 Effect.forkIn(next.scope),
461 );
462 }
463 }),
464 ),
465 ),
466 Effect.catchTags({
467 RelayClientInstallError: (error) =>
468 Clock.currentTimeMillis.pipe(
469 Effect.flatMap((failedAt) => Ref.set(lastInstallFailureAtRef, failedAt)),
470 Effect.andThen(
471 Effect.logWarning("Could not install the pinned relay client", {
472 reason: error.reason,
473 message: error.message,
474 cause: error.cause,
475 }),
476 ),
477 ),
478 }),
479 Effect.ensuring(Ref.set(installInFlightRef, false)),
480 );
481 });
482
483 const ensurePinnedRelayClient = Effect.gen(function* () {
484 const now = yield* Clock.currentTimeMillis;
485 const lastFailureAt = yield* Ref.get(lastInstallFailureAtRef);
486 if (lastFailureAt !== null && now - lastFailureAt < RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS) {
487 return;
488 }
489 yield* installPinnedRelayClient;
490 }).pipe(Effect.forkIn(runtimeScope), Effect.asVoid);
491
492 // Retry installs skipped by applyConfig without the failure gate.
493 const retryPinnedRelayClientInstall = Effect.gen(function* () {
494 while (true) {
495 yield* Effect.sleep(Duration.millis(RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS));
496 const desiredConfig = yield* Ref.get(desiredConfigRef);
497 if (!desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel") return;
498 // An installed pin still needs activation after a failed probe or while an older connector runs.
499 const active = yield* Ref.get(activeRef);
500 if (active && !needsPinnedRelayClient(active.executable)) return;
501 yield* installPinnedRelayClient;
502 }
503 }).pipe(Effect.ensuring(Ref.set(retryLoopRunningRef, false)));
504
505 const startPinnedRelayClientRetries = Effect.gen(function* () {
506 if (yield* Ref.getAndSet(retryLoopRunningRef, true)) return;
507 yield* Effect.forkIn(retryPinnedRelayClientInstall, runtimeScope);
508 });
509
510 // Registration retries can last for the connector lifetime; keep this loop untraced.
511 const watchConnectorRegistration = Effect.fnUntraced(
512 function* (connector: ActiveConnector) {
513 yield* Effect.sleep(CONNECTOR_REGISTRATION_TIMEOUT);
514 if (yield* Deferred.isDone(connector.registered)) return true;
515 yield* Effect.logWarning(
516 "Relay client has not registered a tunnel connection; requesting recovery",
517 {
518 pid: Number(connector.child.pid),
519 tunnelId: connector.config.tunnelId,
520 tunnelName: connector.config.tunnelName,
521 },
522 );
523 yield* Queue.offer(recoveryRequests, connector.config);
524 return false;
525 },
526 Effect.repeat({ until: (connected) => connected }),
527 );
528
529 // Only an explicit 503 counts: a failed request says nothing about the
530 // Readiness polling runs for the connector lifetime; keep this loop untraced.
531 const watchConnectorReadiness = Effect.fnUntraced(function* (connector: ActiveConnector) {
532 const readyUrl = `http://${yield* Deferred.await(connector.metricsAddress)}/ready`;
533 let notReadyChecks = 0;
534 let lastReady: boolean | null = null;
535 while (true) {
536 yield* Effect.sleep(CONNECTOR_READINESS_INTERVAL);
537 const status = yield* httpClient.get(readyUrl).pipe(
538 Effect.map((response) => response.status),
539 Effect.scoped,
540 Effect.withTracerEnabled(false),
541 Effect.timeoutOption(CONNECTOR_READINESS_REQUEST_TIMEOUT),
542 Effect.orElseSucceed(() => Option.none<number>()),
543 );
544 if (Option.isNone(status)) continue;
545 if (status.value === 200 || status.value === 503) {
546 const ready = status.value === 200;
547 if (ready !== lastReady) yield* Queue.offer(readinessChanges, undefined);
548 lastReady = ready;
549 }
550 if (status.value === 200) {
551 notReadyChecks = 0;
552 yield* Ref.set(notReadyChecksBeforeRestartRef, CONNECTOR_NOT_READY_CHECKS_BEFORE_RESTART);
553 continue;
554 }
555 if (status.value !== 503) continue;
556 notReadyChecks += 1;
557 const threshold = yield* Ref.get(notReadyChecksBeforeRestartRef);
558 if (notReadyChecks < threshold) continue;
559 notReadyChecks = 0;
560 yield* Ref.set(
561 notReadyChecksBeforeRestartRef,
562 Math.min(threshold * 2, CONNECTOR_NOT_READY_CHECKS_MAX),
563 );
564 yield* Effect.logWarning("Relay client has no tunnel connection; restarting it", {
565 pid: Number(connector.child.pid),
566 notReadyChecks: threshold,
567 tunnelId: connector.config.tunnelId,
568 tunnelName: connector.config.tunnelName,
569 });
570 yield* connector.child.kill().pipe(Effect.ignore);
571 }
572 });
573
574 const switchToHttp2 = Effect.fnUntraced(function* (connector: ActiveConnector) {
575 const active = yield* Ref.get(activeRef);
576 const desiredConfig = yield* Ref.get(desiredConfigRef);
577 if ((yield* Ref.get(http2Ref)) || active !== connector || !desiredConfig) return;
578 yield* Ref.set(http2Ref, true);
579 yield* secrets
580 .set(
581 HTTP2_FALLBACK_STATE,
582 new TextEncoder().encode(
583 encodeHttp2Fallback({ fellBackAt: yield* Clock.currentTimeMillis }),
584 ),
585 )
586 .pipe(
587 Effect.catch(() => Effect.logWarning("Could not persist the relay client HTTP/2 fallback")),
588 );
589 yield* Effect.logWarning("Relay client keeps losing QUIC connections; switching to HTTP/2", {
590 pid: Number(connector.child.pid),
591 tunnelId: connector.config.tunnelId,
592 tunnelName: connector.config.tunnelName,
593 });
594 // Shutdown must not interrupt cleanup after active ownership is cleared.
595 yield* Effect.uninterruptible(stopActive);
596 const status = yield* reconcileConfig(desiredConfig);
597 // A failed spawn has no supervisor to request recovery.
598 if (status.status !== "running") {
599 yield* Queue.offer(recoveryRequests, desiredConfig);
600 }
601 }, reconcileSemaphore.withPermits(1));
602
603 const noteQuicStall = Effect.fnUntraced(function* (connector: ActiveConnector) {
604 if (transportChosenByUser || (yield* Ref.get(http2Ref))) return;
605 const now = yield* Clock.currentTimeMillis;
606 if ((yield* Ref.get(activeRef)) !== connector) return;
607 const stalls = yield* Ref.updateAndGet(quicStallsRef, (previous) => [
608 ...previous.filter((at) => now - at < QUIC_STALL_WINDOW_MS),
609 now,
610 ]);
611 if (stalls.length < QUIC_STALLS_BEFORE_HTTP2) return;
612 // Stopping the connector interrupts the output observer that called this.
613 yield* Effect.forkIn(switchToHttp2(connector), runtimeScope);
614 });
615
616 const observeConnectorOutput = (connector: ActiveConnector) => {
617 let rejectedRegistrations = 0;
618
619 return connector.child.all.pipe(
620 Stream.decodeText(),
621 Stream.splitLines,
622 Stream.map((line) => line.trim()),
623 Stream.filter((line) => line.length > 0),
624 Stream.runForEach((line) => {
625 const output = line.replaceAll(connector.config.connectorToken, "<redacted>");
626 const attributes = {
627 pid: Number(connector.child.pid),
628 tunnelId: connector.config.tunnelId,
629 tunnelName: connector.config.tunnelName,
630 output,
631 };
632 switch (classifyRelayClientOutput(line)) {
633 case "connected":
634 rejectedRegistrations = 0;
635 return Deferred.succeed(connector.registered, undefined).pipe(
636 Effect.andThen(
637 Ref.set(connector.protocol, RELAY_CLIENT_PROTOCOL.exec(line)?.[1] ?? null),
638 ),
639 Effect.andThen(
640 Effect.logInfo("Relay client tunnel connection registered", attributes),
641 ),
642 Effect.andThen(Queue.offer(tunnelConnections, undefined)),
643 Effect.andThen(Queue.offer(readinessChanges, undefined)),
644 Effect.andThen(
645 RelayClient.isPinnedManagedRelayClient(connector.executable)
646 ? Ref.getAndSet(prunedRef, true).pipe(
647 Effect.flatMap((pruned) =>
648 pruned ? Effect.void : relayClient.pruneManagedVersions,
649 ),
650 )
651 : Effect.void,
652 ),
653 Effect.asVoid,
654 );
655 case "unregistered":
656 return Effect.logInfo("Relay client tunnel connection unregistered", attributes);
657 case "warning":
658 if (/\btimeout: no recent network activity\b/u.test(line)) {
659 return noteQuicStall(connector).pipe(
660 Effect.andThen(
661 Effect.logWarning("Relay client reported a transport warning", attributes),
662 ),
663 );
664 }
665 if (isRejectedRelayClientTunnelOutput(line)) {
666 rejectedRegistrations += 1;
667 if (rejectedRegistrations >= TUNNEL_AUTHORIZATION_FAILURES_BEFORE_RECOVERY) {
668 rejectedRegistrations = 0;
669 return Effect.logWarning(
670 "Relay client tunnel was rejected; requesting recovery",
671 attributes,
672 ).pipe(
673 Effect.andThen(Queue.offer(recoveryRequests, connector.config)),
674 Effect.asVoid,
675 );
676 }
677 }
678 return Effect.logWarning("Relay client reported a transport warning", attributes);
679 case "debug": {
680 const metricsAddress = RELAY_CLIENT_METRICS_LINE.exec(line)?.[1];
681 return metricsAddress
682 ? Deferred.succeed(connector.metricsAddress, metricsAddress).pipe(
683 Effect.andThen(Effect.logDebug("Relay client output", attributes)),
684 )
685 : Effect.logDebug("Relay client output", attributes);
686 }
687 }
688 }),
689 Effect.catchCause((cause) =>
690 Effect.logWarning("Relay client output observer failed", {
691 cause,
692 pid: Number(connector.child.pid),
693 tunnelId: connector.config.tunnelId,
694 tunnelName: connector.config.tunnelName,
695 }),
696 ),
697 );
698 };
699
700 reconcileConfig = Effect.fn("CloudManagedEndpointRuntime.reconcileConfig")(function* (config) {
701 if (!config || config.providerKind !== "cloudflare_tunnel") {
702 yield* stopActive;
703 return config
704 ? { status: "unsupported", providerKind: config.providerKind }
705 : { status: "disabled" };
706 }
707
708 const nextConfigKey = runtimeConfigKey(config);
709 const active = yield* Ref.get(activeRef);
710 if (active?.configKey === nextConfigKey) {
711 const isRunning = yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false));
712 if (isRunning) {
713 return {
714 status: "running",
715 providerKind: "cloudflare_tunnel",
716 pid: Number(active.child.pid),
717 ...(active.config.tunnelId ? { tunnelId: active.config.tunnelId } : {}),
718 ...(active.config.tunnelName ? { tunnelName: active.config.tunnelName } : {}),
719 } satisfies CloudManagedEndpointRuntimeStatus;
720 }
721 }
722
723 yield* stopActive;
724
725 const executable = yield* relayClient.resolve;
726 if (needsPinnedRelayClient(executable)) {
727 yield* ensurePinnedRelayClient;
728 yield* startPinnedRelayClientRetries;
729 }
730 if (executable.status !== "available") {
731 return {
732 status: "failed",
733 providerKind: "cloudflare_tunnel",
734 failure: executable.status === "unsupported" ? "unsupported-platform" : "not-installed",
735 reason:
736 executable.status === "unsupported"
737 ? `Relay client is unsupported on ${executable.platform}-${executable.arch}.`
738 : "The relay client is not installed.",
739 ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}),
740 ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}),
741 } satisfies CloudManagedEndpointRuntimeStatus;
742 }
743
744 const http2 = yield* Ref.get(http2Ref);
745 const connectorScope = yield* Scope.make("sequential");
746 const child = yield* spawner
747 .spawn(
748 ChildProcess.make(
749 executable.executablePath,
750 [
751 "tunnel",
752 "--no-autoupdate",
753 "--loglevel",
754 "info",
755 "--output",
756 "default",
757 "--metrics",
758 "127.0.0.1:0",
759 ...(http2 ? ["--protocol", "http2"] : []),
760 "run",
761 ],
762 {
763 detached: false,
764 env: {
765 ...env,
766 TUNNEL_TOKEN: config.connectorToken,
767 },
768 shell: false,
769 stderr: "pipe",
770 stdout: "pipe",
771 },
772 ),
773 )
774 .pipe(
775 Effect.provideService(Scope.Scope, connectorScope),
776 Effect.tap((child) =>
777 Effect.logInfo("Relay client process started; waiting for tunnel connection", {
778 pid: Number(child.pid),
779 source: executable.source,
780 version: executable.version,
781 protocol: http2 ? "http2" : "auto",
782 tunnelId: config.tunnelId,
783 tunnelName: config.tunnelName,
784 }),
785 ),
786 Effect.catch((cause) =>
787 Effect.logWarning("Failed to start relay client", {
788 cause,
789 tunnelId: config.tunnelId,
790 tunnelName: config.tunnelName,
791 }).pipe(
792 Effect.andThen(Scope.close(connectorScope, Exit.void).pipe(Effect.ignore)),
793 Effect.as({
794 status: "failed",
795 providerKind: "cloudflare_tunnel",
796 failure: "spawn-failed",
797 reason: String(cause),
798 ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}),
799 ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}),
800 } satisfies CloudManagedEndpointRuntimeStatus),
801 ),
802 ),
803 );
804
805 if ("status" in child && child.status === "failed") {
806 return child;
807 }
808
809 if (!("status" in child)) {
810 const connector = {
811 child,
812 executable,
813 scope: connectorScope,
814 configKey: nextConfigKey,
815 config,
816 startedAtMillis: yield* Clock.currentTimeMillis,
817 registered: yield* Deferred.make<void>(),
818 metricsAddress: yield* Deferred.make<string>(),
819 protocol: yield* Ref.make<string | null>(null),
820 } satisfies ActiveConnector;
821 yield* Ref.set(activeRef, connector);
822 yield* Effect.forkIn(observeConnectorOutput(connector), connectorScope);
823 yield* Effect.forkIn(superviseConnector(connector), connectorScope);
824 yield* Effect.forkIn(watchConnectorRegistration(connector), connectorScope);
825 yield* Effect.forkIn(watchConnectorReadiness(connector), connectorScope);
826 return {
827 status: "running",
828 providerKind: "cloudflare_tunnel",
829 pid: Number(child.pid),
830 ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}),
831 ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}),
832 } satisfies CloudManagedEndpointRuntimeStatus;
833 }
834
835 return {
836 status: "failed",
837 providerKind: "cloudflare_tunnel",
838 failure: "spawn-failed",
839 reason: "Relay client did not start.",
840 ...(config.tunnelId ? { tunnelId: config.tunnelId } : {}),
841 ...(config.tunnelName ? { tunnelName: config.tunnelName } : {}),
842 } satisfies CloudManagedEndpointRuntimeStatus;
843 });
844
845 const applyConfig = Effect.fn("CloudManagedEndpointRuntime.applyConfig")(
846 (requested: RelayManagedEndpointRuntimeConfig | null) =>
847 reconcileSemaphore.withPermits(1)(
848 Effect.gen(function* () {
849 const config = connectAllowed ? requested : null;
850 // Keep backoff when recovery returns the same tunnel/token, or every recovery respawns a crash loop.
851 const desired = yield* Ref.get(desiredConfigRef);
852 const unchanged =
853 desired !== null &&
854 config !== null &&
855 runtimeConfigKey(desired) === runtimeConfigKey(config);
856 if (!unchanged) {
857 yield* Ref.set(restartDelayRef, 0);
858 }
859 yield* Ref.set(desiredConfigRef, config);
860 return yield* reconcileConfig(config);
861 }),
862 ),
863 withRelayClientLog,
864 );
865
866 const restartConnector = Effect.fn("CloudManagedEndpointRuntime.restartConnector")(function* (
867 pid: number,
868 stillDead: Effect.Effect<boolean>,
869 ) {
870 const active = yield* Ref.get(activeRef);
871 if (!active || Number(active.child.pid) !== pid || !(yield* stillDead)) return false;
872 yield* Effect.logWarning(
873 "Cloudflare has no live connection from the relay client; restarting it",
874 { pid, tunnelId: active.config.tunnelId, tunnelName: active.config.tunnelName },
875 );
876 // The supervisor starts the replacement with its usual crash-loop backoff.
877 return yield* active.child.kill().pipe(
878 Effect.as(true),
879 Effect.orElseSucceed(() => false),
880 );
881 }, reconcileSemaphore.withPermits(1));
882
883 const readMetrics = <S extends Schema.Decoder<unknown>>(url: string, schema: S) =>
884 httpClient.get(url).pipe(
885 Effect.flatMap(HttpClientResponse.schemaBodyJson(schema)),
886 Effect.scoped,
887 Effect.withTracerEnabled(false),
888 Effect.timeoutOption(CONNECTOR_READINESS_REQUEST_TIMEOUT),
889 Effect.map(Option.getOrNull),
890 Effect.orElseSucceed(() => null),
891 );
892
893 const inspectConnector = Effect.gen(function* () {
894 const active = yield* Ref.get(activeRef);
895 if (!active || !(yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)))) {
896 return null;
897 }
898 const pid = Number(active.child.pid);
899 const protocol = yield* Ref.get(active.protocol);
900 if (!(yield* Deferred.isDone(active.metricsAddress))) {
901 return { pid, readyConnections: null, hostname: null, protocol } satisfies ConnectorFacts;
902 }
903 const metricsUrl = `http://${yield* Deferred.await(active.metricsAddress)}`;
904 const [ready, config] = yield* Effect.all(
905 [
906 readMetrics(`${metricsUrl}/ready`, ConnectorReadyBody),
907 readMetrics(`${metricsUrl}/config`, ConnectorConfigBody),
908 ],
909 { concurrency: 2 },
910 );
911 return {
912 pid,
913 readyConnections: ready?.readyConnections ?? null,
914 hostname: config?.config.ingress?.find((rule) => rule.hostname)?.hostname ?? null,
915 protocol,
916 } satisfies ConnectorFacts;
917 });
918
919 const runtime = CloudManagedEndpointRuntime.of({
920 applyConfig,
921 inspectConnector,
922 readinessChanged: Stream.fromQueue(readinessChanges),
923 recoveryRequests: Stream.fromQueue(recoveryRequests),
924 requestRecovery: Effect.fn("CloudManagedEndpointRuntime.requestRecovery")(function (config) {
925 return Queue.offer(recoveryRequests, config).pipe(Effect.asVoid);
926 }),
927 restartConnector,
928 tunnelConnected: Stream.fromQueue(tunnelConnections),
929 withLinkStateLock: linkStateSemaphore.withPermits(1),
930 });
931
932 yield* Effect.addFinalizer(() => runtime.applyConfig(null));
933 return runtime;
934});
935
936export const layer = Layer.effect(CloudManagedEndpointRuntime, make);