Spaces:
Running
Running
File size: 11,924 Bytes
b88ce1b 6fdc899 b88ce1b 6fdc899 b88ce1b 7a7d699 b88ce1b |
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 |
import express from 'express';
import multer from 'multer';
import archiver from 'archiver';
import { createKey, loadKeys, deleteKey, updateKeyRateLimit, getKeyStats } from './key_manager.js';
import { getRecentLogs, clearLogs, addLog } from './log_manager.js';
import { getSystemStatus, incrementRequestCount, getTodayRequestCount } from './monitor.js';
import { loadAccounts, deleteAccount, toggleAccount, triggerLogin, getAccountStats, addTokenFromCallback, getAccountName, importTokens } from './token_admin.js';
import { createSession, validateSession, destroySession, verifyPassword, adminAuth } from './session.js';
import { loadSettings, saveSettings } from './settings_manager.js';
import tokenManager from '../auth/token_manager.js';
// 配置文件上传
const upload = multer({ dest: 'uploads/' });
const router = express.Router();
// 登录接口(不需要认证)
router.post('/login', async (req, res) => {
try {
const { password } = req.body;
if (!password) {
return res.status(400).json({ error: '请输入密码' });
}
if (verifyPassword(password)) {
const token = createSession();
await addLog('info', '管理员登录成功');
res.json({ success: true, token });
} else {
await addLog('warn', '管理员登录失败:密码错误');
res.status(401).json({ error: '密码错误' });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 登出接口
router.post('/logout', (req, res) => {
const token = req.headers['x-admin-token'];
if (token) {
destroySession(token);
}
res.json({ success: true });
});
// 验证会话接口
router.get('/verify', (req, res) => {
const token = req.headers['x-admin-token'];
if (validateSession(token)) {
res.json({ valid: true });
} else {
res.status(401).json({ valid: false });
}
});
// 以下所有路由需要认证
router.use(adminAuth);
// 生成新密钥
router.post('/keys/generate', async (req, res) => {
try {
const { name, rateLimit, key } = req.body;
const newKey = await createKey(name, rateLimit, key);
await addLog('success', `密钥已生成: ${name || '未命名'}`);
res.json({ success: true, key: newKey.key, name: newKey.name, rateLimit: newKey.rateLimit });
} catch (error) {
await addLog('error', `生成密钥失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 获取所有密钥
router.get('/keys', async (req, res) => {
try {
const keys = await loadKeys();
// 返回密钥列表(隐藏部分字符)
const safeKeys = keys.map(k => ({
...k,
key: k.key.substring(0, 10) + '...' + k.key.substring(k.key.length - 4)
}));
res.json(keys); // 在管理界面显示完整密钥
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 删除密钥
router.delete('/keys/:key', async (req, res) => {
try {
const { key } = req.params;
await deleteKey(key);
await addLog('warn', `密钥已删除: ${key.substring(0, 10)}...`);
res.json({ success: true });
} catch (error) {
await addLog('error', `删除密钥失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 更新密钥频率限制
router.patch('/keys/:key/ratelimit', async (req, res) => {
try {
const { key } = req.params;
const { rateLimit } = req.body;
await updateKeyRateLimit(key, rateLimit);
await addLog('info', `密钥频率限制已更新: ${key.substring(0, 10)}...`);
res.json({ success: true });
} catch (error) {
await addLog('error', `更新频率限制失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 获取密钥统计
router.get('/keys/stats', async (req, res) => {
try {
const stats = await getKeyStats();
res.json(stats);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 获取日志
router.get('/logs', async (req, res) => {
try {
const limit = parseInt(req.query.limit) || 100;
const logs = await getRecentLogs(limit);
res.json(logs);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 清空日志
router.delete('/logs', async (req, res) => {
try {
await clearLogs();
await addLog('info', '日志已清空');
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 获取系统状态
router.get('/status', async (req, res) => {
try {
const status = getSystemStatus();
res.json(status);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 获取今日请求统计
router.get('/today-requests', async (req, res) => {
try {
const todayRequests = getTodayRequestCount();
res.json({ todayRequests });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Token 管理路由
// 获取所有账号
router.get('/tokens', async (req, res) => {
try {
const accounts = await loadAccounts();
// 隐藏敏感信息,只返回必要字段
const safeAccounts = accounts.map((acc, index) => ({
index,
access_token: acc.access_token?.substring(0, 20) + '...',
refresh_token: acc.refresh_token ? 'exists' : 'none',
expires_in: acc.expires_in,
timestamp: acc.timestamp,
enable: acc.enable !== false,
created: new Date(acc.timestamp).toLocaleString()
}));
res.json(safeAccounts);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 删除账号
router.delete('/tokens/:index', async (req, res) => {
try {
const index = parseInt(req.params.index);
await deleteAccount(index);
await addLog('warn', `Token 账号 ${index} 已删除`);
res.json({ success: true });
} catch (error) {
await addLog('error', `删除 Token 失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 启用/禁用账号
router.patch('/tokens/:index', async (req, res) => {
try {
const index = parseInt(req.params.index);
const { enable } = req.body;
await toggleAccount(index, enable);
await addLog('info', `Token 账号 ${index} 已${enable ? '启用' : '禁用'}`);
res.json({ success: true });
} catch (error) {
await addLog('error', `切换 Token 状态失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 启用/禁用账号 (POST方法支持)
router.post('/tokens/toggle', async (req, res) => {
try {
const { index, enable } = req.body;
await toggleAccount(index, enable);
await addLog('info', `Token 账号 ${index} 已${enable ? '启用' : '禁用'}`);
res.json({ success: true });
} catch (error) {
await addLog('error', `切换 Token 状态失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 触发登录流程
router.post('/tokens/login', async (req, res) => {
try {
await addLog('info', '开始 Google OAuth 登录流程');
const result = await triggerLogin();
res.json(result);
} catch (error) {
await addLog('error', `登录失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 获取 Token 统计
router.get('/tokens/stats', async (req, res) => {
try {
const stats = await getAccountStats();
res.json(stats);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 获取 Token 使用统计(轮询信息)
router.get('/tokens/usage', async (req, res) => {
try {
const usageStats = tokenManager.getUsageStats();
res.json(usageStats);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 手动添加 Token(通过回调链接)
router.post('/tokens/callback', async (req, res) => {
try {
const { callbackUrl } = req.body;
if (!callbackUrl) {
return res.status(400).json({ error: '请提供回调链接' });
}
await addLog('info', '正在通过回调链接添加 Token...');
const result = await addTokenFromCallback(callbackUrl);
await addLog('success', 'Token 已通过回调链接成功添加');
res.json(result);
} catch (error) {
await addLog('error', `添加 Token 失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 获取账号详细信息(包括名称)
router.post('/tokens/details', async (req, res) => {
try {
const { indices } = req.body;
const accounts = await loadAccounts();
const details = [];
for (const index of indices) {
if (index >= 0 && index < accounts.length) {
const account = accounts[index];
const accountInfo = await getAccountName(account.access_token);
details.push({
index,
email: accountInfo.email,
name: accountInfo.name,
access_token: account.access_token,
refresh_token: account.refresh_token,
expires_in: account.expires_in,
timestamp: account.timestamp,
enable: account.enable !== false
});
}
}
res.json(details);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 批量导出 Token (ZIP格式)
router.post('/tokens/export', async (req, res) => {
try {
const { indices } = req.body;
const accounts = await loadAccounts();
const exportData = [];
for (const index of indices) {
if (index >= 0 && index < accounts.length) {
const account = accounts[index];
const accountInfo = await getAccountName(account.access_token);
exportData.push({
email: accountInfo.email,
name: accountInfo.name,
access_token: account.access_token,
refresh_token: account.refresh_token,
expires_in: account.expires_in,
timestamp: account.timestamp,
created: new Date(account.timestamp).toLocaleString(),
enable: account.enable !== false
});
}
}
await addLog('info', `批量导出了 ${exportData.length} 个 Token 账号`);
// 创建 ZIP 文件
const archive = archiver('zip', { zlib: { level: 9 } });
const timestamp = new Date().toISOString().split('T')[0];
res.attachment(`tokens_export_${timestamp}.zip`);
res.setHeader('Content-Type', 'application/zip');
archive.pipe(res);
// 添加 tokens.json 文件到 ZIP
archive.append(JSON.stringify(exportData, null, 2), { name: 'tokens.json' });
await archive.finalize();
} catch (error) {
await addLog('error', `批量导出失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 批量导入 Token (ZIP格式)
router.post('/tokens/import', upload.single('file'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: '请上传文件' });
}
await addLog('info', '正在导入 Token 账号...');
const result = await importTokens(req.file.path);
await addLog('success', `成功导入 ${result.count} 个 Token 账号`);
res.json(result);
} catch (error) {
await addLog('error', `导入失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
// 获取系统设置
router.get('/settings', async (req, res) => {
try {
const settings = await loadSettings();
res.json(settings);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 保存系统设置
router.post('/settings', async (req, res) => {
try {
const result = await saveSettings(req.body);
await addLog('success', '系统设置已更新');
res.json(result);
} catch (error) {
await addLog('error', `保存设置失败: ${error.message}`);
res.status(500).json({ error: error.message });
}
});
export default router;
export { incrementRequestCount, addLog };
|