mirror of
https://github.com/LittleChest/Dohna-NS.git
synced 2026-05-06 22:44:50 +08:00
handler: Custom upstream servers
This commit is contained in:
parent
e2482fe548
commit
86fa917b6a
@ -17,6 +17,13 @@ Read [Dohna NS Documentation](https://dohna-ns.sbs/) to learn how to install Doh
|
||||
| Vercel | [](https://vercel.com/new/clone?repository-url=https://github.com/LittleChest/Dohna-NS) |
|
||||
| Netlify | [](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.
|
||||
|
||||
@ -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),
|
||||
};
|
||||
|
||||
13
common.js
13
common.js
@ -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) {
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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: "*" };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user