Hosting proxy pac on workers
Hello, just trying to follow the documentation regarding using Gateway without WARP. I've seen the possibility to host the pac file on workers :
https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/agentless/pac-files
here is the template :
=========================================================
function FindProxyForURL(url, host) {
// No proxy for private (RFC 1918) IP addresses (intranet sites)
if (
isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0")
isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0")
isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0")
) {
return "DIRECT";
}
// No proxy for localhost
if (isInNet(dnsResolve(host), "127.0.0.0", "255.0.0.0")) {
return "DIRECT";
}
// Proxy all
return "HTTPS 3ele0ss56t.proxy.cloudflare-gateway.com:443";
}
=========================================================
but once added in a worker, I can't deploy it, I've got the following error : No event handlers were registered. This script does nothing. ==> I customized my proxy endpoint of course
Cloudflare Docs
HTTP | Cloudflare Zero Trust docs
You can apply Gateway HTTP and DNS policies at the browser level by configuring a Proxy Auto-Configuration (PAC) file.
2 Replies
Hey. I'd suggest you ask in #workers-help or #workers-discussions
This is the right code below, if it can help someone else :
export default {
async fetch(request) {
const pac =
function FindProxyForURL(url, host) {
// No proxy for private (RFC 1918) IP addresses (intranet sites)
if (
isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") ||
isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") ||
isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0")
) {
return "DIRECT";
}
// No proxy for localhost
if (isInNet(dnsResolve(host), "127.0.0.0", "255.0.0.0")) {
return "DIRECT";
}
// Proxy all
return "HTTPS 3ele0ss56t.proxy.cloudflare-gateway.com:443";
}
;
return new Response(pac, {
headers: {
"content-type": "text/plain;charset=UTF-8",
},
});
},
};