Skip to content

Commit 2cb5e1f

Browse files
committed
增加 cloudflare.worker.js 文件,懂的都懂
1 parent 5ab117b commit 2cb5e1f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

cloudflare.worker.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
感谢@赖嘉伟Gary 提供 Worker 代码
3+
4+
用途:
5+
使用此方式,不需要购买海外服务器,替换 /class/Class.ChatGPT.php 中的 api.openai.com 域名后,直接将本项目开源代码部署到国内服务器即可使用。
6+
7+
使用方法:
8+
1、在 CloudFlare 添加一个域名,注意只能填一级域名,不能填字域名,所以可以新注册一个便宜域名专门做这个事情;
9+
2、添加域名,同时 在域名注册商处把域名的 dns 设为 CloudFlare 的 dns 地址;
10+
3、等待域名生效,大概需要十几分钟到一个小时不等;
11+
4、添加一个 Worker ,把以下代码复制到进去;
12+
5、点击 Worker 的 Trigger 界面,设置自定义域名,选择第一步添加的域名;
13+
6、等全部生效后,就可以用你的自定义域名替换 /class/Class.ChatGPT.php 中的 api.openai.com 域名。
14+
15+
*/
16+
async function handleRequest(request) {
17+
const url = new URL(request.url)
18+
const fetchAPI = request.url.replace(url.host, 'api.openai.com')
19+
20+
// 添加跨域处理
21+
const corsHeaders = {
22+
'Access-Control-Allow-Origin': '*',
23+
'Access-Control-Allow-Methods': 'OPTIONS',
24+
'Access-Control-Allow-Headers': '*',
25+
};
26+
if (request.method === 'OPTIONS') return new Response(null, { headers: corsHeaders });
27+
28+
return fetch(fetchAPI, { headers: request.headers, method: request.method, body: request.body })
29+
}
30+
31+
addEventListener("fetch", (event) => {
32+
event.respondWith(handleRequest(event.request))
33+
})

0 commit comments

Comments
 (0)