ChatGPT代理

基于JS+DENO实现

main.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const OPENAI_API_HOST = "api.openai.com";

Deno.serve(async (request) => {
const url = new URL(request.url);
url.host = OPENAI_API_HOST;
url.protocol = 'https'
url.port = "443"

const newRequest = new Request(url.toString(), {
headers: request.headers,
method: request.method,
body: request.body,
redirect: "follow",
});
return await fetch(newRequest);
});

docker Deno

1
docker run -d -p 8000:8000 -v $PWD:/app denoland/deno:1.42.1 run --allow-net /app/main.ts

cloudflare worker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export default {
async fetch(request, env) {
try {
const OPENAI_API_HOST = "api.openai.com";
const oldUrl = new URL(request.url);``

if (oldUrl.pathname === "/") {
return new Response(`https://${oldUrl.hostname}/v1`, { status: 200 });
}

const newUrl = new URL(request.url);
newUrl.hostname = OPENAI_API_HOST;
// 处理请求体
let body = null;
if (request.method !== "GET" && request.method !== "HEAD") {
// 读取原始请求体并转换为buffer
body = await request.arrayBuffer();
}
const modifiedRequest = new Request(newUrl, {
method: request.method,
headers: request.headers,
body: body,
});

return await fetch(modifiedRequest);
} catch (e) {
return new Response(e.stack, { status: 500 });
}
},
};

相关文章

Windows10隐藏我的电脑相关文件夹

Sublime安装实时预览md文档插件

GitHub获取邮箱

win11设置win10右键

注册表删除wps云图标

mac用pip3安装django,django-admin:command not found问题

IDEA-Yapi下载版本