-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworker.js
More file actions
41 lines (34 loc) · 1.36 KB
/
worker.js
File metadata and controls
41 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Cloudflare Worker for Proxying DNS-over-HTTPS (DoH) Requests
* Author: SeRaMo ( https://github.com/seramo/ )
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
// Default DoH endpoint (you can change this to any DoH server)
const DOH_ENDPOINT = "https://cloudflare-dns.com/dns-query";
// Build the target URL using the base endpoint and original query string
const targetUrl = DOH_ENDPOINT + url.search;
// Clone the incoming request and forward it to the target DoH server
const modifiedRequest = new Request(targetUrl, {
method: request.method,
headers: request.headers,
body: request.method === 'POST' ? request.body : undefined,
redirect: 'follow',
});
try {
// Forward the request and get the response from the DoH server
const response = await fetch(modifiedRequest);
// Return the response back to the client
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
} catch (error) {
// Return an error response if the request fails
return new Response('Error fetching the target DoH server.', { status: 500 });
}
}