1+ /**
2+ * Cloudflare Worker for Internal URL Rewriting
3+ *
4+ * This worker rewrites requests from dataplat.dbatools.io to dataplat.github.io
5+ * internally without exposing the upstream host to users.
6+ *
7+ * Route: dataplat.dbatools.io/*
8+ */
9+
10+ addEventListener ( 'fetch' , event => {
11+ event . respondWith ( handleRequest ( event . request ) )
12+ } )
13+
14+ async function handleRequest ( request ) {
15+ // Parse the request URL
16+ const url = new URL ( request . url )
17+
18+ // Only process requests for dataplat.dbatools.io
19+ if ( url . hostname !== 'dataplat.dbatools.io' ) {
20+ // Pass through all other traffic unchanged
21+ return fetch ( request )
22+ }
23+
24+ // Create a new URL with the GitHub Pages hostname
25+ const rewrittenUrl = new URL ( request . url )
26+ rewrittenUrl . hostname = 'dataplat.github.io'
27+
28+ // Create a new request with the rewritten URL
29+ // Preserve the original method, headers, and body
30+ const modifiedRequest = new Request ( rewrittenUrl . toString ( ) , {
31+ method : request . method ,
32+ headers : request . headers ,
33+ body : request . body ,
34+ redirect : 'manual' // Important: Handle redirects manually to prevent GitHub Pages redirects
35+ } )
36+
37+ try {
38+ // Fetch from the GitHub Pages URL
39+ const response = await fetch ( modifiedRequest )
40+
41+ // Handle GitHub Pages redirects internally
42+ if ( response . status >= 300 && response . status < 400 ) {
43+ const location = response . headers . get ( 'Location' )
44+ if ( location ) {
45+ // If GitHub Pages tries to redirect, modify the Location header
46+ const redirectUrl = new URL ( location , rewrittenUrl . toString ( ) )
47+ if ( redirectUrl . hostname === 'dataplat.github.io' ) {
48+ redirectUrl . hostname = 'dataplat.dbatools.io'
49+ }
50+
51+ // Create a new response with the corrected Location header
52+ const newResponse = new Response ( response . body , {
53+ status : response . status ,
54+ statusText : response . statusText ,
55+ headers : response . headers
56+ } )
57+ newResponse . headers . set ( 'Location' , redirectUrl . toString ( ) )
58+ return newResponse
59+ }
60+ }
61+
62+ // Create a new response to ensure we can modify headers if needed
63+ const newResponse = new Response ( response . body , {
64+ status : response . status ,
65+ statusText : response . statusText ,
66+ headers : response . headers
67+ } )
68+
69+ // Remove any headers that might expose the upstream host
70+ newResponse . headers . delete ( 'server' )
71+ newResponse . headers . delete ( 'x-github-request-id' )
72+
73+ // Set CORS headers if needed (optional, depending on your requirements)
74+ newResponse . headers . set ( 'Access-Control-Allow-Origin' , '*' )
75+
76+ return newResponse
77+
78+ } catch ( error ) {
79+ // Log error for debugging (visible in Cloudflare dashboard)
80+ console . error ( 'Worker error:' , error )
81+
82+ // Return a generic error response
83+ return new Response ( 'Service temporarily unavailable' , {
84+ status : 503 ,
85+ headers : {
86+ 'Content-Type' : 'text/plain' ,
87+ 'Cache-Control' : 'no-cache'
88+ }
89+ } )
90+ }
91+ }
0 commit comments