feat: add headless t3 connect setup for SSH hosts (#3749)

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: codex <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-07-19 18:59:23 +02:00
1 parent 53e3c98a55
commit 7a820abfdd
35 files changed
+3018 -145

No files matched your search

+42
View File
@@ -1,19 +1,45 @@
import * as RelayClient from "@t3tools/shared/relayClient";
import { assert, it } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as References from "effect/References";
import * as Terminal from "effect/Terminal";
import {
acquireRelayClientForLink,
formatHeadlessAuthorizationPrompt,
formatRelayClientReady,
headlessSessionConfig,
isPublishAgentActivityEnabledValue,
recoverBootServiceOffer,
reportCloudDisconnectResults,
} from "./connect.ts";
it("explains how to complete headless authorization", () => {
assert.equal(
formatHeadlessAuthorizationPrompt("https://example.test/connect"),
[
"Headless authorization",
"Open this URL on a device with a browser:",
" https://example.test/connect",
"",
"After signing in, return here and enter the code shown in your browser.",
].join("\n"),
);
});
it("formats relay readiness without printing its installation path", () => {
assert.equal(formatRelayClientReady("2026.5.2"), "✓ Relay client ready · cloudflared 2026.5.2");
});
const readHeadlessSessionConfig = (env: Record<string, string>) =>
headlessSessionConfig.pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))));
const managedExecutable = {
status: "available",
executablePath: "/tmp/cloudflared",
@@ -21,6 +47,22 @@ const managedExecutable = {
version: RelayClient.CLOUDFLARED_VERSION,
} as const;
it.effect("detects headless operation from individual SSH config values", () =>
Effect.gen(function* () {
assert.isFalse(yield* readHeadlessSessionConfig({}));
assert.isFalse(yield* readHeadlessSessionConfig({ CI: "true" }));
assert.isTrue(yield* readHeadlessSessionConfig({ SSH_CONNECTION: "client server" }));
assert.isTrue(yield* readHeadlessSessionConfig({ SSH_TTY: "/dev/pts/1" }));
}),
);
it.effect("treats cancelling optional background setup as a successful skip", () =>
Effect.gen(function* () {
const result = yield* recoverBootServiceOffer(Effect.fail(new Terminal.QuitError({})));
assert.isFalse(result);
}),
);
it.effect("does not install the relay client when the user declines the managed download", () =>
Effect.gen(function* () {
let installCalls = 0;
+245 -56
View File
@@ -6,9 +6,12 @@ import {
} from "@t3tools/contracts";
import { RelayOkResponse } from "@t3tools/contracts/relay";
import * as RelayClient from "@t3tools/shared/relayClient";
import * as Terminal from "effect/Terminal";
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
import * as Cause from "effect/Cause";
import * as Config from "effect/Config";
import * as Console from "effect/Console";
import * as Crypto from "effect/Crypto";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
@@ -16,6 +19,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import { Command, Flag, GlobalFlag, Prompt } from "effect/unstable/cli";
import {
FetchHttpClient,
@@ -25,8 +29,10 @@ import {
} from "effect/unstable/http";
import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient";
import packageJson from "../../package.json" with { type: "json" };
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import * as BootService from "../cloud/bootService.ts";
import * as CliState from "../cloud/CliState.ts";
import * as CliTokenManager from "../cloud/CliTokenManager.ts";
import {
@@ -38,6 +44,8 @@ import { relayUrlConfig } from "../cloud/publicConfig.ts";
import { headlessRelayClientTracingLayer } from "../cloud/relayTracing.ts";
import * as ServerConfig from "../config.ts";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import * as ExternalLauncher from "../process/externalLauncher.ts";
import * as ProcessRunner from "../processRunner.ts";
import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
@@ -46,6 +54,81 @@ const jsonFlag = Flag.boolean("json").pipe(
Flag.withDefault(false),
);
const isCloudCliTokenManagerError = Schema.is(CliTokenManager.CloudCliTokenManagerError);
const headlessFlag = Flag.boolean("headless").pipe(
Flag.withDescription("Authorize without a local browser using out-of-band OAuth."),
Flag.withDefault(false),
);
/**
* Inside an SSH session there is no local browser to complete the loopback
* OAuth callback, so out-of-band OAuth is the only flow that can work.
*/
export const headlessSessionConfig = Config.all({
sshConnection: Config.string("SSH_CONNECTION").pipe(Config.option),
sshTty: Config.string("SSH_TTY").pipe(Config.option),
}).pipe(
Config.map(({ sshConnection, sshTty }) => Option.isSome(sshConnection) || Option.isSome(sshTty)),
);
const promptForOutOfBandOAuthCode = Effect.fn("cloud.cli.prompt_for_out_of_band_oauth_code")(
function* ({ authorizeUrl, validate }: CliTokenManager.OutOfBandOAuthPromptInput) {
yield* Console.log(formatHeadlessAuthorizationPrompt(authorizeUrl));
return yield* Prompt.run(Prompt.text({ message: "Authorization code", validate }));
},
);
export function formatHeadlessAuthorizationPrompt(authorizeUrl: string): string {
return [
"Headless authorization",
"Open this URL on a device with a browser:",
` ${authorizeUrl}`,
"",
"After signing in, return here and enter the code shown in your browser.",
].join("\n");
}
/** Returns the connected account identity, if the flow could determine one. */
const authorizeCli = Effect.fn("cloud.cli.authorize")(function* (options: {
readonly headless: boolean;
}) {
const tokens = yield* CliTokenManager.CloudCliTokenManager;
const useOutOfBandOAuth = options.headless || (yield* headlessSessionConfig);
if (!useOutOfBandOAuth) {
const authorization = yield* tokens.get;
if (authorization._tag === "Authorized") {
return authorization.token.identity ?? null;
}
yield* Console.log("\nHeadless mode enabled. A new authorization link is ready below.");
}
// A stored credential whose refresh fails (revoked, expired grant) must
// fall through to a fresh out-of-band authorization, not dead-end the command.
const existing = yield* tokens.getExisting.pipe(
Effect.catchTag("CloudCliCredentialRefreshError", () =>
Console.log(
"The stored T3 Connect credential could not be refreshed; signing in again.",
).pipe(Effect.as(Option.none())),
),
);
if (Option.isSome(existing)) {
return existing.value.identity ?? null;
}
const { token, identity } = yield* CliTokenManager.outOfBandOAuthLogin(
promptForOutOfBandOAuthCode,
).pipe(
Effect.mapError((cause) =>
// Ctrl-C / EOF at the prompt is a QuitError; let it propagate so the CLI
// cancels quietly instead of dumping an authorization error.
Terminal.isQuitError(cause) || isCloudCliTokenManagerError(cause)
? cause
: new CliTokenManager.CloudCliAuthorizationError({ cause }),
),
);
yield* tokens.store(token);
return identity;
});
function bytesToString(value: Uint8Array): string {
return new TextDecoder().decode(value);
}
@@ -313,6 +396,21 @@ const disconnectCloud = Effect.fn("cloud.cli.disconnect")(function* (options: {
if (options.clearAuthorization) {
const tokens = yield* CliTokenManager.CloudCliTokenManager;
yield* tokens.clear;
// uninstall itself no-ops when nothing is installed (and on non-Linux),
// so no status pre-check that could mask a real removal failure.
const bootService = yield* BootService.BootService;
yield* bootService.uninstall.pipe(
Effect.tap((removed) =>
removed ? Console.log("Removed the T3 Code background service.") : Effect.void,
),
Effect.catchTag("BootServiceUnsupportedError", () => Effect.succeed(false)),
Effect.catch((error) =>
Console.warn(`Could not remove the background service: ${error.message}`).pipe(
Effect.as(false),
),
),
);
}
yield* reportCloudDisconnectResults({
@@ -326,7 +424,7 @@ const disconnectCloud = Effect.fn("cloud.cli.disconnect")(function* (options: {
}
});
const runCloudCommand = <A, E>(
const runCloudCommand = Effect.fn("cloud.cli.run_cloud_command")(function* <A, E>(
flags: { readonly baseDir: Option.Option<string> },
run: Effect.Effect<
A,
@@ -335,6 +433,8 @@ const runCloudCommand = <A, E>(
| CliTokenManager.CloudCliTokenManager
| RelayClient.RelayClient
| EnvironmentAuth.EnvironmentAuth
| BootService.BootService
| Crypto.Crypto
| FileSystem.FileSystem
| HttpClient.HttpClient
| Prompt.Environment
@@ -344,37 +444,79 @@ const runCloudCommand = <A, E>(
options?: {
readonly quietLogs?: boolean;
},
) =>
Effect.gen(function* () {
const logLevel = yield* GlobalFlag.LogLevel;
const config = yield* resolveCliAuthConfig(flags, logLevel);
const minimumLogLevel = options?.quietLogs ? "Error" : config.logLevel;
const runtimeLayer = Layer.mergeAll(
ServerSecretStore.layer,
CliTokenManager.layer.pipe(Layer.provide(ServerSecretStore.layer)),
RelayClient.layerCloudflared({ baseDir: config.baseDir }),
EnvironmentAuth.runtimeLayer,
ServerEnvironment.layer,
headlessRelayClientTracingLayer,
).pipe(
Layer.provideMerge(FetchHttpClient.layer),
Layer.provideMerge(ServerConfig.layer(config)),
Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
) {
const logLevel = yield* GlobalFlag.LogLevel;
const config = yield* resolveCliAuthConfig(flags, logLevel);
const minimumLogLevel = options?.quietLogs ? "Error" : config.logLevel;
const runtimeLayer = Layer.mergeAll(
ServerSecretStore.layer,
CliTokenManager.layer.pipe(
Layer.provide(ServerSecretStore.layer),
Layer.provide(ExternalLauncher.layer),
),
RelayClient.layerCloudflared({ baseDir: config.baseDir }),
EnvironmentAuth.runtimeLayer,
ServerEnvironment.layer,
BootService.layer({
baseDir: config.baseDir,
logsDir: config.logsDir,
cliVersion: packageJson.version,
}).pipe(Layer.provide(ProcessRunner.layer)),
headlessRelayClientTracingLayer,
).pipe(
Layer.provideMerge(FetchHttpClient.layer),
Layer.provideMerge(ServerConfig.layer(config)),
Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
);
return yield* run.pipe(Effect.provide(runtimeLayer));
});
const connectedAs = (identity: string | null): string => (identity ? ` as ${identity}` : "");
export function formatRelayClientReady(version: string): string {
return `✓ Relay client ready · cloudflared ${version}`;
}
const linkEnvironmentForConnect = Effect.fn("cloud.cli.link_environment")(function* (options: {
readonly headless: boolean;
readonly publishOnly?: boolean;
}) {
const publishOnly = options.publishOnly ?? false;
if (!publishOnly) {
const relayClient = yield* RelayClient.RelayClient;
const installed = yield* acquireRelayClientForLink(
relayClient,
confirmRelayClientInstall,
reportRelayClientInstallProgress,
);
return yield* run.pipe(Effect.provide(runtimeLayer));
});
if (Option.isNone(installed)) {
yield* Console.log("T3 Connect setup cancelled. The relay client was not installed.");
return null;
}
yield* Console.log(formatRelayClientReady(installed.value.version));
}
const identity = yield* authorizeCli(options);
yield* CliState.setCliDesiredCloudLink(true, publishOnly ? "publish_only" : "managed");
if (publishOnly) {
const secrets = yield* ServerSecretStore.ServerSecretStore;
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, stringToBytes("true"));
}
return { identity } as const;
});
const connectLoginCommand = Command.make("login", {
...projectLocationFlags,
headless: headlessFlag,
}).pipe(
Command.withDescription("Authorize the T3 Connect CLI without enabling remote access."),
Command.withHandler((flags) =>
runCloudCommand(
flags,
Effect.gen(function* () {
const tokens = yield* CliTokenManager.CloudCliTokenManager;
yield* tokens.get;
yield* Console.log("Signed in to T3 Connect.");
yield* Console.log("T3 Connect\n");
const identity = yield* authorizeCli(flags);
yield* Console.log(`✓ Signed in${connectedAs(identity)}`);
}),
),
),
@@ -382,6 +524,7 @@ const connectLoginCommand = Command.make("login", {
const connectLinkCommand = Command.make("link", {
...projectLocationFlags,
headless: headlessFlag,
publishOnly: Flag.boolean("publish-only").pipe(
Flag.withDescription(
"Link to publish agent activity only — no managed tunnel. Reach this environment out of band (e.g. Tailscale).",
@@ -394,41 +537,15 @@ const connectLinkCommand = Command.make("link", {
runCloudCommand(
flags,
Effect.gen(function* () {
// A publish-only link needs no Cloudflare tunnel, so skip installing the
// relay client entirely.
if (!flags.publishOnly) {
const relayClient = yield* RelayClient.RelayClient;
const installed = yield* acquireRelayClientForLink(
relayClient,
confirmRelayClientInstall,
reportRelayClientInstallProgress,
);
if (Option.isNone(installed)) {
yield* Console.log("T3 Connect setup cancelled. The relay client was not installed.");
return;
}
yield* Console.log("T3 Connect\n");
const linked = yield* linkEnvironmentForConnect(flags);
if (linked) {
yield* Console.log(
`Using relay client ${installed.value.version} from ${installed.value.executablePath}.`,
flags.publishOnly
? `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start T3 to publish agent activity (no managed tunnel).`
: `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start the server with \`t3 serve\` to make this machine reachable.`,
);
}
const tokens = yield* CliTokenManager.CloudCliTokenManager;
yield* tokens.get;
yield* CliState.setCliDesiredCloudLink(
true,
flags.publishOnly ? "publish_only" : "managed",
);
if (flags.publishOnly) {
// A publish-only link exists solely to publish; without the publish
// flag the link would be inert and the success message a lie.
const secrets = yield* ServerSecretStore.ServerSecretStore;
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, stringToBytes("true"));
}
yield* Console.log(
flags.publishOnly
? "This environment will publish agent activity to your mobile clients the next time T3 starts (no managed tunnel)."
: "This T3 environment will be available through T3 Connect the next time T3 starts.",
);
}),
),
),
@@ -566,8 +683,80 @@ const connectLogoutCommand = Command.make("logout", {
),
);
export const connectCommand = Command.make("connect").pipe(
Command.withDescription("Manage headless T3 Connect access."),
const offerBootService = Effect.gen(function* () {
const bootService = yield* BootService.BootService;
const { supported, installed, current } = yield* bootService.status;
if (!supported) {
// Don't prompt for something that can only fail; background setup is
// Linux/systemd-only for now.
return false;
}
if (installed && current) {
yield* Console.log("T3 Code is already set up to run in the background on this machine.");
return true;
}
const wanted = yield* Prompt.run(
Prompt.confirm({
message: installed
? "The installed T3 Code background service is from an older setup. Update it now?"
: "Run T3 Code in the background whenever this machine boots? " +
"It stays reachable through T3 Connect even after you log out.",
initial: true,
}),
);
if (!wanted) {
return false;
}
const plan = yield* bootService.install;
yield* Console.log(`Background service installed. Logs: ${plan.logPath}`);
return true;
});
export const recoverBootServiceOffer = <R>(
offer: Effect.Effect<boolean, BootService.BootServiceError | Terminal.QuitError, R>,
) =>
offer.pipe(
Effect.catchTags({
QuitError: () => Effect.succeed(false),
BootServiceUnsupportedError: (error) =>
Console.log(`Skipping background setup: ${error.message}`).pipe(Effect.as(false)),
BootServiceCommandError: (error) =>
Console.warn(`Background setup did not finish: ${error.message}`).pipe(Effect.as(false)),
BootServiceInstallError: (error) =>
Console.warn(`Background setup did not finish: ${error.message}`).pipe(Effect.as(false)),
}),
);
export const connectCommand = Command.make("connect", {
...projectLocationFlags,
headless: headlessFlag,
}).pipe(
Command.withDescription("Set up T3 Connect for this machine."),
Command.withHandler((flags) =>
runCloudCommand(
flags,
Effect.gen(function* () {
yield* Console.log("T3 Connect\n");
const linked = yield* linkEnvironmentForConnect(flags);
if (!linked) {
return;
}
// Show which account was linked so an unexpected identity (an
// authorization code for a different account) is visible before the
// machine is brought online.
yield* Console.log(`✓ Connected${connectedAs(linked.identity)}`);
// Connect itself already succeeded; a boot-service failure must not
// fail the command, just tell the user what happened and move on.
const background = yield* recoverBootServiceOffer(offerBootService);
yield* Console.log(
background
? "\n✓ Background service ready\n\nT3 Code will stay reachable after you log out."
: "\nNext\n Start the server with `t3 serve` to make this machine reachable.",
);
}),
),
),
Command.withSubcommands([
connectLoginCommand,
connectLinkCommand,