feat: 支持托管中转主机和节点接入线路
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { syncRelay, RelayError } from "@/lib/relays";
|
||||
export const runtime = "nodejs";
|
||||
export async function POST(
|
||||
request: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const headers = { "Cache-Control": "no-store" };
|
||||
try {
|
||||
const reader = request.body?.getReader();
|
||||
if (!reader) return new Response(null, { status: 400, headers });
|
||||
const chunks: Uint8Array[] = [];
|
||||
let size = 0;
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
size += value.byteLength;
|
||||
if (size > 16384) {
|
||||
await reader.cancel();
|
||||
return new Response(null, { status: 413, headers });
|
||||
}
|
||||
chunks.push(value);
|
||||
}
|
||||
return Response.json(
|
||||
await syncRelay(
|
||||
(await context.params).id,
|
||||
request.headers.get("authorization")?.replace(/^Bearer /, "") || "",
|
||||
JSON.parse(Buffer.concat(chunks).toString("utf8")),
|
||||
),
|
||||
{ headers },
|
||||
);
|
||||
} catch (e) {
|
||||
return Response.json(
|
||||
{ error: e instanceof RelayError ? e.message : "无效报告。" },
|
||||
{ status: e instanceof RelayError ? 403 : 400, headers },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user