Update uptime.config.ts

支持跨域访问
pull/160/head
XUZHONGXIN 2025-09-05 14:32:23 +08:00 committed by GitHub
parent 3284a45307
commit e091cbecac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 56 additions and 2 deletions

View File

@ -247,5 +247,59 @@ const maintenances: MaintenanceConfig[] = [
},
]
// Don't forget this, otherwise compilation fails.
export { pageConfig, workerConfig, maintenances }
export default {
async fetch(request, env, ctx) {
const targetDomain = "http://se-xl-services.xinnew.top"; // 你的目标域名
const url = new URL(request.url);
// 处理预检请求
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: corsHeaders()
});
}
// 构造目标 URL
const path = url.pathname + url.search;
const proxyUrl = targetDomain + path;
try {
const res = await fetch(proxyUrl, {
method: request.method,
headers: request.headers,
body: request.method !== "GET" && request.method !== "HEAD" ? request.body : undefined,
redirect: "follow"
});
// 复制原始响应头
const headers = new Headers(res.headers);
// 添加 CORS
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods", "GET,HEAD,POST,PUT,DELETE,OPTIONS");
headers.set("Access-Control-Allow-Headers", "*");
// 返回响应
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers
});
} catch (err) {
return new Response(`Error fetching ${proxyUrl}: ${err.message}`, {
status: 500,
headers: corsHeaders()
});
}
}
};
function corsHeaders() {
return {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,POST,PUT,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "*"
};
}