File size: 11,519 Bytes
c298b8e
 
 
 
 
 
 
f4e0a06
c298b8e
 
f4e0a06
c298b8e
f4e0a06
 
 
 
 
 
 
 
c298b8e
 
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
 
f4e0a06
c298b8e
 
f4e0a06
 
c298b8e
f4e0a06
c298b8e
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
f4e0a06
c298b8e
f4e0a06
c298b8e
f4e0a06
 
 
 
 
c298b8e
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
 
 
f4e0a06
 
 
 
 
 
 
c298b8e
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
 
 
 
f4e0a06
 
c298b8e
 
 
 
 
 
 
f4e0a06
c298b8e
 
 
 
 
 
 
 
 
 
 
 
 
 
f4e0a06
c298b8e
 
 
 
 
f4e0a06
 
c298b8e
 
 
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
 
f4e0a06
c298b8e
f4e0a06
 
 
 
c298b8e
 
 
 
 
 
f4e0a06
 
c298b8e
 
 
 
 
f4e0a06
 
 
c298b8e
f4e0a06
 
c298b8e
 
 
 
f4e0a06
c298b8e
f4e0a06
c298b8e
 
f4e0a06
c298b8e
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
f4e0a06
 
 
c298b8e
f4e0a06
 
 
 
 
c298b8e
f4e0a06
c298b8e
 
f4e0a06
 
 
 
 
 
 
 
 
 
 
 
 
 
c298b8e
 
 
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
<!DOCTYPE html>
<html>
<head>
    <title>API Keys Management</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        #loginForm, #mainContent {
            margin: 20px 0;
        }
        
        .actions {
            margin: 20px 0;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 4px;
        }
        
        .key-list {
            margin-top: 20px;
        }
        
        .key-item {
            display: flex;
            align-items: center;
            padding: 12px;
            border: 1px solid #ddd;
            margin: 5px 0;
            border-radius: 4px;
            background-color: white;
        }
        
        .key-text {
            flex-grow: 1;
            margin: 0 10px;
            font-family: monospace;
        }
        
        .key-status {
            margin: 0 10px;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.9em;
        }
        
        .status-valid {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-invalid {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        .status-cooling {
            background-color: #fff3cd;
            color: #856404;
        }
        
        button {
            padding: 8px 16px;
            margin: 0 5px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            background-color: #007bff;
            color: white;
        }
        
        button:hover {
            background-color: #0056b3;
        }
        
        button.delete {
            background-color: #dc3545;
        }
        
        button.delete:hover {
            background-color: #c82333;
        }
        
        input[type="text"], input[type="password"] {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 300px;
        }
        
        .loading {
            opacity: 0.5;
            pointer-events: none;
        }
        
        .error {
            color: #dc3545;
            margin: 10px 0;
        }
        
        .success {
            color: #28a745;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>API Keys Management</h1>
        
        <!-- 登录表单 -->
        <div id="loginForm">
            <input type="password" id="password" placeholder="Enter admin password">
            <button onclick="login()">Login</button>
        </div>

        <!-- 主要内容 -->
        <div id="mainContent" style="display: none;">
            <!-- 添加新key -->
            <div class="actions">
                <input type="text" id="newKey" placeholder="Enter new API key">
                <button onclick="addKey()">Add Key</button>
            </div>
            
            <!-- 批量操作 -->
            <div class="actions">
                <button onclick="checkAllKeys()">Check All Keys</button>
                <button onclick="deleteSelected()" class="delete">Delete Selected</button>
                <button onclick="selectAll()" id="selectAllBtn">Select All</button>
            </div>
            
            <!-- 统计信息 -->
            <div id="stats" class="actions">
                Loading stats...
            </div>
            
            <!-- Key列表 -->
            <div id="keyList" class="key-list">
                Loading keys...
            </div>
        </div>
    </div>

    <script>
        let adminPassword = '';
        let allSelected = false;
        
        // 登录
        async function login() {
            const password = document.getElementById('password').value;
            try {
                const response = await fetch('/api/admin/login', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({password})
                });
                
                if (response.ok) {
                    adminPassword = password;
                    document.getElementById('loginForm').style.display = 'none';
                    document.getElementById('mainContent').style.display = 'block';
                    loadKeys();
                } else {
                    alert('Invalid password');
                }
            } catch (error) {
                alert('Login failed');
            }
        }
        
        // 加载keys
        async function loadKeys() {
            try {
                const response = await fetch(`/api/keys?password=${encodeURIComponent(adminPassword)}`);
                const data = await response.json();
                updateKeyList(data.keys);
                updateStats(data.keys);
            } catch (error) {
                alert('Error loading keys');
            }
        }
        
        // 更新key列表
        function updateKeyList(keys) {
            const keyList = document.getElementById('keyList');
            keyList.innerHTML = '';
            
            keys.forEach(keyInfo => {
                const keyItem = document.createElement('div');
                keyItem.className = 'key-item';
                
                const statusClass = `status-${keyInfo.status}`;
                const coolingInfo = keyInfo.cooling_until ? 
                    `(Cooling until ${new Date(keyInfo.cooling_until).toLocaleString()})` : '';
                
                keyItem.innerHTML = `
                    <input type="checkbox" value="${keyInfo.key}">
                    <div class="key-text">${keyInfo.key}</div>
                    <div class="key-status ${statusClass}">
                        ${keyInfo.status.toUpperCase()} ${coolingInfo}
                    </div>
                    <button onclick="checkKey('${keyInfo.key}')">Check</button>
                    <button onclick="deleteKey('${keyInfo.key}')" class="delete">Delete</button>
                `;
                
                keyList.appendChild(keyItem);
            });
        }
        
        // 更新统计信息
        function updateStats(keys) {
            const stats = document.getElementById('stats');
            const total = keys.length;
            const valid = keys.filter(k => k.status === 'valid').length;
            const cooling = keys.filter(k => k.status === 'cooling').length;
            const invalid = keys.filter(k => k.status === 'invalid').length;
            
            stats.innerHTML = `
                Total Keys: ${total} | 
                Valid: ${valid} | 
                Cooling: ${cooling} | 
                Invalid: ${invalid}
            `;
        }
        
        // 添加新key
        async function addKey() {
            const key = document.getElementById('newKey').value.trim();
            if (!key) return;
            
            try {
                const response = await fetch('/api/keys/add', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({password: adminPassword, key})
                });
                
                if (response.ok) {
                    document.getElementById('newKey').value = '';
                    loadKeys();
                } else {
                    const data = await response.json();
                    alert(data.detail || 'Error adding key');
                }
            } catch (error) {
                alert('Error adding key');
            }
        }
        
        // 检查所有keys
        async function checkAllKeys() {
            try {
                const response = await fetch(`/api/keys/check-all?password=${encodeURIComponent(adminPassword)}`, {
                    method: 'POST'
                });
                
                if (response.ok) {
                    loadKeys();
                    alert('All keys checked');
                } else {
                    alert('Error checking keys');
                }
            } catch (error) {
                alert('Error checking keys');
            }
        }
        
        // 删除选中的keys
        async function deleteSelected() {
            const selected = Array.from(document.querySelectorAll('input[type="checkbox"]:checked'))
                .map(cb => cb.value);
                
            if (!selected.length) return;
            if (!confirm(`Delete ${selected.length} selected keys?`)) return;
            
            try {
                const response = await fetch('/api/keys/delete-batch', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({
                        password: adminPassword,
                        keys: selected
                    })
                });
                
                if (response.ok) {
                    loadKeys();
                } else {
                    alert('Error deleting keys');
                }
            } catch (error) {
                alert('Error deleting keys');
            }
        }
        
        // 全选/取消全选
        function selectAll() {
            allSelected = !allSelected;
            document.querySelectorAll('input[type="checkbox"]')
                .forEach(cb => cb.checked = allSelected);
            document.getElementById('selectAllBtn').textContent = 
                allSelected ? 'Deselect All' : 'Select All';
        }
        
        // 检查单个key
        async function checkKey(key) {
            try {
                const response = await fetch(`/api/keys/check/${key}?password=${encodeURIComponent(adminPassword)}`);
                if (response.ok) {
                    loadKeys();
                } else {
                    alert('Error checking key');
                }
            } catch (error) {
                alert('Error checking key');
            }
        }
        
        // 删除单个key
        async function deleteKey(key) {
            if (!confirm('Delete this key?')) return;
            
            try {
                const response = await fetch(`/api/keys/${key}?password=${encodeURIComponent(adminPassword)}`, {
                    method: 'DELETE'
                });
                
                if (response.ok) {
                    loadKeys();
                } else {
                    alert('Error deleting key');
                }
            } catch (error) {
                alert('Error deleting key');
            }
        }
        
        // 按Enter键登录
        document.getElementById('password').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                login();
            }
        });
        
        // 按Enter键添加key
        document.getElementById('newKey').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                addKey();
            }
        });
    </script>
</body>
</html>