From 46ad4c563ee1ea3defb4b182c29ac4bf3bf66696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Wed, 21 Aug 2024 22:10:27 +0800 Subject: [PATCH 1/3] Update index.js --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index 03be7e8..8918e9d 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,18 @@ const dohjson = 'https://security.cloudflare-dns.com/dns-query' const contype = 'application/dns-message' const jstontype = 'application/dns-json' const r404 = new Response(null, {status: 404}); +const path = '/dns-query'; // Set to '' to allow all // developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker export default { async fetch(r, env, ctx) { + const RequestPath = new URL(r.url).pathname; + + //Check path + if (!RequestPath.startsWith(path)) { + return r404; + } + return handleRequest(r); }, }; From ab105c03488ae893abda11e4c46dbe444e6cc9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Wed, 21 Aug 2024 23:41:32 +0800 Subject: [PATCH 2/3] Update index.js --- index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 8918e9d..1184355 100644 --- a/index.js +++ b/index.js @@ -5,18 +5,11 @@ const dohjson = 'https://security.cloudflare-dns.com/dns-query' const contype = 'application/dns-message' const jstontype = 'application/dns-json' const r404 = new Response(null, {status: 404}); -const path = '/dns-query'; // Set to '' to allow all +const path = ''; // default allow all, must start with / if specified, eg. "/dns-query" // developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker export default { async fetch(r, env, ctx) { - const RequestPath = new URL(r.url).pathname; - - //Check path - if (!RequestPath.startsWith(path)) { - return r404; - } - return handleRequest(r); }, }; @@ -27,6 +20,11 @@ async function handleRequest(request) { let res = r404; const { method, headers, url } = request const searchParams = new URL(url).searchParams + const RequestPath = new URL(url).pathname; + //Check path + if (!RequestPath.startsWith(path)) { + return r404; + } if (method == 'GET' && searchParams.has('dns')) { res = fetch(doh + '?dns=' + searchParams.get('dns'), { method: 'GET', From b3af74645ce25fb454ae429075d06680945cec9d Mon Sep 17 00:00:00 2001 From: tina-hello <77915071+tina-hello@users.noreply.github.com> Date: Thu, 22 Aug 2024 09:37:37 +0700 Subject: [PATCH 3/3] Allow custom path --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1184355..f1741b8 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,8 @@ const doh = 'https://security.cloudflare-dns.com/dns-query' const dohjson = 'https://security.cloudflare-dns.com/dns-query' const contype = 'application/dns-message' const jstontype = 'application/dns-json' +const path = ''; // default allow all, must start with '/' if specified, eg. "/dns-query" const r404 = new Response(null, {status: 404}); -const path = ''; // default allow all, must start with / if specified, eg. "/dns-query" // developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker export default { @@ -19,10 +19,10 @@ async function handleRequest(request) { // blog.cloudflare.com/workers-optimization-reduces-your-bill let res = r404; const { method, headers, url } = request - const searchParams = new URL(url).searchParams - const RequestPath = new URL(url).pathname; + const {searchParams, pathname} = new URL(url) + //Check path - if (!RequestPath.startsWith(path)) { + if (!pathname.startsWith(path)) { return r404; } if (method == 'GET' && searchParams.has('dns')) {