I cloned my website to another host for staging purposes and tried to use the Cloudflare worker to resolve the website from the staging host for my static ip, my home ip, but was unsuccessful. Here is the script. Any idea what could be the problem?
And if the script is fine, how should I do the dns part? Because I cant give the domain DNS A record the worker url!
const stagingIP = ‘StagingHostIP’;
const productionIP = ‘ProductionHostIP’;
const allowedIPs = [‘MyHomeIP’];
addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request));
})
async function handleRequest(request) {
const clientIP = request.headers.get(‘CF-Connecting-IP’);
let url = request.url;
if (allowedIPs.includes(clientIP)) {
url = url.replace(productionIP, stagingIP);
} else {
url = url.replace(stagingIP, productionIP);
}
const init = {
headers: request.headers,
cf: {
resolveOverride: url,
},
};
return fetch(request, init);
}