先贴一下仓库地址 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
-
准备环境:
- 确保您拥有一个 Cloudflare 账号,并已激活 Workers 服务。
-
创建 Worker:
- 登录 Cloudflare 控制面板,进入
计算 (Workers)→Workers 和 Pages。 - 点击
创建应用程序→创建 Worker。 - 保持默认的
Hello World!模板,直接点击 部署。
- 登录 Cloudflare 控制面板,进入
-
编辑代码:
- 部署成功后,点击
编辑代码进入在线编辑器。 - 清空编辑器:将编辑器中现有的
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法,但是遇到了很多奇怪的问题
有没有佬可以救一救
获得稳定的 SOCKS5 代理源
- 原理: 将如机场订阅转换为 SOCKS5 格式。
- 工具: 使用 aggregator 等项目,它可以自动抓取,获得免费机场的订阅。
- 转换: 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/上有地址



