-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (97 loc) · 3 KB
/
Copy pathserver.js
File metadata and controls
106 lines (97 loc) · 3 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
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const cors = require('cors');
const kiroProxy = require('./kiro-proxy');
const app = express();
const PORT = process.env.PORT || 3000;
// 启用CORS
app.use(cors());
// Kiro 反向代理与管理路由 (仅对它们应用 express.json() 中间件以避免干扰其他代理的 raw stream 转发)
app.use('/kiro', express.json(), kiroProxy.router);
app.use('/admin', express.json(), kiroProxy.adminRouter);
// 健康检查端点
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// OpenAI API 代理
app.use('/v1', createProxyMiddleware({
target: 'https://api.openai.com',
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
// 转发Authorization头
if (req.headers.authorization) {
proxyReq.setHeader('Authorization', req.headers.authorization);
}
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({
error: 'Proxy error',
message: err.message
});
}
}));
// Anthropic API 代理
app.use('/anthropic', createProxyMiddleware({
target: 'https://api.anthropic.com',
changeOrigin: true,
pathRewrite: {
'^/anthropic': '' // 移除/anthropic前缀
},
onProxyReq: (proxyReq, req, res) => {
if (req.headers['x-api-key']) {
proxyReq.setHeader('x-api-key', req.headers['x-api-key']);
}
if (req.headers['anthropic-version']) {
proxyReq.setHeader('anthropic-version', req.headers['anthropic-version']);
}
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({
error: 'Proxy error',
message: err.message
});
}
}));
// Google AI (Gemini) 代理
app.use('/gemini', createProxyMiddleware({
target: 'https://generativelanguage.googleapis.com',
changeOrigin: true,
pathRewrite: {
'^/gemini': ''
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({
error: 'Proxy error',
message: err.message
});
}
}));
// 根路径信息
app.get('/', (req, res) => {
res.json({
name: 'AI API Proxy',
version: '2.0.0-vps-kiro',
endpoints: {
openai: '/v1/*',
anthropic: '/anthropic/*',
gemini: '/gemini/*',
kiro: '/kiro/v1/*',
admin: '/admin/*',
health: '/health'
},
usage: {
openai: 'Use /v1/chat/completions with your OpenAI API key in Authorization header',
anthropic: 'Use /anthropic/v1/messages with your API key in x-api-key header',
gemini: 'Use /gemini/v1beta/models/* with your API key as query parameter',
kiro: 'Use /kiro/v1/chat/completions with compatible models (gpt-4o, claude-3-5-sonnet)',
admin: 'Use admin endpoints with your ADMIN_KEY in Authorization Bearer'
}
});
});
app.listen(PORT, () => {
console.log(`🚀 AI API Proxy server running on port ${PORT}`);
console.log(`📊 Health check: http://localhost:${PORT}/health`);
});