Cloudflare为同一域名下的不同ip列表提供不同的页面



这就是我要做的

我想在mysite.com上有一个维护页面,但我仍然想为我们的内部组织提供应用程序

我能够实现为我的组织提供mysite.com的方式是通过区域锁定,但我不确定如何接近外部世界,因为他们没有得到任何东西

看看这个。我正在用它来实现你提到的完全相同的事情。我还为其他可能需要它们的人提供了一些额外的选项。

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// IPs allowed to bypass the maintenance page
const white_list = [
'1.1.1.1',   // MyIP1
'2.2.2.2',   // MyIP2
'3.3.3.3'    // MyIP3
];
// Custom IP for flexibility of having a custom response
// E.g. using automation to verify that the page is always under maintenance 
// Obviously it applies only if you're have scheduled maintenance. In your case, you can safely ignore this.
const custom_whitelist = "4.4.4.4" // MyCustomIP
async function handleRequest(request) {
//return fetch(request)     //Uncomment this and comment below to allow everyone to access (to disable the maintenance page)
return maintenance(request) 
}
async function maintenance(request) {
let modifiedHeaders = new Headers()
modifiedHeaders.set('Content-Type', 'text/html')
modifiedHeaders.append('Pragma', 'no-cache')
//Check requester IP and compare with whitelist, allow access if match.
if (white_list.indexOf(request.headers.get("cf-connecting-ip")) > -1) {
return fetch(request)
}
//Else, return maintenance page if IP is not whitelisted
else {
var current_ip = ""
current_ip = request.headers.get("cf-connecting-ip")
// Customise headers or other response properties for a custom IP. 
// E.g. if using a powershell script to check if 100+ domains are properly set to maintenance, 
// you would want the status to remain 200 instead of 503, which would result in a false negative.
// Can be removed if not needed.
if (current_ip == custom_whitelist){
return new Response(maintenanceContent, {
headers: modifiedHeaders
})
}
else {
// Return modified response. HTTP Code 503 is used for maintenance pages.
return new Response(maintenanceContent, {
status: 503,
headers: modifiedHeaders
})
}
}
}
// Place your maintenance page content in HTML below.
let maintenanceContent = `
<!doctype html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
`;

最新更新