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

50 lines
1.8 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";
2026-05-25 16:15:34 +07:00
import { isTailscaleInstalled, isTailscaleLoggedIn, 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);
2026-04-10 18:16:14 +07:00
const EXTENDED_PATH = `/usr/local/bin:/opt/homebrew/bin:/usr/bin:/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 isDaemonRunning() {
2026-04-10 18:16:14 +07:00
try {
await execAsync(`tailscale --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, 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;
2026-05-10 21:54:54 +07:00
const hasCachedPassword = !!(getCachedPassword() || await loadEncryptedPassword());
return NextResponse.json({ installed, loggedIn, platform, brewAvailable, daemonRunning, hasCachedPassword });
2026-04-10 18:16:14 +07:00
} catch (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
}