-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
616 lines (530 loc) · 19.3 KB
/
Copy pathadmin.html
File metadata and controls
616 lines (530 loc) · 19.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI API 代理 - 管理面板</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
background: white;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header h1 {
color: #667eea;
margin-bottom: 10px;
}
.header p {
color: #666;
}
.card {
background: white;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.card h2 {
color: #333;
margin-bottom: 15px;
border-bottom: 2px solid #667eea;
padding-bottom: 10px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
color: #333;
font-weight: 500;
}
.form-group input,
.form-group select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 14px;
}
.form-group input:focus,
.form-group select:focus {
outline: none;
border-color: #667eea;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5568d3;
}
.btn-danger {
background: #f56565;
color: white;
}
.btn-danger:hover {
background: #e53e3e;
}
.btn-success {
background: #48bb78;
color: white;
}
.btn-success:hover {
background: #38a169;
}
.account-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 15px;
margin-top: 20px;
}
.account-item {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 15px;
position: relative;
}
.account-item.enabled {
border-left: 4px solid #48bb78;
}
.account-item.disabled {
border-left: 4px solid #f56565;
opacity: 0.7;
}
.account-item h3 {
color: #333;
margin-bottom: 10px;
font-size: 16px;
}
.account-info {
font-size: 13px;
color: #666;
margin-bottom: 5px;
}
.account-actions {
margin-top: 10px;
display: flex;
gap: 10px;
}
.status-badge {
display: inline-block;
padding: 3px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: 500;
}
.status-badge.active {
background: #c6f6d5;
color: #22543d;
}
.status-badge.inactive {
background: #fed7d7;
color: #742a2a;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 20px;
}
.stat-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 8px;
text-align: center;
}
.stat-card h3 {
font-size: 32px;
margin-bottom: 5px;
}
.stat-card p {
font-size: 14px;
opacity: 0.9;
}
.alert {
padding: 12px;
border-radius: 5px;
margin-bottom: 15px;
display: none;
}
.alert.success {
background: #c6f6d5;
color: #22543d;
border: 1px solid #9ae6b4;
}
.alert.error {
background: #fed7d7;
color: #742a2a;
border: 1px solid #fc8181;
}
.alert.show {
display: block;
}
.loading {
text-align: center;
padding: 40px;
color: #666;
}
.tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.tab {
padding: 10px 20px;
background: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
transition: all 0.3s;
}
.tab.active {
background: #667eea;
color: white;
}
.tab:hover:not(.active) {
background: #f7fafc;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🚀 AI API 代理管理面板</h1>
<p>多账号管理 | Token 自动刷新 | 负载均衡</p>
</div>
<div class="alert" id="alert"></div>
<div class="tabs">
<button class="tab active" onclick="switchTab('dashboard')">仪表盘</button>
<button class="tab" onclick="switchTab('accounts')">账号管理</button>
<button class="tab" onclick="switchTab('add')">添加账号</button>
<button class="tab" onclick="switchTab('settings')">设置</button>
</div>
<!-- 仪表盘 -->
<div id="dashboard" class="tab-content active">
<div class="card">
<h2>📊 统计概览</h2>
<div class="stats-grid">
<div class="stat-card">
<h3 id="totalAccounts">0</h3>
<p>总账号数</p>
</div>
<div class="stat-card">
<h3 id="activeAccounts">0</h3>
<p>活跃账号</p>
</div>
<div class="stat-card">
<h3 id="totalRequests">0</h3>
<p>总请求数</p>
</div>
<div class="stat-card">
<h3 id="successRate">0%</h3>
<p>成功率</p>
</div>
</div>
</div>
</div>
<!-- 账号管理 -->
<div id="accounts" class="tab-content">
<div class="card">
<h2>📋 账号列表</h2>
<div id="accountList" class="account-list">
<div class="loading">正在加载...</div>
</div>
</div>
</div>
<!-- 添加账号 -->
<div id="add" class="tab-content">
<div class="card">
<h2>➕ 添加新账号</h2>
<form id="addAccountForm">
<div class="form-group">
<label>服务商</label>
<select id="provider" required>
<option value="openai">OpenAI</option>
<option value="anthropic">Anthropic (Claude)</option>
<option value="gemini">Google Gemini</option>
</select>
</div>
<div class="form-group">
<label>邮箱</label>
<input type="email" id="email" required placeholder="account@example.com">
</div>
<div class="form-group">
<label>Access Token</label>
<input type="text" id="accessToken" required placeholder="sk-...">
</div>
<div class="form-group">
<label>Refresh Token (可选)</label>
<input type="text" id="refreshToken" placeholder="用于自动刷新">
</div>
<div class="form-group">
<label>
<input type="checkbox" id="enabled" checked>
启用此账号
</label>
</div>
<button type="submit" class="btn btn-primary">添加账号</button>
</form>
</div>
</div>
<!-- 设置 -->
<div id="settings" class="tab-content">
<div class="card">
<h2>⚙️ 系统设置</h2>
<div class="form-group">
<label>管理员密钥</label>
<input type="password" id="adminKey" placeholder="用于 API 调用认证">
</div>
<div class="form-group">
<label>Worker 地址</label>
<input type="text" id="workerUrl" placeholder="https://your-worker.workers.dev">
</div>
<button class="btn btn-success" onclick="saveSettings()">保存设置</button>
</div>
<div class="card">
<h2>📖 使用说明</h2>
<div style="line-height: 1.8; color: #666;">
<p><strong>1. 配置管理员密钥:</strong>在 Cloudflare Workers 环境变量中设置 <code>ADMIN_KEY</code></p>
<p><strong>2. 添加账号:</strong>填写服务商、邮箱和 Token 信息</p>
<p><strong>3. 使用代理:</strong>将 API 请求发送到 Worker 地址的对应端点</p>
<p><strong>示例:</strong></p>
<pre style="background: #f7fafc; padding: 10px; border-radius: 5px; overflow-x: auto;">
# OpenAI
curl https://your-worker.workers.dev/v1/chat/completions
# Anthropic
curl https://your-worker.workers.dev/anthropic/v1/messages
# Gemini
curl https://your-worker.workers.dev/gemini/v1beta/models</pre>
</div>
</div>
</div>
</div>
<script>
// 全局配置
let config = {
adminKey: localStorage.getItem('adminKey') || '',
workerUrl: localStorage.getItem('workerUrl') || window.location.origin
};
// 初始化
document.addEventListener('DOMContentLoaded', () => {
loadSettings();
loadAccounts();
loadStats();
// 定期刷新统计
setInterval(() => {
loadAccounts();
loadStats();
}, 30000);
});
// 切换标签页
function switchTab(tabName) {
document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
event.target.classList.add('active');
document.getElementById(tabName).classList.add('active');
}
// 加载设置
function loadSettings() {
document.getElementById('adminKey').value = config.adminKey;
document.getElementById('workerUrl').value = config.workerUrl;
}
// 保存设置
function saveSettings() {
config.adminKey = document.getElementById('adminKey').value;
config.workerUrl = document.getElementById('workerUrl').value;
localStorage.setItem('adminKey', config.adminKey);
localStorage.setItem('workerUrl', config.workerUrl);
showAlert('设置已保存', 'success');
}
// API 请求辅助函数
async function apiRequest(endpoint, options = {}) {
const url = `${config.workerUrl}${endpoint}`;
const headers = {
'Authorization': `Bearer ${config.adminKey}`,
'Content-Type': 'application/json',
...options.headers
};
try {
const response = await fetch(url, { ...options, headers });
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || '请求失败');
}
return data;
} catch (error) {
showAlert(error.message, 'error');
throw error;
}
}
// 加载账号列表
async function loadAccounts() {
try {
const accounts = await apiRequest('/admin/accounts');
displayAccounts(accounts);
} catch (error) {
document.getElementById('accountList').innerHTML =
'<div class="loading">加载失败,请检查管理员密钥和 Worker 地址</div>';
}
}
// 显示账号列表
function displayAccounts(accounts) {
const container = document.getElementById('accountList');
if (accounts.length === 0) {
container.innerHTML = '<div class="loading">暂无账号,请添加</div>';
return;
}
container.innerHTML = accounts.map(account => `
<div class="account-item ${account.enabled ? 'enabled' : 'disabled'}">
<h3>${account.email}</h3>
<div class="account-info">
<span class="status-badge ${account.enabled ? 'active' : 'inactive'}">
${account.enabled ? '✓ 已启用' : '✗ 已禁用'}
</span>
</div>
<div class="account-info">服务商:${account.provider}</div>
<div class="account-info">ID:${account.id}</div>
${account.lastUsed ? `<div class="account-info">最后使用:${new Date(account.lastUsed).toLocaleString()}</div>` : ''}
<div class="account-actions">
<button class="btn btn-success" onclick="toggleAccount('${account.id}', ${!account.enabled})">
${account.enabled ? '禁用' : '启用'}
</button>
<button class="btn btn-primary" onclick="refreshToken('${account.id}')">刷新Token</button>
<button class="btn btn-danger" onclick="deleteAccount('${account.id}')">删除</button>
</div>
</div>
`).join('');
}
// 加载统计信息
async function loadStats() {
try {
const [accounts, stats] = await Promise.all([
apiRequest('/admin/accounts'),
apiRequest('/admin/stats')
]);
const totalAccounts = accounts.length;
const activeAccounts = accounts.filter(a => a.enabled).length;
let totalRequests = 0;
let successRequests = 0;
Object.values(stats).forEach(s => {
totalRequests += s.totalRequests || 0;
successRequests += s.successRequests || 0;
});
const successRate = totalRequests > 0
? Math.round(successRequests / totalRequests * 100)
: 0;
document.getElementById('totalAccounts').textContent = totalAccounts;
document.getElementById('activeAccounts').textContent = activeAccounts;
document.getElementById('totalRequests').textContent = totalRequests;
document.getElementById('successRate').textContent = successRate + '%';
} catch (error) {
// 忽略统计加载错误
}
}
// 添加账号
document.getElementById('addAccountForm').addEventListener('submit', async (e) => {
e.preventDefault();
const account = {
provider: document.getElementById('provider').value,
email: document.getElementById('email').value,
accessToken: document.getElementById('accessToken').value,
refreshToken: document.getElementById('refreshToken').value || undefined,
enabled: document.getElementById('enabled').checked
};
try {
await apiRequest('/admin/accounts', {
method: 'POST',
body: JSON.stringify(account)
});
showAlert('账号添加成功', 'success');
e.target.reset();
switchTab('accounts');
loadAccounts();
loadStats();
} catch (error) {
// Error already shown
}
});
// 切换账号状态
async function toggleAccount(id, enabled) {
try {
await apiRequest(`/admin/accounts/${id}`, {
method: 'PUT',
body: JSON.stringify({ enabled })
});
showAlert('账号状态已更新', 'success');
loadAccounts();
loadStats();
} catch (error) {
// Error already shown
}
}
// 刷新 Token
async function refreshToken(id) {
try {
await apiRequest(`/admin/refresh/${id}`, { method: 'POST' });
showAlert('Token 刷新成功', 'success');
loadAccounts();
} catch (error) {
// Error already shown
}
}
// 删除账号
async function deleteAccount(id) {
if (!confirm('确定要删除此账号吗?')) return;
try {
await apiRequest(`/admin/accounts/${id}`, { method: 'DELETE' });
showAlert('账号已删除', 'success');
loadAccounts();
loadStats();
} catch (error) {
// Error already shown
}
}
// 显示提示
function showAlert(message, type) {
const alert = document.getElementById('alert');
alert.textContent = message;
alert.className = `alert ${type} show`;
setTimeout(() => {
alert.classList.remove('show');
}, 3000);
}
</script>
</body>
</html>