Giter VIP home page Giter VIP logo

openai-proxy's Introduction

OpenAI-Proxy

Cloudflare Worker

创建Cloudflare Worker服务->HTTP路由器,快速编辑->配置如下

// 是否拒绝所有无 Origin 请求
const ALLOW_NO_ORIGIN = false;
// 允许的Origin列表
const ALLOWED_ORIGIN = [/^https?:\/\/.*\.example\.com$/, /^https?:\/\/test\.com$/];
const validateOrigin = (req) => {
  const origin = req.headers.get('Origin')
  if (origin) {
    for (let i = 0; i < ALLOWED_ORIGIN.length; i++) {
      if (ALLOWED_ORIGIN[i].exec(origin)) {
        return true
      }
    }
  }
  return ALLOW_NO_ORIGIN
}
export default {
  async fetch(request) {
    if (validateOrigin(request)) {
      const requestUrl = new URL(request.url)
      requestUrl.host = "api.openai.com"
      request = new Request(requestUrl, request)
      //如需前端发送API Key,注释掉下一行
      request.headers.set('Authorization', 'Bearer sk-your-token')
      return await fetch(request)
    } else {
      return new Response('[CloudFlare Workers] REQUEST NOT ALLOWED', {status: 403});
    }
  }
}

默认配置允许源域名是*.example.com或test.com的请求正常反代,修改ALLOWED_ORIGIN更改允许源域名列表

如修改ALLOW_NO_ORIGIN为true,则不检查源域名,所有请求都正常反代。

注意:默认worker域名国内被干扰,点击触发器->添加自定义域,添加CF里正常使用的域名访问worker

openai-proxy's People

Contributors

xqdoo00o 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.