Cloudflare 的 Workers

用 Cloudflare 的 Workers 来代理 OpenAI 的 API 地址,配合自己的域名即可在境内实现访问OpenAI。
因为 Cloudflare Workers 有每天免费 10 万次的请求额度,也有可以免费注册的域名,所以几乎可以说是零成本。而且该方法理论上支持所有被认证的网站,而不只是 OpenAI。

使用这个方案需要你有以下东西:

  • 一个没有被 GFW 认证的域名
  • 一个 Cloudflare 账号

具体步骤

openai proxy

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
const TELEGRAPH_URL = 'https://api.openai.com';

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url);
url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');

const modifiedRequest = new Request(url.toString(), {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});

const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);

// 添加允许跨域访问的响应头
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');

return modifiedResponse;
}

Advanced version

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
addEventListener("fetch", (event) => {
event.respondWith(fetchAndApply(event.request));
});

async function fetchAndApply(request) {
let response = null;
let method = request.method;

let url = new URL(request.url);
let url_hostname = url.hostname;
url.protocol = "https:";
url.host = "api.openai.com";

let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set("Host", url.host);
new_request_headers.set("Referer", url.protocol + "//" + url_hostname);

let original_response = await fetch(url.href, {
method: method,
headers: new_request_headers,
body: request.body,
});

let original_response_clone = original_response.clone();
let original_text = null;
let response_headers = original_response.headers;
let new_response_headers = new Headers(response_headers);
let status = original_response.status;

new_response_headers.set("Cache-Control", "no-store");
new_response_headers.set("access-control-allow-origin", "*");
new_response_headers.set("access-control-allow-credentials", true);
new_response_headers.delete("content-security-policy");
new_response_headers.delete("content-security-policy-report-only");
new_response_headers.delete("clear-site-data");

original_text = original_response_clone.body;
response = new Response(original_text, {
status,
headers: new_response_headers,
});

return response;
}

Nginx as Proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
http {
#... (other configuration settings) ...

server {
listen 80;
server_name example.com; # Replace this with your domain name

location /openai/ {
proxy_pass https://api.openai.com/;
proxy_set_header Host api.openai.com;
proxy_set_header Authorization "Bearer YOUR_OPENAI_API_KEY"; # Replace with your actual OpenAI API key
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

Caddy as Proxy

1
2
3
4
5
6
7
8
9
10
11
example.com { # Replace this with your domain name
route /openai/* {
uri strip_prefix /openai
reverse_proxy https://api.openai.com {
header_up Host api.openai.com
header_up Authorization "Bearer {env.OPENAI_API_KEY}"
header_up -Origin
header_down -Server
}
}
}