Dnsflare - 简洁的Cloudflare域名解析面板

使用 Cloudflare 生成子API密钥进行登陆管理,其次是所有的API访问请求都是在本地浏览器执行的(对api.cloudflare.com的反代仅解决跨域问题)。

部署面板

Fork原作者的GitHub项目 Dnsflare, 使用CloudFlare Pages 部署

解决跨域

可以使用CloudFlare Workers对API进行反代解决跨域问题。源码中全局API路径位于 /src/utils/requests.ts 第4行 baseURL, 改为 https://反代域名/client/v4

 1async function handleRequest(request) {
 2    const { origin, pathname, searchParams } = new URL(request.url);
 3    const param = searchParams.toString() ? "?" + searchParams.toString() : "";
 4    const requestHeaders = request.headers.get("Access-Control-Request-Headers");
 5    const requestMethod = request.headers.get("Access-Control-Request-Method");
 6    const requestOrigin = request.headers.get("Origin") ? request.headers.get("Origin") : origin;
 7    
 8    // 响应预检请求
 9    if (request.method == "OPTIONS") {
10        return new Response('{"Access": "OPTIONS"}', {
11            status: 200,
12            headers: {
13                //"Access-Control-Allow-Origin": requestOrigin ? requestOrigin : "*", //不限制请求来源
14                "Access-Control-Allow-Origin" : "https://dns.moththe.com",      //限制CROS域名,改成你自己的
15                "Access-Control-Allow-Credentials": "true",
16                "Access-Control-Allow-Headers": requestHeaders ? requestHeaders : "*",
17                "Access-Control-Allow-Methods": requestMethod ? requestMethod : "*",
18                "Content-Type": "application/json"
19            }
20        });
21    }
22    
23    // 处理正式请求
24    const newRequest = new Request("https://api.cloudflare.com" + pathname + param, request);
25    // 请求头删除来源
26    newRequest.headers.set("referrer-policy", "no-referrer");
27    newRequest.headers.delete("Referer");
28    newRequest.headers.delete("Origin");
29    // 发起请求
30    const response = await fetch(newRequest);
31    const newResponse = new Response(response.body, response);
32    
33    // 处理响应
34    newResponse.headers.set("Access-Control-Allow-Origin", requestOrigin ? requestOrigin : "*");
35    newResponse.headers.set("Access-Control-Allow-Credentials", "true");
36    newResponse.headers.set("Access-Control-Allow-Headers", requestHeaders ? requestHeaders : "*");
37    newResponse.headers.set("Access-Control-Allow-Methods", requestMethod ? requestMethod : "*");
38    newResponse.headers.set('Access-Control-Expose-Headers', '*');
39    newResponse.headers.set("Content-Type", "application/json");
40    return newResponse;
41}
42addEventListener('fetch', event => {
43    event.respondWith(handleRequest(event.request));
44})

登录密钥

在 Cloudflare 生成 API密钥。

【我的个人资料】→【API令牌】→ 点击【创建令牌】→ 选择【编辑区域DNS】的模板

版权

评论