Recently, I worked on a project called ChatGPT and wanted to share it with my family. However, since I couldn't directly access the OpenAI API in China, I needed to use a proxy for the official interface.
I followed the method provided in chatgptProxyAPI.
Using Cloudflare Pages#
- Open GitHub and create a repository. You can fill in any repository name.
- Create a new file.
Name the file _worker.js
. If you want to proxy other addresses, you can directly modify TELEGRAPH_URL
.
The content is:
const TELEGRAPH_URL = 'https://api.openai.com';
export default {
async fetch(request, env) {
const NewResponse = await handleRequest(request)
return NewResponse
},
};
async function handleRequest(request) {
const url = new URL(request.url);
const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
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);
// Add response header to allow cross-origin access
modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
return modifiedResponse;
}
Then, commit the changes.
- Open Cloudflare and create a new pages, and connect it to your own Git.
- Choose the repository you just created.
- Use the default configuration (no need for any modifications, proceed to the next step).
- Wait for the deployment to complete, and return to the created pages page to see the address.
How to use:#
How to use in my project
- Open
https://power-chat.younglele.cn
. - Fill in
https://the domain address obtained above/v1
in the "OpenAI API Proxy" setting.
- Save to start accessing the OpenAI API without the need for a VPN.
There is another way to use Cloudflare Worker, but you need to have your own domain name and bind it to Cloudflare. It is still recommended to use the pages method.
Using Cloudflare Worker#
- Create an application in Workers and Pages.
- Click "Create Worker".
- Modify the name and click "Deploy" (modify the code after deployment).
- Click "Edit Code".
Paste the following code into the code editor, click "Save and Deploy".
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);
const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
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);
// Add response header to allow cross-origin access
modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
return modifiedResponse;
}
After successful deployment, return to the created workers page.
After doing this, you will find that it is still not working because workers.dev is still blocked. You need to bind a domain.
However, this custom domain currently only supports domains that are active on Cloudflare. Click on the left side Websites and follow the steps to add your own domain or purchase one on Cloudflare.