|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { STORAGE_KEY } from "../../../constant"; |
| 3 | +async function handle( |
| 4 | + req: NextRequest, |
| 5 | + { params }: { params: { path: string[] } }, |
| 6 | +) { |
| 7 | + if (req.method === "OPTIONS") { |
| 8 | + return NextResponse.json({ body: "OK" }, { status: 200 }); |
| 9 | + } |
| 10 | + const folder = STORAGE_KEY; |
| 11 | + const fileName = `${folder}/backup.json`; |
| 12 | + |
| 13 | + const requestUrl = new URL(req.url); |
| 14 | + let endpoint = requestUrl.searchParams.get("endpoint"); |
| 15 | + if (!endpoint?.endsWith("/")) { |
| 16 | + endpoint += "/"; |
| 17 | + } |
| 18 | + const endpointPath = params.path.join("/"); |
| 19 | + |
| 20 | + // only allow MKCOL, GET, PUT |
| 21 | + if (req.method !== "MKCOL" && req.method !== "GET" && req.method !== "PUT") { |
| 22 | + return NextResponse.json( |
| 23 | + { |
| 24 | + error: true, |
| 25 | + msg: "you are not allowed to request " + params.path.join("/"), |
| 26 | + }, |
| 27 | + { |
| 28 | + status: 403, |
| 29 | + }, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + // for MKCOL request, only allow request ${folder} |
| 34 | + if ( |
| 35 | + req.method == "MKCOL" && |
| 36 | + !new URL(endpointPath).pathname.endsWith(folder) |
| 37 | + ) { |
| 38 | + return NextResponse.json( |
| 39 | + { |
| 40 | + error: true, |
| 41 | + msg: "you are not allowed to request " + params.path.join("/"), |
| 42 | + }, |
| 43 | + { |
| 44 | + status: 403, |
| 45 | + }, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + // for GET request, only allow request ending with fileName |
| 50 | + if ( |
| 51 | + req.method == "GET" && |
| 52 | + !new URL(endpointPath).pathname.endsWith(fileName) |
| 53 | + ) { |
| 54 | + return NextResponse.json( |
| 55 | + { |
| 56 | + error: true, |
| 57 | + msg: "you are not allowed to request " + params.path.join("/"), |
| 58 | + }, |
| 59 | + { |
| 60 | + status: 403, |
| 61 | + }, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + // for PUT request, only allow request ending with fileName |
| 66 | + if ( |
| 67 | + req.method == "PUT" && |
| 68 | + !new URL(endpointPath).pathname.endsWith(fileName) |
| 69 | + ) { |
| 70 | + return NextResponse.json( |
| 71 | + { |
| 72 | + error: true, |
| 73 | + msg: "you are not allowed to request " + params.path.join("/"), |
| 74 | + }, |
| 75 | + { |
| 76 | + status: 403, |
| 77 | + }, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + const targetUrl = `${endpoint + endpointPath}`; |
| 82 | + |
| 83 | + const method = req.method; |
| 84 | + const shouldNotHaveBody = ["get", "head"].includes( |
| 85 | + method?.toLowerCase() ?? "", |
| 86 | + ); |
| 87 | + |
| 88 | + const fetchOptions: RequestInit = { |
| 89 | + headers: { |
| 90 | + authorization: req.headers.get("authorization") ?? "", |
| 91 | + }, |
| 92 | + body: shouldNotHaveBody ? null : req.body, |
| 93 | + method, |
| 94 | + // @ts-ignore |
| 95 | + duplex: "half", |
| 96 | + }; |
| 97 | + |
| 98 | + const fetchResult = await fetch(targetUrl, fetchOptions); |
| 99 | + |
| 100 | + console.log("[Any Proxy]", targetUrl, { |
| 101 | + status: fetchResult.status, |
| 102 | + statusText: fetchResult.statusText, |
| 103 | + }); |
| 104 | + |
| 105 | + return fetchResult; |
| 106 | +} |
| 107 | + |
| 108 | +export const POST = handle; |
| 109 | +export const GET = handle; |
| 110 | +export const OPTIONS = handle; |
| 111 | + |
| 112 | +export const runtime = "edge"; |
0 commit comments