正值Deepseek R1大火之际,各个AI服务商纷纷提供了API,但是由于有些服务器做了区域限制(例如英伟达、Groq等),使用API需要挂梯子,比较麻烦,因而开始寻找相关的解决方案,刚好看到技术爬爬虾的Deno API代理项目,我也跟风在Deno上部署了一个,用来反代Groq的API,结果第二天就被封了,和Groq客服对线后,才知道原因是他们发现同一个IP地址上,有90+个API发起了请求,解封后,痛定思痛,准备部署到自己的VPS上,规避掉这个风险。

首先,确保VPS上部署了docker环境,以下安装命令供参考,具体的还需要大家网上搜一下

1
2
3
4
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

环境部署好之后,创建一个目录,例如目录名deno,在该目录下新建两个文件,分别叫做proxy.ts和Dockerfile,如下:

准备工作

打开Dockerfile,粘贴以下内容并保存

1
2
3
4
5
6
7
FROM denoland/deno:latest
WORKDIR /app
USER deno
COPY proxy.ts .
RUN deno cache proxy.ts
EXPOSE 8000
CMD ["deno", "run", "--allow-net", "proxy.ts"]

再打开proxy.ts,粘贴以为内容并保存(特此声明,此反代代码来自于技术爬爬虾,没有进行过更改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
async function handleRequest(request: Request): Promise<Response> {

const url = new URL(request.url);
const pathname = url.pathname;

if (pathname === '/' || pathname === '/index.html') {
return new Response('Proxy is Running!Details:https://github.com/tech-shrimp/deno-api-proxy', {
status: 200,
headers: { 'Content-Type': 'text/html' }
});
}

const targetUrl = `https://${pathname}`;

try {
const headers = new Headers();
const allowedHeaders = ['accept', 'content-type', 'authorization'];
for (const [key, value] of request.headers.entries()) {
if (allowedHeaders.includes(key.toLowerCase())) {
headers.set(key, value);
}
}

const response = await fetch(targetUrl, {
method: request.method,
headers: headers,
body: request.body
});

const responseHeaders = new Headers(response.headers);
responseHeaders.set('Referrer-Policy', 'no-referrer');

return new Response(response.body, {
status: response.status,
headers: responseHeaders
});

} catch (error) {
console.error('Failed to fetch:', error);
return new Response('Internal Server Error', { status: 500 });
}
};

Deno.serve(handleRequest);

还是在这个deno目录下,输入docker build -t deno-proxy .构建镜像,需要注意输入的时候,最后的.别落下了。

构建完成后,运行docker run -p 8000:8000 deno-proxy启动一个新的容器,使用curl http://localhost:8000/api.github.com/验证下服务,如果一切正常,你应该能看到来自GitHub响应。

最后,用nginx把http://localhost:8000反代出去就可以了,反代和证书申请这一块需要自己解决啦。

反代后的API使用方法也很简单,在你要使用的API之前加上这个反代域名就可以了,例如你的反代域名为https://proxyapi.com,API为https://api.groq.com/openai/v1,组合后就是https://proxyapi.com/api.groq.com/openai/v1,把这个填入到API的位置就可以啦。

最后,放一个硅基流动的带AFF的注册邀请链接,https://cloud.siliconflow.cn/i/gLkxqB1b

硅基流动邀请活动

没错,我就是来骗AFF的,按图上意思,注册后邀请方和被邀请方都会赠送2000万Tokens,但是也有朋友反馈不用AFF注册也送2000万Tokens,不喜欢AFF的直接点击这个链接注册即可https://cloud.siliconflow.cn/


番外篇:

技术爬爬虾的原版代码对于什么链接都可以反代,存在风险,对这一块有疑虑的朋友,我这里提供一个思路,在try块里边增加以下代码,就能给反代增加白名单功能,至于要不要这个功能,大家自由发挥。

1
2
3
4
5
const allowedHosts = ['api.groq.com', 'integrate.api.nvidia.com'];//白名单列表,自行增删
const host = new URL(targetUrl).hostname;
if (!allowedHosts.includes(host)) {
return new Response('Forbidden', { status: 403 });
}