Created
November 28, 2021 09:45
-
-
Save aomerk/8cd7f0b5a8415c8a25c5ed48634e2df3 to your computer and use it in GitHub Desktop.
Cloudflare Worker for mirroring incoming requests to external server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener("fetch", (event) => { | |
event.respondWith( | |
handleRequest(event.request).catch( | |
(err) => new Response(err.stack, { | |
status: 500 | |
}) | |
) | |
); | |
}); | |
// handleRequest mirrors the incoming request to an external server, and returns the usual response | |
// that your web application supposed to return. | |
async function handleRequest(request) { | |
// Parse the url of incoming request, to mirror the same request to your strixeye agent. | |
const url = new URL(request.url); | |
// modify host headers | |
url.hostname = TARGET_HOST; | |
url.host = TARGET_HOST; | |
// create new request and send it to strixeye agent | |
mirrorRequest = new Request(url, { | |
body: request.body, | |
headers: request.headers, | |
method: request.method, | |
redirect: request.redirect | |
}) | |
mirrorRequest.headers.append("CUSTOM-HEADER", CUSTOM_HEADER_VALUE) | |
fetch(mirrorRequest) | |
return fetch(request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment