Skip to content

Commit

Permalink
Merge pull request #19 from Fangliding/main
Browse files Browse the repository at this point in the history
Add path check
  • Loading branch information
tina-hello authored Aug 22, 2024
2 parents 71a24aa + b3af746 commit ece0b7a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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});

// developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker
Expand All @@ -18,7 +19,12 @@ 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 {searchParams, pathname} = new URL(url)

//Check path
if (!pathname.startsWith(path)) {
return r404;
}
if (method == 'GET' && searchParams.has('dns')) {
res = fetch(doh + '?dns=' + searchParams.get('dns'), {
method: 'GET',
Expand Down

0 comments on commit ece0b7a

Please sign in to comment.