handler: Custom upstream servers

This commit is contained in:
LittleChest 2025-04-26 14:02:14 +08:00
parent e2482fe548
commit 86fa917b6a
5 changed files with 18 additions and 9 deletions

View File

@ -17,6 +17,13 @@ Read [Dohna NS Documentation](https://dohna-ns.sbs/) to learn how to install Doh
| Vercel | [![Deploy to Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/LittleChest/Dohna-NS) |
| Netlify | [![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/LittleChest/Dohna-NS) |
## Environment Variables
| Key | Default | Description |
| --- | ---------------------------- | ------------------------------------------------ |
| DNS | https://dns.google/dns-query | Specify a DNS over HTTPS server as the upstream. |
| API | https://dns.google/resolve | Specify a JSON API server as the upstream. |
## Self-hosted
You can use [Netlify CLI](https://cli.netlify.com/commands/serve/) or [`workerd`](https://github.com/cloudflare/workerd/blob/main/README.md#getting-started) **(Recommended)** to serve locally.

View File

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

View File

@ -1,7 +1,8 @@
const dns = "https://dns.google/dns-query";
const api = "https://dns.google/resolve";
export default async function handler(request) {
export default async function handler(
request,
dns = "https://dns.google/dns-query",
api = "https://dns.google/resolve"
) {
const { method, headers, url } = request;
const { search, searchParams, pathname } = new URL(url);
const ip = headers.get("x-forwarded-for");
@ -57,14 +58,14 @@ export default async function handler(request) {
}
if (queryData !== undefined) {
res = await queryDns(queryData, ip);
res = await queryDns(queryData, ip, dns);
}
}
return res;
}
async function queryDns(queryData, ip) {
async function queryDns(queryData, ip, dns) {
const hasOptRecord = checkForOptRecord(queryData);
let newQueryData = queryData;
if (!hasOptRecord && ip) {

View File

@ -1,5 +1,5 @@
import handler from "./common";
export default middleware = async (request) => {
return handler(request);
return handler(request, process.env.DNS, process.env.API);
};

View File

@ -1,3 +1,4 @@
import handler from "../../common.js";
export default (request) => handler(request);
export default (request) =>
handler(request, Netlify.env.get("DNS"), Netlify.env.get("API"));
export const config = { path: "*" };