-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server.js
More file actions
250 lines (220 loc) · 7.88 KB
/
Copy pathproxy_server.js
File metadata and controls
250 lines (220 loc) · 7.88 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* ============================================
* MathMate API 代理服务器
* ============================================
* 用途:代理 Flutter Web 应用的 AI API 请求
* 隐藏真实 API Key,提供安全隔离
*
* 运行:node proxy_server.js
* PM2 守护:pm2 start proxy_server.js --name mathmate-api
* ============================================
*/
const express = require('express');
const http = require('http');
const https = require('https');
const url = require('url');
// ============================================
// 配置
// ============================================
const CONFIG = {
PORT: process.env.PORT || 3001,
// 从环境变量读取真实 API Key
DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY || '',
DEEPSEEK_API_URL: process.env.DEEPSEEK_API_URL || 'https://api.deepseek.com/v1',
VOLC_API_KEY: process.env.VOLC_API_KEY || '',
VOLC_API_URL: process.env.VOLC_API_URL || 'https://ark.cn-beijing.volces.com/api/v3',
QWEN_API_KEY: process.env.QWEN_API_KEY || '',
QWEN_API_URL: process.env.QWEN_API_URL || 'https://dashscope.aliyuncs.com/api/v1',
};
// ============================================
// 创建 Express 应用
// ============================================
const app = express();
// 中间件
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// CORS 头
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Request-ID');
res.header('Access-Control-Expose-Headers', 'X-Request-ID');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// 请求日志
app.use((req, res, next) => {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${req.method} ${req.path}`);
next();
});
// ============================================
// 健康检查
// ============================================
app.get('/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
services: {
deepseek: !!CONFIG.DEEPSEEK_API_KEY,
volc: !!CONFIG.VOLC_API_KEY,
qwen: !!CONFIG.QWEN_API_KEY,
}
});
});
// ============================================
// DeepSeek API 代理
// ============================================
app.all('/api/deepseek/*', (req, res) => {
const targetPath = req.path.replace('/api/deepseek', '');
const targetUrl = `${CONFIG.DEEPSEEK_API_URL}${targetPath}`;
proxyRequest(req, res, targetUrl, {
'Authorization': `Bearer ${CONFIG.DEEPSEEK_API_KEY}`,
'Content-Type': 'application/json',
});
});
// ============================================
// 火山引擎 API 代理
// ============================================
app.all('/api/volc/*', (req, res) => {
const targetPath = req.path.replace('/api/volc', '');
const targetUrl = `${CONFIG.VOLC_API_URL}${targetPath}`;
proxyRequest(req, res, targetUrl, {
'Authorization': `Bearer ${CONFIG.VOLC_API_KEY}`,
'Content-Type': 'application/json',
});
});
// ============================================
// Qwen API 代理(支持 SSE)
// ============================================
app.all('/api/qwen/*', async (req, res) => {
const targetPath = req.path.replace('/api/qwen', '');
const targetUrl = `${CONFIG.QWEN_API_URL}${targetPath}`;
// SSE 流式响应处理
if (req.body && req.body.stream === true) {
proxySSE(req, res, targetUrl, {
'Authorization': `Bearer ${CONFIG.QWEN_API_KEY}`,
'Content-Type': 'application/json',
});
} else {
proxyRequest(req, res, targetUrl, {
'Authorization': `Bearer ${CONFIG.QWEN_API_KEY}`,
'Content-Type': 'application/json',
});
}
});
// ============================================
// 通用代理函数
// ============================================
function proxyRequest(req, res, targetUrl, headers) {
const parsedUrl = url.parse(targetUrl);
const options = {
hostname: parsedUrl.hostname,
port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
path: parsedUrl.path,
method: req.method,
headers: {
...headers,
...(req.body ? { 'Content-Length': Buffer.byteLength(JSON.stringify(req.body)) } : {}),
},
};
const proxyReq = (parsedUrl.protocol === 'https:' ? https : http).request(options, (proxyRes) => {
// 复制响应头
Object.keys(proxyRes.headers).forEach(key => {
res.setHeader(key, proxyRes.headers[key]);
});
// 管道响应
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error('代理请求错误:', err);
res.status(500).json({ error: '代理请求失败', message: err.message });
});
// 发送请求体
if (req.body) {
proxyReq.write(JSON.stringify(req.body));
}
proxyReq.end();
}
// ============================================
// SSE 流式代理(用于 Qwen)
// ============================================
async function proxySSE(req, res, targetUrl, headers) {
try {
const response = await fetch(targetUrl, {
method: req.method,
headers: headers,
body: JSON.stringify(req.body),
});
// 设置 SSE 响应头
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no'); // 禁用 Nginx 缓冲
// 管道 SSE 数据流
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
res.write(chunk);
}
res.end();
} catch (error) {
console.error('SSE 代理错误:', error);
res.status(500).json({ error: 'SSE 代理失败', message: error.message });
}
}
// ============================================
// 404 处理
// ============================================
app.use((req, res) => {
res.status(404).json({
error: '未找到请求的 API 端点',
path: req.path,
available_endpoints: ['/api/deepseek/*', '/api/volc/*', '/api/qwen/*', '/health']
});
});
// ============================================
// 错误处理
// ============================================
app.use((err, req, res, next) => {
console.error('服务器错误:', err);
res.status(500).json({
error: '服务器内部错误',
message: err.message
});
});
// ============================================
// 启动服务器
// ============================================
const server = app.listen(CONFIG.PORT, () => {
console.log('==========================================');
console.log('MathMate API 代理服务器');
console.log('运行中:', `http://127.0.0.1:${CONFIG.PORT}`);
console.log('配置的 API 端点:');
console.log(` - DeepSeek: /api/deepseek/*`);
console.log(` - 火山引擎: /api/volc/*`);
console.log(` - Qwen: /api/qwen/*`);
console.log('==========================================');
});
// 优雅关闭
process.on('SIGTERM', () => {
console.log('收到 SIGTERM 信号,关闭服务器...');
server.close(() => {
console.log('服务器已关闭');
process.exit(0);
});
});
process.on('SIGINT', () => {
console.log('\n收到 SIGINT 信号,关闭服务器...');
server.close(() => {
console.log('服务器已关闭');
process.exit(0);
});
});
module.exports = app;