feat(proxy): 初始化代理服务器项目
Browse files- 添加 Dockerfile 用于容器化部署
- 配置 package.json 定义项目依赖与启动脚本
- 实现基于 Express 的 HTTP 代理服务器
- 集成 CORS 支持跨域请求处理
- 配置代理中间件转发请求到 anyrouter.top
- 添加请求日志与错误处理机制
- 支持通过环境变量配置端口
- 实现开发环境与生产环境的启动命令
- Dockerfile +13 -0
- package.json +21 -0
- proxy-server.js +38 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM node:18-alpine
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY package*.json ./
|
6 |
+
|
7 |
+
RUN npm ci --only=production
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
EXPOSE 80 443
|
12 |
+
|
13 |
+
CMD ["npm", "start"]
|
package.json
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "anyrouter-proxy",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"description": "Proxy server for anyrouter.top",
|
5 |
+
"main": "proxy-server.js",
|
6 |
+
"scripts": {
|
7 |
+
"start": "node proxy-server.js",
|
8 |
+
"dev": "nodemon proxy-server.js"
|
9 |
+
},
|
10 |
+
"dependencies": {
|
11 |
+
"express": "^4.18.2",
|
12 |
+
"http-proxy-middleware": "^2.0.6",
|
13 |
+
"cors": "^2.8.5"
|
14 |
+
},
|
15 |
+
"devDependencies": {
|
16 |
+
"nodemon": "^3.0.1"
|
17 |
+
},
|
18 |
+
"keywords": ["proxy", "anyrouter", "express"],
|
19 |
+
"author": "",
|
20 |
+
"license": "MIT"
|
21 |
+
}
|
proxy-server.js
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
3 |
+
const cors = require('cors');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const PORT = process.env.PORT || 80;
|
7 |
+
|
8 |
+
app.use(cors({
|
9 |
+
origin: '*',
|
10 |
+
credentials: true,
|
11 |
+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
12 |
+
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
|
13 |
+
}));
|
14 |
+
|
15 |
+
const proxyOptions = {
|
16 |
+
target: 'https://anyrouter.top',
|
17 |
+
changeOrigin: true,
|
18 |
+
secure: true,
|
19 |
+
followRedirects: true,
|
20 |
+
headers: {
|
21 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
22 |
+
},
|
23 |
+
onProxyReq: (proxyReq, req, res) => {
|
24 |
+
console.log(`Proxying: ${req.method} ${req.url} -> https://anyrouter.top${req.url}`);
|
25 |
+
},
|
26 |
+
onError: (err, req, res) => {
|
27 |
+
console.error('Proxy Error:', err.message);
|
28 |
+
res.status(500).json({ error: 'Proxy request failed', message: err.message });
|
29 |
+
}
|
30 |
+
};
|
31 |
+
|
32 |
+
app.use('/', createProxyMiddleware(proxyOptions));
|
33 |
+
|
34 |
+
app.listen(PORT, '0.0.0.0', () => {
|
35 |
+
console.log(`Proxy server running on port ${PORT}`);
|
36 |
+
console.log(`Proxying requests to: https://anyrouter.top`);
|
37 |
+
console.log(`Access locally at: http://localhost:${PORT}`);
|
38 |
+
});
|