export default { async fetch(request) { const url = new URL(request.url); // Healthcheck if (url.pathname === "/health") { return new Response("OK - Worker running", { status: 200 }); } const ORIGIN_HOST = "line.sir24.biz"; const ORIGIN_PORT = "8080"; const ORIGIN_PROTO = "http"; // Target origin URL const target = new URL(request.url); target.protocol = ORIGIN_PROTO + ":"; target.hostname = ORIGIN_HOST; target.port = ORIGIN_PORT; // Clone headers and force Host const headers = new Headers(request.headers); headers.set("Host", ORIGIN_HOST); headers.set("X-Forwarded-Host", ORIGIN_HOST); // Forward real client IP (origin must read these) const realIp = request.headers.get("CF-Connecting-IP"); if (realIp) { const prior = request.headers.get("X-Forwarded-For"); headers.set("X-Forwarded-For", prior ? `${prior}, ${realIp}` : realIp); } // Remove headers that can cause issues headers.delete("Content-Length"); headers.delete("Accept-Encoding"); // évite certains soucis gzip/brotli const init = { method: request.method, headers, redirect: "manual", }; if (request.method !== "GET" && request.method !== "HEAD") { init.body = await request.arrayBuffer(); } const resp = await fetch(target.toString(), init); // Copy response headers so we can rewrite Location if needed const outHeaders = new Headers(resp.headers); // Rewrite redirects to keep your domain in the client URL const loc = outHeaders.get("Location"); if (loc) { try { const l = new URL(loc, url); // handle relative too // if origin redirects to origin host, rewrite to your public host if (l.hostname === ORIGIN_HOST && (l.port === ORIGIN_PORT || l.port === "")) { l.protocol = url.protocol; l.host = url.host; // includes your hostname + port if any outHeaders.set("Location", l.toString()); } } catch (_) { // ignore invalid Location } } return new Response(resp.body, { status: resp.status, headers: outHeaders, }); }, };