110 lines
3.3 KiB
TypeScript
110 lines
3.3 KiB
TypeScript
import { env } from "@/env.mjs";
|
|
import { createDNSRecord } from "@/lib/cloudflare";
|
|
import {
|
|
createUserRecord,
|
|
getUserRecordByTypeNameContent,
|
|
getUserRecordCount,
|
|
} from "@/lib/dto/cloudflare-dns-record";
|
|
import { checkUserStatus } from "@/lib/dto/user";
|
|
import { getCurrentUser } from "@/lib/session";
|
|
import { generateSecret } from "@/lib/utils";
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const user = checkUserStatus(await getCurrentUser());
|
|
if (user instanceof Response) return user;
|
|
|
|
const {
|
|
CLOUDFLARE_ZONE_ID,
|
|
CLOUDFLARE_API_KEY,
|
|
CLOUDFLARE_EMAIL,
|
|
NEXT_PUBLIC_FREE_RECORD_QUOTA,
|
|
} = env;
|
|
|
|
if (!CLOUDFLARE_ZONE_ID || !CLOUDFLARE_API_KEY || !CLOUDFLARE_EMAIL) {
|
|
return Response.json("API key、zone iD and email are required", {
|
|
status: 400,
|
|
statusText: "API key、zone iD and email are required",
|
|
});
|
|
}
|
|
|
|
// Check quota: 若是管理员则不检查,否则检查
|
|
const user_records_count = await getUserRecordCount(user.id);
|
|
if (
|
|
user.role !== "ADMIN" &&
|
|
Number(NEXT_PUBLIC_FREE_RECORD_QUOTA) > 0 &&
|
|
user_records_count >= Number(NEXT_PUBLIC_FREE_RECORD_QUOTA)
|
|
) {
|
|
return Response.json("Your records have reached the free limit.", {
|
|
status: 409,
|
|
statusText: "Your records have reached the free limit.",
|
|
});
|
|
}
|
|
|
|
const { records } = await req.json();
|
|
const record = {
|
|
...records[0],
|
|
id: generateSecret(16),
|
|
type: "CNAME",
|
|
proxied: false,
|
|
};
|
|
|
|
const user_record = await getUserRecordByTypeNameContent(
|
|
user.id,
|
|
record.type,
|
|
record.name.endsWith(".wr.do") ? record.name : record.name + ".wr.do",
|
|
record.content,
|
|
1,
|
|
);
|
|
if (user_record && user_record.length > 0) {
|
|
return Response.json("Record already exists", {
|
|
status: 403,
|
|
statusText: "Record already exists",
|
|
});
|
|
}
|
|
|
|
const data = await createDNSRecord(
|
|
CLOUDFLARE_ZONE_ID,
|
|
CLOUDFLARE_API_KEY,
|
|
CLOUDFLARE_EMAIL,
|
|
record,
|
|
);
|
|
if (!data.success || !data.result?.id) {
|
|
return Response.json(data.errors, {
|
|
status: 501,
|
|
statusText: `An error occurred. ${data.errors}`,
|
|
});
|
|
} else {
|
|
const res = await createUserRecord(user.id, {
|
|
record_id: data.result.id,
|
|
zone_id: data.result.zone_id,
|
|
zone_name: data.result.zone_name,
|
|
name: data.result.name,
|
|
type: data.result.type,
|
|
content: data.result.content,
|
|
proxied: data.result.proxied,
|
|
proxiable: data.result.proxiable,
|
|
ttl: data.result.ttl,
|
|
comment: data.result.comment ?? "",
|
|
tags: data.result.tags?.join("") ?? "",
|
|
created_on: data.result.created_on,
|
|
modified_on: data.result.modified_on,
|
|
active: 1,
|
|
});
|
|
if (res.status !== "success") {
|
|
return Response.json(res.status, {
|
|
status: 502,
|
|
statusText: `An error occurred. ${res.status}`,
|
|
});
|
|
}
|
|
return Response.json(res.data);
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
return Response.json(error?.statusText || error, {
|
|
status: error?.status || 500,
|
|
statusText: error?.statusText || "Server error",
|
|
});
|
|
}
|
|
}
|