首先,确保VPS上部署了docker环境,以下安装命令供参考,具体的还需要大家网上搜一下
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
环境部署好之后,创建一个目录,例如目录名deno,在该目录下新建两个文件,分别叫做proxy.ts和Dockerfile,如下:
打开Dockerfile,粘贴以下内容并保存
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,粘贴以为内容并保存(特此声明,此反代代码来自于技术爬爬虾,没有进行过更改)
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块里边增加以下代码,就能给反代增加白名单功能,至于要不要这个功能,大家自由发挥。
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 });
}