Files
9router/src/app/api/tunnel/tailscale-check/route.js
T

54 lines
2.2 KiB
JavaScript
Raw Normal View History

2026-04-10 18:16:14 +07:00
import os from "os";
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, isSystemDaemonRunning, getTailscaleBin, TAILSCALE_SOCKET } from "@/lib/tunnel";
2026-05-10 21:54:54 +07:00
import { getCachedPassword, loadEncryptedPassword } from "@/mitm/manager";
2026-04-10 18:16:14 +07:00
const execAsync = promisify(exec);
const EXTENDED_PATH = `/usr/local/bin:/opt/homebrew/bin:/usr/sbin:/usr/bin:/bin:/snap/bin:${process.env.PATH || ""}`;
const PROBE_TIMEOUT_MS = 1500;
2026-04-10 18:16:14 +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
}
async function isCustomDaemonRunning() {
const bin = getTailscaleBin();
if (!bin) return false;
2026-04-10 18:16:14 +07:00
try {
await execAsync(`"${bin}" --socket ${TAILSCALE_SOCKET} status --json`, {
windowsHide: true,
2026-04-10 18:16:14 +07:00
env: { ...process.env, PATH: EXTENDED_PATH },
timeout: PROBE_TIMEOUT_MS
2026-04-10 18:16:14 +07:00
});
return true;
} catch {
try {
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();
// Run independent probes in parallel — none blocks the event loop
const [brewAvailable, customDaemonRunning, systemDaemonRunning] = await Promise.all([
platform === "darwin" ? hasBrew() : Promise.resolve(false),
installed ? isCustomDaemonRunning() : Promise.resolve(false),
installed ? Promise.resolve(isSystemDaemonRunning()) : Promise.resolve(false),
]);
const daemonRunning = customDaemonRunning || systemDaemonRunning;
2026-04-10 18:16:14 +07:00
const loggedIn = daemonRunning ? isTailscaleLoggedIn() : false;
2026-05-10 21:54:54 +07:00
const hasCachedPassword = !!(getCachedPassword() || await loadEncryptedPassword());
return NextResponse.json({ installed, loggedIn, platform, brewAvailable, daemonRunning, customDaemonRunning, systemDaemonRunning, hasCachedPassword });
2026-04-10 18:16:14 +07:00
} catch (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
}