mirror of
https://github.com/skidoodle/erettsegi-browser.git
synced 2026-04-28 13:37:35 +02:00
fix
This commit is contained in:
@@ -1,11 +1,4 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { Agent, fetch } from "undici";
|
||||
|
||||
const insecureAgent = new Agent({
|
||||
connect: {
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
});
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
@@ -40,7 +33,6 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
const externalResponse = await fetch(link, {
|
||||
method: "GET",
|
||||
dispatcher: insecureAgent,
|
||||
});
|
||||
|
||||
if (!externalResponse.ok) {
|
||||
@@ -50,25 +42,20 @@ export async function GET(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
const body = await externalResponse.arrayBuffer();
|
||||
const headers = new Headers(externalResponse.headers);
|
||||
headers.set("Cache-Control", "s-maxage=31536000, stale-while-revalidate");
|
||||
|
||||
const contentType = externalResponse.headers.get("content-type");
|
||||
const headers = new Headers({
|
||||
"Cache-Control": "s-maxage=31536000, stale-while-revalidate",
|
||||
});
|
||||
|
||||
if (contentType) {
|
||||
headers.set("Content-Type", contentType);
|
||||
if (contentType === "application/pdf") {
|
||||
const filename = url.pathname.split("/").pop() ?? "document.pdf";
|
||||
headers.set("Content-Disposition", `inline; filename="${filename}"`);
|
||||
}
|
||||
if (contentType === "application/pdf") {
|
||||
const filename = url.pathname.split("/").pop() ?? "document.pdf";
|
||||
headers.set("Content-Disposition", `inline; filename="${filename}"`);
|
||||
}
|
||||
|
||||
return new Response(body, {
|
||||
return new NextResponse(externalResponse.body, {
|
||||
status: 200,
|
||||
headers,
|
||||
});
|
||||
|
||||
} catch (e: unknown) {
|
||||
console.error("Proxy Error:", e);
|
||||
const errorMessage =
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { Agent, fetch } from "undici";
|
||||
|
||||
const insecureAgent = new Agent({
|
||||
connect: {
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
});
|
||||
|
||||
const ALLOWED_HOSTS = [
|
||||
"localhost:3000",
|
||||
@@ -16,7 +9,7 @@ const ALLOWED_HOSTS = [
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = req.nextUrl;
|
||||
const link = searchParams.get("link");
|
||||
let link = searchParams.get("link");
|
||||
|
||||
if (!link) {
|
||||
return NextResponse.json({
|
||||
@@ -41,26 +34,28 @@ export async function GET(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/proxy" && url.searchParams.has("link")) {
|
||||
const realTarget = url.searchParams.get("link");
|
||||
if (realTarget) {
|
||||
try {
|
||||
const realUrl = new URL(realTarget);
|
||||
link = realTarget;
|
||||
url = realUrl;
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ALLOWED_HOSTS.includes(url.host)) {
|
||||
return NextResponse.json({ error: "Érvénytelen link" }, { status: 400 });
|
||||
}
|
||||
|
||||
const response = await fetch(link, {
|
||||
method: "HEAD",
|
||||
dispatcher: insecureAgent,
|
||||
});
|
||||
|
||||
return NextResponse.json({ status: response.status }, { status: 200 });
|
||||
} catch (e: unknown) {
|
||||
console.error("Validation Error:", e);
|
||||
|
||||
const errorResponse = {
|
||||
error: "Internal Server Error",
|
||||
message: e instanceof Error ? e.message : "An unexpected error occurred",
|
||||
...(process.env.NODE_ENV === "development" &&
|
||||
e instanceof Error && { stack: e.stack }),
|
||||
};
|
||||
|
||||
return NextResponse.json(errorResponse, { status: 500 });
|
||||
} catch (error) {
|
||||
console.error("Validation Error:", error);
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user