Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

4.4 KB
Raw Blame
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { onTestFinished } from "vitest";

export const ROOT = path.resolve(__dirname, "..");
export const GATEWAY = path.join(__dirname, "mso-gateway");
export const CLI = path.join(ROOT, "bin/mso");
export const VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8")).version as string;

function collectRecordedPids(root: string): number[] {
  const pids = new Set<number>();
  const visit = (dir: string) => {
    let entries: fs.Dirent[] = [];
    try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
    for (const entry of entries) {
      const file = path.join(dir, entry.name);
      if (entry.isDirectory()) { visit(file); continue; }
      if (entry.name !== "state.json") continue;
      try {
        const state = JSON.parse(fs.readFileSync(file, "utf8")) as {
          tunnelIdentity?: { pid?: number }; runtimeIdentity?: { pid?: number };
        };
        for (const pid of [state.tunnelIdentity?.pid, state.runtimeIdentity?.pid]) {
          if (typeof pid === "number" && pid > 1) pids.add(pid);
        }
      } catch { /* corrupt/partial fixture state is still safe to delete */ }
    }
  };
  visit(root);
  return [...pids];
}

function cleanupGatewayFixture(dir: string) {
  for (const pid of collectRecordedPids(dir)) {
    try { process.kill(pid, "SIGTERM"); } catch { /* already stopped */ }
  }
  fs.rmSync(dir, { recursive: true, force: true });
}

export function fixture() {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-gateway-"));
  onTestFinished(() => cleanupGatewayFixture(dir));
  const bin = path.join(dir, "bin"), state = path.join(dir, "state"), envFile = path.join(dir, ".env.local");
  const startFile = path.join(dir, "gateway-fake-starts");
  fs.mkdirSync(bin, { mode: 0o700 });
  fs.writeFileSync(envFile, "OS_SESSION_SECRET=fixture-only-not-a-real-secret\n", { mode: 0o600 });

  const curl = path.join(bin, "curl");
  fs.writeFileSync(
    curl,
    `#!/bin/sh\nprintf '%s\\n' '{"status":"ok","buildId":"fixture","runtimeInstanceId":"fixture","version":"${VERSION}"}'\n`,
    { mode: 0o700 },
  );

  const cloudflared = path.join(bin, "cloudflared");
  fs.writeFileSync(
    cloudflared,
    `#!/usr/bin/env node
if (process.argv.includes('--version')) { console.log('cloudflared version fixture'); process.exit(0); }
if (process.env.OS_SESSION_SECRET || process.env.OS_LOGIN_PASSWORD) require('fs').writeFileSync(process.env.HOME + '/cloudflared-secret-env', 'leaked');
if (process.argv.includes('--url')) {
  require('fs').appendFileSync(process.env.HOME + '/gateway-fake-starts', 'start\\n');
  console.error('INF https://mso-gateway-fixture.trycloudflare.com');
}
process.on('SIGTERM', () => process.exit(0));
setInterval(() => {}, 1000);
`,
    { mode: 0o700 },
  );

  const procTcp = path.join(dir, "proc-tcp"), procTcp6 = path.join(dir, "proc-tcp6");
  fs.writeFileSync(procTcp, "  sl  local_address rem_address   st\n");
  fs.writeFileSync(procTcp6, "  sl  local_address rem_address   st\n");
  const baseEnv: NodeJS.ProcessEnv = {
    ...process.env,
    NODE_ENV: "test",
    HOME: dir,
    MSO_GATEWAY_ROOT: ROOT,
    MSO_GATEWAY_ENV: envFile,
    MSO_GATEWAY_STATE_DIR: state,
    MSO_GATEWAY_CURL: curl,
    MSO_GATEWAY_CLOUDFLARED: cloudflared,
    MSO_GATEWAY_LOCAL_URL: "http://127.0.0.1:4005",
    MSO_GATEWAY_SKIP_PUBLIC_PROBE: "1",
    MSO_GATEWAY_PROC_NET_TCP: procTcp,
    MSO_GATEWAY_PROC_NET_TCP6: procTcp6,
  };
  return { dir, bin, state, envFile, curl, cloudflared, startFile, baseEnv };
}

export function runGateway(args: string[], env: NodeJS.ProcessEnv) {
  return execFileSync(GATEWAY, args, { encoding: "utf8", env });
}

export function readFileSnapshot(file: string) {
  const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
  try {
    const stat = fs.fstatSync(fd);
    if (!stat.isFile()) throw new Error(`expected regular file: ${file}`);
    return { mode: stat.mode & 0o777, text: fs.readFileSync(fd, "utf8") };
  } finally {
    fs.closeSync(fd);
  }
}

export function readState(stateDir: string) {
  const statePath = path.join(stateDir, "state.json"), snapshot = readFileSnapshot(statePath);
  const value = JSON.parse(snapshot.text) as {
    mode: string;
    url: string;
    tunnelIdentity: { pid: number };
    runtimeOwned: boolean;
  };
  return { path: statePath, snapshot, value };
}