diff --git a/infra/cloudflare-workers/README.md b/infra/cloudflare-workers/README.md new file mode 100644 index 000000000..ff59654c5 --- /dev/null +++ b/infra/cloudflare-workers/README.md @@ -0,0 +1 @@ +Currently manually copy/paste/deploy via Cloudflare Portal UI. diff --git a/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js b/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js new file mode 100644 index 000000000..6774236ec --- /dev/null +++ b/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js @@ -0,0 +1,35 @@ +/** + * Cloudflare Worker for api.filterlists.com + * + * This worker enables two key functionalities: + * 1. Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-directory-prod.azurewebsites.net`, allowing a custom domain (`api.filterlists.com`) with Azure's free tier. + * 2. Respect Origin Cache-Control: Ensures Cloudflare's edge cache (via Cache API) honors the `Cache-Control` headers sent by the origin for all successful responses, optimizing caching for JSON APIs and static assets. + */ +export default { + async fetch(request, _, ctx) { + const cache = caches.default; + const cacheKey = request; + + let response = await cache.match(cacheKey); + + if (response) { + return response; + } + + const url = new URL(request.url); + const azureHostname = "app-filterlists-directory-prod.azurewebsites.net"; + const originUrl = `https://${azureHostname}${url.pathname}${url.search}`; + const originRequest = new Request(originUrl, request); + + originRequest.headers.set("Host", azureHostname); + + response = await fetch(originRequest); + + if (response.ok) { + const responseToCache = response.clone(); + ctx.waitUntil(cache.put(cacheKey, responseToCache)); + } + + return response; + }, +}; diff --git a/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js b/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js new file mode 100644 index 000000000..b17df1454 --- /dev/null +++ b/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js @@ -0,0 +1,21 @@ +/** + * Cloudflare Worker for next.filterlists.com + * + * Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net`, allowing a custom domain (`next.filterlists.com`) with Azure's free tier. + */ +export default { + async fetch(request, _, __) { + const url = new URL(request.url); + const azureHostname = "app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net"; + const originUrl = `https://${azureHostname}${url.pathname}${url.search}`; + const originRequest = new Request(originUrl, request); + + // re-write Host header to trick App Service free tier into supporting custom domain + originRequest.headers.set("Host", azureHostname); + + // delete cookie to trick Cloudflare into caching static HTML + originRequest.headers.delete('cookie'); + + return await fetch(originRequest); + }, +};