-
-
Save dishengai/c0977cb75c466c3d9f382927da02986b to your computer and use it in GitHub Desktop.
Cloudflare worker,简单转发实现国内访问 ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment