Last active
December 21, 2022 10:43
-
-
Save dz0ny/7035600bbc9b90f6576a06085a9ec3f1 to your computer and use it in GitHub Desktop.
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)); | |
}); | |
function handleOptions(request) { | |
// Make sure the necessary headers are present | |
// for this to be a valid pre-flight request | |
let headers = request.headers; | |
if ( | |
headers.get("Origin") !== null && | |
headers.get("Access-Control-Request-Method") !== null && | |
headers.get("Access-Control-Request-Headers") !== null | |
) { | |
// Handle CORS pre-flight request. | |
// If you want to check or reject the requested method + headers | |
// you can do that here. | |
let respHeaders = { | |
...corsHeaders, | |
// Allow all future content Request headers to go back to browser | |
// such as Authorization (Bearer) or X-Client-Name-Version | |
"Access-Control-Allow-Headers": request.headers.get( | |
"Access-Control-Request-Headers" | |
), | |
"Access-Control-Allow-Origin": request.headers.get("origin"), | |
}; | |
return new Response(null, { | |
headers: respHeaders, | |
}); | |
} else { | |
// Handle standard OPTIONS request. | |
// If you want to allow other HTTP Methods, you can do that here. | |
return new Response(null, { | |
headers: { | |
Allow: "GET, HEAD, POST, OPTIONS", | |
}, | |
}); | |
} | |
} | |
async function proxyJS(request) { | |
const originalResponse = await fetch("https://cdn.firstpromoter.com/fpr.js"); | |
const js = await originalResponse.text(); | |
const patchedBody = js.replaceAll( | |
"t.firstpromoter.com", | |
request.headers.get("host") | |
); | |
return new Response(patchedBody, { | |
status: originalResponse.status, | |
statusText: originalResponse.statusText, | |
headers: originalResponse.headers, | |
}); | |
} | |
async function proxyReq(request) { | |
const url = new URL(request.url); | |
const newRequest = new Request( | |
"https://t.firstpromoter.com" + url.pathname, | |
request | |
); | |
newRequest.headers.set("host", "t.firstpromoter.com"); | |
newRequest.headers.set("x-country", request.headers.get("cf-ipcountry")); | |
newRequest.headers.set("x-real-ip", request.headers.get("x-real-ip")); | |
newRequest.headers.set("X-Forwarded-For", request.headers.get("x-real-ip")); | |
newRequest.headers.set("X-Forwarded-Host", url.host); | |
let response; | |
if (request.method === "OPTIONS") { | |
response = handleOptions(request); | |
} else { | |
response = await fetch(newRequest); | |
response = new Response(response.body, response); | |
response.headers.set("Access-Control-Allow-Credentials", "true"); | |
response.headers.set("access-control-allow-headers", "X-Requested-With"); | |
response.headers.set( | |
"Access-Control-Allow-Origin", | |
request.headers.get("origin") | |
); | |
response.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); | |
} | |
return response; | |
} | |
async function handleRequest(request) { | |
const url = new URL(request.url); | |
if (url.pathname === "/fpr.js") { | |
return await proxyJS(request); | |
} | |
if (url.pathname === "/get_details" || url.pathname === "/tr") { | |
return await proxyReq(request); | |
} | |
return new Response("Not found", { status: 404 }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment