mirror of
https://github.com/Websoft9/websoft9.git
synced 2025-02-02 17:08:38 +08:00
Update cloudflare.js
This commit is contained in:
parent
e1083fd3d8
commit
56d11ce4b5
@ -2,19 +2,60 @@ addEventListener('fetch', event => {
|
|||||||
event.respondWith(handleRequest(event.request))
|
event.respondWith(handleRequest(event.request))
|
||||||
})
|
})
|
||||||
|
|
||||||
// This data source from: https://raw.githubusercontent.com/Websoft9/doc.websoft9.com/refs/heads/main/docs/reference/_include/dockerhub-proxy.md
|
var backends = [];
|
||||||
const backends = []
|
|
||||||
async function handleRequest(request) {
|
|
||||||
// 随机选择一个后端服务器
|
|
||||||
const backend = backends[Math.floor(Math.random() * backends.length)]
|
|
||||||
|
|
||||||
// 构建新的请求 URL
|
async function checkHealth() {
|
||||||
const url = new URL(request.url)
|
const healthChecks = backends.map(async url => {
|
||||||
url.hostname = new URL(backend).hostname
|
const start = Date.now();
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, { method: 'HEAD' });
|
||||||
|
const end = Date.now();
|
||||||
|
return {
|
||||||
|
url,
|
||||||
|
healthy: response.ok,
|
||||||
|
responseTime: response.ok ? end - start : Infinity
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
url,
|
||||||
|
healthy: false,
|
||||||
|
responseTime: Infinity
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 转发请求到选定的后端服务器
|
return await Promise.all(healthChecks);
|
||||||
const modifiedRequest = new Request(url, request)
|
|
||||||
const response = await fetch(modifiedRequest)
|
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleRequest(request) {
|
||||||
|
// 检查健康状态
|
||||||
|
const healthResults = await checkHealth();
|
||||||
|
|
||||||
|
// 过滤出健康的后端服务器
|
||||||
|
const healthyBackends = healthResults.filter(result => result.healthy);
|
||||||
|
|
||||||
|
if (healthyBackends.length === 0) {
|
||||||
|
return new Response('All backend servers are down', { status: 503 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按响应时间排序健康的后端服务器
|
||||||
|
healthyBackends.sort((a, b) => a.responseTime - b.responseTime);
|
||||||
|
|
||||||
|
// 尝试按顺序转发请求到健康的后端服务器
|
||||||
|
for (const backend of healthyBackends) {
|
||||||
|
try {
|
||||||
|
const url = new URL(request.url);
|
||||||
|
url.hostname = new URL(backend.url).hostname;
|
||||||
|
const modifiedRequest = new Request(url, request);
|
||||||
|
const response = await fetch(modifiedRequest);
|
||||||
|
if (response.ok) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to fetch from ${backend.url}: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果所有后端服务器都失败
|
||||||
|
return new Response('Failed to fetch from all backends', { status: 502 });
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user