mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-15 19:52:50 +03:00
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { isAuthenticated } from "@/shared/utils/apiAuth";
|
|
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
|
import {
|
|
getCloudflaredTunnelStatus,
|
|
startCloudflaredTunnel,
|
|
stopCloudflaredTunnel,
|
|
} from "@/lib/cloudflaredTunnel";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const actionSchema = z.object({
|
|
action: z.enum(["enable", "disable"]),
|
|
});
|
|
|
|
function unauthorized() {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
export async function GET(request: NextRequest) {
|
|
if (!(await isAuthenticated(request))) {
|
|
return unauthorized();
|
|
}
|
|
|
|
try {
|
|
const status = await getCloudflaredTunnelStatus();
|
|
return NextResponse.json(status);
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
error: error instanceof Error ? error.message : "Failed to load cloudflared tunnel status",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
if (!(await isAuthenticated(request))) {
|
|
return unauthorized();
|
|
}
|
|
|
|
let rawBody: unknown;
|
|
try {
|
|
rawBody = await request.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const validation = validateBody(actionSchema, rawBody);
|
|
if (isValidationFailure(validation)) {
|
|
return validation.response;
|
|
}
|
|
|
|
const parsed = validation.data;
|
|
|
|
try {
|
|
const status =
|
|
parsed.action === "enable" ? await startCloudflaredTunnel() : await stopCloudflaredTunnel();
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
action: parsed.action,
|
|
status,
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
error: error instanceof Error ? error.message : "Failed to update cloudflared tunnel",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|