Spaces:
XCAPI
/
Sleeping

File size: 8,530 Bytes
aee6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <title>代理端点管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .endpoint-group {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        .endpoint {
            display: flex;
            align-items: center;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #eee;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        .endpoint input[type="text"] {
            flex: 1;
            margin-right: 10px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .endpoint input[type="number"] {
            width: 80px;
            margin: 0 10px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .endpoint input[type="checkbox"] {
            margin: 0 10px;
            transform: scale(1.2);
        }
        button {
            padding: 8px 15px;
            margin: 5px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
        }
        button:hover {
            background-color: #45a049;
        }
        button.delete {
            background-color: #f44336;
        }
        button.delete:hover {
            background-color: #da190b;
        }
        .status {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
            z-index: 1000;
        }
        .success {
            background-color: #4CAF50;
            color: white;
        }
        .error {
            background-color: #f44336;
            color: white;
        }
        h2 {
            color: #333;
            border-bottom: 2px solid #4CAF50;
            padding-bottom: 10px;
        }
        .controls {
            margin: 20px 0;
            padding: 10px;
            background-color: #f5f5f5;
            border-radius: 4px;
            text-align: right;
        }
        .stats-group {
            margin: 20px 0;
        }
        .stat-item {
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        .stat-item p {
            margin: 5px 0;
        }
        .blocked {
            background-color: #ffebee;
        }
    </style>
</head>
<body>
<h1>代理端点管理</h1>

<div class="endpoint-group">
    <h2>Models 端点</h2>
    <div id="modelsEndpoints"></div>
    <button onclick="addEndpoint('models')">添加 Models 端点</button>
</div>

<div class="endpoint-group">
    <h2>Chat 端点</h2>
    <div id="chatEndpoints"></div>
    <button onclick="addEndpoint('chat')">添加 Chat 端点</button>
</div>

<div class="stats-group">
    <h2>请求统计</h2>
    <div id="statsData"></div>
</div>

<div class="controls">
    <button onclick="saveConfig()">保存所有配置</button>
    <button onclick="logout()" style="background-color: #f44336;">退出登录</button>
</div>

<div id="status" class="status"></div>

<script>
    // 获取存储的密钥
    let apiKey = localStorage.getItem('apiKey');

    // 如果没有密钥,提示输入
    if (!apiKey) {
        apiKey = prompt('请输入访问密钥:');
        if (apiKey) {
            localStorage.setItem('apiKey', apiKey);
        } else {
            window.location.href = '/';
        }
    }

    // 显示状态信息
    function showStatus(message, isError = false) {
        const status = document.getElementById('status');
        status.textContent = message;
        status.className = 'status ' + (isError ? 'error' : 'success');
        status.style.display = 'block';
        setTimeout(() => status.style.display = 'none', 3000);
    }

    // 添加端点
    function addEndpoint(type, url = '', weight = 1, enabled = true) {
        const container = document.getElementById(type + 'Endpoints');
        const div = document.createElement('div');
        div.className = 'endpoint';
        div.innerHTML = `
                <input type="text" placeholder="输入端点URL" value="${url}">
                <input type="number" placeholder="权重" value="${weight}" min="1">
                <label>
                    <input type="checkbox" ${enabled ? 'checked' : ''}>
                    启用
                </label>
                <button class="delete" onclick="this.parentElement.remove()">删除</button>
            `;
        container.appendChild(div);
    }

    // 获取端点配置
    function getEndpointsConfig(type) {
        const endpoints = [];
        document.querySelectorAll(`#${type}Endpoints .endpoint`).forEach(el => {
            endpoints.push({
                url: el.querySelector('input[type="text"]').value.trim(),
                weight: parseInt(el.querySelector('input[type="number"]').value) || 1,
                enabled: el.querySelector('input[type="checkbox"]').checked
            });
        });
        return endpoints;
    }

    // 带认证的fetch
    async function fetchWithAuth(url, options = {}) {
        const headers = {
            ...options.headers,
            'Authorization': apiKey
        };

        const response = await fetch(url, { ...options, headers });

        if (response.status === 401) {
            localStorage.removeItem('apiKey');
            window.location.reload();
            return null;
        }

        return response;
    }

    // 保存配置
    async function saveConfig() {
        const config = {
            models: getEndpointsConfig('models'),
            chat: getEndpointsConfig('chat')
        };

        try {
            const response = await fetchWithAuth('/admin/config', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(config)
            });

            if (!response) return;

            if (response.ok) {
                showStatus('配置已保存');
                loadConfig(); // 重新加载配置以更新统计数据
            } else {
                showStatus('保存失败: ' + await response.text(), true);
            }
        } catch (error) {
            showStatus('保存失败: ' + error.message, true);
        }
    }

    // 加载配置和统计数据
    async function loadConfig() {
        try {
            const response = await fetchWithAuth('/admin/config');
            if (!response) return;

            const config = await response.json();

            // 清空现有配置
            document.getElementById('modelsEndpoints').innerHTML = '';
            document.getElementById('chatEndpoints').innerHTML = '';
            document.getElementById('statsData').innerHTML = '';

            // 加载Models端点
            config.models.forEach(ep => {
                addEndpoint('models', ep.url, ep.weight, ep.enabled);
            });

            // 加载Chat端点
            config.chat.forEach(ep => {
                addEndpoint('chat', ep.url, ep.weight, ep.enabled);
            });

            // 显示统计数据
            const statsHtml = Object.entries(config.stats).map(([fingerprint, stat]) => `
                    <div class="stat-item ${stat.blocked ? 'blocked' : ''}">
                        <p><strong>IP:</strong> ${stat.ip}</p>
                        <p><strong>请求次数:</strong> ${stat.chat_count}</p>
                        <p><strong>最后访问:</strong> ${new Date(stat.last_access).toLocaleString()}</p>
                        <p><strong>状态:</strong> ${stat.blocked ? '已拉黑' : '正常'}</p>
                    </div>
                `).join('');

            document.getElementById('statsData').innerHTML = statsHtml || '<p>暂无统计数据</p>';
        } catch (error) {
            showStatus('加载配置失败: ' + error.message, true);
        }
    }

    // 退出登录
    function logout() {
        localStorage.removeItem('apiKey');
        window.location.reload();
    }

    // 页面加载时获取配置
    document.addEventListener('DOMContentLoaded', loadConfig);

</script>
</body>
</html>