Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Latest commit

 

History

History
98 lines (65 loc) · 2.46 KB

cloudflare_proxy_pages.md

File metadata and controls

98 lines (65 loc) · 2.46 KB

由于Workers的域名已经被污染了。在CloudFlare Workers自定义域名的方案之后,我来和大家一起在CloudFlare Pages利用其的Functions部署中转,作为一种备用方法。

打开Github,新建一个仓库

pages1 pages1

点击creating a new file按钮

  1. 创建一个文件名为_worker.js的文件,然后复制以下代码
export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    url.host = "api.openai.com";
    // openai is already set all CORS heasders 
    return fetch(url, {
      headers: request.headers,
      method: request.method,
      body: request.body,
      redirect: 'follow'
    });
  }
}

pages1 pages1

  1. 然后点Commit new file保存

pages1

扩展

如果想中转别的网站把下面代码中的TELEGRAPH_URL换为别网站就行

代码
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);
  // 添加允许跨域访问的响应头
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

部署

  1. 登录到Cloudflare控制台.

  2. 在帐户主页中,选择pages> Create a project > Connect to Git Cloudflare_pages1.png

  3. 选择你 Fork 的项目存储库,在Set up builds and deployments部分中,选择None作为您的框架预设。其他为空。(即保持默认即可)

  4. 点击Save and Deploy部署

然后点Continue to project即可看到访问域名

Cloudflare_pages6.png

用法

参考