2026-04-10 18:16:14 +07:00
|
|
|
import os from "os";
|
2026-05-09 17:48:20 +07:00
|
|
|
import { exec } from "child_process";
|
|
|
|
|
import { promisify } from "util";
|
2026-04-10 18:16:14 +07:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { isTailscaleInstalled, isTailscaleLoggedIn, TAILSCALE_SOCKET } from "@/lib/tunnel/tailscale";
|
|
|
|
|
|
2026-05-09 17:48:20 +07:00
|
|
|
const execAsync = promisify(exec);
|
2026-04-10 18:16:14 +07:00
|
|
|
const EXTENDED_PATH = `/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:${process.env.PATH || ""}`;
|
2026-05-09 17:48:20 +07:00
|
|
|
const PROBE_TIMEOUT_MS = 1500;
|
2026-04-10 18:16:14 +07:00
|
|
|
|
2026-05-09 17:48:20 +07:00
|
|
|
async function hasBrew() {
|
|
|
|
|
try {
|
|
|
|
|
await execAsync("which brew", { windowsHide: true, env: { ...process.env, PATH: EXTENDED_PATH }, timeout: PROBE_TIMEOUT_MS });
|
|
|
|
|
return true;
|
|
|
|
|
} catch { return false; }
|
2026-04-10 18:16:14 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-09 17:48:20 +07:00
|
|
|
async function isDaemonRunning() {
|
2026-04-10 18:16:14 +07:00
|
|
|
try {
|
2026-05-09 17:48:20 +07:00
|
|
|
await execAsync(`tailscale --socket ${TAILSCALE_SOCKET} status --json`, {
|
2026-04-14 11:48:59 +07:00
|
|
|
windowsHide: true,
|
2026-04-10 18:16:14 +07:00
|
|
|
env: { ...process.env, PATH: EXTENDED_PATH },
|
2026-05-09 17:48:20 +07:00
|
|
|
timeout: PROBE_TIMEOUT_MS
|
2026-04-10 18:16:14 +07:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
try {
|
2026-05-09 17:48:20 +07:00
|
|
|
await execAsync("pgrep -x tailscaled", { windowsHide: true, timeout: PROBE_TIMEOUT_MS });
|
2026-04-10 18:16:14 +07:00
|
|
|
return true;
|
|
|
|
|
} catch { return false; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET() {
|
|
|
|
|
try {
|
|
|
|
|
const installed = isTailscaleInstalled();
|
|
|
|
|
const platform = os.platform();
|
2026-05-09 17:48:20 +07:00
|
|
|
// Run independent probes in parallel — none blocks the event loop
|
|
|
|
|
const [brewAvailable, daemonRunning] = await Promise.all([
|
|
|
|
|
platform === "darwin" ? hasBrew() : Promise.resolve(false),
|
|
|
|
|
installed ? isDaemonRunning() : Promise.resolve(false),
|
|
|
|
|
]);
|
2026-04-10 18:16:14 +07:00
|
|
|
const loggedIn = daemonRunning ? isTailscaleLoggedIn() : false;
|
|
|
|
|
return NextResponse.json({ installed, loggedIn, platform, brewAvailable, daemonRunning });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json({ error: error.message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|