import { readFile } from "node:fs/promises"; import path from "node:path"; import { requireAdminAccess } from "@/lib/session"; import { relayInventory } from "@/lib/relays"; import { decryptText } from "@/lib/crypto"; import { getEnv } from "@/lib/env"; export async function GET( _request: Request, context: { params: Promise<{ id: string }> }, ) { await requireAdminAccess(); const id = (await context.params).id; const row = (await relayInventory()).hosts.find((h) => h.id === id); if (!row) return new Response(null, { status: 404 }); const origin = new URL(getEnv().APP_URL); if ( origin.protocol !== "https:" && !["localhost", "127.0.0.1"].includes(origin.hostname) ) return new Response("请配置 HTTPS APP_URL。", { status: 409 }); const source = await readFile( path.join(process.cwd(), "lib/templates/relay-agent.py"), "utf8", ); const token = decryptText(row.token_encrypted); const payload = Buffer.from( JSON.stringify({ credentials: { panel: origin.origin, id, token }, source, }), ).toString("base64"); const script = ( await readFile( path.join(process.cwd(), "lib/templates/install-relay.sh"), "utf8", ) ) .replace("__ID__", `'${id}'`) .replace("__PAYLOAD__", `'${payload}'`); return new Response(script, { headers: { "Content-Type": "text/x-shellscript; charset=utf-8", "Content-Disposition": `attachment; filename="install-relay-${id}.sh"`, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff", }, }); }