import { createHash } from 'crypto'; import { existsSync, readFileSync, readdirSync, writeFileSync } from 'fs'; import { basename, dirname, resolve } from 'path'; import { homedir, hostname as osHostname } from 'os'; import { confirm, input } from '@inquirer/prompts'; import type { CliConfig } from './config.js'; import { envGet, envSet } from './config.js'; import * as d from './utils/display.js'; import { commandExists, readCommand, runInteractive, tryReadCommand } from './utils/shell.js'; const UUID_RE = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; export interface CloudflareSetupOptions { config?: string; force?: boolean; hostname?: string; run?: boolean; skipDns?: boolean; tunnelName?: string; } export interface TailscaleSetupOptions { serve?: boolean; } export interface CloudflareSummary { installed: boolean; loggedIn: boolean; hostname: string; service: string; tunnelId: string; tunnelName: string; configExists: boolean; configPath: string; configSource: 'local' | 'home' | 'none'; credentialsDir: string; disableChunkedEncoding: boolean; running: boolean; } export interface TailscaleSummary { installed: boolean; connected: boolean; ip: string; dnsName: string; serveHttpsUrl: string; } export function getCloudflareSummary(config: CliConfig): CloudflareSummary { const home = homedir(); const detected = findCloudflareConfigPath(config); const configExists = Boolean(detected.path); const cfConfig = configExists ? readFileSync(detected.path, 'utf-8') : ''; const hostname = matchField(cfConfig, /^\s*-\s+hostname:\s+(.+)$/m); const service = matchField(cfConfig, /^\s+service:\s+(.+)$/m); const tunnelId = matchField(cfConfig, /^tunnel:\s+(.+)$/m); const tunnelName = envGet('CLOUDFLARED_TUNNEL_NAME', config.envFile) || buildDefaultCloudflareTunnelName(config, hostname); const credentialsDir = envGet('CLOUDFLARED_CREDENTIALS_FILE', config.envFile) || resolve(home, '.cloudflared'); return { installed: commandExists('cloudflared'), loggedIn: existsSync(resolve(home, '.cloudflared', 'cert.pem')), hostname, service, tunnelId, tunnelName, configExists, configPath: detected.path, configSource: detected.source, credentialsDir, disableChunkedEncoding: /^\s*disableChunkedEncoding:\s*true\s*$/m.test(cfConfig), running: cloudflaredProcessRunning(config), }; } export function getTailscaleSummary(): TailscaleSummary { const installed = commandExists('tailscale'); if (!installed) { return { installed, connected: false, ip: '', dnsName: '', serveHttpsUrl: '', }; } const ip = tryReadCommand('tailscale', ['ip', '-4']) || ''; const statusJson = tryReadCommand('tailscale', ['status', '--json']) || ''; let dnsName = ''; if (statusJson) { try { const parsed = JSON.parse(statusJson) as { Self?: { DNSName?: string } }; dnsName = parsed.Self?.DNSName?.replace(/\.$/, '') || ''; } catch { dnsName = ''; } } return { installed, connected: Boolean(ip), ip, dnsName, serveHttpsUrl: dnsName ? `https://${dnsName}` : '', }; } export async function setupCloudflareTunnel( config: CliConfig, opts: CloudflareSetupOptions = {}, ): Promise { d.header('Cloudflare Tunnel'); if (opts.run) { runCloudflareTunnel(config, { configPath: opts.config }); return; } if (!commandExists('cloudflared')) { d.error('cloudflared is not installed'); if (process.platform === 'win32') { d.command('winget install Cloudflare.cloudflared'); d.command('choco install cloudflared'); } else if (process.platform === 'darwin') { d.command('brew install cloudflare/cloudflare/cloudflared'); } else { d.command('curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb'); d.command('sudo dpkg -i cloudflared.deb'); } d.line(); return; } const version = tryReadCommand('cloudflared', ['--version']) || 'cloudflared'; d.success(version.split('\n')[0] || 'cloudflared ready'); const loginCert = resolve(homedir(), '.cloudflared', 'cert.pem'); if (!existsSync(loginCert)) { d.warn('Cloudflare login is required before tunnel creation'); const shouldLogin = await confirm({ message: 'Run `cloudflared tunnel login` now?', default: true, }); if (!shouldLogin) { d.info('Re-run after authenticating with Cloudflare.'); d.line(); return; } runInteractive('cloudflared', ['tunnel', 'login']); d.success('Cloudflare authentication complete'); } else { d.success('Cloudflare login detected'); } const tunnelName = resolveCloudflareTunnelName(config, opts.tunnelName, opts.hostname); envSet('CLOUDFLARED_TUNNEL_NAME', tunnelName, config.envFile); let tunnelId = findCloudflareTunnelId(tunnelName); if (!tunnelId) { const shouldCreate = await confirm({ message: `Create tunnel \`${tunnelName}\`?`, default: true, }); if (!shouldCreate) { d.info('Skipping tunnel creation.'); d.line(); return; } const output = readCommand('cloudflared', ['tunnel', 'create', tunnelName]); const match = output.match(UUID_RE); if (!match) { throw new Error('Cloudflare tunnel creation did not return a tunnel ID'); } tunnelId = match[0]; d.success(`Tunnel created (${tunnelId.slice(0, 8)}...)`); } else { d.success(`Using existing tunnel ${tunnelName} (${tunnelId.slice(0, 8)}...)`); } let hostname = opts.hostname || ''; if (!hostname) { hostname = await input({ message: 'Hostname to route through Cloudflare', default: 'substrate.example.com', }); } if (hostname && !opts.skipDns) { try { readCommand('cloudflared', ['tunnel', 'route', 'dns', tunnelName, hostname]); d.success(`DNS route ready for ${hostname}`); } catch (error: any) { const detail = commandErrorText(error); const zoneMismatch = extractZoneMismatch(detail, hostname); if (zoneMismatch) { d.warn(`Cloudflare login appears to be targeting the wrong zone (${zoneMismatch})`); d.info(`Requested hostname: ${hostname}`); d.info('Re-run `cloudflared tunnel login` and choose the correct zone for this hostname.'); d.info('Then rerun the tunnel setup after authenticating against the right Cloudflare account/zone.'); } else if (detail.includes('record with that host already exists')) { d.warn(`DNS record already exists for ${hostname}`); d.info('Cloudflare will not create a tunnel CNAME when an A, AAAA, or CNAME record already owns that hostname.'); d.info('Delete or rename the existing DNS record in Cloudflare, then rerun the tunnel setup.'); d.info('If the existing DNS record already points at the correct tunnel, rerun with `--skip-dns` to keep it unchanged.'); } else { d.warn(`Could not confirm DNS routing for ${hostname}`); if (detail) d.info(detail.split('\n')[0] || detail); } } } writeCloudflareConfig(config, { force: opts.force, hostname, tunnelId, }); ensureCloudflareStreamingConfig(config); const credentialsFile = resolve(homedir(), '.cloudflared', `${tunnelId}.json`); if (existsSync(credentialsFile)) { envSet('CLOUDFLARED_CREDENTIALS_FILE', dirname(credentialsFile), config.envFile); d.success('Saved Cloudflare credentials path to .env'); } else { d.warn('Tunnel credentials file was not found automatically'); } d.line(); d.label('Tunnel URL', hostname ? `https://${hostname}` : 'configured'); d.label('Tunnel name', tunnelName); d.label('Tunnel ID', tunnelId); d.label('Config', config.cloudflareConfigFile); d.line(); d.info('Next steps'); d.command('aether-substrate up --cloudflare'); d.command('aether-substrate logs --cloudflare --service cloudflared'); d.command('aether-substrate tunnel cloudflare --run'); d.command(`NEXT_PUBLIC_SUBSTRATE_URL=https://${hostname || 'your-hostname'}`); d.line(); } export function runCloudflareTunnel( config: CliConfig, opts: { configPath?: string } = {}, ): void { const summary = getCloudflareSummary(config); const configPath = opts.configPath || summary.configPath; if (!commandExists('cloudflared')) { throw new Error('cloudflared is not installed'); } if (!configPath) { throw new Error('No Cloudflare config found. Pass --config or create one with `aether-substrate tunnel cloudflare`.'); } ensureCloudflareStreamingConfig(config, { configPath }); const resolvedConfig = resolve(configPath); const runnableConfig = prepareCloudflareRunConfig(config, resolvedConfig); const file = readFileSync(runnableConfig, 'utf-8'); const tunnelId = matchField(file, /^tunnel:\s+(.+)$/m); const target = tunnelId || summary.tunnelName || 'aether-substrate'; d.header('Cloudflare Tunnel'); d.label('Config', runnableConfig); if (summary.hostname) d.label('Hostname', summary.hostname); d.info('Starting cloudflared in the foreground'); d.line(); runInteractive('cloudflared', ['tunnel', '--config', runnableConfig, 'run', target]); } export function importCloudflareConfig( config: CliConfig, opts: { fromPath?: string; force?: boolean } = {}, ): void { const summary = getCloudflareSummary(config); const sourcePath = opts.fromPath || (summary.configSource === 'home' ? summary.configPath : ''); if (!sourcePath) { throw new Error('No home Cloudflare config found to import.'); } if (existsSync(config.cloudflareConfigFile) && !opts.force) { throw new Error(`Repo config already exists at ${config.cloudflareConfigFile}. Re-run with force enabled.`); } const body = readFileSync(sourcePath, 'utf-8'); writeFileSync(config.cloudflareConfigFile, body); ensureCloudflareStreamingConfig(config, { configPath: config.cloudflareConfigFile }); const normalizedBody = readFileSync(config.cloudflareConfigFile, 'utf-8'); const credentialsFile = matchField(normalizedBody, /^credentials-file:\s+(.+)$/m); if (credentialsFile) { envSet('CLOUDFLARED_CREDENTIALS_FILE', dirname(credentialsFile), config.envFile); } d.success(`Imported ${sourcePath} -> ${config.cloudflareConfigFile}`); } export async function setupTailscaleTunnel(opts: TailscaleSetupOptions = {}): Promise { d.header('Tailscale'); if (!commandExists('tailscale')) { d.error('Tailscale is not installed'); if (process.platform === 'darwin') { d.command('brew install tailscale'); } else { d.command('curl -fsSL https://tailscale.com/install.sh | sh'); } d.line(); return; } const version = tryReadCommand('tailscale', ['version']) || 'tailscale'; d.success(version.split('\n')[0] || 'tailscale ready'); const summary = getTailscaleSummary(); if (!summary.connected) { d.warn('Tailscale is installed but not connected'); d.command('sudo tailscale up'); d.line(); return; } d.label('Tailscale IP', summary.ip); if (summary.dnsName) { d.label('MagicDNS', summary.dnsName); } let enableServe = opts.serve; if (enableServe === undefined) { enableServe = await confirm({ message: 'Expose the local Substrate API with `tailscale serve`?', default: true, }); } if (enableServe) { runInteractive('tailscale', ['serve', 'https', '/', 'http://127.0.0.1:3100']); d.success('Tailscale Serve now proxies https://.ts.net -> http://127.0.0.1:3100'); } d.line(); d.info('Recommended frontend setting'); if (summary.serveHttpsUrl) { d.command(`NEXT_PUBLIC_SUBSTRATE_URL=${summary.serveHttpsUrl}`); } else { d.command(`NEXT_PUBLIC_SUBSTRATE_URL=http://${summary.ip}:3100`); } d.info('If your frontend runs on a different origin, add it to SUBSTRATE_ALLOWED_ORIGINS in substrate/.env.'); d.line(); } function findCloudflareTunnelId(tunnelName: string): string { const output = tryReadCommand('cloudflared', ['tunnel', 'list']) || ''; const line = output .split('\n') .map(row => row.trim()) .find(row => row.includes(tunnelName)); if (!line) return ''; const match = line.match(UUID_RE); return match?.[0] || ''; } function writeCloudflareConfig( config: CliConfig, opts: { force?: boolean; hostname: string; tunnelId: string }, ): void { if (existsSync(config.cloudflareConfigFile) && !opts.force) { d.info(`Keeping existing ${config.cloudflareConfigFile}`); return; } const body = [ '# Cloudflare Tunnel config for Aether Substrate', '# Generated by aether-substrate', '', `tunnel: ${opts.tunnelId}`, `credentials-file: /etc/cloudflared/credentials/${opts.tunnelId}.json`, '', 'ingress:', ` - hostname: ${opts.hostname}`, ' service: http://substrate:3100', ' originRequest:', ' connectTimeout: 30s', ' keepAliveTimeout: 90s', ' disableChunkedEncoding: true', '', ' - service: http_status:404', '', ].join('\n'); writeFileSync(config.cloudflareConfigFile, body); d.success(`Wrote ${config.cloudflareConfigFile}`); } export function ensureCloudflareStreamingConfig( config: CliConfig, opts: { configPath?: string; quiet?: boolean } = {}, ): { path: string; changed: boolean; present: boolean } { const detected = opts.configPath ? { path: resolve(opts.configPath), source: 'local' as const } : findCloudflareConfigPath(config); if (!detected.path || !existsSync(detected.path)) { return { path: '', changed: false, present: false }; } const original = readFileSync(detected.path, 'utf-8'); const normalized = normalizeCloudflareStreamingConfig(original); const changed = normalized !== original; if (changed) { writeFileSync(detected.path, normalized); if (!opts.quiet) { d.success(`Updated ${detected.path} with Cloudflare SSE streaming settings`); } } else if (!opts.quiet && /^\s*disableChunkedEncoding:\s*true\s*$/m.test(original)) { d.info(`Cloudflare SSE setting already present in ${detected.path}`); } return { path: detected.path, changed, present: /^\s*disableChunkedEncoding:\s*true\s*$/m.test(normalized), }; } // Ensures `disableChunkedEncoding: true` is set on the substrate ingress rule. Cloudflare's // default chunked encoding buffers SSE streams, which breaks LLM streaming and live events. function normalizeCloudflareStreamingConfig(body: string): string { const hasTrailingNewline = body.endsWith('\n'); const lines = body.split('\n'); for (let i = 0; i < lines.length; i++) { if (/^\s*disableChunkedEncoding:\s*/.test(lines[i])) { lines[i] = lines[i].replace(/^(\s*)disableChunkedEncoding:\s*.*$/, '$1disableChunkedEncoding: true'); return `${lines.join('\n')}${hasTrailingNewline ? '\n' : ''}`; } } for (let i = 0; i < lines.length; i++) { const match = lines[i].match(/^(\s*)originRequest:\s*$/); if (!match) continue; const originIndent = match[1]; const childIndent = `${originIndent} `; let insertAt = i + 1; while (insertAt < lines.length) { const line = lines[insertAt]; if (!line.trim()) break; const lineIndent = line.match(/^(\s*)/)?.[1] ?? ''; if (lineIndent.length <= originIndent.length) break; insertAt += 1; } lines.splice(insertAt, 0, `${childIndent}disableChunkedEncoding: true`); return `${lines.join('\n')}${hasTrailingNewline ? '\n' : ''}`; } for (let i = 0; i < lines.length; i++) { const match = lines[i].match(/^(\s*)service:\s+http:\/\/substrate:3100\s*$/); if (!match) continue; const serviceIndent = match[1]; lines.splice( i + 1, 0, `${serviceIndent}originRequest:`, `${serviceIndent} disableChunkedEncoding: true`, ); return `${lines.join('\n')}${hasTrailingNewline ? '\n' : ''}`; } return body; } // Rewrites the in-container credentials path in the config to the host's real path when // running `cloudflared --run` outside Docker, so the same config works in both modes. function prepareCloudflareRunConfig(config: CliConfig, configPath: string): string { const resolvedConfig = resolve(configPath); const body = readFileSync(resolvedConfig, 'utf-8'); const credentialsField = matchField(body, /^credentials-file:\s+(.+)$/m); const tunnelId = matchField(body, /^tunnel:\s+(.+)$/m); const hostCredentialsFile = resolveCloudflareHostCredentialsFile(config, credentialsField, tunnelId); if (!hostCredentialsFile) { const credentialsDir = envGet('CLOUDFLARED_CREDENTIALS_FILE', config.envFile) || resolve(homedir(), '.cloudflared'); throw new Error( `Could not find Cloudflare credentials for local \`--run\`. Expected ${tunnelId ? `${tunnelId}.json in ${credentialsDir}` : `a tunnel JSON file in ${credentialsDir}`}.` ); } if (credentialsField && resolve(credentialsField) === hostCredentialsFile) { return resolvedConfig; } const rewritten = credentialsField ? body.replace(/^credentials-file:\s+(.+)$/m, `credentials-file: ${hostCredentialsFile}`) : `credentials-file: ${hostCredentialsFile}\n${body}`; const runConfigPath = `${resolvedConfig}.local-run.yml`; writeFileSync(runConfigPath, rewritten); d.info(`Using host credentials file ${hostCredentialsFile}`); return runConfigPath; } function resolveCloudflareHostCredentialsFile( config: CliConfig, configuredPath: string, tunnelId: string, ): string { const candidates: string[] = []; if (configuredPath) { candidates.push(resolve(configuredPath)); } const credentialsDir = envGet('CLOUDFLARED_CREDENTIALS_FILE', config.envFile) || resolve(homedir(), '.cloudflared'); if (configuredPath) { candidates.push(resolve(credentialsDir, basename(configuredPath))); } if (tunnelId) { candidates.push(resolve(credentialsDir, `${tunnelId}.json`)); } const uniqueCandidates = Array.from(new Set(candidates)); const match = uniqueCandidates.find(path => existsSync(path)); if (match) return match; if (existsSync(credentialsDir)) { const jsonFiles = readdirSync(credentialsDir) .filter(name => name.toLowerCase().endsWith('.json')) .map(name => resolve(credentialsDir, name)); if (jsonFiles.length === 1) { return jsonFiles[0]; } } return ''; } function matchField(text: string, pattern: RegExp): string { const match = text.match(pattern); return match?.[1]?.trim() || ''; } function findCloudflareConfigPath(config: CliConfig): { path: string; source: 'local' | 'home' | 'none'; } { if (existsSync(config.cloudflareConfigFile)) { return { path: config.cloudflareConfigFile, source: 'local' }; } const homeDir = resolve(homedir(), '.cloudflared'); const preferredHome = resolve(homeDir, 'aether-substrate.yml'); if (existsSync(preferredHome)) { return { path: preferredHome, source: 'home' }; } if (existsSync(homeDir)) { const candidates = readdirSync(homeDir) .filter(name => name.endsWith('.yml') || name.endsWith('.yaml')) .map(name => resolve(homeDir, name)) .filter(path => { try { const body = readFileSync(path, 'utf-8'); return /^tunnel:\s+/m.test(body) && /hostname:/m.test(body); } catch { return false; } }); if (candidates.length > 0) { return { path: candidates[0], source: 'home' }; } } return { path: '', source: 'none' }; } function cloudflaredProcessRunning(config: CliConfig): boolean { try { const container = tryReadCommand(config.runtime.cmd, [ 'ps', '--filter', 'name=aether-cloudflared', '--format', '{{.Names}}', ]) || ''; if (container.split('\n').some(line => line.trim() === 'aether-cloudflared')) { return true; } } catch { // Fall through to host process checks. } if (process.platform === 'win32') { const output = tryReadCommand('tasklist', ['/FI', 'IMAGENAME eq cloudflared.exe']) || ''; return output .split('\n') .some(line => line.toLowerCase().includes('cloudflared.exe')); } const output = tryReadCommand('pgrep', ['-fl', 'cloudflared']) || ''; return output .split('\n') .some(line => line.includes('cloudflared') && !line.includes('pgrep')); } function resolveCloudflareTunnelName( config: CliConfig, explicitName?: string, requestedHostname?: string, ): string { if (explicitName) return sanitizeCloudflareTunnelName(explicitName); const configuredName = envGet('CLOUDFLARED_TUNNEL_NAME', config.envFile); if (configuredName) return sanitizeCloudflareTunnelName(configuredName); return buildDefaultCloudflareTunnelName(config, requestedHostname); } // Generates a deterministic tunnel name per machine+install. The hash suffix keeps two // substrate checkouts on the same host from stomping on each other's tunnel registration. function buildDefaultCloudflareTunnelName(config: CliConfig, requestedHostname?: string): string { const source = requestedHostname || osHostname() || 'local'; const slug = sanitizeCloudflareTunnelName(source) .replace(/^aether-substrate-?/, '') .replace(/^-+|-+$/g, ''); const hash = createHash('sha1') .update(`${source}|${config.substrateDir}`) .digest('hex') .slice(0, 6); const base = 'aether-substrate'; const suffixParts = [slug, hash].filter(Boolean); const suffix = suffixParts.join('-'); const maxSuffixLength = 63 - base.length - 1; const trimmedSuffix = suffix.slice(0, maxSuffixLength).replace(/-+$/g, ''); return trimmedSuffix ? `${base}-${trimmedSuffix}` : `${base}-${hash}`; } function sanitizeCloudflareTunnelName(value: string): string { return value .toLowerCase() .replace(/[^a-z0-9-]+/g, '-') .replace(/-+/g, '-') .replace(/^-+|-+$/g, '') .slice(0, 63); } function commandErrorText(error: any): string { const stdout = typeof error?.stdout === 'string' ? error.stdout : error?.stdout?.toString?.('utf8') || ''; const stderr = typeof error?.stderr === 'string' ? error.stderr : error?.stderr?.toString?.('utf8') || ''; const message = typeof error?.message === 'string' ? error.message : ''; return [stderr, stdout, message] .map(part => part.trim()) .filter(Boolean) .join('\n'); } // When `cloudflared tunnel route dns` fails because the user is logged into the wrong // Cloudflare zone, it echoes "." — pull that out so we can tell them. function extractZoneMismatch(detail: string, hostname: string): string { const normalizedHostname = hostname.trim().toLowerCase(); if (!normalizedHostname) return ''; const escapedHostname = normalizedHostname.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const match = detail.toLowerCase().match(new RegExp(`${escapedHostname}\\.([a-z0-9.-]+)`)); if (!match) return ''; const wrongZone = match[1]?.replace(/\.+$/, '') || ''; return wrongZone && wrongZone !== normalizedHostname ? wrongZone : ''; }