diff --git a/README.md b/README.md index 098f7c8..0796203 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/_worker.js b/_worker.js index bb27634..5ff5896 100644 --- a/_worker.js +++ b/_worker.js @@ -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), }; diff --git a/common.js b/common.js index c0a409d..0ee3e4d 100644 --- a/common.js +++ b/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) { diff --git a/middleware.js b/middleware.js index 4d7ddf8..2259bb7 100644 --- a/middleware.js +++ b/middleware.js @@ -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); }; diff --git a/netlify/edge-functions/middleware.js b/netlify/edge-functions/middleware.js index 3304002..5b1d535 100644 --- a/netlify/edge-functions/middleware.js +++ b/netlify/edge-functions/middleware.js @@ -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: "*" };