Skip to content

Commit b5dbf7e

Browse files
Add Cloudflare Worker for internal URL rewriting
Introduces a Cloudflare Worker (cloudflare-worker.js) that transparently rewrites requests from dataplat.dbatools.io to dataplat.github.io, handling redirects and removing headers that could expose the upstream host. Adds wrangler.toml configuration for deployment and routing.
1 parent 0294168 commit b5dbf7e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cloudflare-worker.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

wrangler.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name = "dataplat-url-rewriter"
2+
main = "cloudflare-worker.js"
3+
compatibility_date = "2024-12-01"
4+
5+
# Optional: Set the account ID if you have multiple Cloudflare accounts
6+
# account_id = "your-account-id-here"
7+
8+
# Route configuration (can also be set in Cloudflare dashboard)
9+
[[routes]]
10+
pattern = "dataplat.dbatools.io/*"
11+
zone_name = "dbatools.io"

0 commit comments

Comments
 (0)