handler: Refactor

This commit is contained in:
LittleChest 2026-06-14 12:41:20 +08:00
parent 1b6191580d
commit 0d95560738
5 changed files with 87 additions and 76 deletions

View File

@ -1,4 +1,4 @@
import handler from "./common";
import handler from "./handler/dns";
export default {
fetch: async (request, env) =>

View File

@ -45,19 +45,16 @@ export default async function handler(
}
const { method, headers, url } = request;
const { search, searchParams, pathname } = new URL(url);
const { search, searchParams } = new URL(url);
const ip =
rawIP ||
headers.get("x-forwarded-for").split(",")[0].trim() ||
headers.get("x-real-ip");
let res = new Response(null, { status: 404 });
let res = new Response(null, { status: 400 });
// JSON API
if (pathname === "/resolve") {
res = new Response(null, { status: 400 });
if (method === "GET" && searchParams.has("name")) {
if (concurrent) {
res = await Promise.any(
@ -101,12 +98,8 @@ export default async function handler(
res = new Response(null, { status: 500 });
}
}
}
// DNS Query
if (pathname === "/dns-query") {
res = new Response(null, { status: 400 });
let queryData;
// GET
@ -158,7 +151,6 @@ export default async function handler(
concurrent,
);
}
}
return res;
}

19
handler/entrypoint.js Normal file
View File

@ -0,0 +1,19 @@
import dnsHandler from "./dns";
export default async function handler(
request,
dns,
api,
ipv4Prefix = 32,
ipv6Prefix = 128,
concurrent = false,
rawIP,
) {
const { pathname } = new URL(request.url);
let res = new Response(null, { status: 404 });
// DNS over HTTPS & JSON API
if (pathname === "/dns-query" || pathname === "/resolve") {
res = dnsHandler(request, dns, api, ipv4Prefix, ipv6Prefix, concurrent, rawIP);
}
}

View File

@ -1,4 +1,4 @@
import handler from "./common";
import handler from "./handler/dns";
export default middleware = async (request) => {
return handler(

View File

@ -1,4 +1,4 @@
import handler from "../../common.js";
import handler from "../../handler/dns.js";
export default async (request) =>
handler(
request,