feat: 接入 AnyTLS 节点托管和用户流量同步
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { syncAnyTLSNode } from "@/lib/node-agent";
|
||||
import { ProvisionError } from "@/lib/certificates";
|
||||
export const runtime = "nodejs";
|
||||
export async function POST(
|
||||
request: Request,
|
||||
context: { params: Promise<{ slug: string }> },
|
||||
) {
|
||||
const headers = { "Cache-Control": "no-store" };
|
||||
try {
|
||||
const reader = request.body?.getReader();
|
||||
if (!reader) throw new ProvisionError("缺少报告。");
|
||||
let length = 0;
|
||||
const chunks: Uint8Array[] = [];
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
length += value.byteLength;
|
||||
if (length > 2 * 1024 * 1024) {
|
||||
await reader.cancel();
|
||||
return new Response(null, { status: 413, headers });
|
||||
}
|
||||
chunks.push(value);
|
||||
}
|
||||
const payload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
return Response.json(
|
||||
await syncAnyTLSNode(
|
||||
(await context.params).slug,
|
||||
request.headers.get("authorization")?.replace(/^Bearer /, "") ?? "",
|
||||
payload,
|
||||
),
|
||||
{ headers },
|
||||
);
|
||||
} catch (error) {
|
||||
return Response.json(
|
||||
{
|
||||
error:
|
||||
error instanceof ProvisionError ? error.message : "节点同步失败。",
|
||||
},
|
||||
{ status: error instanceof ProvisionError ? 400 : 500, headers },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { SING_BOX_VERSION } from "@/lib/anytls";
|
||||
import { createHash } from "node:crypto";
|
||||
export const runtime = "nodejs";
|
||||
export async function GET(
|
||||
request: Request,
|
||||
context: { params: Promise<{ arch: string }> },
|
||||
) {
|
||||
const { arch } = await context.params;
|
||||
if (!["amd64", "arm64"].includes(arch))
|
||||
return new Response(null, { status: 404 });
|
||||
try {
|
||||
const bytes = await readFile(
|
||||
path.join(
|
||||
process.cwd(),
|
||||
"data/artifacts",
|
||||
`sing-box-${SING_BOX_VERSION}-linux-${arch}`,
|
||||
),
|
||||
);
|
||||
const checksum = createHash("sha256").update(bytes).digest("hex");
|
||||
if (new URL(request.url).searchParams.has("sha256"))
|
||||
return new Response(checksum, {
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
});
|
||||
return new Response(bytes, {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Cache-Control": "no-store",
|
||||
"X-Content-SHA256": checksum,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
return new Response(
|
||||
"尚未构建节点程序,请运行 scripts/build-sing-box.sh。",
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user