Skip to content

Instantly share code, notes, and snippets.

@aomerk
Created November 28, 2021 09:45
Show Gist options
  • Save aomerk/8cd7f0b5a8415c8a25c5ed48634e2df3 to your computer and use it in GitHub Desktop.
Save aomerk/8cd7f0b5a8415c8a25c5ed48634e2df3 to your computer and use it in GitHub Desktop.
Cloudflare Worker for mirroring incoming requests to external server
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