-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-enhanced.js
More file actions
398 lines (335 loc) · 10.6 KB
/
Copy pathworker-enhanced.js
File metadata and controls
398 lines (335 loc) · 10.6 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Cloudflare Workers AI API 代理 - 增强版
// 支持 OpenAI, Anthropic, Google Gemini
// 新增:账号池管理、Token 自动刷新、负载均衡
// 账号管理相关接口
interface Account {
id: string;
email: string;
provider: 'openai' | 'anthropic' | 'gemini';
accessToken: string;
refreshToken?: string;
expiresAt?: number;
enabled: boolean;
lastUsed?: number;
createdAt: number;
}
interface AccountStats {
totalRequests: number;
successRequests: number;
failedRequests: number;
lastError?: string;
}
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const path = url.pathname;
// CORS 预检请求
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*',
}
});
}
// 管理接口 - 需要管理员密钥
if (path.startsWith('/admin')) {
return handleAdminRequest(request, env, path);
}
// 健康检查
if (path === '/health') {
return jsonResponse({
status: 'ok',
timestamp: new Date().toISOString(),
version: '2.0.0-enhanced'
});
}
// 根路径信息
if (path === '/') {
return jsonResponse({
name: 'AI API Proxy (Enhanced)',
version: '2.0.0',
features: [
'Multi-account management',
'Auto token refresh',
'Load balancing',
'Account rotation'
],
endpoints: {
openai: '/v1/*',
anthropic: '/anthropic/*',
gemini: '/gemini/*',
health: '/health',
admin: '/admin/* (requires auth)'
}
});
}
// API 代理请求
return handleProxyRequest(request, env, path, url);
}
};
// 处理管理接口
async function handleAdminRequest(request, env, path) {
// 验证管理员密钥
const authHeader = request.headers.get('Authorization');
const adminKey = env.ADMIN_KEY || 'admin-secret-key';
if (!authHeader || authHeader !== `Bearer ${adminKey}`) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
const method = request.method;
// GET /admin/accounts - 列出所有账号
if (path === '/admin/accounts' && method === 'GET') {
const accounts = await listAccounts(env);
return jsonResponse(accounts);
}
// POST /admin/accounts - 添加账号
if (path === '/admin/accounts' && method === 'POST') {
const account = await request.json();
const result = await addAccount(env, account);
return jsonResponse(result);
}
// PUT /admin/accounts/:id - 更新账号
if (path.startsWith('/admin/accounts/') && method === 'PUT') {
const id = path.split('/')[3];
const updates = await request.json();
const result = await updateAccount(env, id, updates);
return jsonResponse(result);
}
// DELETE /admin/accounts/:id - 删除账号
if (path.startsWith('/admin/accounts/') && method === 'DELETE') {
const id = path.split('/')[3];
const result = await deleteAccount(env, id);
return jsonResponse(result);
}
// GET /admin/stats - 获取统计信息
if (path === '/admin/stats' && method === 'GET') {
const stats = await getStats(env);
return jsonResponse(stats);
}
// POST /admin/refresh/:id - 手动刷新 Token
if (path.startsWith('/admin/refresh/') && method === 'POST') {
const id = path.split('/')[3];
const result = await refreshAccountToken(env, id);
return jsonResponse(result);
}
return jsonResponse({ error: 'Not found' }, 404);
}
// 处理代理请求
async function handleProxyRequest(request, env, path, url) {
// 确定目标服务
let provider = null;
let targetUrl = '';
let newPath = path;
if (path.startsWith('/v1')) {
provider = 'openai';
targetUrl = 'https://api.openai.com';
} else if (path.startsWith('/anthropic')) {
provider = 'anthropic';
targetUrl = 'https://api.anthropic.com';
newPath = path.replace('/anthropic', '');
} else if (path.startsWith('/gemini')) {
provider = 'gemini';
targetUrl = 'https://generativelanguage.googleapis.com';
newPath = path.replace('/gemini', '');
} else {
return jsonResponse({ error: 'Invalid endpoint' }, 404);
}
// 获取可用账号
const account = await getAvailableAccount(env, provider);
if (!account) {
return jsonResponse({
error: 'No available accounts',
message: 'All accounts are unavailable or disabled'
}, 503);
}
// 检查 Token 是否即将过期
if (account.expiresAt && account.expiresAt < Date.now() + 300000) {
// 5分钟内过期,尝试刷新
await refreshAccountToken(env, account.id);
// 重新获取账号信息
const refreshedAccount = await getAccount(env, account.id);
if (refreshedAccount) {
Object.assign(account, refreshedAccount);
}
}
// 构建代理请求
const proxyUrl = `${targetUrl}${newPath}${url.search}`;
const headers = new Headers(request.headers);
// 设置认证头
if (provider === 'openai') {
headers.set('Authorization', `Bearer ${account.accessToken}`);
} else if (provider === 'anthropic') {
headers.set('x-api-key', account.accessToken);
}
headers.set('Host', new URL(targetUrl).host);
headers.delete('cf-connecting-ip');
headers.delete('cf-ray');
try {
const proxyRequest = new Request(proxyUrl, {
method: request.method,
headers: headers,
body: request.body
});
const response = await fetch(proxyRequest);
// 记录请求统计
await recordRequest(env, account.id, response.ok);
// 更新最后使用时间
await updateAccountLastUsed(env, account.id);
const proxyResponse = new Response(response.body, response);
proxyResponse.headers.set('Access-Control-Allow-Origin', '*');
proxyResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
proxyResponse.headers.set('Access-Control-Allow-Headers', '*');
return proxyResponse;
} catch (error) {
await recordRequest(env, account.id, false, error.message);
return jsonResponse({
error: 'Proxy error',
message: error.message
}, 500);
}
}
// 账号管理函数
async function listAccounts(env) {
const { keys } = await env.ACCOUNTS.list();
const accounts = [];
for (const key of keys) {
const data = await env.ACCOUNTS.get(key.name, 'json');
if (data) {
// 不返回敏感信息
const { accessToken, refreshToken, ...safeData } = data;
accounts.push(safeData);
}
}
return accounts;
}
async function addAccount(env, account) {
const id = account.id || crypto.randomUUID();
const newAccount = {
...account,
id,
enabled: account.enabled !== false,
createdAt: Date.now()
};
await env.ACCOUNTS.put(id, JSON.stringify(newAccount));
const { accessToken, refreshToken, ...safeData } = newAccount;
return { success: true, account: safeData };
}
async function updateAccount(env, id, updates) {
const existing = await env.ACCOUNTS.get(id, 'json');
if (!existing) {
return { success: false, error: 'Account not found' };
}
const updated = { ...existing, ...updates };
await env.ACCOUNTS.put(id, JSON.stringify(updated));
const { accessToken, refreshToken, ...safeData } = updated;
return { success: true, account: safeData };
}
async function deleteAccount(env, id) {
await env.ACCOUNTS.delete(id);
await env.STATS.delete(`stats:${id}`);
return { success: true };
}
async function getAccount(env, id) {
return await env.ACCOUNTS.get(id, 'json');
}
// 获取可用账号(负载均衡)
async function getAvailableAccount(env, provider) {
const { keys } = await env.ACCOUNTS.list();
const candidates = [];
for (const key of keys) {
const account = await env.ACCOUNTS.get(key.name, 'json');
if (account && account.enabled && account.provider === provider) {
// 检查 Token 是否有效
if (!account.expiresAt || account.expiresAt > Date.now()) {
candidates.push(account);
}
}
}
if (candidates.length === 0) {
return null;
}
// 轮询:选择最少使用的账号
candidates.sort((a, b) => (a.lastUsed || 0) - (b.lastUsed || 0));
return candidates[0];
}
// Token 刷新(示例实现,需要根据实际 API 调整)
async function refreshAccountToken(env, id) {
const account = await getAccount(env, id);
if (!account || !account.refreshToken) {
return { success: false, error: 'Cannot refresh token' };
}
try {
// 这里需要根据具体服务商的刷新接口实现
// 示例:OpenAI 的刷新逻辑
let refreshUrl = '';
let body = {};
if (account.provider === 'openai') {
// OpenAI 刷新逻辑(需要实际API)
refreshUrl = 'https://auth.openai.com/oauth/token';
body = {
grant_type: 'refresh_token',
refresh_token: account.refreshToken
};
}
// 实际刷新请求
// const response = await fetch(refreshUrl, { ... });
// const data = await response.json();
// 更新账号信息
// await updateAccount(env, id, {
// accessToken: data.access_token,
// expiresAt: Date.now() + data.expires_in * 1000
// });
return { success: true, message: 'Token refresh not implemented yet' };
} catch (error) {
return { success: false, error: error.message };
}
}
// 记录请求统计
async function recordRequest(env, accountId, success, error = null) {
const key = `stats:${accountId}`;
const stats = await env.STATS.get(key, 'json') || {
totalRequests: 0,
successRequests: 0,
failedRequests: 0
};
stats.totalRequests++;
if (success) {
stats.successRequests++;
} else {
stats.failedRequests++;
stats.lastError = error;
}
await env.STATS.put(key, JSON.stringify(stats));
}
// 更新最后使用时间
async function updateAccountLastUsed(env, id) {
const account = await getAccount(env, id);
if (account) {
account.lastUsed = Date.now();
await env.ACCOUNTS.put(id, JSON.stringify(account));
}
}
// 获取统计信息
async function getStats(env) {
const { keys } = await env.STATS.list();
const allStats = {};
for (const key of keys) {
const accountId = key.name.replace('stats:', '');
const stats = await env.STATS.get(key.name, 'json');
if (stats) {
allStats[accountId] = stats;
}
}
return allStats;
}
// 辅助函数
function jsonResponse(data, status = 200) {
return new Response(JSON.stringify(data, null, 2), {
status,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
}