使用CF Workers和Pages搭建节点及GPT中转服务

使用 Workers 构建无服务器功能。使用 Pages 部署网站和全栈应用程序
一、Workers:
1、官网注册登录,左侧菜单点击"Workers和Pages"——创建Workers

2、部署 "Hello World" 脚本,部署后点击编辑worker代码

3、替换代码,https://github.com/3Kmfi6HP/EDtunnel/blob/main/_worker.js

4、替换代码中userID为你的uuid,保存并部署后打开https://vless.xxx.workers.dev/{uuid}

绑定域名:左侧菜单点击"网站-添加站点"绑定自有域名,打开部署好的一个workers点击"触发器-添加自定义域"

workers地址如:https://github.com/3Kmfi6HP/EDtunnel/blob/main/_worker.js

二、Pages:使用git库或者直接上传,等待部署后点击链接访问站点

三、Workers GPT中转:

// @ts-nocheck
addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  // 构建 GPT API 的 URL
  const gptApiUrl = 'https://api.openai.com/v1/chat/completions';
  // 转发本地请求到 GPT API
  const gptApiResponse = await fetch(gptApiUrl, {
    method: request.method,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer sess-Vye45UwA1UKHDwpksE76wtGhneBK9Gn6bHccxxxx',
    },
    body: request.body, // 传递本地请求的 body 数据
  });

  // 从 GPT API 的响应创建新的 Response 对象
  const response = new Response(gptApiResponse.body, {
    status: gptApiResponse.status,
    statusText: gptApiResponse.statusText,
    headers: {
      'Content-Type': 'application/json',
      // 允许跨域访问,根据需要设置其他 CORS 头
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
    },
  });

  return response;
}

  

posted @ 2023-10-24 15:07  鱿鱼须须  阅读(1405)  评论(0编辑  收藏  举报