windows-iso-downloader/cloudflare-worker/worker.js
Shekhar Vaidya 8b036e94ba feat: route Microsoft API calls through Cloudflare Worker to avoid IP blocks
- Add cloudflare-worker/worker.js: edge proxy forwarding requests to allowed
  Microsoft hosts from Cloudflare distributed IPs. Set CF_WORKER_URL env var
  to activate; omit to keep direct-to-Microsoft behaviour (self-hosters unaffected).
- Wrap all outbound Microsoft URLs in proxyURL() in main.go so the Worker
  sits transparently in front of every session and download-link request.
- Fix Back button navigating to external referrer when user lands directly
  on a product URL (#5) — use window.history.state?.idx to detect whether
  there is prior in-app history before calling navigate(-1).

Closes #5, Closes #6

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 12:01:03 +05:30

54 lines
1.6 KiB
JavaScript

const ALLOWED_HOSTS = [
"www.microsoft.com",
"vlscppe.microsoft.com",
"ov-df.microsoft.com",
];
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (env.CF_WORKER_SECRET) {
const secret = request.headers.get("X-Worker-Secret");
if (secret !== env.CF_WORKER_SECRET) {
return new Response(JSON.stringify({ error: "Forbidden" }), {
status: 403,
headers: { "Content-Type": "application/json" },
});
}
}
// Target host comes from the ?host= param; path+search are forwarded as-is
const targetHost = url.searchParams.get("host");
if (!targetHost || !ALLOWED_HOSTS.includes(targetHost)) {
return new Response(JSON.stringify({ error: "Invalid or missing host parameter" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
url.searchParams.delete("host");
url.hostname = targetHost;
url.protocol = "https:";
url.port = "";
const upstreamRequest = new Request(url.toString(), {
method: request.method,
headers: {
"User-Agent": request.headers.get("User-Agent") || "Mozilla/5.0",
"Referer": request.headers.get("Referer") || "",
"Accept": request.headers.get("Accept") || "application/json",
},
});
const upstream = await fetch(upstreamRequest);
return new Response(upstream.body, {
status: upstream.status,
headers: {
"Content-Type": upstream.headers.get("Content-Type") || "application/json",
"Access-Control-Allow-Origin": "*",
},
});
},
};