[开源]轻磅更新Cloudflare SpectreProxy API代理优化版+部署指南

:tieba_003: 先贴一下仓库地址 GitHub - XyzenSun/SpectreProxy: 支持HTTP,Websocket,DoH/DoT协议,支持AI API代理,支持流式传输,使用原生 TCP Socket 解决 fetch API 的隐私泄露问题并提供灵活的回退策略解决TCP Socket 目前无法连接到 Cloudflare 自身的 IP 段的问题的Cloudflare Worker代理程序。
原理方面大家可以看Gayhub或者看我的上一个帖子 https://linux.do/t/topic/832937/76
这几天爆肝了一下,疯狂的踩坑和寻找白嫖之道,搞出来个 API代理优化版 ,因为之前的支持的协议有点多,全是socket套接字编程写的想鼠,于是重新搞个分支版本,专门用来代理AI API,做了一些优化,自动重试,智能路由等,当然这些都不重要,反正这个版本用来代理API更好用就对辣,下面简单说一下部署指南:
也可以参考https://linux.do/t/topic/836702 佬的教程,顺便偷一下佬的图

第一步:创建并部署 Worker

  1. 准备环境:

    • 确保您拥有一个 Cloudflare 账号,并已激活 Workers 服务。
  2. 创建 Worker:

    • 登录 Cloudflare 控制面板,进入 计算 (Workers)Workers 和 Pages
    • 点击 创建应用程序创建 Worker
    • 保持默认的 Hello World! 模板,直接点击 部署
  3. 编辑代码:

    • 部署成功后,点击 编辑代码 进入在线编辑器。
    • 清空编辑器:将编辑器中现有的 Hello World 代码全部删除。
    • 粘贴新代码:打开以下链接,复制其中所有内容,并粘贴到编辑器中。

第二步:修改核心配置

在编辑器中,找到以下几个关键配置项并根据需求进行修改。

1. 全局配置 (DEFAULT_CONFIG)

// 全局配置
const DEFAULT_CONFIG = {
  // ⬇️ 必须修改,设置访问密码
  AUTH_TOKEN: "your-secret-password", 

  // ⬇️ 可选修改,默认目标 URL,这个网站不是很稳定,可以随便选个其他网站,是为了伪装+未认证流量转发
  DEFAULT_DST_URL: "https://httpbin.org/get",

  // ⬇️ 建议保持 true,开启后可查看详细日志,方便排错
  DEBUG_MODE: true,

  // ⬇️ 建议保持 true,增强隐私保护
  ENABLE_UA_RANDOMIZATION: true,

  // ⬇️ 可选开启,如果需要更强的随机性,设为 true
  ENABLE_ACCEPT_LANGUAGE_RANDOMIZATION: false,

  // ⬇️ 建议保持 true,在直连失败时自动使用 SOCKS5 代理
  ENABLE_SOCKS5_FALLBACK: true, 
};

2. SOCKS5 代理源 (SOCKS5_API_URLS)

这里需要填入自己的 SOCKS5 代理 API 地址。当 Worker 需要 SOCKS5 代理时,会从这些地址中随机选择一个来获取代理信息。
下面的这个方法不完全,不够傻瓜式,后续我再整个傻瓜式一点的,docker启动!由于已经燃尽了,需要等一等,大佬们有精力的话可以先整一下,目前这个方案算是给号很多的用的,过几天会再优化下代码,可以不搭建socks api,直接填到代码里或者读环境变量,或者填txt文件,更加的简单。此外,今天折腾了很久的SNI PROXY,也就是cf反代IP法,但是遇到了很多奇怪的问题:tieba_087: 有没有佬可以救一救

获得稳定的 SOCKS5 代理源

  1. 原理: 将如机场订阅转换为 SOCKS5 格式。
  2. 工具: 使用 aggregator 等项目,它可以自动抓取,获得免费机场的订阅。
  3. 转换: YouTube 视频教程:将机场订阅转换为socks5
// Socks5 API
const SOCKS5_API_URLS = [
  // ⬇️ 将下面的 URL 替换为您自己搭建的 SOCKS5 代理 API
  "https://my-proxy-api.com/get-socks5",
  "https://another-proxy-api.com/socks5",
];

3. 智能路由规则 (HOST_REQUEST_CONFIG)

此配置决定了访问特定域名时,是直接连接 (nativeFetch) 还是强制使用 SOCKS5 代理 (socks5)。

配置技巧

  • 对于 使用了 Cloudflare 网络 的目标 API(如 api.openai.com),直接将其设置为 socks5。这样可以避免因 Worker 无法直连 Cloudflare IP 而触发回退,从而提升请求效率。
  • 使用 ITDog TCP Ping 工具检测目标域名,如果 IP 信息显示 Anycast/cloudflare.com,则表明其使用了 Cloudflare。
// 主机请求方式配置集合 (key: host, value: 'nativeFetch' | 'socks5')
const HOST_REQUEST_CONFIG = new Map([
  // ⬇️ OpenAI 使用了 Cloudflare,强制设为 'socks5',这样不需要从nativeFetch回退到socks5,提升性能
  ["api.openai.com", "socks5"],

  // ⬇️ Google 没使用cloudflare,设置成nativeFetch,可以不定义,不定义的默认就是nativeFetch
  ["generativelanguage.googleapis.com", "nativeFetch"],

  // ⬇️ Anthropic 使用了 Cloudflare,强制设为 'socks5',与openai同理
  ["api.anthropic.com", "socks5"],

  ["api.cohere.ai", "nativeFetch"],
  ["httpbin.org", "nativeFetch"],
]);

如何使用 (常见平台示例)

部署完成后,用就很简单了。基本用法是在 原始 API 地址前 加上 https://<你的Worker域名>/<你的AUTH_TOKEN>/

URL 格式: https://<你的Worker域名>/<你的AUTH_TOKEN>/<原始API地址>

URL 预设格式: https://<你的Worker域名>/<你的AUTH_TOKEN>/<预设别名> (更推荐)

1. New-API / Cherry-Studio

  • 渠道 OpenAI: 在 API 地址栏填入 https://your-worker.workers.dev/your-token/https://api.openai.com
  • 渠道 Gemini: 在 API 地址栏填入 https://your-worker.workers.dev/your-token/https://generativelanguage.googleapis.com

简化技巧:可以直接使用 URL 预设,如 OpenAI 渠道地址填 https://your-worker.workers.dev/your-token/openai

2. Gemini Balance

  • 在主界面找到 API 基础 URL 设置。
  • 将其从 https://generativelanguage.googleapis.com/v1beta
  • 修改为 https://your-worker.workers.dev/your-token/https://generativelanguage.googleapis.com/v1beta

3. GPT Load

  • 将 API 地址修改为 https://your-worker.workers.dev/your-token/上有地址
52 个赞

太强了,大佬

太强了佬,支持支持

厉害了,马上尝试

期待socket方案 能解决很多问题 :grinning_face_with_smiling_eyes:

好东西,学到了订阅转代理池的方法 :dog_face:

刚想用就更新了,期待 SOCKS5 代理源的简便解决方案。

佬,我按照教程配置后,在 gpt-load 上测试 gemini,始终返回[status 400] Invalid target URL. Protocol (http/https) is required.,预设格式和原始地址都尝试过了 :smiling_face_with_tear:

佬,我私信你我搭建好的,佬你试试,对照下格式

1 个赞

好的,谢谢!

这个socket 5的 方式有点讨厌了,等佬加油搞个简单的。之前帖子的方案好像是不行了。

:tieba_087:之前那个也是可以的,只要有一种回退源就可以,这个版本是对api代理进行了更多的适配,性能优化,自动重试,回退只保留了socks5,以前打算用反代IP解决socket的限制的,但是我搞了几天没搞定,于是把回退方法只保留socks5,因为socks5有很多渠道可以搞到很多免费的

支持一下

更新这么快

嘿嘿 写了个CORS

可以代理各种https:// 不限于API

:tieba_020:美汁汁 :lark_030:

好强欸w

又在l站发现新大佬的厉害项目

支持大佬

请问佬,为啥会出现这结果?

我的设置跟上面的都一样,只差在 ENABLE_SOCKS5_FALLBACK: false

这是 Cherry Studio 中的检测结果:

@xyzen

佬 请问一下用gemini的话是不是可以不设置socks5了?