Coder Social home page Coder Social logo

gentsuncheng / worker-openai-proxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hahahumble/worker-openai-proxy

0.0 0.0 0.0 4 KB

☁️ 通过 Cloudflare Workers 代理 OpenAI API,实现内地正常访问。

Home Page: https://api.speechgpt.app

JavaScript 100.00%

worker-openai-proxy's Introduction

☁️ Cloudflare Workers OpenAI 代理

🚀 使用

注意

  • 该代理只供 speechgpt.app 使用。
  • 不保证稳定性,不保证长期可用,长期使用建议自行部署。

🛠️ 部署

Deploy to Cloudflare Workers

🔍 原理

该方案的原理是通过 Cloudflare Workers 代理 OpenAI API,配合自己可以正常访问的域名,实现在内地正常访问 OpenAI API。

🎁 捐赠

如果你觉得这个项目对你有帮助,可以考虑捐赠,谢谢!

📚 参考

本项目基于以下项目修改而来:

📝 说明

如何限制只有自己的域名可以访问?

const OPENAI_URL = 'https://api.openai.com';

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

async function handleRequest(request) {
  const origin = request.headers.get('Origin');
  const allowedOrigin = '<允许访问的域名>';

  if (origin === allowedOrigin) {
    const url = new URL(request.url);
    url.host = OPENAI_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, {
      status: response.status,
      statusText: response.statusText,
      headers: response.headers
    });

    modifiedResponse.headers.set('Access-Control-Allow-Origin', allowedOrigin);

    return modifiedResponse;
  } else {
    return new Response('Unauthorized', { status: 403 });
  }
}

<允许访问的域名> 替换为你自己的域名即可。

worker-openai-proxy's People

Contributors

hahahumble avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.