Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.6 KB
Raw Blame
/**
 * Daily TLS cert renewer.
 *
 * When `data/certs/meta.json` indicates a Tailscale-issued cert, this service
 * re-runs `tailscale cert` every 24h. The CLI is a no-op when the cached cert
 * has >1/3 lifetime remaining (~60 days for a 90-day LE cert), and fetches a
 * fresh one otherwise. If the cert file's mtime changes after the run, we
 * hot-swap the live HTTPS server's SecureContext so new TLS handshakes pick
 * up the new cert without a process restart.
 *
 * Self-signed certs don't renew (the script issues 10-year certs for local
 * use); the renewer is a no-op in that mode.
 */
import { execFile } from '../lib/childProcess.js';
import { existsSync, readFileSync, statSync } from 'fs';
import { promisify } from 'util';
import { PATHS } from '../lib/fileUtils.js';
import { findTailscale } from '../lib/tailscale.js';
import { certPaths } from '../../lib/certPaths.js';
import { readCertMeta } from '../../lib/certMeta.js';

const execFileAsync = promisify(execFile);

const { cert: CERT_PATH, key: KEY_PATH, meta: META_PATH } = certPaths(PATHS.data);
const RENEW_INTERVAL_MS = 24 * 60 * 60 * 1000;

async function renewOnce(httpsServer) {
  const meta = readCertMeta(META_PATH);
  if (!meta || meta.mode !== 'tailscale' || !meta.hostname) return;

  const bin = findTailscale();
  if (!bin) {
    console.log(`⚠️ cert renewer: tailscale CLI not found, skipping renewal`);
    return;
  }

  const mtimeBefore = existsSync(CERT_PATH) ? statSync(CERT_PATH).mtimeMs : 0;

  const { stderr } = await execFileAsync(bin, [
    'cert',
    `--cert-file=${CERT_PATH}`,
    `--key-file=${KEY_PATH}`,
    meta.hostname
  ]).catch(err => ({ stderr: err.message }));

  if (stderr && !/Wrote (public|private)/.test(stderr)) {
    console.log(`⚠️ cert renewer: tailscale cert stderr: ${stderr.trim().split('\n')[0]}`);
  }

  const mtimeAfter = existsSync(CERT_PATH) ? statSync(CERT_PATH).mtimeMs : 0;
  if (mtimeAfter > mtimeBefore) {
    httpsServer.setSecureContext({
      cert: readFileSync(CERT_PATH),
      key: readFileSync(KEY_PATH)
    });
    console.log(`🔒 cert renewer: hot-swapped renewed cert for ${meta.hostname}`);
  }
}

export function initCertRenewer(httpsServer) {
  if (!httpsServer || typeof httpsServer.setSecureContext !== 'function') return;
  const meta = readCertMeta(META_PATH);
  if (!meta || meta.mode !== 'tailscale') return;

  renewOnce(httpsServer).catch(err => console.error(`❌ cert renewer initial run: ${err.message}`));
  setInterval(() => {
    renewOnce(httpsServer).catch(err => console.error(`❌ cert renewer: ${err.message}`));
  }, RENEW_INTERVAL_MS).unref();

  console.log(`🔒 cert renewer: scheduled daily renewal for ${meta.hostname}`);
}